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

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