在基于 Xtext 的 Eclipse 插件中正确初始化和检索首选项
我正在使用 Xtext 2 编写一个 Eclipse 插件。我通过编写自己的 RootPreferencePage
类来提供自己的首选项:
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");
}
}
并像往常一样将其绑定到 UI 模块中:
public Class<? extends org.eclipse.xtext.ui.editor.preferences.LanguageRootPreferencePage> bindLanguageRootPreferencePage() {
return org.grammaticalframework.eclipse.ui.editor.preferences.GFLanguageRootPreferencePage.class;
}
这工作正常;我可以保存首选项,关闭并重新打开 Eclipse,再次转到首选项窗口并查看我保存的值。 然而问题是当我尝试以编程方式检索首选项值时。我使用以下代码:
IPreferencesService prefs = Platform.getPreferencesService();
String s = prefs.getString(QUALIFIER, "PREV", "fallback", null);
当处于同一 Eclipse 实例中时,此方法工作正常,但在重新启动 Eclipse 后,我尝试以编程方式检索首选项失败。有趣的是,我知道通过检查首选项窗口我的首选项已正确保存。
我猜这是此处和<中描述的偏好范围的问题a href="http://www.eclipse.org/articles/Article-Preferences/preferences.htm">此处,但我无法弄清楚我在以编程方式检索偏好时做错了什么价值观。
更新
我后来注意到,当我从 init()
方法中删除对 setDefault(...)
的调用时,事情完全正常了正确。也就是说,我可以通过 UI 设置首选项、重新加载 Eclipse,并以编程方式检索这些值,不会出现任何问题。
所以现在的问题是我需要找到调用 setDefault(...)
的正确位置。基于同一篇文章,我扩展了 `` 扩展点,如下所示:
<extension point="org.eclipse.core.runtime.preferences">
<initializer
class="org.grammaticalframework.eclipse.ui.editor.preferences.GFPreferenceInitializer">
</initializer>
</extension>
并使用实现类:
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");
}
}
此代码正在执行,但由于某种原因,当我打开首选项窗口并单击“恢复默认值”时,这些字段只是空白。我尝试设置/初始化的默认值似乎没有进入首选项窗口,所以我又被困住了!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我想我已经解决了我自己的问题。
我需要在
init()
方法中指定首选项存储,如下所示:我真的应该更仔细地阅读我链接到的文章!
Ok, I think I have solved my own problem.
I needed to specify the preference store in the
init()
method as follows:I really should have read the articles I linked to more carefully!