Making the Preferences Panel Work with Defaults

Now that we’ve arranged for the GraphView and ColorGraphView classes to set their initial values from the defaults database, we will modify GraphPaper’s Preferences panel to let the user see those values and change them. We’ll use the same Preferences panel that we created back in Chapter 17, except that we’ll add two new controls — OK and Revert buttons.

The OK button will write the current Preferences panel values into the defaults database. This button will send the okay: action message to the Preferences panel controller object (PrefController).

The Revert button will redisplay the information in the Preferences panel by reading it out of the database. This button will send the revert: action message to PrefController. (Recall that we added the okay: and revert: action methods in IB and unparsed them into PrefController class files back in Chapter 17).

We’ll also modify the PrefController class so that the code for setting up the initial value of each color well is moved from the awakeFromNib method to the revert: method. This way, we won’t have to duplicate the same code in two different methods.

Modifying the Preferences Panel

  1. Open GraphPaper’s auxiliary nib file, Preferences.nib, in IB (double-click it in PB).

  2. Make the Preferences panel big enough to add Revert and OK buttons, as shown in Figure 21-5.

The Preferences panel with Revert and OK buttons

Figure 21-5. The Preferences panel with Revert and OK buttons

  1. Add the Revert and OK buttons (using the Cocoa-Views palette), as shown in Figure 21-5. Make them line up nicely with the box using the blue guidelines.

  2. Make the OK button the default button. To do this, select the button, type Command-1 to display the NSPanel Attributes Info dialog, click the pop-up menu labeled “<no key>”, and drag to Return (to make the Return key a key equivalent for the OK button).

    The OK button should turn blue (or whatever “Appearance” color you’ve selected in System Preferences).

  3. Connect the Revert button to the PrefController instance icon so that it sends the revert: message.

  4. Connect the OK button to the PrefController instance icon so that it sends the okay: message, as shown in Figure 21-6.

Connecting the OK button to PrefController in IB

Figure 21-6. Connecting the OK button to PrefController in IB

  1. Save Preferences.nib.

Changes to the PrefController Class

Now we need to modify the PrefController class so that it implements the revert: and okay: methods.

  1. Back in PB, add the following #import directive to the beginning of the PrefController.m class file:

                            #import "defaults.h"
  2. Move the three lines that set color wells from the awakeFromNib method in the PrefController.m file to the revert: method. The revert: method should look like this:

    - (IBAction)revert:(id)sender
    {     [self setUpWell:axesColorWell ];
                                 [self setUpWell:labelColorWell ];
                                 [self setUpWell:graphColorWell ];
    }

The revert: method calls the setUpWell: method for each of the color wells. Recall that the setUpWell: method asks the ColorGraphView class for these values. Because we previously modified the ColorGraphView to get these values from the defaults database, no further modification is required in the PrefController class.

We will also arrange for the Preferences window to show up where it was last left by the user in awakeFromNib.

  1. Add the lines shown here in bold to awakeFromNib:

    - (void)awakeFromNib
    {
        [ [NSColorPanel sharedColorPanel] setContinuous:YES];
        [window setFrameUsingName:@"Preferences"];
                                [window setFrameAutosaveName:@"Preferences"];
                                [self revert:nil];
    }
  2. Insert the lines shown here in bold into the okay: method in PrefController.m (the method stub should already be there, as it was for revert:):

    - (IBAction)okay:(id)sender
    {
        NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                            
        [defs setObject:DataForColor([axesColorWell color])
                                         forKey:GP_AxesColor];
                            
        [defs setObject:DataForColor([labelColorWell color])
                                         forKey:GP_LabelColor];
                            
        [defs setObject:DataForColor([graphColorWell color])
                                         forKey:GP_GraphColor];
                            
        [window orderOut:nil];
    }

The okay: method gets the current value for each of the color wells, converts each value to an NSData object using our DataForColor( ) function, and stores these objects in the defaults database. Finally, it orders out (i.e., dismisses) the Preferences window, because most users expect the Preferences window to disappear when they press the OK button.

Testing the Updated Preferences Panel

  1. Graph a function and choose the GraphPaper Preferences menu command to bring up the Preferences panel.

  2. Change some colors and click the Revert button; notice how the original colors return to the Preferences panel.

  3. Change some colors and click OK.

  4. Quit GraphPaper.

  5. Run GraphPaper again and graph a function. Note that the values that you set in the Preferences panel during the last run of the GraphPaper are still in effect. Quit GraphPaper.

  6. Double-click the ~/Library/Preferences/GraphPaper.plist file in the Finder to view the GraphPaper defaults in PropertyListEditor, as shown in Figure 21-7. The long hexadecimal strings are the NSData representations of the NSColor objects.

GraphPaper.plist file in PropertyListEditor after changing colors in the Preferences panel

Figure 21-7. GraphPaper.plist file in PropertyListEditor after changing colors in the Preferences panel

There is one last problem that needs to be resolved to complete this phase. Earlier in this chapter, we said that the default values should be stored in a file that has the fully-qualified domain name of the application’s publisher. But GraphPaper’s default values are being stored in a file called GraphPaper.plist, not something like com.nitroba.GraphPaper.plist. This needs to be fixed!

It turns out that the NSUserDefaults class decides which name to use based on the application identifier that is set in PB. In previous chapters, we set this application identifier to the name of the application. Now it’s time to change this.

  1. Activate PB.

  2. Click on the Targets vertical tab and select the GraphPaper target.

  3. Click the Application Settings tab.

  4. Change the name in the Identifier field from “GraphPaper” to “com.nitroba.GraphPaper” (the .plist extension will be added automatically).

  5. Build and run GraphPaper, saving all files first.

  6. Make a change to the Preferences panel, then quit the program.

  7. Verify that the preferences are now saved in the file com.nitroba.GraphPaper.plist in your ~/Library/Preferences folder, and not in GraphPaper.plist.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.141.219