home *** CD-ROM | disk | FTP | other *** search
/ NeXT Enterprise Objects Framework 1.1 / NeXT Enterprise Objects Framework 1.1.iso / NextDeveloper / Examples / EnterpriseObjects / SHLExamples / Sorting / SortController.m < prev   
Encoding:
Text File  |  1994-12-07  |  4.6 KB  |  213 lines

  1. /*--------------------------------------------------------------------------
  2.  *
  3.  *     You may freely copy, distribute, and reuse the code in this example.
  4.  *     SHL Systemhouse disclaims any warranty of any kind, expressed or  
  5.  *    implied, as to its fitness for any particular use.
  6.  *
  7.  *
  8.  *    SortController
  9.  *
  10.  *    Inherits From:        NSObject
  11.  *
  12.  *    Conforms To:        None
  13.  *
  14.  *    Declared In:        SortController.h
  15.  *
  16.  *
  17.  *------------------------------------------------------------------------*/
  18. #import <appkit/appkit.h>
  19. #import <eointerface/eointerface.h>
  20. #import <eoaccess/eoaccess.h>
  21. #import <foundation/NSArray.h> 
  22.  
  23. #import "SortController.h"
  24. #import "SlidingMatrix.h"
  25.  
  26.  
  27. @implementation SortController
  28.  
  29. - appDidInit:sender
  30. {
  31.     int             count, iterator;
  32.        NSEnumerator     *enumerator;
  33.     id                 object, cell;
  34.     NXRect            frameRect;
  35.     NXSize            cellSize;
  36.     id                attributes;
  37.  
  38.     [window disableDisplay];
  39.     entity = [(EODatabaseDataSource *)[(EOController *)controller
  40.                         dataSource] entity];
  41.  
  42.     attributes = [entity attributes];
  43.     [attributeOrder getFrame:&frameRect];
  44.     [attributeOrder getCellSize:&cellSize];
  45.     cellSize.width = frameRect.size.width;
  46.     [attributeOrder setCellSize:&cellSize];
  47.  
  48.     count = [attributes count];
  49.     [attributeOrder renewRows:count cols:1];
  50.     [attributeOrder sizeToCells];
  51.  
  52.     enumerator = [attributes objectEnumerator];
  53.     iterator = 0;
  54.  
  55.     while ((object = [enumerator nextObject]) != nil) {
  56.         cell = [attributeOrder cellAt:iterator:0];
  57.         [cell setTitle: [[(EOAttribute *)object name] cString]];
  58.         [cell setTag: 0];
  59.         iterator++;
  60.     }
  61.  
  62.     [scrollView setDocView:attributeOrder];
  63.     [controller fetch];
  64.     [window reenableDisplay];
  65.     [window display];
  66.     [hints orderFront:nil];
  67.     [window makeKeyAndOrderFront:nil];
  68.     return self;
  69. }
  70.  
  71.  
  72. - _orderArrayFromList
  73. {
  74.     id    attributeDetails;
  75.     id    orderArray = [[[NSMutableArray allocWithZone:[self zone]]
  76.                             initWithCapacity:0] autorelease];
  77.     int    iterator, count;
  78.     id    ordering, current;
  79.  
  80.     //  List containing cells with sorting details
  81.     attributeDetails = [attributeOrder cellList];
  82.  
  83.     // get ordering from attribute list
  84.     count = [attributeDetails count];
  85.  
  86.     for (iterator = 0; iterator < count; iterator++) {
  87.  
  88.         current = [attributeDetails objectAt:iterator];
  89.         if ([current tag]) {
  90.             //  there is a sorting order defined
  91.             ordering = [[EOAttributeOrdering allocWithZone:[self zone]]
  92.                 initWithAttribute:[entity attributeNamed:
  93.                     [NSString stringWithCString:[current title]]]
  94.                 ordering: [current tag]];
  95.             [(NSMutableArray *)orderArray addObject: ordering];
  96.         }
  97.     }
  98.     return orderArray;
  99. }
  100.  
  101.  
  102. - _orderArrayFromTable
  103. {
  104.     id    orderArray = [[[NSMutableArray allocWithZone:[self zone]]
  105.                             initWithCapacity:0] autorelease];
  106.     int    iterator, iterator2, orderCount, attributeCount;
  107.     id    ordering=nil, current, detail=nil;
  108.     id    orderList = nil, attributeDetails;
  109.  
  110.     //  List containing cells with sorting details
  111.     attributeDetails = [attributeOrder cellList];
  112.  
  113.     // get ordering from table view columns
  114.     orderList = [tableView columnList];
  115.     orderCount = [orderList count];
  116.     attributeCount = [attributeDetails count];
  117.  
  118.  
  119.     for (iterator = 0; iterator < orderCount; iterator++) {
  120.         current = [[[orderList objectAt: iterator] identifier] key];
  121.         detail = nil;                
  122.  
  123.         for (iterator2 = 0; (detail == nil) && (iterator2 < attributeCount);
  124.                              iterator2++) {
  125.             detail = [attributeDetails objectAt:iterator2];
  126.                 
  127.             if (detail) {
  128.                 if (NXOrderStrings([detail title], [current cString],
  129.                     YES, -1, NULL))
  130.                     detail = nil;
  131.             }
  132.         }
  133.  
  134.         if (detail) {
  135.  
  136.             if ([detail tag]) {                
  137.             // if there is a sort order imposed...
  138.  
  139.                 ordering = [[EOAttributeOrdering allocWithZone:[self zone]]
  140.                     initWithAttribute:[entity attributeNamed:current]
  141.                     ordering:[detail tag]];
  142.  
  143.                 [(NSMutableArray *)orderArray addObject: ordering];
  144.             }
  145.         }
  146.     }
  147.  
  148.     return orderArray;        
  149. }        
  150.  
  151.  
  152. - _sortedFetch
  153. {
  154.     NSArray *order;
  155.  
  156.     if ([priority selectedRow] == 0)
  157.          order = [[self _orderArrayFromList] retain];
  158.     else
  159.          order = [[self _orderArrayFromTable] retain];
  160.  
  161.     [window disableDisplay];
  162.     [(EODatabaseDataSource *)[controller dataSource]
  163.             setFetchOrder: order];
  164.     [controller fetch];
  165.     [order release];
  166.     [window reenableDisplay];
  167.     [window display];
  168.     return self;
  169. }
  170.     
  171.  
  172. - changeOrder:sender
  173. {
  174.     id current;
  175.  
  176.     current = [attributeOrder selectedCell];
  177.  
  178.     switch ([[sender selectedCell] tag]) {
  179.     case 0:
  180.         [current setIcon: "low"];
  181.         [current setTag:1];
  182.         break;
  183.     case 1:
  184.         [current setIcon: "high"];
  185.         [current setTag:2];
  186.         break;
  187.     case 2:
  188.         [current setIcon: "empty"];
  189.         [current setTag:0];
  190.         break;
  191.     }
  192.  
  193.     [attributeOrder display];
  194.     return self;
  195. }
  196.  
  197.  
  198. - doIt: sender
  199. {
  200.     [self _sortedFetch];
  201.     return self;
  202. }
  203.  
  204.  
  205. - ofSorts:sender
  206. {
  207.     [window makeKeyAndOrderFront:nil];    
  208.     return self;
  209. }
  210.  
  211.  
  212. @end
  213.