home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Developer / StopWatch / Source / ExpenseEditor.m < prev    next >
Encoding:
Text File  |  1993-07-23  |  2.4 KB  |  141 lines

  1. /*
  2.  * For legal stuff see the file COPYRIGHT
  3.  */
  4. #import "ExpenseEditor.h"
  5. #import "Controller.h"
  6.  
  7. @implementation ExpenseEditor
  8.  
  9. - awakeFromNib
  10. {
  11.   [descriptionField setScrollable:YES];
  12.   return self;
  13. }
  14.  
  15. /*
  16.  * Allow only one ExpenseEditor object to be created.
  17.  */
  18. + new
  19. {
  20.   static id editor;
  21.  
  22.   if ( ! editor ) {
  23.     editor = [[ExpenseEditor alloc] init];
  24.     [NXApp loadNibSection:"ExpenseEditor.nib" owner:editor withNames:NO];
  25.   }
  26.  
  27.   return editor;
  28. }
  29.  
  30. - init
  31. {
  32.   [super init];
  33.   return self;
  34. }
  35.  
  36. - free
  37. {
  38.   return [super free];
  39. }
  40.  
  41. - (float)amount
  42. {
  43.   return [amountField floatValue];
  44. }
  45.  
  46. - (const char *)dateString
  47. {
  48.   return [dateField stringValue];
  49. }
  50.  
  51. - (const char *)description
  52. {
  53.   return [descriptionField stringValue];
  54. }
  55.  
  56. - (void)clearFields
  57. {
  58.   [dateField        setStringValue:""];
  59.   [descriptionField setStringValue:""];
  60.   [amountField      setStringValue:""];
  61. }
  62.  
  63. /*
  64.  * Display the contents of an expense object
  65.  */
  66. - (void)loadExpense:(Expense *)expense
  67. {
  68.   if ( expense == nil ) {
  69.     [self clearFields];
  70.     [dateField setStringValue:currentDate()];
  71.   } else {
  72.     [dateField        setStringValue:[expense dateString]];
  73.     [descriptionField setStringValue:[expense description]];
  74.     [amountField      setFloatValue: [expense amount]];
  75.   }
  76. }
  77.  
  78. /*
  79.  * Copy the data from the form into the expense object. If the
  80.  * object is nil, allocate one and return it.
  81.  */
  82. - (Expense *)saveExpense:(Expense *)expense
  83. {
  84.   if ( expense == nil )
  85.     expense = [[Expense alloc] init];
  86.   
  87.   [expense setDateString: [dateField stringValue]];
  88.   [expense setDescription:[descriptionField stringValue]];
  89.   [expense setAmount:     [amountField floatValue]];
  90.  
  91.   return expense;
  92. }
  93.  
  94. /*
  95.  * This routine should check that there is enough data to be useful...
  96.  */
  97. - (Expense *)editItem:(Expense *)expense
  98. {
  99.   int value ;
  100.  
  101.   [self loadExpense:expense];
  102.   [form selectTextAt:0];    /* always select the first field */
  103.  
  104.   value = [NXApp runModalFor:panel] ;
  105.   [panel close];
  106.  
  107.   switch ( value  ) {
  108.   case NX_RUNSTOPPED:
  109.     return [self saveExpense:expense];    /* copy data back into struct */
  110.  
  111.   default:
  112.   case NX_RUNABORTED:
  113.     return nil;
  114.   }
  115. }
  116.  
  117. - cancel:sender
  118. {
  119.   [NXApp abortModal];
  120.   return self;
  121. }
  122.  
  123. - ok:sender
  124. {
  125.   int dummy;
  126.  
  127.   /* This is pretty cheesey. */
  128.   if ( sscanf( [dateField stringValue], "%d/%d/%d",
  129.           &dummy, &dummy, &dummy ) != 3 ) {
  130.     NXRunAlertPanel( [NXApp name], "Valid date (mm/dd/yy) required.",
  131.             "Pardon me", NULL, NULL );
  132.   } else
  133.     [NXApp stopModal];
  134.  
  135.   return self;
  136. }
  137.  
  138. @end
  139.  
  140.  
  141.