home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 (1993) / nebula.bin / SourceCode / MiniExamples / FilterFunctions / DateTextField.m < prev    next >
Encoding:
Text File  |  1991-09-29  |  2.3 KB  |  78 lines

  1.  
  2. /*
  3.  *      You may freely copy, distribute and reuse the code
  4.  *      in this example.  NeXT disclaims any warranty of
  5.  *      any kind, expressed or implied, as to its fitness
  6.  *      for any particular use.
  7.  *
  8.  *      Written by: Susan Rayl
  9.  *
  10.  *      Created 21-Mar-91
  11.  */
  12.  
  13. #import "DateTextField.h"
  14. #import "DateTFCell.h"
  15. #import <strings.h>
  16.  
  17. @implementation DateTextField
  18.  
  19. /* Set the Cell for this class of Control to be a DateTFCell.  The DateTFCell
  20.  * will change the text filter function of the Text object acting as the 
  21.  * fieldEditor for this DateTextField.  Do this in the Cell since there are 
  22.  * methods there specifically for editing the text in the Cell and thus for 
  23.  * managing the Text object which supports the Cell.
  24.  * 
  25.  * This is a better design . . . isn't it?  Well, it works.  But there's a 
  26.  * BETTER design.  The functionality in the DateTFCell class should be 
  27.  * placed in an abstract class, called FilterTextCell, and subclasses 
  28.  * should be made from this class for specific filtering functionality.  See 
  29.  * the implementation of the TimeTFCell and SSTFCell classes for this preferred 
  30.  * solution.
  31.  */
  32. + initialize
  33. {
  34.     /* do whatever the superclass would do */
  35.     [super initialize];
  36.     
  37.     /* set a custom Cell class for this Control subclass */
  38.     [DateTextField setCellClass:[DateTFCell class]];
  39.     return self;
  40. }
  41.  
  42. - textDidEnd:anObject endChar:(unsigned short)whyEnd
  43. {
  44.     /* textDidEnd:endChar: should call endEditing: on the Control's cell to 
  45.      * remove the fieldEditor from the View hierarchy.
  46.      */
  47.     [[self cell] endEditing:anObject];
  48.     
  49.     /* do whatever the superclass does but do it AFTER you have changed the
  50.      * Text object.
  51.      */
  52.     [super textDidEnd:anObject endChar:whyEnd];
  53.     return self;
  54. }
  55.  
  56. - textWillEnd:textObject
  57. {
  58.     char *currVal;
  59.     
  60.     /* get the current value in the DateTextField */
  61.     currVal = (char *)[self stringValue];
  62.     
  63.     /* if the string is 8 chars long then, because it has been filtered, it 
  64.      * must completely and correctly specify a date string.  Else, the user 
  65.      * has terminated the editing of the field before completing the date.  
  66.      * By returning nil, the DateTextField will remain the firstResponder 
  67.      * and the user must finish entering the date.
  68.      */
  69.     if (strlen(currVal) == 8) {
  70.         return nil;
  71.     } else {
  72.         [super textWillEnd:textObject];
  73.         return self;
  74.     }
  75. }
  76.  
  77. @end
  78.