3. A custom preferences sheet which parametrizes the word counter

Figure 15.1. The Count Words preferences sheet
The Count Words preferences sheet

A preferences sheet does not have to implement the AppPart interface. A preferences sheet is simply a specialized component contained in a PreferencesEditor.

In XMLmind XML Editor, user preferences such as displayScaling, lastOpenedFiles, etc[7] are stored in a Preferences object. This object is accessed using App.getPreferences.

Action EditPreferencesAction (declared as "editPreferencesAction" in DesktopApp.xxe_gui) creates a PreferencesEditor.

There are two kinds of preferences sheet:

The custom preferences sheet we are going to describe here belong to this second kind.

Excerpts from CountWordsOptions.java:

public class CountWordsOptions extends AppPreferencesSheet {
    private SpinnerNumberModel countedWordMinCharsModel;
    private JSpinner countedWordMinChars;
    
    public CountWordsOptions() {
        super("countWords", "Count Words");1
    }

    protected PreferencesSheetPane createPane() {2
        PreferencesSheetPane form = 
            new PreferencesSheetPane(new FlowLayout(FlowLayout.LEFT, 5, 2));

        JLabel label = new JLabel("Do not count words having less than");
        form.add(label);

        countedWordMinCharsModel = new SpinnerNumberModel(1, 1, 1000, 1);
        countedWordMinChars = new JSpinner(countedWordMinCharsModel);
        form.add(countedWordMinChars);

        label = new JLabel("characters");
        form.add(label);

        return form;
    }

    public void focusPane() {3
        countedWordMinChars.requestFocus();
    }

    public void fillPane(Preferences prefs) {4
        int count = prefs.getInt("countedWordMinChars", 1, 1000, 1);
        countedWordMinCharsModel.setValue(new Integer(count));
    }

    public boolean validatePane(Preferences prefs) {5
        int count = countedWordMinCharsModel.getNumber().intValue();
        prefs.putInt("countedWordMinChars", count);

        return true;
    }

    public void applyPreferences(Preferences prefs) {6
        AppPart part = app.getPart("countWordsTool");
        if (part != null) {
            part.applyPreferences();
        }
    }
}

1

A preferences sheet has a sheet ID: "countWords" and a label: "Count Words". The label is displayed by the GUI of the preferences editor. The sheet ID is needed, for example, to specify that sheet A is a “child” of sheet B. Example: in XMLmind XML Editor, sheet "featuresOptions" is the child of sheet "generalOptions" (see DesktopApp.xxe_gui).

2

PreferencesSheet.createPane creates the GUI of the preferences sheet. It must create and return a PreferencesSheetPane, which is simply a JPanel with a ``special connection'' to its JScrollPane parent.

3

PreferencesSheet.focusPane is invoked when the sheet is selected. It should give the keyboard focus to the first component of the form it has created in PreferencesSheet.createPane.

4

PreferencesSheet.fillPane fills the form using values read from the Preferences object which is the argument of this method.

5

PreferencesSheet.validatePane stores in the Preferences argument all the values specified by the user in the form.

6

PreferencesSheet.applyPreferences delegates to the CountWordsTool the task of actually applying to itself the user preferences specified in the Preferences argument. This works because, in the case of PreferencesSheet.applyPreferences, the Preferences argument is identical to the Preferences object returned by App.getPreferences.

The instance of CountWordsTool which is part of the App is obtained using App.getPart. This is why CountWordsOptions is an AppPreferencesSheet and not a “plain” PreferencesSheet.



[7] More information about supported preferences keys in the Section 6, “The "Preferences" dialog box” in XMLmind XML Editor - Online Help.