home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP Advantage / NeXTstep_Advantage.img / YourCall / CallController.m < prev    next >
Text File  |  1993-04-14  |  7KB  |  226 lines

  1. /*    You may freely copy, distribute and reuse the code in this example.
  2.  *    NeXT disclaims any warranty of any kind, expressed or implied, as to
  3.  *    its fitness for any particular use.
  4.  */
  5.  
  6. #import "CallController.h"
  7.  
  8. @implementation CallController : Object
  9.  
  10. /* 
  11.  * Purpose: Initialize the CallController
  12.  *
  13.  * Inherited from Object, overridden by CallController
  14.  * to initialize callTable--the HashTable that holds CallRecords.  
  15.  *
  16.  * Looks for the file "call.log" in the application's main bundle.
  17.  * If not found, concatenates the filename "call.log" to the path 
  18.  * to the main bundle, so that the file may be saved in the main bundle 
  19.  * by the saveCall: method.    
  20.  * If the file "call.log" in the main bundle can be opened as a typed stream, 
  21.  * reads the stream, and assigns its contents (a HashTable containing 
  22.  * CallRecords) to the callTable instance variable.
  23.  * If the file can't be opened, creates a new instance of HashTable and 
  24.  * assigns it to the callTable. 
  25.  */
  26.  
  27. - init
  28. {
  29.     NXTypedStream *callStream;
  30.     BOOL fileFound;
  31.  
  32.     [super init];
  33.     fileFound = [[NXBundle mainBundle] 
  34.     getPath:callFilePath forResource:"call" ofType:"log"];
  35.     if (!fileFound) {
  36.     strcat(callFilePath,"/call.log"); // used in saveCall: as the file path
  37.     }
  38.     callStream = NXOpenTypedStreamForFile(callFilePath, NX_READONLY);
  39.     if (callStream) {
  40.     callTable = NXReadObject(callStream);
  41.     NXCloseTypedStream(callStream);
  42.     }
  43.     else {
  44.     callTable = [[HashTable alloc] initKeyDesc:"*" valueDesc:"@"];
  45.     }
  46.     return self;
  47. }
  48.  
  49.  
  50. /* Purpose: Initialize YourCall's user interface after unarchiving
  51.  *
  52.  * This method is an "informal protocol" implemented CallController.
  53.  * An awakeFromNib message is sent to each object in a user interface archive
  54.  * after all objects have been unarchived.
  55.  * Through this method, CallController prepares the user interface to accept 
  56.  * input.  
  57.  * This code first selects the first field in the customer form.
  58.  * It then puts the Call Information window in front of any others on 
  59.  * the screen.
  60.  */
  61.  
  62. - awakeFromNib
  63. {
  64.   [customerForm selectText:self];
  65.   [[customerForm window] makeKeyAndOrderFront:self];
  66.   return self;
  67. }
  68.  
  69.  
  70. /*
  71.  * Purpose: Retrieve a call from the database.  
  72.  *
  73.  * Takes the customer name currently entered in customerFrom, 
  74.  * looks up the corresponding CallRecord in the callTable HashTable, and
  75.  * displays that record in the call form with the name field selected.  
  76.  * If no such record is found, or if no name is entered, 
  77.  * displays an attention panel.  
  78.  */
  79.  
  80. - retrieveCall:sender
  81. {
  82.     const char *fetchName;
  83.     CallRecord *fetchRecord = nil;
  84.  
  85.     fetchName = [customerForm stringValueAt:0]; 
  86.     if (fetchName && strlen(fetchName)) {
  87.     fetchRecord = [callTable valueForKey:fetchName];
  88.     if (fetchRecord) {
  89.         [customerForm setStringValue:[fetchRecord street] at:1]; 
  90.         [customerForm setStringValue:[fetchRecord city] at:2]; 
  91.         [customerForm setStringValue:[fetchRecord state] at:3]; 
  92.         [customerForm setStringValue:[fetchRecord phone] at:4]; 
  93.         [questionText setStringValue:[fetchRecord question]];
  94.         [answerText setStringValue:[fetchRecord answer]];
  95.         [customerForm selectText:self];
  96.     }
  97.     else {
  98.         NXRunAlertPanel("Search Failed", "Customer %s not found", 
  99.         NULL, NULL, NULL, fetchName);
  100.     }
  101.     }
  102.     else {
  103.     NXRunAlertPanel("Search Failed", "Please enter a customer name", 
  104.         NULL, NULL, NULL);
  105.     }
  106.     return self;
  107. }
  108.  
  109.  
  110. /*
  111.  * Purpose: Save the information in the form to the database
  112.  *
  113.  * Reads the name from the form and tests whether it contains a valid string. 
  114.  * If the name string exists, creates a new CallRecord object, 
  115.  * reads data from the Call Information form, writes it in the new CallRecord,
  116.  * and stores the CallRecord in the callTable, using customer name as the key. 
  117.  * Opens the typed stream callStream, writes the callTable to the stream,  
  118.  * then saves the stream in the file "call.log" in the application's 
  119.  * main bundle.
  120.  */
  121.  
  122. - saveCall:sender
  123. {
  124.     const char *formName = NULL;
  125.     CallRecord *newRecord;
  126.     NXTypedStream *callStream;
  127.  
  128.     formName = [customerForm stringValueAt:0];
  129.     if (formName && strlen(formName)){
  130.     newRecord = [[CallRecord alloc] init];
  131.     [newRecord setName:[customerForm stringValueAt:0]];
  132.     [newRecord setStreet:[customerForm stringValueAt:1]];
  133.     [newRecord setCity:[customerForm stringValueAt:2]];
  134.     [newRecord setState:[customerForm stringValueAt:3]];
  135.     [newRecord setPhone:[customerForm stringValueAt:4]];
  136.     [newRecord setQuestion:[questionText stringValue]];
  137.      [newRecord setAnswer:[answerText stringValue]];
  138.     [callTable insertKey:[newRecord name] value:newRecord]; 
  139.     callStream = NXOpenTypedStreamForFile(callFilePath, NX_WRITEONLY);
  140.     if (callStream) {
  141.         NXWriteObject(callStream, callTable);
  142.         NXCloseTypedStream(callStream);
  143.     }
  144.     return self;
  145.     }
  146.     else return nil;
  147. }
  148.  
  149.  
  150. /*
  151.  * Purpose: Begin a new call entry
  152.  *
  153.  * Clears the text in the Call Information form by setting all entries to NULL, 
  154.  * without saving the data.  
  155.  * Places the cursor in the first field of the customerForm.  
  156.  */
  157.  
  158. - clearForm:sender;
  159. {
  160.     [customerForm setStringValue:NULL at:0];
  161.     [customerForm setStringValue:NULL at:1];
  162.     [customerForm setStringValue:NULL at:2];
  163.     [customerForm setStringValue:NULL at:3];
  164.     [customerForm setStringValue:NULL at:4];
  165.     [questionText setStringValue:NULL];
  166.     [answerText setStringValue:NULL];
  167.     [customerForm selectText:self];
  168.     return self;
  169. }
  170.  
  171.  
  172. /*
  173.  * Purpose: Display the application's Info panel
  174.  *
  175.  * Displays the panel, loading its interface file first if it hasn't 
  176.  * yet been loaded.
  177.  *
  178.  * To add this enhancement to your application (after completing the  
  179.  * steps in Chapter 2):
  180.  *
  181.  * 1.  Open the YourCall.nib file in Interface Builder.  
  182.  *
  183.  * 2.  From the Interface Builder menu, choose Project/New Module/New InfoPanel 
  184.  *
  185.  * 3.  Modify the contents of the panel.
  186.  *
  187.  * 4.  Save the panel as "InfoPanel.nib"
  188.  *
  189.  * 5.  Open the CallController.h and CallController.m files and add this 
  190.  *     method.
  191.  *
  192.  * 6.  From the files display in Interface Builder, Choose Classes, 
  193.  *     then CallController, then Parse
  194.  * 
  195.  * 7.  Control drag from the menu's Info item to the CallController
  196.  *
  197.  * 8.  Double-click on CallController's -showInfoPanel: method
  198.  *
  199.  * 9.  Save the interface file in Interface Builder, then click Yes in the
  200.  *     attention panel to add the file to the project.  
  201.  */
  202.  
  203. - showInfoPanel:sender
  204. {
  205.     if (!infoPanel)
  206.         [NXApp loadNibSection:"InfoPanel.nib" owner:self];
  207.     [infoPanel makeKeyAndOrderFront:self];
  208.     return self;
  209. }
  210.  
  211.  
  212. /* 
  213.  * Purpose: Free the CallController
  214.  *
  215.  * Inherited from Object, overridden by CallController to
  216.  * free the callTable HashTable, which in turn frees all CallRecords.  
  217.  */
  218.  
  219. - free
  220. {
  221.     [callTable free];
  222.     return [super free];
  223. }
  224.  
  225. @end
  226.