home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / EODEV.Z / InventoryEntry.m < prev    next >
Encoding:
Text File  |  1996-08-24  |  6.4 KB  |  225 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 "InventoryEntry.h"
  10. #import <EOInterface/EOInterface.h>
  11. #import <EOControl/EOControl.h>
  12. #import <BusinessLogic/BusinessLogic.h>
  13.  
  14. @implementation InventoryEntry
  15.  
  16. // This method is called when the window change size.
  17. - (void)_winResize:(NSNotification *)notification
  18. {
  19.     NSRect frame;
  20.  
  21.     frame = [currentView frame];
  22.  
  23.     [gamePlayerView setFrame:frame];
  24.     [gameMediaView setFrame:frame];
  25.     [movieMediaView setFrame:frame];
  26. }
  27.  
  28. - (void)awakeFromNib
  29. {
  30.  
  31.     // Remove the datasource on the details controllers
  32.     // It will be replaced by a detailDatasource when
  33.     // we connect the association
  34.     //
  35.     [gamePlayerController setDataSource:nil];
  36.     [gameMediaController setDataSource:nil];
  37.     [movieMediaController setDataSource:nil];
  38.  
  39.     // Reset all the view positions
  40.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_winResize:) name: NSWindowDidResizeNotification object:[currentView window]];
  41.  
  42.     // sort all this stuff
  43.     [productController setSortOrdering:[NSArray arrayWithObjects:[EOSortOrdering sortOrderingWithKey:@"name" selector:EOCompareAscending], nil]];
  44.     [movieController setSortOrdering:[NSArray arrayWithObjects:[EOSortOrdering sortOrderingWithKey:@"title" selector:EOCompareAscending], nil]];
  45. }
  46.  
  47. - (void)dealloc
  48. {
  49.     [gamePlayerView release];
  50.     [gameMediaView release];
  51.     [movieMediaView release];
  52.     [currentAssociation release];
  53.     [super dealloc];
  54. }
  55.  
  56. - (void)_swapToController:(EODisplayGroup *)aController view:(NSView *)aView firstKey:(NSView *)o
  57. {
  58.  
  59.     if (!didInitViews) {
  60.         // Setup
  61.         [self _winResize:nil];
  62.         superView = [currentView superview];
  63.         currentView = nil;
  64.  
  65.         // Retain the views
  66.         [gamePlayerView retain];
  67.         [gameMediaView retain];
  68.         [movieMediaView retain];
  69.  
  70.         // Remove those from their current superview
  71.         [gamePlayerView removeFromSuperview];
  72.         [gameMediaView removeFromSuperview];
  73.         [movieMediaView removeFromSuperview];
  74.         didInitViews = YES;
  75.     }
  76.  
  77.     // Swap the views
  78.     if(aView != currentView) {
  79.         [currentView removeFromSuperview];
  80.         currentView = aView;
  81.         if(aView) {
  82.             [superView addSubview:aView];
  83.         }
  84.     }
  85.  
  86.     // Swap the controllers
  87.     if(aController != currentController) {
  88.         // clear currentController
  89.         [currentController setDataSource:nil];
  90.         
  91.         currentController = aController;
  92.         [currentAssociation breakConnection];
  93.         [currentAssociation release];
  94.  
  95.         if(aController) {
  96.             currentAssociation = [[EOMasterDetailAssociation alloc] initWithObject:aController];
  97.             [currentAssociation bindAspect:@"parent" displayGroup:productController key:@"self"];
  98.             [currentAssociation establishConnection];
  99.         }
  100.     }
  101.  
  102.     // Set the nextKey
  103.     [attachKeyFrom setNextKeyView:o];
  104.     
  105. }
  106.  
  107. static BOOL needResort = YES;
  108.  
  109. // EOController delegate
  110. - (BOOL)displayGroup:(EODisplayGroup *)displayGroup shouldChangeSelectionToIndexes:(NSArray *)newIndexes;
  111. {
  112.     NSArray *allObj;
  113.     NSNumber *newIndex;
  114.     id newObj;
  115.     
  116.     if(displayGroup != productController)
  117.         return YES;
  118.  
  119.     if(needResort) {
  120.         [displayGroup updateDisplayedObjects];
  121.         needResort = NO;
  122.     }
  123.     
  124.     if(newIndexes && [newIndexes count]) {
  125.         newIndex = [newIndexes objectAtIndex:0];
  126.     } else {
  127.         return YES;
  128.     }
  129.     allObj = [displayGroup displayedObjects];
  130.     newObj = [allObj objectAtIndex:[newIndex intValue]];
  131.     
  132.     if([newObj isKindOfClass:[GamePlayer class]]) {
  133.         [self _swapToController:gamePlayerController view:gamePlayerView firstKey:gamePlayerFirstKey];
  134.     } else if([newObj isKindOfClass:[GameMedia class]]) {
  135.         [self _swapToController:gameMediaController view:gameMediaView firstKey:gameMediaFirstKey];
  136.     } else if([newObj isKindOfClass:[MovieMedia class]]) {
  137.         [self _swapToController:movieMediaController view:movieMediaView firstKey:movieMediaFirstKey];
  138.     } else {
  139.         [NSException raise:NSInvalidArgumentException format:@"*** Unknown class !"];
  140.     }
  141.     return YES;
  142. }
  143.  
  144. // Target/action
  145. - (unsigned)_newIndex
  146. {
  147.     NSArray * selection = [productController selectionIndexes];
  148.  
  149.     if (![selection count])
  150.         return 0;
  151.  
  152.     return [[selection objectAtIndex:0] unsignedIntValue] + 1;
  153. }
  154.  
  155.  
  156. - (void)insertVideoTape:sender
  157. {
  158.     id new = [[VideoTape new] autorelease];
  159.  
  160.     [[[productController dataSource] editingContext] insertObject:new];
  161.     [productController insertObject:new atIndex:[self _newIndex]];
  162.     needResort = YES;
  163. }
  164.  
  165. - (void)insertVideoDisc:sender
  166. {
  167.     id new = [[LaserDisc new] autorelease];
  168.  
  169.     [[[productController dataSource] editingContext] insertObject:new];
  170.     [productController insertObject:new atIndex:[self _newIndex]];
  171.     needResort = YES;
  172. }
  173.  
  174. - (void)insertGamePlayer:sender
  175. {
  176.     id new = [[GamePlayer new] autorelease];
  177.  
  178.     [[[productController dataSource] editingContext] insertObject:new];
  179.     [productController insertObject:new atIndex:[self _newIndex]];
  180.     needResort = YES;
  181. }
  182.  
  183. - (void)insertGameMedia:sender
  184. {
  185.     id new = [[GameMedia new] autorelease];
  186.  
  187.     [[[productController dataSource] editingContext] insertObject:new];
  188.     [productController insertObject:new atIndex:[self _newIndex]];
  189.     needResort = YES;
  190. }
  191.  
  192. @end
  193.  
  194.  
  195. // The application menu items are wired up directly to the EditingContext
  196. // so we can handle enabling and disabling by implementing
  197. // validateMenuItem:.
  198. // For multi-document apps this scheme is not appropriate.  In that case,
  199. // the menu items would be wired up to the application object which in turn
  200. // would implement validateMenuItem: to check the editing context of the 
  201. // foreground document.
  202. @implementation EOEditingContext(menuUpdate)
  203.  
  204. - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
  205. {
  206.     SEL action = [menuItem action];
  207.  
  208.     if(action == @selector(saveChanges:))
  209.         return [self hasChanges];
  210.     else if(action == @selector(undo:))
  211.         return [[self undoManager] canUndo];
  212.     else if(action == @selector(redo:))
  213.         return [[self undoManager] canRedo];
  214.     else if(action == @selector(revert:))
  215.         return [self hasChanges];
  216.     else if(action == @selector(refetch:))
  217.         return YES;
  218.     else
  219.         return YES;
  220. }
  221.  
  222. @end
  223.  
  224.  
  225.