home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / OPENSTEP / Utilities / Comics-1.0-MIS / Browser.subproj / BrowserController.m < prev    next >
Encoding:
Text File  |  1997-09-25  |  4.7 KB  |  146 lines

  1. /*
  2.  File:       BrowserController.m
  3.  
  4.  Contains:   Source code for the Controller of the "Browser" window
  5.  
  6.  Written by: Eric Simenel
  7.  
  8.  Created:    May 1997
  9.  
  10.  Copyright:  (c)1997 by Apple Computer, Inc., all rights reserved.
  11.  
  12.  Change History (most recent first):
  13.  
  14.  You may incorporate this sample code into your applications without
  15.  restriction, though the sample code has been provided "AS IS" and the
  16.  responsibility for its operation is 100% yours.  However, what you are
  17.  not permitted to do is to redistribute the source as "DSC Sample Code"
  18.  after having made changes. If you're going to re-distribute the source,
  19.  we require that you make it clear in the source that the code was
  20.  descended from Apple Sample Code, but that you've made changes.
  21. */
  22.  
  23. #import "BrowserController.h"
  24. #import "ComicsObj.h"
  25.  
  26. @implementation BrowserController
  27.  
  28. - (id)init
  29. {
  30.     if (self = [super init])
  31.       {
  32.         // the browser lies in a subproject, so let's get its nib
  33.         if (![NSBundle loadNibNamed:@"Browser" owner:self])
  34.           {
  35.             NSLog(@"Unable to load Browser.nib");
  36.             [self release];
  37.             return nil;
  38.           }
  39.         // I have a lttle less than 1500 titles currently, hence the following capacity...
  40.         array = [[NSMutableArray alloc] initWithCapacity:1500];
  41.         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(comicsChanged:) name:ComicsDidChangeNotification object:nil];
  42.       }
  43.     return self;
  44. }
  45. - (void)comicsChanged:(NSNotification *)note
  46. {
  47.     [browser loadColumnZero];
  48. }
  49. - (void)dealloc
  50. {
  51.     [[NSNotificationCenter defaultCenter] removeObserver:self name:ComicsDidChangeNotification object:nil];
  52.     [array release];
  53.     [super dealloc];
  54. }
  55.  
  56. - (void)awakeFromNib
  57. {
  58. #if debug
  59.     NSLog(@"in BrowserController:awakeFromNib");
  60. #endif
  61.  
  62.     // classic initialization, we load the first column
  63.     [browser loadColumnZero];
  64.  
  65.     // set up the browser the way I like
  66.     [browser setMinColumnWidth:100];
  67.     [browser setMaxVisibleColumns:5];
  68.     [browser setSeparatesColumns:NO];
  69.  
  70.     // let's show the window
  71.     [[browser window] makeKeyAndOrderFront:nil];
  72.     
  73. #if debug
  74.     NSLog(@"end of BrowserController:awakeFromNib");
  75. #endif
  76. }
  77.  
  78. // Unlike numberOfRowsInTableView which is called a zillion times, numberOfRowsInColumn is only being called once
  79. // only when the user clicked on a cell in the previous column and the browser wants to display the content of the
  80. // current column. This means that we can (and should) do the whole preparation of the column and computation of its
  81. // number of rows in this method. In this case, the call to sortArray for the 5th column.
  82.  
  83. - (int)browser:(NSBrowser *)sender numberOfRowsInColumn:(int)column
  84. {
  85.     short brand, series, state, kind;
  86.  
  87.     // the first 4 colummns have all only 3 cells
  88.     if (column < 4) return 3;
  89.  
  90.     // if not, let's see what the user has currently selected in the first colums
  91.     brand = [sender selectedRowInColumn:0];
  92.     series = [sender selectedRowInColumn:1];
  93.     state = [sender selectedRowInColumn:2];
  94.     kind = [sender selectedRowInColumn:3];
  95.  
  96. #if debug
  97.     NSLog(@"column 4, brand= %d, series= %d, state= %d, kind= %d", brand, series, state, kind);
  98. #endif
  99.  
  100.     // so that we can retrieve the appropriate titles (we don't care for sorting in this browser
  101.     [comicsBase sortArray:array withBrand:brand withSeries:series withKind:kind withState:state withSort:0];
  102.     return [array count];
  103. }
  104.  
  105. - (void)browser:(NSBrowser *)sender willDisplayCell:(id)cell atRow:(int)row column:(int)column
  106. {
  107.     switch(column)
  108.       {
  109.         case 0: switch(row)
  110.           {
  111.             case 0: [cell setStringValue:@"All Brands"]; break;
  112.             case 1: [cell setStringValue:@"Marvel"]; break;
  113.             case 2: [cell setStringValue:@"DC & Others"]; break;
  114.           } break;
  115.         case 1: switch(row)
  116.           {
  117.             case 0: [cell setStringValue:@"All Series"]; break;
  118.             case 1: [cell setStringValue:@"Long"]; break;
  119.             case 2: [cell setStringValue:@"Mini"]; break;
  120.           } break;
  121.         case 2: switch(row)
  122.           {
  123.             case 0: [cell setStringValue:@"All States"]; break;
  124.             case 1: [cell setStringValue:@"Dead"]; break;
  125.             case 2: [cell setStringValue:@"Live"]; break;
  126.           } break;
  127.         case 3: switch(row)
  128.           {
  129.             case 0: [cell setStringValue:@"All Kinds"]; break;
  130.             case 1: [cell setStringValue:@"Main"]; break;
  131.             case 2: [cell setStringValue:@"Dual"]; break;
  132.           } break;
  133.         case 4:
  134.             [cell setStringValue:[[array objectAtIndex:row] title]];
  135.             break;
  136.       }
  137.     
  138.     // the 5th column is the last
  139.     [cell setLeaf:(column == 4)];
  140.  
  141.     // this means this cell is ready for display
  142.     [cell setLoaded: YES];
  143. }
  144.  
  145. @end
  146.