home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / EODEV.Z / CustomColumns.m < prev    next >
Encoding:
Text File  |  1996-08-31  |  4.3 KB  |  108 lines

  1. /*
  2.         Copyright (c) 1996, NeXT Software, Inc.
  3.         All rights reserved.
  4.  
  5.         You may freely copy, distribute and reuse the code in this example.
  6.         NeXT disclaims any warranty of any kind, expressed or implied,
  7.         as to its fitness for any particular use.
  8. */
  9. #import "CustomColumns.h"
  10.  
  11. // silly category so model objects all know how to get and set their
  12. // modification times
  13. @interface NSObject (_modificationDate)
  14. - (void)_setModificationDate:(NSCalendarDate *)date;
  15. - (NSCalendarDate *)_modificationDate;
  16. @end
  17.  
  18. @implementation CustomColumns
  19.  
  20. + (void)initialize {
  21.     id sharedInstance = [self new];
  22.  
  23.     // register our column type with the application
  24.     [EOMApp registerColumnName:@"Modification Date" forClass:[EOModel class] provider:sharedInstance];
  25.     [EOMApp registerColumnName:@"Modification Date" forClass:[EOEntity class] provider:sharedInstance];
  26.     [EOMApp registerColumnName:@"Modification Date" forClass:[EORelationship class] provider:sharedInstance];
  27.     [EOMApp registerColumnName:@"Modification Date" forClass:[EOAttribute class] provider:sharedInstance];
  28.     [EOMApp registerColumnName:@"Modification Date" forClass:[EOStoredProcedure class] provider:sharedInstance];
  29.  
  30.     // register for object change notification so we can change objects modification times
  31.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_objectsChangedInEditingContext:) name:EOObjectsChangedInEditingContextNotification object:nil];    
  32. }
  33.  
  34. // Method from EOColumnProvider protcol
  35. - (void)initColumn:(NSTableColumn *)col class:(Class)class name:(NSString *)name displayGroup:(EODisplayGroup *)displayGroup document:(EOModelerDocument *)doc
  36. {
  37.     EOColumnAssociation *assoc;
  38.     NSCell    *cell;
  39.     NSDateFormatter *formatter;
  40.     
  41.     // fix up the column    
  42.     [[col headerCell] setStringValue:name];
  43.  
  44.     // it will be a text field, so set that up
  45.     cell = [[NSTextFieldCell alloc] initTextCell:@""];
  46.     [col setDataCell:cell];
  47.     [cell setEnabled:YES];
  48.     [cell setEditable:NO];
  49.     [cell setScrollable: YES];
  50.     [col setEditable: NO];
  51.  
  52.     formatter = [[NSDateFormatter alloc] initWithDateFormat:@"%m/%d/%y: %H:%M" allowNaturalLanguage:NO];
  53.     [cell setFormatter:formatter];
  54.     [formatter release];
  55.  
  56.     // and connect up the displayGroup
  57.     assoc = [[EOColumnAssociation alloc] initWithObject:col];
  58.     [assoc bindAspect:@"value" displayGroup:displayGroup key:@"_modificationDate"];
  59.     [assoc establishConnection];
  60.     [assoc release];
  61. }
  62.  
  63. + (void)_objectsChangedInEditingContext:(NSNotification *)notification
  64. {
  65.     // objects changed in some editing context.  If its a model object
  66.     // then set a new modification time in the user dictionary
  67.     EOEditingContext *editingContext = [notification object];
  68.     if ([editingContext isKindOfClass:[EOEditingContext class]]) {
  69.         NSDictionary *userInfo = [notification userInfo];
  70.         NSArray *updatedObjects = [userInfo objectForKey:@"updated"];
  71.         int i = [updatedObjects count];
  72.  
  73.         // we must turn off observer notification so that our change to this object
  74.         // does not cause us to get called back again ad infinitum.
  75.         // This has the unfortunate side-effect of disabling undo for this
  76.         // change.  Alternatively we could compare the state of the modified
  77.         // object to its currentEventSnapshot and verify that something other
  78.         // than our entry in the userInfo had changed.  This would stop
  79.         // the infinite recursion.
  80.         [EOObserverCenter suppressObserverNotification];
  81.         while (i--) {
  82.             id object = [updatedObjects objectAtIndex:i];
  83.             [object _setModificationDate:[NSCalendarDate date]];
  84.         }
  85.         [EOObserverCenter enableObserverNotification];
  86.     }
  87. }
  88. @end
  89.  
  90.  
  91. @implementation NSObject (_modificationDate)
  92. - (void)_setModificationDate:(NSCalendarDate *)date {
  93.     NSMutableDictionary *userInfo = [[(EOModel *)self userInfo] mutableCopy];
  94.     if (!userInfo) userInfo = [NSMutableDictionary new];
  95.     [userInfo setObject:[date description] forKey:@"modificationDate"];
  96.     [(EOModel *)self setUserInfo:userInfo];
  97.     [userInfo release];
  98. }
  99.  
  100. - (NSCalendarDate *)_modificationDate {
  101. NSString *dateString = [[(EOModel *)self userInfo] objectForKey:@"modificationDate"];
  102.  
  103.     if (dateString) 
  104.         return [[[NSCalendarDate alloc] initWithString:dateString] autorelease];
  105.     return nil;
  106. }
  107. @end
  108.