home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / DEV.Z / TAController.m < prev    next >
Encoding:
Text File  |  1996-07-30  |  10.2 KB  |  357 lines

  1. /*
  2.   You may freely copy, distribute, and reuse the code in this example.
  3.   NeXT disclaims any warranty of any kind, expressed or implied, as to its
  4.   fitness for any particular use.
  5. */
  6.  
  7.  
  8. #import "TAController.h"
  9. #import "Converter.h"
  10. @implementation TAController
  11.  
  12. - (void)addRecord:(id)sender
  13. {
  14.     Country *aCountry;
  15.     NSString *countryName = [countryField stringValue];
  16.     
  17.     /* look up country name in dictionary */
  18.     /* if exists, remove record in dictionary, else create new one*/
  19.  
  20.     if (countryName && (![countryName isEqualToString:@""])) {
  21.         aCountry = [countryDict objectForKey:countryName];
  22.  
  23.         if (aCountry && recordNeedsSaving) {
  24.            /* remove old Country object from dictionary */
  25.            [countryDict removeObjectForKey:[aCountry name]];
  26.          }
  27.         else if (!aCountry)  /* record is new */
  28.             aCountry = [[Country alloc] init];
  29.         else return;   /* record already exists and hasn't changed */
  30.  
  31.         /* extract field values and put in dictionary and keys array */
  32.         [self extractFields:aCountry];
  33.         [countryDict setObject:aCountry forKey:[aCountry name]];
  34.         [countryKeys addObject:[aCountry name]];
  35.         
  36.         /* sort array */
  37.         [countryKeys sortUsingSelector:@selector(compare:)];
  38.         
  39.         recordNeedsSaving=NO;
  40.         [commentsLabel setStringValue:[NSString stringWithFormat:@"Notes and Itinerary for %@", [countryField stringValue]]];
  41.         [countryField selectText:self];
  42.         [tableView reloadData];
  43.         [tableView selectRow:[countryKeys indexOfObject:[aCountry name]] byExtendingSelection:NO];
  44.  
  45.     }
  46.     return;
  47.  
  48. }
  49.  
  50. - (void)blankFields:(id)sender
  51. {
  52.     [countryField setStringValue:@""];
  53.  
  54.     [[logisticsForm cellAtIndex:LGairports] setStringValue:@""];
  55.     [[logisticsForm cellAtIndex:LGairlines] setStringValue:@""];
  56.     [[logisticsForm cellAtIndex:LGtransportation] setStringValue:@""];
  57.     [[logisticsForm cellAtIndex:LGhotels] setStringValue:@""];
  58.  
  59.     [currencyNameField setStringValue:@""];
  60.     [currencyRateField setFloatValue:0.000];
  61.     [languagesField setStringValue:@""];
  62.     [englishSpokenSwitch setState:NO];
  63.  
  64.     [currencyDollarsField setFloatValue:0.00];
  65.     [currencyLocalField setFloatValue:0.00];
  66.     [celsius setIntValue:0];
  67.     [fahrenheit setIntValue:0];
  68.  
  69.     [commentsField setString:@""];
  70.  
  71.     [countryField selectText:self];
  72.  
  73.     return;
  74. }
  75.  
  76. - (void)populateFields:(Country *)aRec
  77. {
  78.     [countryField setStringValue:[aRec name]];
  79.  
  80.     [[logisticsForm cellAtIndex:LGairports] setStringValue:[aRec airports]];
  81.     [[logisticsForm cellAtIndex:LGairlines] setStringValue:[aRec airlines]];
  82.     [[logisticsForm cellAtIndex:LGtransportation] setStringValue:[aRec transportation]];
  83.     [[logisticsForm cellAtIndex:LGhotels] setStringValue:[aRec hotels]];
  84.  
  85.     [currencyNameField setStringValue:[aRec currencyName]];
  86.     [currencyRateField setFloatValue:[aRec currencyRate]];
  87.     [languagesField setStringValue:[aRec languages]];
  88.     [englishSpokenSwitch setState:[aRec englishSpoken]];
  89.  
  90.     [commentsField setString:[aRec comments]];
  91.  
  92.     [countryField selectText:self];
  93.  
  94.     return;
  95.  
  96. }
  97.  
  98. - (void)extractFields:(Country *)aRec
  99. {
  100.     [aRec setName:[countryField stringValue]];
  101.  
  102.     [aRec setAirports:[[logisticsForm cellAtIndex:LGairports] stringValue]];
  103.     [aRec setAirlines:[[logisticsForm cellAtIndex:LGairlines] stringValue]];
  104.     [aRec setTransportation:[[logisticsForm cellAtIndex:LGtransportation] stringValue]];
  105.     [aRec setHotels:[[logisticsForm cellAtIndex:LGhotels] stringValue]];
  106.  
  107.     [aRec setCurrencyName:[currencyNameField stringValue]];
  108.     [aRec setCurrencyRate:[currencyRateField floatValue]];
  109.     [aRec setLanguages:[languagesField stringValue]];
  110.     [aRec setEnglishSpoken:[englishSpokenSwitch state]];
  111.  
  112.     [aRec setComments:[commentsField string]];
  113.      
  114.     return;
  115. }
  116.  
  117.  
  118. - (void)convertCelsius:(id)sender
  119. {
  120.     
  121.     return;
  122. }
  123.  
  124. - (void)convertCurrency:(id)sender
  125. {
  126.  
  127.     [currencyLocalField setFloatValue:[converter convertAmount:[currencyDollarsField floatValue] byRate:[currencyRateField floatValue]]];
  128.  
  129.     return;
  130. }
  131.  
  132. - (void)deleteRecord:(id)sender
  133. {
  134.     Country *aCountry;
  135.     NSString *countryName = [countryField stringValue];
  136.     
  137.     if (countryName && (![countryName isEqualToString:@""])) {
  138.         aCountry = [countryDict objectForKey:countryName];
  139.  
  140.         if (aCountry) {
  141.             [countryDict removeObjectForKey:countryName];
  142.             [countryKeys removeObject:countryName];
  143.             [self blankFields:self];
  144.             [tableView reloadData];
  145.         }
  146.     }    
  147.     return;
  148. }
  149.  
  150. - (void)switchChecked:(id)sender { recordNeedsSaving = YES; }
  151.  
  152. - (void)handleTVClick:(id)sender
  153. {
  154.     Country *aRec, *newRec, *newerRec;
  155.     int index = [sender selectedRow];
  156.  
  157.     /* does current obj need to be saved? */
  158.     if (recordNeedsSaving) {
  159.         /* is current object already in dictionary? */
  160.         if ( aRec = [countryDict objectForKey:[countryField stringValue]] ) {
  161.             /* remove if it's been changed */
  162.             if (aRec) {
  163.                 [countryDict removeObjectForKey:[aRec name]];
  164.                 [countryKeys removeObject:[aRec name]];
  165.             }
  166.         }
  167.  
  168.         /* Create Country obj, add it to dict and add name to keys array */
  169.         newRec = [[Country alloc] init];
  170.         [self extractFields:newRec];
  171.         [countryDict setObject:newRec forKey:[countryField stringValue]];
  172.         [newRec release];
  173.         [countryKeys addObject:[countryField stringValue]];
  174.  
  175.         /* sort array here */
  176.         [countryKeys sortUsingSelector:@selector(compare:)];
  177.         [tableView reloadData];
  178.     }
  179.     if (index >= 0 && index < [countryKeys count]) {
  180.         newerRec = [countryDict objectForKey:[countryKeys objectAtIndex:index]];
  181.         [self populateFields:newerRec];
  182.         [commentsLabel setStringValue:[NSString stringWithFormat:
  183.                @"Notes and Itinerary for %@", [countryField stringValue]]];
  184.         recordNeedsSaving=NO;
  185.     }
  186. }
  187.  
  188. - (void)nextRecord:(id)sender
  189. {
  190.     int r;
  191.  
  192.     r = [tableView selectedRow];
  193.     if (r == [countryKeys indexOfObject:[countryKeys lastObject]])
  194.         r = 0; /* wrap to beginning */
  195.     else
  196.         r++;
  197.     [tableView selectRow:r byExtendingSelection:NO];
  198.     [self handleTVClick:tableView];
  199.     
  200.     return;
  201. }
  202.  
  203. - (void)priorRecord:(id)sender
  204. {
  205.     int r;
  206.  
  207.     r = [tableView selectedRow];
  208.     if (r == 0)
  209.         /* wrap to end */
  210.         r = [countryKeys indexOfObject:[countryKeys lastObject]]; 
  211.     else
  212.         r--;
  213.     [tableView selectRow:r byExtendingSelection:NO];
  214.     [self handleTVClick:tableView];
  215.     
  216.     return;
  217. }
  218.  
  219.  
  220. /* ** Enabling/disabling menu items, an alternative to "wrapping"
  221.    ** the selection in nextRecord: and priorRecord:.
  222.    ** Uncomment to see what happens.
  223.    ** See NSMenuActionResponder protocol spec for explanation */
  224.  
  225. /*   
  226. - (BOOL)validateMenuItem:(NSMenuItem *)anItem
  227. {
  228.     int row = [tableView selectedRow];
  229.     if ([[anItem title] isEqualToString:@"Next Record"] &&
  230.         (row == [countryKeys indexOfObject:[countryKeys lastObject]])) {
  231.          return NO;
  232.     }
  233.     if ([[anItem title] isEqualToString:@"Prior Record"] && row == 0 ) {
  234.          return NO;
  235.     }
  236.     return YES;
  237. }
  238. **************************************************** */
  239.  
  240. - (id)init
  241. {
  242.     NSString *storePath = [[NSBundle mainBundle] pathForResource:@"TravelData" ofType:nil];
  243.     [super init];
  244.  
  245.     countryDict = [NSUnarchiver unarchiveObjectWithFile:storePath];
  246.  
  247.     if (!countryDict) {
  248.         countryDict = [[NSMutableDictionary alloc] initWithCapacity:10];
  249.         countryKeys = [[NSMutableArray alloc] initWithCapacity:10];
  250.     } else
  251.         countryDict = [countryDict retain];
  252.     
  253.     recordNeedsSaving=NO;
  254.     
  255.     return self;
  256. }
  257.  
  258. - (void)dealloc
  259. {
  260.     [countryDict release];
  261.     [countryKeys release];
  262.     [super dealloc];
  263. }
  264.  
  265. - (void)awakeFromNib
  266. {
  267.     NSArray *tmpArray = [[countryDict allKeys] /* 1 */
  268.                          sortedArrayUsingSelector:@selector(compare:)];
  269.     countryKeys = [[NSMutableArray alloc] initWithArray:tmpArray];
  270.  
  271.     /* select first field */
  272.     [countryField selectText:self];
  273.     
  274.     [[NSNotificationCenter defaultCenter] addObserver:self
  275.                           selector:@selector(textDidChange:)
  276.                           name:@"NSControlTextDidChangeNotification" object:nil];
  277.     
  278.     [tableView setDataSource:self];
  279.     [tableView setDelegate:self];
  280.     [tableView sizeLastColumnToFit];
  281.  
  282.     [commentsField setDelegate:self];
  283.     [currencyRateField setDelegate:self];
  284.  
  285.     [[currencyRateField cell] setEntryType:NSFloatType];
  286.     [[currencyRateField cell] setFloatingPointFormat:YES left:2 right:1];       
  287.     [[currencyDollarsField cell] setEntryType:NSFloatType];
  288.     [[currencyDollarsField cell] setFloatingPointFormat:YES left:5 right:2];       
  289.     [[currencyLocalField cell] setEntryType:NSFloatType];
  290.     [[currencyLocalField cell] setFloatingPointFormat:YES left:5 right:2];
  291.     [[celsius cell] setEntryType:NSFloatType];
  292.     [[celsius cell] setFloatingPointFormat:YES left:2 right:1];
  293. }
  294.  
  295. /* NSTableView data source methods */
  296.  
  297. - (int)numberOfRowsInTableView:(NSTableView *)theTableView
  298. {
  299.     return [countryKeys count];
  300. }
  301.  
  302. - (id)tableView:(NSTableView *)theTableView
  303.       objectValueForTableColumn:(NSTableColumn *)theColumn
  304.             row:(int)rowIndex
  305. {
  306.     if ([[theColumn identifier] intValue]==0)
  307.         return [countryKeys objectAtIndex:rowIndex];
  308.     else
  309.         return nil;
  310. }
  311.       
  312.     
  313.  
  314. /* Delegation and notification methods */
  315.  
  316. - (void)textDidChange:(NSNotification *)notification
  317. {
  318.     recordNeedsSaving=YES;
  319. }
  320.  
  321. - (void)controlTextDidChange:(NSNotification *)notification
  322. {
  323.     recordNeedsSaving=YES;
  324. }
  325.  
  326. - (BOOL)textShouldBeginEditing:(NSText *)textObj
  327. {
  328.     recordNeedsSaving=YES;
  329.     return YES;
  330. }
  331.  
  332. - (BOOL)applicationShouldTerminate:(id)sender
  333. {
  334.     NSString *storePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"TravelData"];
  335.     /* save current record if it is new or changed */
  336.    [self addRecord:self];
  337.  
  338.    /* save dictionary (if it has contents) to archive file */
  339.    if (countryDict && [countryDict count])
  340.         [NSArchiver archiveRootObject:countryDict toFile:storePath];
  341.  
  342.     return YES;
  343. }
  344.  
  345. - (BOOL)control:(NSControl *)control isValidObject:(id)obj
  346. {
  347.     if (control == currencyRateField) {
  348.         if ([obj floatValue] < 0.0) {
  349.             NSRunAlertPanel(@"Travel Advisor", @"Rate cannot be negative.", nil, nil, nil);
  350.             return NO;
  351.         }
  352.     }
  353.     return YES;
  354. }
  355.  
  356. @end
  357.