09 - Eclipse Plugin Preference Pages Tutorial: Page 2 of 2

Let’s Customize Preference Pages

In this section we will modify the preference page so as to create a single String Text Box where user can set his preference for the name of folder which will be displayed in the Property Manager View. 

Modify the PreferenceConstants class to add another constant FOLDER_NAME as shown in bold below.

public class PreferenceConstants {
     public static final String P_PATH = "pathPreference";
     public static final String P_BOOLEAN = "booleanPreference";
     public static final String P_CHOICE = "choicePreference";
     public static final String P_STRING = "stringPreference";
     public static final String FOLDER_NAME = "folderName";

Next, Modify the createFieldEditors() in PropertyManagerPreferencePage class to create String filed editor. Provide FOLDER_NAME constant as the name of this field editor (as shown below). Also declare class level reference variable to hold String Field Editor object.

private StringFieldEditor fieldEditor; 
public void createFieldEditors() {
    fieldEditor = new StringFieldEditor(PreferenceConstants.
        FOLDER_NAME, "Folder Name: ", getFieldEditorParent());

Following figure shows, how preference page will look like after above customization.

       Eclipse Plugin Propert Manager Preferences


Next, Override checkState() method defined in FieldEditorPreferencePage for the purpose of customized validations. I could have very well use fieldEditor.setEmptyStringAllowed to perform same validation. However, I am manually checking it here so that you can get an idea as to how to use this method for complex validations.

protected void checkState() {
              if(fieldEditor.getStringValue()!= null && !fieldEditor.getStringValue().equals("")){
                        setErrorMessage("Folder name cannot be blank!");

The FieldEditorPropertyPage listens for FieldEditor.IS_VALID property change events and then calls checkState() and setValid() as necessary. The String field editors are never in an invalid state and thus do not issue FieldEditor.IS_VALID property change events, only FieldEditor.VALUE property change events. You must override the FieldEditorPreferencePage propertyChange() method to call the checkState() method when the FieldEditor.VALUE property change event is received.


public void propertyChange(PropertyChangeEvent event) {
              if (event.getProperty().equals(FieldEditor.VALUE)) {

With above change an error message is displayed across the top of the preference page and the Apply and OK buttons are disabled (as shown below)

        Eclipse Error Message at the top of preference Page

Next, Modify com.myplugin.rmp.preferences. PreferenceInitializer (Generated by create preference page wizard) to set the default value for folder name. In general Initializers are used to initialize default preference values. Modify initializeDefaultPreferences() as shown below

public void initializeDefaultPreferences() {
              IPreferenceStore store = RmpPlugin.getDefault()
              store.setDefault(PreferenceConstants.FOLDER_NAME,"WorkSpace Property Files");

With above change, folder name will be defaulted to “WorkSpace Property Files” whenever user clicks on default button in preferences dialog (as shown below)

    Eclpse Plugin Folder Name Default

Next Modify PropertyManagerView class so as to use preference rather than hard coded folder name. Modify initialize method as follows:

public void initialize() {
                  IPreferenceStore store = RmpPlugin.getDefault()

                  String folderName = store.getString(PreferenceConstants.FOLDER_NAME);

                   TreeParent root = new TreeParent(folderName);
                   try {
                        IWorkspace workspace = ResourcesPlugin.getWorkspace();
                        IProject[] projects = workspace.getRoot().getProjects();

rest of the method is same

With above mentioned change, on every refresh on root node of Properties view – folder name will be picked up from preferences

      Eclipse Folder Name From Preferences

Like us on Facebook