Advertisement
Guest User

Kaelin Colclasure

a guest
Aug 15th, 2010
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @implementation NewAccountSheetController
  2.  
  3. - (id)init;
  4. {
  5.     if ((self = [super initWithWindowNibName:@"NewAccountSheet"]) != nil) {
  6.     }
  7.     return self;
  8. }
  9.  
  10. - (void)beginSheetForWindow:(NSWindow *)window;
  11. {
  12.     // We want the window loaded early, as it has the side effect of finishing the initialization of the controller from the nib!
  13.     NSWindow * sheet = [self window];
  14.     // We don't want an undo stack entry for the object's creation!
  15.     NSUndoManager * undoManager = [managedObjectContext undoManager];
  16.     [undoManager disableUndoRegistration];
  17.     [objectController add:self];
  18.     [managedObjectContext processPendingChanges];
  19.     [undoManager enableUndoRegistration];
  20.     [NSApp beginSheet:sheet
  21.        modalForWindow:window
  22.         modalDelegate:self
  23.        didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
  24.           contextInfo:nil];
  25. }
  26.  
  27. - (void)controlTextDidChange:(NSNotification *)notification;
  28. {
  29.     NSError * error = nil;
  30.     BOOL validationResult = [[objectController content] validateForInsert:&error];
  31.     if (error != nil) {
  32.         // 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:.
  33.         if ([error code] == NSValidationMissingMandatoryPropertyError)
  34.             validationResult = YES;
  35.     }
  36.     [self setValidForInsert:validationResult];
  37. }
  38.  
  39. - (IBAction)endSheet:(id)sender;
  40. {
  41.     int tag = [sender selectedTag];
  42.     if (tag == 0) {
  43.         [managedObjectContext commitEditing];
  44.         NSError * error = nil;
  45.         if (![[objectController content] validateForInsert:&error]) {
  46.             [self presentError:error
  47.                 modalForWindow:[self window]
  48.                       delegate:nil
  49.             didPresentSelector:nil
  50.                    contextInfo:nil];
  51.             return;
  52.         }
  53.     }
  54.     [NSApp endSheet:[self window]
  55.          returnCode:tag];
  56. }
  57.  
  58. - (BOOL)isValidForInsert;
  59. {
  60.     return isValidForInsert;
  61. }
  62.  
  63. - (void)setValidForInsert:(BOOL)validationResult;
  64. {
  65.     if (isValidForInsert != validationResult) {
  66.         [self willChangeValueForKey:@"isValidForInsert"];
  67.         isValidForInsert = validationResult;
  68.         [self didChangeValueForKey:@"isValidForInsert"];
  69.     }
  70. }
  71.  
  72. - (void)sheetDidEnd:(NSWindow *)sheet
  73.          returnCode:(int)returnCode
  74.         contextInfo:(id)contextInfo;
  75. {
  76.     if (returnCode == 0) {
  77.         [objectController commitEditing];
  78.         NSManagedObject * newObject = [targetController newObject];
  79.         NSArray * keys = [[[newObject entity] attributesByName] allKeys];
  80.         [newObject setValuesForKeysWithDictionary:[[objectController content] dictionaryWithValuesForKeys:keys]];
  81.         // 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…
  82.         NSError * error = nil;
  83.         if ([newObject validateForInsert:&error])
  84.             [targetController addObject:newObject];
  85.         else
  86.             [NSApp presentError:error];
  87.     }
  88.     [objectController setContent:nil];
  89.     [managedObjectContext reset];
  90.     [sheet orderOut:self];
  91. }
  92.  
  93. - (void)windowDidLoad;
  94. {
  95.     NSPersistentStoreCoordinator * coordinator = [[targetController managedObjectContext] persistentStoreCoordinator];
  96.     [managedObjectContext setPersistentStoreCoordinator:coordinator];
  97. }
  98.  
  99. - (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)sender;
  100. {
  101.     return [managedObjectContext undoManager];
  102. }
  103.  
  104. - (IBAction)undo:(id)sender;
  105. {
  106.     [[managedObjectContext undoManager] undo];
  107. }
  108.  
  109. - (IBAction)redo:(id)sender;
  110. {
  111.     [[managedObjectContext undoManager] redo];
  112. }
  113.  
  114. - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem;
  115. {
  116.     if ([anItem action] == @selector(undo:))
  117.         return [[managedObjectContext undoManager] canUndo];
  118.     if ([anItem action] == @selector(redo:))
  119.         return [[managedObjectContext undoManager] canRedo];
  120.     return YES;
  121. }
  122.  
  123. @synthesize managedObjectContext;
  124. @synthesize objectController;
  125. @synthesize targetController;
  126.  
  127. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement