Eclipse:OSGI 首选项与 PreferenceStore
我正在开发一个 Eclipse 插件(或者实际上是一个基于 Eclipse 的应用程序的插件),它需要用户输入一些配置。
从文档来看,似乎有两个首选 API - 一个位于 org.eclipse.core.runtime.preferences
,扩展/实现OSGI 首选项 API,另一个一个,JFace 特定的,位于 org.eclipse.jface.preference
。然后我们有 org.eclipse.ui.preferences
也是如此。
OSGI API 有一个分层节点树 - 首选项节点(Preferences
或 IEclipsePreferences
)可以有多个子节点,这些子节点本身可以包含单独的名称-值对以及更多子节点。这似乎适合我的用例 - 我有动态数量的“首选项组”,每个组都有大约三个字符串属性(名称、描述、命令),它们可以很好地映射到这些节点。
JFace API 没有这样的层次结构,每个插件只有一个扁平的 IPreferenceStore
。但它提供了首选项编辑器页面,然后可以通过实现 IWorkbenchPreferencePage
和 使用“org.eclipse.ui.preferencePages”扩展点。 (我仍然需要自己实现部分首选项页面,但这个 API 似乎为此提供了良好的基础。
) jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/ui/preferences/package-summary.html" rel="noreferrer">org.eclipse.ui.preferences< /code> API 以某种方式桥接这两个 API 都通过提供基于 IEclipsePreferences 的 IPreferenceStore 实现来实现,但我仍然不知道如何使用它。
所以我的问题是:如何在首选项对话框中使用分层 OSGI 首选项
?我只需要一级,但我需要用户能够动态添加新节点(每个节点大约有三个偏好)。 (不过,这些节点不必有新的首选项页面。)
I'm working on an Eclipse plugin (or in fact, a plugin for an Eclipse-based application) which needs some configuration to be entered by the user.
From looking at the documentation, there seem to be two preference APIs - one in org.eclipse.core.runtime.preferences
, extending/implementing the OSGI prefererence API, another one, JFace specific, in org.eclipse.jface.preference
. Then we have org.eclipse.ui.preferences
, too.
The OSGI API has a hierarchic Node tree - a preference node (Preferences
or IEclipsePreferences
) can have multiple subnodes, which themselves can contain both individual name-value-pairs as well as more subnodes. This seems to be right for my use case - I have a dynamic number of "preference groups", each with about three string properties (name, description, command), which would nicely map to these nodes.
The JFace API has no such hierarchy, only a flat IPreferenceStore
for each plugin. But it provides preference editor pages, which then can be included in the usual preferences dialog (Window / Preferences) by implementing IWorkbenchPreferencePage
and using the "org.eclipse.ui.preferencePages" extension point. (I still have to implement part of the preference page myself, but this API provides a good base for this, it seems.)
It seems that the org.eclipse.ui.preferences
API somehow bridges both these APIs by providing an IPreferenceStore implementation based on the IEclipsePreferences, but I still can't see how to use this.
So here my question: How can I use the hierarchical OSGI Preferences
in the preferences-dialog? I only need one level, but I need the user to be able to dynamically add new nodes (with about three preferences each). (These nodes do not have to have new preference-pages, though.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎在首选项页面级别,它希望与首选项存储一起工作。大多数插件从 org.eclipse.ui.plugin.AbstractUIPlugin.getPreferenceStore() 提供的默认值获取首选项存储。这可以松散地转换为带有 InstanceScope 的 ScopedPreferenceStore 以及与其bundle.id 匹配的节点。
获取匹配的 IEclipsePreferences 对象的等效方法是 InstanceScope.INSTANCE.getNode("bundle.id") 。这将允许您在下面添加更多节点,但无法从 IPreferenceStore 访问它们。但是,您的首选项页面可以将其首选项存储设置为插件的主要首选项,并且仍然使用 IEclipsePreferences 或辅助 IPreferenceStore 来访问额外的首选项(您只需自己编写代码,类似于 org.eclipse.ui。内部.dialogs.EditorsPreferencePage)。
It seems that at the preference page level, it wants to work with a preference store. Most plugins get their preference store from the default provided by
org.eclipse.ui.plugin.AbstractUIPlugin.getPreferenceStore()
. That translates loosely to a ScopedPreferenceStore with an InstanceScope with a node that matches their bundle.id.The equivalent to get the matching IEclipsePreferences object would be
InstanceScope.INSTANCE.getNode("bundle.id")
. That would allow you to add further nodes underneath, but they wouldn't be accessible from your IPreferenceStore. However, your preference page could set its preference store to the main one for your plugin, and still use IEclipsePreferences or a secondary IPreferenceStore to access extra preferences (you just have to code it yourself, similar toorg.eclipse.ui.internal.dialogs.EditorsPreferencePage
).我通过重写 getPreference 存储来解决这个问题,如下所示:
对我有用
I tackled this problem by overriding the getPreference store as follows:
works for me