我正在尝试以编程方式创建首选项页面,我需要使用首选项页面而不在plugin.xml中定义preferencePage扩展点 我非常接近解决方案,我能够在第一次加载应用程序时加载页面并保存值,
我的代码的核心是
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工作!!!
但是当我第二次进入首选项窗口时,我在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中缺少某些东西 在这一点上,我无法修复我的代码,有人成功地以编程方式创建了首选项页面???
答案 0 :(得分:3)
这是java代码,它允许以编程方式创建首选项页面:
//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();
答案 1 :(得分:2)
最后我发现只是我不能通过使用默认首选项页面查看器的代码来执行此操作!
所以我意识到每次都会调用一个加载PreferenceDialog的处理程序,每次都会创建节点和页面。这是我发现的唯一方法,也是有效的。
答案 2 :(得分:1)
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();
这将完美无缺。