Eclipse:以编程方式创建首选项页面

发布于 2024-09-29 13:13:55 字数 1876 浏览 0 评论 0原文

我正在尝试以编程方式创建首选项页面,我需要使用首选项页面而不在plugin.xml 中定义首选项页面扩展点 我非常接近解决方案,我能够在应用程序第一次加载时加载页面并保存值,

我的代码的核心是

PreferenceManager pmngr= PlatformUI.getWorkbench().getPreferenceManager();
 //this come from other plugins that implements my personal IPreferences 
    PreferencePageRCP page =new PreferencePageRCP((IPreferences) element.createExecutableExtension("class"));

    PreferenceNodeRCP node= new PreferenceNodeRCP(page.getId(), page.getTitle(),null,PreferencePageRCP.class.getName());

    node.setPage(page);
     pmngr.addToRoot(node);

PreferencePageRCP 是我的自定义 PreferencePage,所以此时我的 PreferencePage 正在工作!

但是当我第二次进入首选项窗口时,我在 PreferenceNode.createPage 上遇到了错误,所以现在我做了自己的 PreferenceNode 类覆盖 createPage 但现在我收到了 UI 错误

Problems occurred when invoking code from plug-in: "org.eclipse.jface".
!STACK 0
org.eclipse.swt.SWTException: Widget is disposed
 at org.eclipse.swt.SWT.error(SWT.java:4083)
 at org.eclipse.swt.SWT.error(SWT.java:3998)
 at org.eclipse.swt.SWT.error(SWT.java:3969)
 at org.eclipse.swt.widgets.Widget.error(Widget.java:468)
 at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:340)
 at org.eclipse.swt.widgets.Control.setVisible(Control.java:3370)
 at org.eclipse.jface.dialogs.DialogPage.setVisible(DialogPage.java:470)
 at org.eclipse.jface.preference.FieldEditorPreferencePage.setVisible(FieldEditorPreferencePage.java:374)
 at org.eclipse.jface.preference.PreferenceDialog.showPage(PreferenceDialog.java:1323)
 at org.eclipse.ui.internal.dialogs.FilteredPreferenceDialog.showPage(FilteredPreferenceDialog.java:673)
 at org.eclipse.jface.preference.PreferenceDialog$10.run(PreferenceDialog.java:708)
 at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
 andContributionItem.java:796
.................

所以第二次 UI 中缺少了一些东西 我现在无法修复我的代码,有人成功以编程方式创建首选项页面???

I'm trying to create a preference page programmatically, I need to work with preference pages without define preferencePage extension point in plugin.xml
I'm very close to solution, I'm able to load page and save the value the first time application loads,

the core of my code is

PreferenceManager pmngr= PlatformUI.getWorkbench().getPreferenceManager();
 //this come from other plugins that implements my personal IPreferences 
    PreferencePageRCP page =new PreferencePageRCP((IPreferences) element.createExecutableExtension("class"));

    PreferenceNodeRCP node= new PreferenceNodeRCP(page.getId(), page.getTitle(),null,PreferencePageRCP.class.getName());

    node.setPage(page);
     pmngr.addToRoot(node);

where PreferencePageRCP is my Custom PreferencePage so a this point I have my PreferencePage working!!!

but when I go a second time to preference window I got an error on PreferenceNode.createPage, so now I did my own PreferenceNode class overriding createPage but now I got an UI error

Problems occurred when invoking code from plug-in: "org.eclipse.jface".
!STACK 0
org.eclipse.swt.SWTException: Widget is disposed
 at org.eclipse.swt.SWT.error(SWT.java:4083)
 at org.eclipse.swt.SWT.error(SWT.java:3998)
 at org.eclipse.swt.SWT.error(SWT.java:3969)
 at org.eclipse.swt.widgets.Widget.error(Widget.java:468)
 at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:340)
 at org.eclipse.swt.widgets.Control.setVisible(Control.java:3370)
 at org.eclipse.jface.dialogs.DialogPage.setVisible(DialogPage.java:470)
 at org.eclipse.jface.preference.FieldEditorPreferencePage.setVisible(FieldEditorPreferencePage.java:374)
 at org.eclipse.jface.preference.PreferenceDialog.showPage(PreferenceDialog.java:1323)
 at org.eclipse.ui.internal.dialogs.FilteredPreferenceDialog.showPage(FilteredPreferenceDialog.java:673)
 at org.eclipse.jface.preference.PreferenceDialog$10.run(PreferenceDialog.java:708)
 at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
 andContributionItem.java:796
