home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / MiscKit / Source / MiscSwapKit / MiscSwapView_ByObject.m < prev    next >
Encoding:
Text File  |  1994-03-15  |  5.6 KB  |  227 lines

  1. /* MiscSwapView_ByObject.m                 
  2.  *
  3.  * This is a MiscSwapView category. I can handle swapping of different 
  4.  * contentViews (controlled by MiscSwapContentsController's) into ourself by
  5.  * comparing trigger objects.
  6.  *
  7.  * For more interface-info see the header file. More in depth information
  8.  * can be found here in the source-code.
  9.  *
  10.  * Written by:         Thomas Engel
  11.  * Created:            24.01.1994 (Copyleft)
  12.  * Last modified:     24.02.1994
  13.  */
  14.  
  15. //#import "MiscSwapView.h"
  16. //#import "MiscSwapContentsController.h"
  17. #import <misckit/misckit.h> // since for MiscKit the headers were moved by Don
  18.  
  19. @implementation MiscSwapView(ByObject)
  20.  
  21.  
  22. - trigger
  23. {
  24.     return trigger;
  25. }
  26.  
  27. - swapContentView:sender
  28. {    
  29.     // This is the method that has to be triggered by each trigger for a nice
  30.     // swap. It incorporates the delegate and the new&previous viewCtrl.
  31.     // Our delegate gets informed before the viewControllers are.
  32.     // The new controller will get willSwapIn which should cause a revert or
  33.     // any other method causing the contoller to update/init its view. It's
  34.     // the last chance before it will be displayed.
  35.     // We could check wether the controller has really changed or not but
  36.     // this might cause a problem when using a inspector because the same
  37.     // inspector might show different data. 
  38.     // Maybe we could and a switch here...right now I'll leave it this way.
  39.     // It should work fine.
  40.     // Be sure that whenever you send the message you really have some changes
  41.     // to show. Otherwise it might be not that perfect. 
  42.     //
  43.     // Anyway. The same view will never swap in twice! SwapView take care of
  44.     // that inside setContentView. So you don't have the overhead of a doubled
  45.     // drawing time.
  46.     
  47.     id    oldController;
  48.     
  49.     trigger = sender;
  50.  
  51.     if( [delegate respondsTo:@selector(viewWillSwap:)] )
  52.         [delegate viewWillSwap:self];
  53.  
  54.     oldController = currentController;
  55.     currentController = [self findController];
  56.  
  57.     [oldController willSwapOut];
  58.     [currentController willSwapIn];
  59.  
  60.     [self setContentView:[currentController view]];
  61.     
  62.     if( [delegate respondsTo:@selector(viewDidSwap:)] )
  63.         [delegate viewDidSwap:self];
  64.  
  65.     [oldController didSwapOut];
  66.     [currentController didSwapIn];
  67.     
  68.     return self;
  69. }
  70.  
  71. - addController:sender
  72. {
  73.     // Adding a viewController has to ensure that there is only one controller
  74.     // for one trigger. The controller added last will be the only known
  75.     // controller after adding has finished!
  76.     // To find interferring controllers we use our findController method.
  77.     // This needs the trigger to check and the right tagComparison setting.
  78.     // If you change the tagComparison setting later the results might not be
  79.     // correct!
  80.     
  81.     id    oldTrigger;
  82.     id    oldController;
  83.     
  84.     oldTrigger = trigger;
  85.     trigger = [sender trigger];
  86.     
  87.     // If there already is someone...remove him.!
  88.     
  89.     oldController = [self findController];
  90.     if( oldController ) [self removeController:oldController];
  91.     
  92.     [controllers addObject:sender];
  93.     
  94.     trigger = oldTrigger;
  95.     return self;
  96. }
  97.  
  98. - removeController:sender
  99. {
  100.     // Here we remove  aViewController from the subviewControllers list.
  101.      // ATTENTION: This does not cause the view to disappear when it is the
  102.      // current swapView contents!
  103.  
  104.     [controllers removeObject:sender];
  105.     return self;
  106. }
  107.  
  108. - removeAllControllers
  109. {
  110.     [controllers empty];
  111.     return self;
  112. }
  113.  
  114. - controllers
  115. {
  116.     return controllers;
  117. }
  118.  
  119. - contentsController
  120. {
  121.     return currentController;
  122. }
  123.  
  124. - setTagComparison:(BOOL)flag
  125. {
  126.     // Should we compare the tags first...and then the objects ?
  127.     
  128.     tagComparison = flag;
  129.     return self;
  130. }
  131.  
  132. - (BOOL)doesTagComparison
  133. {
  134.     return tagComparison;
  135. }
  136.  
  137. - findController
  138. {
  139.      // This is the basic comparison center. Subclasses of this class should
  140.     // implement only the new findControllerByTag(Object) methods and leave
  141.     // this method untouched. Sometime there is a way of finding a more
  142.     // 'useable' trigger even inside the swapAction method.
  143.  
  144.     id    newController;
  145.  
  146.     newController = nil;    
  147.     
  148.     if( tagComparison && 
  149.         [trigger respondsTo:@selector(tag)])
  150.         newController = [self findControllerByTag:[trigger tag]];
  151.     if( !newController ) newController = [self findControllerByObject:trigger];
  152.  
  153.     return newController;
  154. }
  155.  
  156. - findControllerByTag:(int)aTag
  157. {
  158.     // Here we try to find the right controller by comparing the tags. 
  159.     // Really simple. Well...the trigger has to have a tag different from
  160.     // zero.
  161.     
  162.     id        aViewController;
  163.     int        i;
  164.             
  165.     if( aTag == 0 ) return nil;
  166.     
  167.     aViewController = nil;    
  168.     
  169.     // Ok now lets find out what viewController refers to this tag.
  170.     
  171.     for( i=0; i<[controllers count]; i++ )
  172.     {
  173.         if( aTag == [[controllers objectAt:i] triggerTag] )
  174.         {
  175.             aViewController = [controllers objectAt:i];
  176.             break;
  177.         };
  178.     }
  179.     return aViewController;
  180. }
  181.  
  182. - findControllerByObject:aTrigger
  183. {
  184.     // Here we simple compare the objects. They have to be the SAME...not only
  185.     // similar!!!
  186.     
  187.     id        aViewController;
  188.     int        i;
  189.     
  190.     aViewController = nil;    
  191.     
  192.     for( i=0; i<[controllers count]; i++ )
  193.     {
  194.         if( aTrigger == [[controllers objectAt:i] trigger] )
  195.         {
  196.             aViewController = [controllers objectAt:i];
  197.             break;
  198.         };
  199.     }
  200.     return aViewController;
  201. }
  202.  
  203. @end
  204.  
  205. /*
  206.  * History: 24.02.94 Made it a MiscswapView Category.
  207.  *
  208.  *            24.01.94 Made it a subclass of MiscSwapView
  209.  *
  210.  *            08.01.94 Switched to tagComparison for better reading.
  211.  *                     choosesByTagFirst was not that nice.
  212.  *
  213.  *            21.12.93 Code transferred from the old swapPopManager and
  214.  *                     some viewController methods added plus the trigger object
  215.  *                     which now stores the object triggering our swap.
  216.  *
  217.  *            20.12.93 Enlightened the controller to check the tags if they are
  218.  *                     set. This helps to localize apps.
  219.  *
  220.  *            04.12.93 Added a delegate to this class the enable better 
  221.  *                     command-key handling.
  222.  *
  223.  *            04.11.93 First steps to a general-purpose swapPopManager.
  224.  *
  225.  *
  226.  * Bugs: - I'm not sure about what to free....
  227.  */