[364] | 1 | //
|
---|
| 2 | // NJRValidatingField.m
|
---|
| 3 | // Pester
|
---|
| 4 | //
|
---|
| 5 | // Created by Nicholas Riley on 11/27/07.
|
---|
| 6 | // Copyright 2007 Nicholas Riley. All rights reserved.
|
---|
| 7 | //
|
---|
| 8 |
|
---|
| 9 | #import "NJRValidatingField.h"
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | @implementation NJRValidatingField
|
---|
| 13 |
|
---|
| 14 | - (void)_propagateValidationChange;
|
---|
| 15 | {
|
---|
| 16 | NSText *fieldEditor = [self currentEditor];
|
---|
| 17 | NSDictionary *userInfo = nil;
|
---|
| 18 | if (fieldEditor != nil) userInfo = [NSDictionary dictionaryWithObject: fieldEditor forKey: @"NSFieldEditor"];
|
---|
| 19 | [[NSNotificationCenter defaultCenter] postNotificationName: NSControlTextDidChangeNotification object: self userInfo: userInfo];
|
---|
| 20 | [self selectText: self];
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | - (void)validationFailedSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo {
|
---|
| 24 | // modal delegate callback method for NSBeginAlertSheet() function; called in the above method
|
---|
| 25 | if (returnCode == NSAlertOtherReturn) { // cancel
|
---|
| 26 | [self abortEditing]; // abort edit session and reinstate original value
|
---|
| 27 | [self _propagateValidationChange];
|
---|
| 28 | } else if (returnCode == NSAlertAlternateReturn) { // set to min/max/default value
|
---|
| 29 | [self setObjectValue: [(NSDictionary *)contextInfo objectForKey: @"proposedValue"]];
|
---|
| 30 | [self validateEditing];
|
---|
| 31 | [self _propagateValidationChange];
|
---|
| 32 | }
|
---|
| 33 | [(NSDictionary *)contextInfo release];
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | - (void)handleDidFailToFormatString:(NSString *)string errorDescription:(NSString *)error label:(NSString *)label;
|
---|
| 37 | {
|
---|
| 38 | NSString *alertMessage = nil;
|
---|
| 39 | NSString *alternateButtonString = nil;
|
---|
| 40 | NSNumber *proposedValue = nil;
|
---|
| 41 | NSDictionary *contextInfo;
|
---|
| 42 |
|
---|
| 43 | NSString *alertInformation = [NSString localizedStringWithFormat:
|
---|
| 44 | NSLocalizedString(@"The %@ field must be set to a value between %@ and %@.",
|
---|
| 45 | @"Informative text for alert posed by text field when invalid value entered"),
|
---|
| 46 | label, [[self formatter] minimum], [[self formatter] maximum]];
|
---|
| 47 | NSString *defaultButtonString = NSLocalizedString(@"Edit", @"Name of Edit button");
|
---|
| 48 | NSString *otherButtonString = NSLocalizedString(@"Cancel", @"Name of Cancel button");
|
---|
| 49 |
|
---|
| 50 | if ([error isEqualToString:
|
---|
| 51 | NSLocalizedStringFromTableInBundle(@"Fell short of minimum", @"Formatter",
|
---|
| 52 | [NSBundle bundleForClass:[NSFormatter class]],
|
---|
| 53 | @"Presented when user value smaller than minimum")]) {
|
---|
| 54 | proposedValue = [[self formatter] minimum];
|
---|
| 55 | alertMessage = [NSString stringWithFormat:
|
---|
| 56 | NSLocalizedString(@"%@ is too small for the %@ field.",
|
---|
| 57 | @"Message text for alert posed by numeric field when too-small value entered"),
|
---|
| 58 | string, label];
|
---|
| 59 | alternateButtonString = [NSString localizedStringWithFormat:
|
---|
| 60 | NSLocalizedString(@"Set to %@",
|
---|
| 61 | @"Name of alternate button for alert posed by numeric field when too-small value entered"),
|
---|
| 62 | proposedValue];
|
---|
| 63 | } else if ([error isEqualToString:
|
---|
| 64 | NSLocalizedStringFromTableInBundle(@"Maximum exceeded", @"Formatter",
|
---|
| 65 | [NSBundle bundleForClass:[NSFormatter class]],
|
---|
| 66 | @"Presented when user value larger than maximum")]) {
|
---|
| 67 | proposedValue = [[self formatter] maximum];
|
---|
| 68 | alertMessage = [NSString stringWithFormat:
|
---|
| 69 | NSLocalizedString(@"%@ is too large for the %@ field.",
|
---|
| 70 | @"Message text for alert posed by numeric field when too-large value entered"),
|
---|
| 71 | string, label];
|
---|
| 72 | alternateButtonString = [NSString localizedStringWithFormat:
|
---|
| 73 | NSLocalizedString(@"Set to %@",
|
---|
| 74 | @"Name of alternate button for alert posed by numeric field when too-large value entered"),
|
---|
| 75 | proposedValue];
|
---|
| 76 | } else if ([error isEqualToString:
|
---|
| 77 | NSLocalizedStringFromTableInBundle(@"Invalid number", @"Formatter",
|
---|
| 78 | [NSBundle bundleForClass:[NSFormatter class]],
|
---|
| 79 | @"Presented when user typed illegal characters: no valid object")]) {
|
---|
| 80 | alertMessage = [NSString stringWithFormat:
|
---|
| 81 | NSLocalizedString(@"%@ is not a valid entry for the %@ field.",
|
---|
| 82 | @"Message text for alert posed by text field when invalid value entered"),
|
---|
| 83 | string, label];
|
---|
| 84 | alternateButtonString = nil;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | contextInfo = [NSDictionary dictionaryWithObject: proposedValue forKey: @"proposedValue"];
|
---|
| 88 | [contextInfo retain];
|
---|
| 89 | NSBeep();
|
---|
| 90 | NSBeginAlertSheet(alertMessage, defaultButtonString, alternateButtonString, otherButtonString, [self window],
|
---|
| 91 | self, @selector(validationFailedSheetDidEnd:returnCode:contextInfo:), NULL, contextInfo,
|
---|
| 92 | alertInformation);
|
---|
| 93 | }
|
---|
| 94 | @end
|
---|