Search code examples
javaeclipse-rcppreferencesrcp

Eclipse Preference Pages


I have created a preference page in eclipse the preference page has two fields

  1. server url
  2. store location

If the user open this preferences dialog, change the value of url and apply it the product is restarted and after restart when I check the value in the url field it is changed as expected. When I change the values of both url and directory only one of them is updated depends on which one is changed later. Here is my init method which initialize the preferences

public class DataStorePreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {

public static final String SERVER_URL = "prefs_server_url";
public static final String WORKSPACE_DIR = "prefs_workspace_dir";
public static final String KEEP_LOCKS = "prefs_keep_locks";
//public static final String RELEASE = "prefs_release";
public DataStorePreferencePage() {
    super(GRID);
}


@Override
public void init(IWorkbench workbench) {
    setPreferenceStore(Activator.getDefault().getPreferenceStore());
    getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent event) {
            String property = event.getProperty();
            System.setProperty("datastoreserver_url", property);
            if (property.equals(DataStorePreferencePage.WORKSPACE_DIR) ||
                    property.equals(DataStorePreferencePage.SERVER_URL)) {

                if(MessageDialog.openConfirm(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Information", "New settings will be applied after a restart.\nRestart now?"))
                    PlatformUI.getWorkbench().restart();

            }
        }
    });                     
}

@Override
protected void createFieldEditors() {
    StringFieldEditor urlEditor = new StringFieldEditor(SERVER_URL, "DataStore Server URL", getFieldEditorParent());
    StringFieldEditor workspaceDirEditor = new DirectoryFieldEditor(WORKSPACE_DIR, "Workspace directory:", getFieldEditorParent());
    BooleanFieldEditor keepLocksEditor = new BooleanFieldEditor(KEEP_LOCKS, "Keep locks (default setting):", getFieldEditorParent());
    //BooleanFieldEditor releaseEditor = new BooleanFieldEditor(RELEASE, "Release (default setting):", getFieldEditorParent());

    addField(workspaceDirEditor);
    addField(urlEditor);
    addField(keepLocksEditor);
    //addField(releaseEditor);
}

@Override
public boolean performOk() {

    return super.performOk();

}


}

Question:

Where is the new value stored? From where eclipse get this changed value in any .ini file?

How can I change both the properties at the same time?

Thanks


Solution

  • Wait until performOk or performApply is called before checking for restart.

    The preference values are stored in the preference store. You can get them with:

    IPreferenceStore store = getPreferenceStore();
    
    String dir = store.getString(WORKSPACE_DIR);
    
    String url = store.getString(SERVER_URL);