home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Examples / AppKit / Yap / Preferences.m < prev    next >
Text File  |  1996-01-19  |  7KB  |  201 lines

  1. /*
  2.  *  Preferences.m
  3.  *  Author: Ali Ozer
  4.  *  Created: Oct 24, 1995 for Edit
  5.  *  Modified: Jan 96 for Yap
  6.  *
  7.  *  Preferences controller... Manages the display, editing, and persistence of a
  8.  *  a set of defaults.
  9.  *
  10.  *  This module allows for UI where there is or there isn't an OK button. 
  11.  *  If you wish to have an OK button, connect OK to ok:, Revert to revert:, 
  12.  *  and don't call commitDisplayedValues from the various action messages. 
  13.  *  The ivar displayedValues is a mirror of the UI. These are committed by
  14.  *  copying these values to curValues.
  15.  *
  16.  *  You may freely copy, distribute and reuse the code in this example.
  17.  *  NeXT disclaims any warranty of any kind, expressed or implied,
  18.  *  as to its fitness for any particular use.
  19.  */
  20.  
  21. #import <AppKit/AppKit.h>
  22. #import "Preferences.h"
  23. #import "IntegerFormatter.h"
  24.  
  25. static NSDictionary *defaultValues() {
  26.     static NSDictionary *dict = nil;
  27.     if (!dict) {
  28.         dict = [[NSDictionary alloc] initWithObjectsAndKeys:
  29.             [NSNumber numberWithBool:NO], ShowGridLines,
  30.             [NSNumber numberWithBool:NO], ShowCache,
  31.             [NSNumber numberWithBool:YES], ClearCache, 
  32.             [NSNumber numberWithBool:YES], DeleteBackup, 
  33.             [NSNumber numberWithInt:612], ViewWidth, 
  34.             [NSNumber numberWithInt:792], ViewHeight, 
  35.         nil];
  36.     }
  37.     return dict;
  38. }
  39.  
  40. @implementation Preferences
  41.  
  42. static Preferences *sharedInstance = nil;
  43.  
  44. + (id)objectForKey:(id)key {
  45.     return [[[self sharedInstance] preferences] objectForKey:key];
  46. }
  47.  
  48. + (void)saveDefaults {
  49.     if (sharedInstance) {
  50.     [Preferences savePreferencesToDefaults:[sharedInstance preferences]];
  51.     }
  52. }
  53.  
  54. + (Preferences *)sharedInstance {
  55.     return sharedInstance ? sharedInstance : [[self alloc] init];
  56. }
  57.  
  58. - (id)init {
  59.     if (sharedInstance) {
  60.     [self dealloc];
  61.     } else {
  62.         [super init];
  63.         curValues = [[[self class] preferencesFromDefaults] copyWithZone:[self zone]];
  64.         [self discardDisplayedValues];
  65.         sharedInstance = self;
  66.     }
  67.     return sharedInstance;
  68. }
  69.  
  70. - (void)dealloc {
  71. }
  72.  
  73. - (NSDictionary *)preferences {
  74.     return curValues;
  75. }
  76.  
  77. - (void)showPanel:(id)sender {
  78.     if (!dontDeleteBackupButton) {
  79.         NSFormatter *intFormatter;
  80.         if (![NSBundle loadNibNamed:@"Preferences" owner:self])  {
  81.             NSLog(@"Failed to load Preferences.nib");
  82.             NSBeep();
  83.             return;
  84.         }
  85.         intFormatter = [[IntegerFormatter allocWithZone:[self zone]] initWithMinValue:1 maxValue:10000];
  86.         [[viewWidthField cell] setFormatter:intFormatter];
  87.         [[viewHeightField cell] setFormatter:intFormatter];
  88.         [intFormatter release];
  89.         [self updateUI];
  90.         [[dontDeleteBackupButton window] center];
  91.     }
  92.     [[dontDeleteBackupButton window] makeKeyAndOrderFront:nil];
  93. }
  94.  
  95. - (void)updateUI {
  96.     [dontDeleteBackupButton setState:[[displayedValues objectForKey:DeleteBackup] boolValue] ? 0 : 1];
  97.     [dontClearCacheButton setState:[[displayedValues objectForKey:ClearCache] boolValue] ? 0 : 1];
  98.     [showCacheButton setState:[[displayedValues objectForKey:ShowCache] boolValue] ? 1 : 0];
  99.     [showGridLinesButton setState:[[displayedValues objectForKey:ShowGridLines] boolValue] ? 1 : 0];
  100.     [viewWidthField setObjectValue:[displayedValues objectForKey:ViewWidth]];
  101.     [viewHeightField setObjectValue:[displayedValues objectForKey:ViewHeight]];
  102. }
  103.  
  104. /* To catch changes in text fields */
  105. - (void)controlTextDidEndEditing:(NSNotification *)notification {
  106.     [self miscChanged:nil];
  107. }
  108.  
  109. /* To catch all other changes */
  110. - (void)miscChanged:(id)sender {
  111.     static NSNumber *yes = nil;
  112.     static NSNumber *no = nil;
  113.     
  114.     if (!yes) {
  115.         yes = [[NSNumber alloc] initWithBool:YES];
  116.         no = [[NSNumber alloc] initWithBool:NO];
  117.     }
  118.  
  119.     [displayedValues setObject:[dontDeleteBackupButton state] ? no : yes forKey:DeleteBackup];
  120.     [displayedValues setObject:[dontClearCacheButton state] ? no : yes forKey:ClearCache];
  121.     [displayedValues setObject:[showCacheButton state] ? yes : no forKey:ShowCache];
  122.     [displayedValues setObject:[showGridLinesButton state] ? yes : no forKey:ShowGridLines];
  123.     [displayedValues setObject:[viewWidthField objectValue] forKey:ViewWidth];
  124.     [displayedValues setObject:[viewHeightField objectValue] forKey:ViewHeight];
  125.  
  126.     [self commitDisplayedValues];
  127.     [[NSNotificationCenter defaultCenter] postNotificationName:PreferencesChangedNotification object:self];
  128. }
  129.  
  130. /**** Commit/revert etc ****/
  131.  
  132. - (void)commitDisplayedValues {
  133.     if (curValues != displayedValues) {
  134.         [curValues release];
  135.         curValues = [displayedValues copyWithZone:[self zone]];
  136.     }
  137. }
  138.  
  139. - (void)discardDisplayedValues {
  140.     if (curValues != displayedValues) {
  141.         [displayedValues release];
  142.         displayedValues = [curValues mutableCopyWithZone:[self zone]];
  143.         [self updateUI];
  144.     }
  145. }
  146.  
  147. - (void)ok:(id)sender {
  148.     [self commitDisplayedValues];
  149. }
  150.  
  151. - (void)revertToDefault:(id)sender {
  152.     curValues = [defaultValues() copyWithZone:[self zone]];
  153.     [self discardDisplayedValues];
  154. }
  155.  
  156. - (void)revert:(id)sender {
  157.     [self discardDisplayedValues];
  158. }
  159.  
  160. /**** Code to deal with defaults ****/
  161.    
  162. #define getBoolDefault(name) \
  163.   {NSString *str = [defaults stringForKey:name]; \
  164.       [dict setObject:str ? [NSNumber numberWithBool:[str hasPrefix:@"Y"]] : [defaultValues() objectForKey:name] forKey:name];}
  165.  
  166. #define getIntDefault(name) \
  167.   {int val = [defaults integerForKey:name]; \
  168.       [dict setObject:val ? [NSNumber numberWithInt:val] : [defaultValues() objectForKey:name] forKey:name];}
  169.  
  170. + (NSDictionary *)preferencesFromDefaults {
  171.     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  172.     NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:10];
  173.  
  174.     getBoolDefault(DeleteBackup);
  175.     getBoolDefault(ClearCache);
  176.     getBoolDefault(ShowCache);
  177.     getBoolDefault(ShowGridLines);
  178.     getIntDefault(ViewWidth);
  179.     getIntDefault(ViewHeight);
  180.  
  181.     return dict;
  182. }
  183.  
  184. #define setBoolDefault(name) \
  185.   {if ([[defaultValues() objectForKey:name] isEqual:[dict objectForKey:name]]) [defaults removeObjectForKey:name]; else [defaults setBool:[[dict objectForKey:name] boolValue] forKey:name];}
  186.  
  187. #define setIntDefault(name) \
  188.   {if ([[defaultValues() objectForKey:name] isEqual:[dict objectForKey:name]]) [defaults removeObjectForKey:name]; else [defaults setInteger:[[dict objectForKey:name] intValue] forKey:name];}
  189.  
  190. + (void)savePreferencesToDefaults:(NSDictionary *)dict {
  191.     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  192.     setBoolDefault(ClearCache);
  193.     setBoolDefault(ShowCache);
  194.     setBoolDefault(ShowGridLines);
  195.     setBoolDefault(DeleteBackup);
  196.     setIntDefault(ViewWidth);
  197.     setIntDefault(ViewHeight);
  198. }
  199.  
  200. @end
  201.