Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 887
John J. Camilleri Avatar asked Nov 01 '11 08:11

John J. Camilleri


1 Answers

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!

like image 100
John J. Camilleri Avatar answered Oct 01 '22 02:10

John J. Camilleri