home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / Palettes / MORegex / MORegexPalette.m < prev    next >
Encoding:
Text File  |  1993-10-04  |  8.0 KB  |  264 lines

  1. // MORegexPalette.m
  2. //
  3. // by Mike Ferris
  4. // Part of MOKit - MORegexPalette
  5. // Copyright 1993, all rights reserved.
  6.  
  7. #import "MORegexPalette.h"
  8. #import "MOKit/MORegexFormCell.h"
  9. #import "MOKit/MORegexTextCell.h"
  10. #import <appkit/appkit.h>
  11. #import <objc/objc-runtime.h>
  12.  
  13. #define CLASS_VERSION    0
  14.  
  15. #define BUNDLE_TYPE                            "bundle"
  16.  
  17. #define MOSTRING_CLASS_NAME                    "MOString"
  18. #define MOREGEXFORMCELL_CLASS_NAME            "MORegexFormCell"
  19. #define MOREGEXTEXTCELL_CLASS_NAME            "MORegexTextCell"
  20. #define MODATEFORMCELL_CLASS_NAME            "MODateFormCell"
  21. #define MODATETEXTCELL_CLASS_NAME            "MODateTextCell"
  22.  
  23.  
  24. #define FORM_GENERIC_NAME                    "Generic:"
  25. #define FORM_INT_NAME                        "Integer:"
  26. #define FORM_FLOAT_NAME                        "Float:"
  27. #define FORM_TIME_NAME                        "Time:"
  28. #define FORM_DATE_NAME                        "Date:"
  29.  
  30. #define INT_REGEX        "^[ ]*([+-]?[0-9]+)[ ]*$"
  31.  
  32. #define FLOAT_REGEX        "^[ ]*(([+-]?[0-9]*).?([0-9]*)[eE]?([0-9]*))[ ]*$"
  33.  
  34. #define TIME_REGEX_1    "^[ ]*(([0]?[0-9]|[1][0-2]):([0-5][0-9])[ ]*([aA][mM]?|[pP][mM]?))[ ]*$"
  35. #define TIME_REGEX_2    "^[ ]*(([0]?[0-9]|[1][0-9]|[2][0-3]):([0-5][0-9])())[ ]*$"
  36.  
  37.  
  38. @implementation  MORegexPalette
  39.  
  40. static id MOStringClass;
  41. static id MORegexFormCellClass;
  42. static id MORegexTextCellClass;
  43. static id MODateFormCellClass;
  44. static id MODateTextCellClass;
  45.  
  46. + MO_loadClassBundle:(const char *)className
  47. // Finds the bundle of the same name as the class, grabs it and loads the
  48. // class from it.
  49. {
  50.     char pathBuff[MAXPATHLEN+1];
  51.     
  52.     // Load the bundle
  53.     if (objc_lookUpClass(className) == nil)  {
  54.         // class is not already loaded... load it.
  55.         id classBundle;
  56.         
  57.         // Look for the bundle in the main bundle first, 
  58.         // else try in this classes bundle.
  59.         if (![[NXBundle mainBundle] getPath:pathBuff forResource:className 
  60.                     ofType:BUNDLE_TYPE])  {
  61.             if (![[NXBundle bundleForClass:[self class]] getPath:pathBuff 
  62.                         forResource:className ofType:BUNDLE_TYPE])  {
  63.                 NXLogError("[MORegexPalette loadClassBundle] failed to "
  64.                         "find %s class bundle.", className);
  65.                 return nil;
  66.             }
  67.         }
  68.         classBundle = [[NXBundle allocFromZone:[self zone]] 
  69.                     initForDirectory:pathBuff];
  70.         if (!classBundle)  {
  71.             NXLogError("[MORegexPalette loadClassBundle] failed to "
  72.                         "create bundle for class %s.", className);
  73.             return nil;
  74.         }
  75.         if (![classBundle classNamed:className])  {
  76.             NXLogError("[MORegexPalette loadClassBundle] failed to "
  77.                         "load %s class from bundle.", className);
  78.             return nil;
  79.         }
  80.     }
  81.     
  82.     return self;
  83. }
  84.  
  85. + initialize
  86. // Set the version.
  87. {
  88.     if (self == [MORegexPalette class])  {
  89.         [self setVersion:CLASS_VERSION];
  90.  
  91.         // Load the MOString class if necessary
  92.         [self MO_loadClassBundle:MOSTRING_CLASS_NAME];
  93.         MOStringClass = 
  94.                     objc_lookUpClass(MOSTRING_CLASS_NAME);
  95.         if (!MOStringClass)  {
  96.             NXLogError("[MORegexPalette initialize]: failed to find "
  97.                         "MOString class.");
  98.         }
  99.         // Load the MORegexFormCell class if necessary
  100.         [self MO_loadClassBundle:MOREGEXFORMCELL_CLASS_NAME];
  101.         MORegexFormCellClass = 
  102.                     objc_lookUpClass(MOREGEXFORMCELL_CLASS_NAME);
  103.         if (!MORegexFormCellClass)  {
  104.             NXLogError("[MORegexPalette initialize]: failed to find "
  105.                         "MORegexFormCell class.");
  106.         }
  107.         // Load the MORegexTextCell class if necessary
  108.         [self MO_loadClassBundle:MOREGEXTEXTCELL_CLASS_NAME];
  109.         MORegexTextCellClass = 
  110.                     objc_lookUpClass(MOREGEXTEXTCELL_CLASS_NAME);
  111.         if (!MORegexTextCellClass)  {
  112.             NXLogError("[MORegexPalette initialize]: failed to find "
  113.                         "MORegexTextCell class.");
  114.         }
  115.         // Load the MODateFormCell class if necessary
  116.         [self MO_loadClassBundle:MODATEFORMCELL_CLASS_NAME];
  117.         MODateFormCellClass = 
  118.                     objc_lookUpClass(MODATEFORMCELL_CLASS_NAME);
  119.         if (!MODateFormCellClass)  {
  120.             NXLogError("[MORegexPalette initialize]: failed to find "
  121.                         "MODateFormCell class.");
  122.         }
  123.         // Load the MODateTextCell class if necessary
  124.         [self MO_loadClassBundle:MODATETEXTCELL_CLASS_NAME];
  125.         MODateTextCellClass = 
  126.                     objc_lookUpClass(MODATETEXTCELL_CLASS_NAME);
  127.         if (!MODateTextCellClass)  {
  128.             NXLogError("[MORegexPalette initialize]: failed to find "
  129.                         "MODateTextCell class.");
  130.         }
  131.     }
  132.     return self;
  133. }
  134.  
  135. - finishInstantiate
  136. // First load up the class bundles, one for each included class.
  137. // Then set up all the palette controls.
  138. {
  139.     id protoCell;
  140.     NXSize size;
  141.         
  142.     // Set up the generic MORegexFormCell
  143.     protoCell = [[MORegexFormCellClass allocFromZone:[self zone]] 
  144.                         initTextCell:NULL];
  145.     [genericForm setPrototype:protoCell];
  146.     [genericForm addEntry:FORM_GENERIC_NAME];
  147.     [genericForm sizeToFit];
  148.  
  149.     // Set up the integer MORegexFormCell
  150.     protoCell = [[MORegexFormCellClass allocFromZone:[self zone]] 
  151.                         initTextCell:NULL];
  152.     [protoCell addRegexStr:INT_REGEX];
  153.     [intForm setPrototype:protoCell];
  154.     [intForm addEntry:FORM_INT_NAME];
  155.     [intForm sizeToFit];
  156.  
  157.     // Set up the float MORegexFormCell
  158.     protoCell = [[MORegexFormCellClass allocFromZone:[self zone]] 
  159.                         initTextCell:NULL];
  160.     [protoCell addRegexStr:FLOAT_REGEX];
  161.     [floatForm setPrototype:protoCell];
  162.     [floatForm addEntry:FORM_FLOAT_NAME];
  163.     [floatForm sizeToFit];
  164.  
  165.     // Set up the time MORegexFormCell
  166.     protoCell = [[MORegexFormCellClass allocFromZone:[self zone]] 
  167.                         initTextCell:NULL];
  168.     [protoCell addRegexStr:TIME_REGEX_1];
  169.     [protoCell addRegexStr:TIME_REGEX_2];
  170.     [timeForm setPrototype:protoCell];
  171.     [timeForm addEntry:FORM_TIME_NAME];
  172.     [timeForm sizeToFit];
  173.  
  174.     // Set up the date MODateFormCell
  175.     protoCell = [[MODateFormCellClass allocFromZone:[self zone]] 
  176.                         initTextCell:NULL];
  177.     [dateForm setPrototype:protoCell];
  178.     [dateForm addEntry:FORM_DATE_NAME];
  179.     [dateForm sizeToFit];
  180.  
  181.     // Set up the generic MORegexTextCell
  182.     protoCell = [[MORegexTextCellClass allocFromZone:[self zone]] 
  183.                         initTextCell:NULL];
  184.     [protoCell setBezeled:YES];
  185.     [protoCell setBackgroundGray:NX_WHITE];
  186.     [protoCell setTextGray:NX_BLACK];
  187.     [protoCell setEditable:YES];
  188.     [genericMatrix setPrototype:protoCell];
  189.     size.width = 100; size.height = 21;
  190.     [genericMatrix setCellSize:&size];
  191.     size.width = 4; size.height = 4;
  192.     [genericMatrix setIntercell:&size];
  193.     [genericMatrix renewRows:1 cols:1];
  194.     [genericMatrix sizeToCells];
  195.     
  196.     // Set up the integer MORegexTextCell
  197.     protoCell = [[MORegexTextCellClass allocFromZone:[self zone]] 
  198.                         initTextCell:NULL];
  199.     [protoCell setBezeled:YES];
  200.     [protoCell setBackgroundGray:NX_WHITE];
  201.     [protoCell setTextGray:NX_BLACK];
  202.     [protoCell setEditable:YES];
  203.     [protoCell addRegexStr:INT_REGEX];
  204.     [intMatrix setPrototype:protoCell];
  205.     size.width = 100; size.height = 21;
  206.     [intMatrix setCellSize:&size];
  207.     size.width = 4; size.height = 4;
  208.     [intMatrix setIntercell:&size];
  209.     [intMatrix renewRows:1 cols:1];
  210.     [intMatrix sizeToCells];
  211.     
  212.     // Set up the float MORegexTextCell
  213.     protoCell = [[MORegexTextCellClass allocFromZone:[self zone]] 
  214.                         initTextCell:NULL];
  215.     [protoCell setBezeled:YES];
  216.     [protoCell setBackgroundGray:NX_WHITE];
  217.     [protoCell setTextGray:NX_BLACK];
  218.     [protoCell setEditable:YES];
  219.     [protoCell addRegexStr:FLOAT_REGEX];
  220.     [floatMatrix setPrototype:protoCell];
  221.     size.width = 100; size.height = 21;
  222.     [floatMatrix setCellSize:&size];
  223.     size.width = 4; size.height = 4;
  224.     [floatMatrix setIntercell:&size];
  225.     [floatMatrix renewRows:1 cols:1];
  226.     [floatMatrix sizeToCells];
  227.     
  228.     // Set up the time MORegexTextCell
  229.     protoCell = [[MORegexTextCellClass allocFromZone:[self zone]] 
  230.                         initTextCell:NULL];
  231.     [protoCell setBezeled:YES];
  232.     [protoCell setBackgroundGray:NX_WHITE];
  233.     [protoCell setTextGray:NX_BLACK];
  234.     [protoCell setEditable:YES];
  235.     [protoCell addRegexStr:TIME_REGEX_1];
  236.     [protoCell addRegexStr:TIME_REGEX_2];
  237.     [timeMatrix setPrototype:protoCell];
  238.     size.width = 100; size.height = 21;
  239.     [timeMatrix setCellSize:&size];
  240.     size.width = 4; size.height = 4;
  241.     [timeMatrix setIntercell:&size];
  242.     [timeMatrix renewRows:1 cols:1];
  243.     [timeMatrix sizeToCells];
  244.     
  245.     // Set up the time MORegexTextCell
  246.     protoCell = [[MODateTextCellClass allocFromZone:[self zone]] 
  247.                         initTextCell:NULL];
  248.     [protoCell setBezeled:YES];
  249.     [protoCell setBackgroundGray:NX_WHITE];
  250.     [protoCell setTextGray:NX_BLACK];
  251.     [protoCell setEditable:YES];
  252.     [dateMatrix setPrototype:protoCell];
  253.     size.width = 100; size.height = 21;
  254.     [dateMatrix setCellSize:&size];
  255.     size.width = 4; size.height = 4;
  256.     [dateMatrix setIntercell:&size];
  257.     [dateMatrix renewRows:1 cols:1];
  258.     [dateMatrix sizeToCells];
  259.     
  260.     return self;
  261. }
  262.  
  263. @end
  264.