Correctly initializing and retrieving preferences in a Xtext-based Eclipse Plugin
Asked Answered
G

1

6

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!

Globular answered 1/11, 2011 at 8:50 Comment(4)
What are you using as your QUALIFIER and your KEY? See org.eclipse.xtext.ui.editor.preferences.PreferenceStoreAccessImpl.getWritablePreferenceStore() for where XText creates the preference store.Ranchero
In my case the QUALIFIER is "org.grammaticalframework.eclipse.GF", which I got directly from my plugin.xml file. In this example, KEY would be "PREF".Globular
I've found that when I comment out getPreferenceStore().setDefault(..); in the init() method, then things seem to work properly.. so maybe this setting of the default values is simply in the wrong place?Globular
the vogella link is now: vogella.com/tutorials/EclipsePreferences/article.htmlSinuation
G
5

Ok, I think I have solved my own problem. I needed to specify the preference store in the init() method as follows:

package org.grammaticalframework.eclipse.ui.editor.preferences;
public class GFLanguageRootPreferencePage extends LanguageRootPreferencePage {
    ...
    @Override
    public void init(IWorkbench workbench) {
        setPreferenceStore(GFActivator.getInstance().getPreferenceStore());
    }
}

I really should have read the articles I linked to more carefully!

Globular answered 2/11, 2011 at 8:54 Comment(1)
Additionally, I also needed to start using org.grammaticalframework.eclipse.ui as the qualifier.Globular

© 2022 - 2024 — McMap. All rights reserved.