Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "FAQ How do I launch the preference page that belongs to my plug-in?"

 
m (Added note about deprecated API access)
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Sometimes, you want to allow the user to edit preferences for your
+
Sometimes, you want to allow the user to edit preferences for a plug-in quickly, without manually opening the Preference dialog via '''Window > Preferences''' and locating the page for the plug-in.
plug-in quickly, without manually opening the Preference
+
dialog via '''Window > Preferences''' and locating the page
+
for your plug-in. You can do this by launching the Preference dialog
+
yourself with a customized <tt>PreferenceManager</tt>, a class
+
responsible for building the tree of preference nodes available on the
+
left-hand side of the Preference dialog. 
+
  
 +
The <code>org.eclipse.ui.dialogs.PreferencesUtil</code> in bundle <code>org.eclipse.ui.workbench</code> can be used for this:
  
 
 
For each preference page
 
you want to display, create a <tt>PreferenceNode</tt> instance
 
that contains a reference to your page and some unique page ID.
 
Pages are then added to a preference manager.  If you want to
 
create a hierarchy of pages, use the <tt>PreferenceManager</tt>
 
method <tt>addTo</tt>, where the path is a period-delimited
 
series of page IDs above the page being added. Finally, create
 
an instance of <tt>PreferenceDialog</tt>, passing in the preference
 
manager you have created.  Here is sample code for opening the
 
Preference dialog on a single preference page called <tt>MyPreferencePage</tt>:
 
 
<pre>
 
<pre>
  IPreferencePage page = new MyPreferencePage();
+
PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(
  PreferenceManager mgr = new PreferenceManager();
+
shell, "org.eclipse.licensing.ui.licensesPreferencePage",
  IPreferenceNode node = new PreferenceNode("1", page);
+
new String[] {"org.eclipse.licensing.ui.licensesPreferencePage"}, null);
  mgr.addToRoot(node);
+
dialog.open();
  PreferenceDialog dialog = new PreferenceDialog(shell, mgr);
+
  dialog.create();
+
  dialog.setMessage(page.getTitle());
+
  dialog.open();
+
 
</pre>
 
</pre>
  
 +
== Alternative solution ==
  
 +
''This solution is old and probably not useful anymore (Please note, that the constructor "PreferenceDialog()" is not API anymore). Maybe it should be removed from this wiki page, but the author of this edit is not sure whether there really is no cases where it is useful.''
  
 +
This solution works by launching the Preference dialog yourself with a customized <tt>PreferenceManager</tt>, a class responsible for building the tree of preference nodes available on the left-hand side of the Preference dialog. 
  
 +
For each preference page you want to display, create a <tt>PreferenceNode</tt> instance that contains a reference to your page and some unique page ID. Pages are then added to a preference manager.  If you want to create a hierarchy of pages, use the <tt>PreferenceManager</tt> method <tt>addTo</tt>, where the path is a period-delimited series of page IDs above the page being added. Finally, create an instance of <tt>PreferenceDialog</tt>, passing in the preference manager you have created.  Here is sample code for opening the Preference dialog on a single preference page called <tt>MyPreferencePage</tt>:
  
 +
<source lang="java">
 +
IPreferencePage page = new MyPreferencePage();
 +
PreferenceManager mgr = new PreferenceManager();
 +
IPreferenceNode node = new PreferenceNode("1", page);
 +
mgr.addToRoot(node);
 +
PreferenceDialog dialog = new PreferenceDialog(shell, mgr);
 +
dialog.create();
 +
dialog.setMessage(page.getTitle());
 +
dialog.open();
 +
</source>
  
 
== See Also: ==
 
== See Also: ==
 
+
*[[FAQ How do I create my own preference page?]]
 
+
[[FAQ_How_do_I_create_my_own_preference_page%3F]]
+
  
 
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
 
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>

Latest revision as of 12:15, 21 July 2020

Sometimes, you want to allow the user to edit preferences for a plug-in quickly, without manually opening the Preference dialog via Window > Preferences and locating the page for the plug-in.

The org.eclipse.ui.dialogs.PreferencesUtil in bundle org.eclipse.ui.workbench can be used for this:

PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(
	shell, "org.eclipse.licensing.ui.licensesPreferencePage",  
	new String[] {"org.eclipse.licensing.ui.licensesPreferencePage"}, null);
dialog.open();

Alternative solution

This solution is old and probably not useful anymore (Please note, that the constructor "PreferenceDialog()" is not API anymore). Maybe it should be removed from this wiki page, but the author of this edit is not sure whether there really is no cases where it is useful.

This solution works by launching the Preference dialog yourself with a customized PreferenceManager, a class responsible for building the tree of preference nodes available on the left-hand side of the Preference dialog.

For each preference page you want to display, create a PreferenceNode instance that contains a reference to your page and some unique page ID. Pages are then added to a preference manager. If you want to create a hierarchy of pages, use the PreferenceManager method addTo, where the path is a period-delimited series of page IDs above the page being added. Finally, create an instance of PreferenceDialog, passing in the preference manager you have created. Here is sample code for opening the Preference dialog on a single preference page called MyPreferencePage:

IPreferencePage page = new MyPreferencePage();
PreferenceManager mgr = new PreferenceManager();
IPreferenceNode node = new PreferenceNode("1", page);
mgr.addToRoot(node);
PreferenceDialog dialog = new PreferenceDialog(shell, mgr);
dialog.create();
dialog.setMessage(page.getTitle());
dialog.open();

See Also:


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.

Back to the top