#21 Preference Page
- Download:
- source code Project Files in Zip (114 KB)
- mp4 Full Size H.264 Video (31.6 MB)
- m4v Smaller H.264 Video (18.1 MB)
- webm Full Size VP8 Video (18.5 MB)
- ogv Full Size Theora Video (40.6 MB)
In the episode demonstrated method separating preferences and preferences UI was originally published by Kathir:
http://random-eclipse-tips.blogspot.de/2009/02/eclipse-keeping-preferences-and.html
Non-UI plugin
For the preferences we use the non-UI plugin preference store. This allows us to use Xtext generator without the UI part. In the org.xtext.example.mydsl.preferences package we define preference store preference accessor and initializer classes:
public class MyDslPreferences {
public static final String P_GREETING_TEXT = "greetingText";
public static String getGreetingText() {
return getPreferenceStore().get(P_GREETING_TEXT, "");
}
public static IEclipsePreferences getPreferenceStore() {
return InstanceScope.INSTANCE.getNode("org.xtext.example.mydsl");
}
}
public class PreferenceInitializer extends AbstractPreferenceInitializer {
public void initializeDefaultPreferences() {
getPreferenceStore().put(MyDslPreferences.P_GREETING_TEXT, "Hello");
}
public static IEclipsePreferences getPreferenceStore() {
return DefaultScope.INSTANCE.getNode("org.xtext.example.mydsl");
}
}
The initializer has to be bound in the plugin.xml
<extension point="org.eclipse.core.runtime.preferences">
<initializer class="org.xtext.example.mydsl.preferences.PreferenceInitializer">
</initializer>
</extension>
Now we can use the customizable value in the generator:
class MyDslGenerator implements IGenerator {
override void doGenerate(Resource resource, IFileSystemAccess fsa) {
fsa.generateFile('greetings.txt', resource.allContents.filter(typeof(Greeting)).map [
'''
«MyDslPreferences.greetingText» «name»
'''
].join)
}
}
Note: In the video the name attribute was converted to the Name class during "Organize Imports". I fixed that mistake off screen later on.
Addons plugin
In the org.xtext.example.mydsl.ui.addons plugin we define preference page based on the non-UI plugin preference store.
In the plugin.xml we define our preference page and preference initializer (preference initializer referenced from the non-UI plugin org.xtext.example.mydsl)
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension point="org.eclipse.ui.preferencePages">
<page category="org.xtext.example.mydsl.MyDsl"
class="org.xtext.example.mydsl.ui.addons.preferences.TextPreferencePage"
id="org.xtext.example.mydsl.ui.addons.preferences.TextPreferencePage"
name="Text">
</page>
</extension>
<extension point="org.eclipse.core.runtime.preferences">
<initializer class="org.xtext.example.mydsl.preferences.PreferenceInitializer">
</initializer>
</extension>
</plugin>
The TextPreferencePage.java defines preference page with a single text field.
public class TextPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
public TextPreferencePage() {
super(GRID);
IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, "org.xtext.example.mydsl");
setPreferenceStore(store);
setDescription("A demonstration of a preference page implementation");
}
public void createFieldEditors() {
addField(new StringFieldEditor(MyDslPreferences.P_GREETING_TEXT, "Greeting text:", getFieldEditorParent()));
}
public void init(IWorkbench workbench) {}
}
Version information
- Eclipse: 4.3 (Kepler)
- Xtext: 2.4.2