.................

So the second time there is something missing in UI
I at this point I'm not able to fix my code, there is someone who had success in create Preference Page Programmatically???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨轻弹 2024-10-06 13:13:55

下面是 java 代码,它允许以编程方式创建首选项页面:


//create an instance of the custom MyPreference class
IPreferencePage page = new MyPreference(); 
page.setTitle("Custom Configurations");

//创建一个新的PreferenceNode,它将出现在Preference窗口中 PreferenceNode 节点 = new PreferenceNode("1", 页);

//使用工作台的首选项管理器 PreferenceManager pm= PlatformUI.getWorkbench().getPreferenceManager();

pm.addToRoot(节点); //在PreferenceManager Shell中添加节点

shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

//实例化首选项对话框 PreferenceDialog pd = new PreferenceDialog(shell, pm);

//这一行很重要,它告诉 PreferenceDialog 应该写入哪个首选项存储 pd.setPreferenceStore(Activator.getDefault().getPreferenceStore()); pd.create(); pd.open();

Here is the java code, which allows the creation of a preference Page programmatically:


//create an instance of the custom MyPreference class
IPreferencePage page = new MyPreference(); 
page.setTitle("Custom Configurations");

//create a new PreferenceNode that will appear in the Preference window PreferenceNode node = new PreferenceNode("1", page);

//use workbenches's preference manager PreferenceManager pm= PlatformUI.getWorkbench().getPreferenceManager();

pm.addToRoot(node); //add the node in the PreferenceManager

Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

//instantiate the PreferenceDialog PreferenceDialog pd = new PreferenceDialog(shell, pm);

//this line is important, it tell's the PreferenceDialog which preference-store it should write to pd.setPreferenceStore(Activator.getDefault().getPreferenceStore()); pd.create(); pd.open();

红玫瑰 2024-10-06 13:13:55

最后我发现我根本无法使用默认首选项页面查看器通过代码来执行此操作!

所以我实现了一个处理程序,每次调用时加载 PreferenceDialog 并每次创建节点和页面。这是我找到的唯一方法并且有效。

Finally I discovered That simply I can't do this by code using default preference page viewer!

So I realized an handler that load a PreferenceDialog everytime is called and every time create nodes and pages. that the only way I find and it works.

雾里花 2024-10-06 13:13:55

Skip给出的答案几乎是正确的。

引发异常是因为,
一旦对话框关闭,页面就会从 IPreferenceNode 中删除,但该节点仍然保留在 PreferenceManager 中,因此会引发找不到页面的异常。

在将节点添加到 PreferenceManager 之前,我们必须手动删除节点。

pm.removeAll()

//create an instance of the custom MyPreference class
IPreferencePage page = new MyPreference(); 
page.setTitle("Custom Configurations");

//create a new PreferenceNode that will appear in the Preference window
PreferenceNode node = new PreferenceNode("1", page);

//use workbenches's preference manager
PreferenceManager pm= PlatformUI.getWorkbench().getPreferenceManager();

pm.removeAll(); // removes the previous nodes    
pm.addToRoot(node); //add the node in the PreferenceManager

Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

//instantiate the PreferenceDialog
PreferenceDialog pd = new PreferenceDialog(shell, pm); 

//this line is important, it tell's the PreferenceDialog which preference-store it should write to
pd.setPreferenceStore(Activator.getDefault().getPreferenceStore());
pd.create();
pd.open();

这将完美地工作。

Answer given by Skip is almost right.

The exception is raised because,
once the dialog is closed, page is removed from IPreferenceNode, but the node still remains in the PreferenceManager, thus it raises exception for not finding the page.

We must manually remove the nodes before adding the nodes to PreferenceManager.

pm.removeAll()

//create an instance of the custom MyPreference class
IPreferencePage page = new MyPreference(); 
page.setTitle("Custom Configurations");

//create a new PreferenceNode that will appear in the Preference window
PreferenceNode node = new PreferenceNode("1", page);

//use workbenches's preference manager
PreferenceManager pm= PlatformUI.getWorkbench().getPreferenceManager();

pm.removeAll(); // removes the previous nodes    
pm.addToRoot(node); //add the node in the PreferenceManager

Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

//instantiate the PreferenceDialog
PreferenceDialog pd = new PreferenceDialog(shell, pm); 

//this line is important, it tell's the PreferenceDialog which preference-store it should write to
pd.setPreferenceStore(Activator.getDefault().getPreferenceStore());
pd.create();
pd.open();

This will work perfectly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文