Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default values for Preferences in Eclipse RCP application

I have an RCP application and I need to set some values in the preference page as defaults when the RCP run as an application. I need to hide the Fast View bar and some menus from a certain peresepective that is why I need to set these settings from the preference page.

My application is not a product yet, so I cant use the plugin_customization.ini file to change in it.

Can anyone tell me a way to change the default values programmatically via the preference page or any other way?

like image 403
Shereen Avatar asked Aug 01 '12 11:08

Shereen


People also ask

How do I set preferences in Eclipse?

To invoke this dialog, click on the Window menu and select the Preferences menu item. The preference pages are organized by category. You can locate a preference page by expanding the nodes of the tree in the left hand side of the dialog box.

Where are Eclipse preferences stored?

Preferences are stored in the workspace of your application in the . metadata/. plugins/org. eclipse.

What is RCP Eclipse?

While the Eclipse platform is designed to serve as an open tools platform, it is architected so that its components could be used to build just about any client application. The minimal set of plug-ins needed to build a rich client application is collectively known as the Rich Client Platform.


2 Answers

Define extension points for your preference page, and for that page's initializer class:

An example preference page implementation might look like:

public class MyPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
    public static final String PREF_USE_METRIC_UNITS = "PREF_USE_METRIC_UNITS";
    public static final String PREF_AUTO_UPDATE = "PREF_AUTO_UPDATE";
    public static final String PREF_USER_FILES_DIR = "PREF_USER_FILES_DIR";
    public static final String PREF_FAVORITE_ANIMAL = "PREF_FAVORITE_ANIMAL";

    public MyPreferencePage() {
        super(GRID);
    }

    public void createFieldEditors() {
        Composite parent = getFieldEditorParent();
        addField(new BooleanFieldEditor(PREF_USE_METRIC_UNITS, "Use &metric units", parent));
        addField(new BooleanFieldEditor(PREF_AUTO_UPDATE, "&Auto update", parent));

        addField(new DirectoryFieldEditor(PREF_USER_FILES_DIR, "User files &path:", parent));

        addField(new StringFieldEditor(PREF_FAVORITE_ANIMAL, "Favorite &animal:", parent));
    }

    public void init(IWorkbench workbench) {
        setPreferenceStore(Plugin.getPlugin().getPreferenceStore());
    }
}

You then set the actual default values in your AbstractPreferenceInitializer class:

public class MyPreferenceInitializer extends AbstractPreferenceInitializer {
    @Override
    public void initializeDefaultPreferences() {
        IPreferenceStore store = Plugin.getPlugin().getPreferenceStore();

        store.setDefault(MyPreferencePage.PREF_USE_METRIC_UNITS, true);
        store.setDefault(MyPreferencePage.PREF_AUTO_UPDATE, false);

        String userHome = System.getProperty("user.home");
        String defaultPath = userHome + "\\Local Settings\\Temp\\";
        store.setDefault(MyPreferencePage.PREF_USER_FILES_DIR, defaultPath);

        store.setDefault(MyPreferencePage.PREF_FAVORITE_ANIMAL, "platypus");
    }
}
like image 165
stracka Avatar answered Oct 12 '22 04:10

stracka


You can use the extension point org.eclipse.core.runtime.preferences

 <extension
         point="org.eclipse.core.runtime.preferences">
         <scope name="SamplePreferencePage" class="com.example.SamlePreferencePage"/>
      <initializer
            class="com.example.PreferenceInitializer">
      </initializer>
  </extension>
like image 31
aphex Avatar answered Oct 12 '22 04:10

aphex