001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.dialogs;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.jface.dialogs.IDialogSettings;
014: import org.eclipse.jface.preference.IPreferenceNode;
015: import org.eclipse.jface.preference.PreferenceManager;
016: import org.eclipse.swt.widgets.Shell;
017: import org.eclipse.ui.IWorkbench;
018: import org.eclipse.ui.IWorkbenchWindow;
019: import org.eclipse.ui.PlatformUI;
020: import org.eclipse.ui.activities.WorkbenchActivityHelper;
021: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
022: import org.eclipse.ui.internal.WorkbenchPlugin;
023:
024: /**
025: * Prefence dialog for the workbench including the ability to load/save
026: * preferences.
027: */
028: public class WorkbenchPreferenceDialog extends FilteredPreferenceDialog {
029: /**
030: * There can only ever be one instance of the workbench's preference dialog.
031: * This keeps a handle on this instance, so that attempts to create a second
032: * dialog should just fail (or return the original instance).
033: *
034: * @since 3.1
035: */
036: private static WorkbenchPreferenceDialog instance = null;
037:
038: /**
039: * The bounds of this dialog will be persisted in the dialog settings.
040: * This is defined at the most concrete level of the hierarchy so that
041: * different concrete implementations don't necessarily store their bounds
042: * in the same settings.
043: *
044: * @since 3.2
045: */
046: private static final String DIALOG_SETTINGS_SECTION = "WorkbenchPreferenceDialogSettings"; //$NON-NLS-1$
047:
048: /**
049: * Creates a workbench preference dialog to a particular preference page. It
050: * is the responsibility of the caller to then call <code>open()</code>.
051: * The call to <code>open()</code> will not return until the dialog
052: * closes, so this is the last chance to manipulate the dialog.
053: *
054: * @param shell
055: * The Shell to parent the dialog off of if it is not
056: * already created. May be <code>null</code>
057: * in which case the active workbench window will be used
058: * if available.
059: * @param preferencePageId
060: * The identifier of the preference page to open; may be
061: * <code>null</code>. If it is <code>null</code>, then the
062: * preference page is not selected or modified in any way.
063: * @return The selected preference page.
064: * @since 3.1
065: */
066: public static final WorkbenchPreferenceDialog createDialogOn(
067: Shell shell, final String preferencePageId) {
068: final WorkbenchPreferenceDialog dialog;
069:
070: if (instance == null) {
071: /*
072: * There is no existing preference dialog, so open a new one with
073: * the given selected page.
074: */
075:
076: Shell parentShell = shell;
077: if (parentShell == null) {
078: // Determine a decent parent shell.
079: final IWorkbench workbench = PlatformUI.getWorkbench();
080: final IWorkbenchWindow workbenchWindow = workbench
081: .getActiveWorkbenchWindow();
082: if (workbenchWindow != null) {
083: parentShell = workbenchWindow.getShell();
084: } else {
085: parentShell = null;
086: }
087: }
088:
089: // Create the dialog
090: final PreferenceManager preferenceManager = PlatformUI
091: .getWorkbench().getPreferenceManager();
092: dialog = new WorkbenchPreferenceDialog(parentShell,
093: preferenceManager);
094: if (preferencePageId != null) {
095: dialog.setSelectedNode(preferencePageId);
096: }
097: dialog.create();
098: PlatformUI.getWorkbench().getHelpSystem().setHelp(
099: dialog.getShell(),
100: IWorkbenchHelpContextIds.PREFERENCE_DIALOG);
101:
102: } else {
103: /*
104: * There is an existing preference dialog, so let's just select the
105: * given preference page.
106: */
107: dialog = instance;
108: if (preferencePageId != null) {
109: dialog.setCurrentPageId(preferencePageId);
110: }
111:
112: }
113:
114: // Get the selected node, and return it.
115: return dialog;
116: }
117:
118: /**
119: * Creates a new preference dialog under the control of the given preference
120: * manager.
121: *
122: * @param parentShell
123: * the parent shell
124: * @param manager
125: * the preference manager
126: */
127: public WorkbenchPreferenceDialog(Shell parentShell,
128: PreferenceManager manager) {
129: super (parentShell, manager);
130: Assert
131: .isTrue((instance == null),
132: "There cannot be two preference dialogs at once in the workbench."); //$NON-NLS-1$
133: instance = this ;
134:
135: }
136:
137: /* (non-Javadoc)
138: * @see org.eclipse.jface.window.Window#close()
139: */
140: public boolean close() {
141: instance = null;
142: return super .close();
143: }
144:
145: /**
146: * Differs from super implementation in that if the node is found but should
147: * be filtered based on a call to
148: * <code>WorkbenchActivityHelper.filterItem()</code> then
149: * <code>null</code> is returned.
150: *
151: * @see org.eclipse.jface.preference.PreferenceDialog#findNodeMatching(java.lang.String)
152: */
153: protected IPreferenceNode findNodeMatching(String nodeId) {
154: IPreferenceNode node = super .findNodeMatching(nodeId);
155: if (WorkbenchActivityHelper.filterItem(node)) {
156: return null;
157: }
158: return node;
159: }
160:
161: /* (non-Javadoc)
162: * @see org.eclipse.jface.dialogs.Dialog#okPressed()
163: */
164: protected void okPressed() {
165: super .okPressed();
166: }
167:
168: /* (non-Javadoc)
169: * @see org.eclipse.jface.window.Dialog#getDialogBoundsSettings()
170: *
171: * @since 3.2
172: */
173: protected IDialogSettings getDialogBoundsSettings() {
174: IDialogSettings settings = WorkbenchPlugin.getDefault()
175: .getDialogSettings();
176: IDialogSettings section = settings
177: .getSection(DIALOG_SETTINGS_SECTION);
178: if (section == null) {
179: section = settings.addNewSection(DIALOG_SETTINGS_SECTION);
180: }
181: return section;
182: }
183:
184: /* (non-Javadoc)
185: * @see org.eclipse.jface.window.Dialog#getDialogBoundsStrategy()
186: *
187: * Overridden to persist only the location, not the size, since the current
188: * page dictates the most appropriate size for the dialog.
189: * @since 3.2
190: */
191: protected int getDialogBoundsStrategy() {
192: return DIALOG_PERSISTLOCATION;
193: }
194: }
|