KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > WorkbenchPreferenceDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.dialogs;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.jface.dialogs.IDialogSettings;
15 import org.eclipse.jface.preference.IPreferenceNode;
16 import org.eclipse.jface.preference.PreferenceManager;
17 import org.eclipse.swt.widgets.Shell;
18 import org.eclipse.ui.IWorkbench;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.PlatformUI;
21 import org.eclipse.ui.activities.WorkbenchActivityHelper;
22 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
23 import org.eclipse.ui.internal.WorkbenchPlugin;
24
25 /**
26  * Prefence dialog for the workbench including the ability to load/save
27  * preferences.
28  */

29 public class WorkbenchPreferenceDialog extends FilteredPreferenceDialog {
30     /**
31      * There can only ever be one instance of the workbench's preference dialog.
32      * This keeps a handle on this instance, so that attempts to create a second
33      * dialog should just fail (or return the original instance).
34      *
35      * @since 3.1
36      */

37     private static WorkbenchPreferenceDialog instance = null;
38     
39     /**
40      * The bounds of this dialog will be persisted in the dialog settings.
41      * This is defined at the most concrete level of the hierarchy so that
42      * different concrete implementations don't necessarily store their bounds
43      * in the same settings.
44      *
45      * @since 3.2
46      */

47     private static final String JavaDoc DIALOG_SETTINGS_SECTION = "WorkbenchPreferenceDialogSettings"; //$NON-NLS-1$
48

49     
50     /**
51      * Creates a workbench preference dialog to a particular preference page. It
52      * is the responsibility of the caller to then call <code>open()</code>.
53      * The call to <code>open()</code> will not return until the dialog
54      * closes, so this is the last chance to manipulate the dialog.
55      *
56      * @param shell
57      * The Shell to parent the dialog off of if it is not
58      * already created. May be <code>null</code>
59      * in which case the active workbench window will be used
60      * if available.
61      * @param preferencePageId
62      * The identifier of the preference page to open; may be
63      * <code>null</code>. If it is <code>null</code>, then the
64      * preference page is not selected or modified in any way.
65      * @return The selected preference page.
66      * @since 3.1
67      */

68     public static final WorkbenchPreferenceDialog createDialogOn(Shell shell, final String JavaDoc preferencePageId) {
69         final WorkbenchPreferenceDialog dialog;
70
71         if (instance == null) {
72             /*
73              * There is no existing preference dialog, so open a new one with
74              * the given selected page.
75              */

76
77             Shell parentShell = shell;
78             if (parentShell == null) {
79                 // Determine a decent parent shell.
80
final IWorkbench workbench = PlatformUI.getWorkbench();
81                 final IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
82                 if (workbenchWindow != null) {
83                     parentShell = workbenchWindow.getShell();
84                 } else {
85                     parentShell = null;
86                 }
87             }
88
89             // Create the dialog
90
final PreferenceManager preferenceManager = PlatformUI.getWorkbench()
91                     .getPreferenceManager();
92             dialog = new WorkbenchPreferenceDialog(parentShell, preferenceManager);
93             if (preferencePageId != null) {
94                 dialog.setSelectedNode(preferencePageId);
95             }
96             dialog.create();
97             PlatformUI.getWorkbench().getHelpSystem().setHelp(
98                     dialog.getShell(),
99                     IWorkbenchHelpContextIds.PREFERENCE_DIALOG);
100
101         } else {
102             /*
103              * There is an existing preference dialog, so let's just select the
104              * given preference page.
105              */

106             dialog = instance;
107             if (preferencePageId != null) {
108                 dialog.setCurrentPageId(preferencePageId);
109             }
110
111         }
112
113         // Get the selected node, and return it.
114
return dialog;
115     }
116
117     /**
118      * Creates a new preference dialog under the control of the given preference
119      * manager.
120      *
121      * @param parentShell
122      * the parent shell
123      * @param manager
124      * the preference manager
125      */

126     public WorkbenchPreferenceDialog(Shell parentShell, PreferenceManager manager) {
127         super(parentShell, manager);
128         Assert.isTrue((instance == null),
129                 "There cannot be two preference dialogs at once in the workbench."); //$NON-NLS-1$
130
instance = this;
131         
132     }
133
134
135     /* (non-Javadoc)
136      * @see org.eclipse.jface.window.Window#close()
137      */

138     public boolean close() {
139         instance = null;
140         return super.close();
141     }
142
143
144     /**
145      * Differs from super implementation in that if the node is found but should
146      * be filtered based on a call to
147      * <code>WorkbenchActivityHelper.filterItem()</code> then
148      * <code>null</code> is returned.
149      *
150      * @see org.eclipse.jface.preference.PreferenceDialog#findNodeMatching(java.lang.String)
151      */

152     protected IPreferenceNode findNodeMatching(String JavaDoc nodeId) {
153         IPreferenceNode node = super.findNodeMatching(nodeId);
154         if (WorkbenchActivityHelper.filterItem(node)) {
155             return null;
156         }
157         return node;
158     }
159     
160     /* (non-Javadoc)
161      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
162      */

163     protected void okPressed() {
164         super.okPressed();
165     }
166     
167     /* (non-Javadoc)
168      * @see org.eclipse.jface.window.Dialog#getDialogBoundsSettings()
169      *
170      * @since 3.2
171      */

172     protected IDialogSettings getDialogBoundsSettings() {
173         IDialogSettings settings = WorkbenchPlugin.getDefault().getDialogSettings();
174         IDialogSettings section = settings.getSection(DIALOG_SETTINGS_SECTION);
175         if (section == null) {
176             section = settings.addNewSection(DIALOG_SETTINGS_SECTION);
177         }
178         return section;
179     }
180     
181     /* (non-Javadoc)
182      * @see org.eclipse.jface.window.Dialog#getDialogBoundsStrategy()
183      *
184      * Overridden to persist only the location, not the size, since the current
185      * page dictates the most appropriate size for the dialog.
186      * @since 3.2
187      */

188     protected int getDialogBoundsStrategy() {
189         return DIALOG_PERSISTLOCATION;
190     }
191 }
192
Popular Tags