I am writing an Eclipse plugin using Xtext 2. I have provided my own preferences by writing my own RootPreferencePage
class:
package org.grammaticalframework.eclipse.ui.editor.preferences;
import org.eclipse.xtext.ui.editor.preferences.LanguageRootPreferencePage;
public class GFLanguageRootPreferencePage extends LanguageRootPreferencePage {
@Override
protected void createFieldEditors() {
addField(new StringFieldEditor("PREF", "&Label:", getFieldEditorParent()));
}
@Override
public void init(IWorkbench workbench) {
getPreferenceStore().setDefault("PREF", "default-value");
}
}
and binding it in the UI module as usual:
public Class<? extends org.eclipse.xtext.ui.editor.preferences.LanguageRootPreferencePage> bindLanguageRootPreferencePage() {
return org.grammaticalframework.eclipse.ui.editor.preferences.GFLanguageRootPreferencePage.class;
}
This works fine; I can save a preference, close and reopen Eclipse, go to the preferences window again and see the value that I saved. However the problem is when I try to retrieve the preference values programmatically. I use the following bit of code:
IPreferencesService prefs = Platform.getPreferencesService();
String s = prefs.getString(QUALIFIER, "PREV", "fallback", null);
This works fine when staying within the same instance of Eclipse, but after restarting Eclipse my attempt to retrieve the preference programmatically fails. The funny thing is I know my preferences are correctly saved by checking in the preference window.
I guess this is an issue with the preference scope as described here and here, but I can't figure out what I'm doing wrong in my programmatic retrieval of preference values.
UPDATE
I have since noticed that when I remove the call to setDefault(...)
from within the init()
method, things work entirely correctly. That is, I can set preferences via the UI, reload Eclipse, and retrieve those values programmatically without a problem.
So the issue has now become that I need to find the correct place for my call to setDefault(...)
. Based on this same article, I have extended the `` extension point as follows:
<extension point="org.eclipse.core.runtime.preferences">
<initializer
class="org.grammaticalframework.eclipse.ui.editor.preferences.GFPreferenceInitializer">
</initializer>
</extension>
and with the implementing class:
package org.grammaticalframework.eclipse.ui.editor.preferences;
public class GFPreferenceInitializer extends AbstractPreferenceInitializer {
@Override
public void initializeDefaultPreferences() {
IPreferenceStore store = GFActivator.getInstance().getPreferenceStore();
store.setDefault("PREV", "default-value");
}
}
This code is being executed, but for some reason when I open my preferences window and click "Restore defaults", the fields are simply blanked.. the defaults I am trying to set/initialize do not seem to make their way to the preferences window, so I am stuck yet again!
org.eclipse.xtext.ui.editor.preferences.PreferenceStoreAccessImpl.getWritablePreferenceStore()
for where XText creates the preference store. – Ranchero"org.grammaticalframework.eclipse.GF"
, which I got directly from myplugin.xml
file. In this example, KEY would be"PREF"
. – GlobulargetPreferenceStore().setDefault(..);
in theinit()
method, then things seem to work properly.. so maybe this setting of the default values is simply in the wrong place? – Globular