Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @implementation NewAccountSheetController
- - (id)init;
- {
- if ((self = [super initWithWindowNibName:@"NewAccountSheet"]) != nil) {
- }
- return self;
- }
- - (void)beginSheetForWindow:(NSWindow *)window;
- {
- // We want the window loaded early, as it has the side effect of finishing the initialization of the controller from the nib!
- NSWindow * sheet = [self window];
- // We don't want an undo stack entry for the object's creation!
- NSUndoManager * undoManager = [managedObjectContext undoManager];
- [undoManager disableUndoRegistration];
- [objectController add:self];
- [managedObjectContext processPendingChanges];
- [undoManager enableUndoRegistration];
- [NSApp beginSheet:sheet
- modalForWindow:window
- modalDelegate:self
- didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
- contextInfo:nil];
- }
- - (void)controlTextDidChange:(NSNotification *)notification;
- {
- NSError * error = nil;
- BOOL validationResult = [[objectController content] validateForInsert:&error];
- if (error != nil) {
- // Hack to enable the Create button even though we haven't committed the edit to the last required field… This is particularly ugly b/c now we have to validate *again* in -endSheet:.
- if ([error code] == NSValidationMissingMandatoryPropertyError)
- validationResult = YES;
- }
- [self setValidForInsert:validationResult];
- }
- - (IBAction)endSheet:(id)sender;
- {
- int tag = [sender selectedTag];
- if (tag == 0) {
- [managedObjectContext commitEditing];
- NSError * error = nil;
- if (![[objectController content] validateForInsert:&error]) {
- [self presentError:error
- modalForWindow:[self window]
- delegate:nil
- didPresentSelector:nil
- contextInfo:nil];
- return;
- }
- }
- [NSApp endSheet:[self window]
- returnCode:tag];
- }
- - (BOOL)isValidForInsert;
- {
- return isValidForInsert;
- }
- - (void)setValidForInsert:(BOOL)validationResult;
- {
- if (isValidForInsert != validationResult) {
- [self willChangeValueForKey:@"isValidForInsert"];
- isValidForInsert = validationResult;
- [self didChangeValueForKey:@"isValidForInsert"];
- }
- }
- - (void)sheetDidEnd:(NSWindow *)sheet
- returnCode:(int)returnCode
- contextInfo:(id)contextInfo;
- {
- if (returnCode == 0) {
- [objectController commitEditing];
- NSManagedObject * newObject = [targetController newObject];
- NSArray * keys = [[[newObject entity] attributesByName] allKeys];
- [newObject setValuesForKeysWithDictionary:[[objectController content] dictionaryWithValuesForKeys:keys]];
- // In principal we should never see an error here, b/c we only enable the Create button if newObject has passed -validateForInsert: already, but JIC…
- NSError * error = nil;
- if ([newObject validateForInsert:&error])
- [targetController addObject:newObject];
- else
- [NSApp presentError:error];
- }
- [objectController setContent:nil];
- [managedObjectContext reset];
- [sheet orderOut:self];
- }
- - (void)windowDidLoad;
- {
- NSPersistentStoreCoordinator * coordinator = [[targetController managedObjectContext] persistentStoreCoordinator];
- [managedObjectContext setPersistentStoreCoordinator:coordinator];
- }
- - (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)sender;
- {
- return [managedObjectContext undoManager];
- }
- - (IBAction)undo:(id)sender;
- {
- [[managedObjectContext undoManager] undo];
- }
- - (IBAction)redo:(id)sender;
- {
- [[managedObjectContext undoManager] redo];
- }
- - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem;
- {
- if ([anItem action] == @selector(undo:))
- return [[managedObjectContext undoManager] canUndo];
- if ([anItem action] == @selector(redo:))
- return [[managedObjectContext undoManager] canRedo];
- return YES;
- }
- @synthesize managedObjectContext;
- @synthesize objectController;
- @synthesize targetController;
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement