Proper initialization and preferences in the 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 bind it in the user interface 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 great; I can save my preference, close and open Eclipse again, return to the settings window again and see what value I saved. However, the problem is that I am trying to get 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 if you stay in the same instance of Eclipse, but after restarting Eclipse my attempt to get the preference programmatically fails. The funny thing is that I know that my settings are correctly saved by checking in the preferences window.

I assume this is a problem with the preference area described here and here , but I can "I don’t know what I am doing wrong in my programmatic search for preference values.

UPDATE

Since then, I noticed that when I setDefault(...) call from the init() method, everything works perfectly correctly. That is, I can set the settings using the user interface, restart Eclipse and get these values ​​programmatically without problems.

So now the problem is that I need to find the right place for my call to setDefault(...) . Based on the same article , I expanded 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 implementation 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 executed, but for some reason, when I open the settings window and click "Restore Defaults", the fields are simply empty. By default, I try to set / initialize, it does not seem to make the settings window way, so I'm stuck again!

+6
source share
1 answer

Ok, I think I solved my problem. I needed to specify a 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 had to read articles that I related more carefully!

+5
source

Source: https://habr.com/ru/post/900490/


All Articles