001: /*******************************************************************************
002: * Copyright (c) 2005, 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.dialogs;
011:
012: import org.eclipse.core.runtime.IAdaptable;
013: import org.eclipse.jface.preference.IPreferencePage;
014: import org.eclipse.jface.preference.PreferenceDialog;
015: import org.eclipse.jface.preference.PreferenceManager;
016: import org.eclipse.jface.preference.PreferencePage;
017: import org.eclipse.swt.widgets.Shell;
018: import org.eclipse.ui.internal.dialogs.FilteredPreferenceDialog;
019: import org.eclipse.ui.internal.dialogs.PropertyDialog;
020: import org.eclipse.ui.internal.dialogs.WorkbenchPreferenceDialog;
021:
022: /**
023: * The PreferencesUtil class is the class that opens a properties or preference
024: * dialog on a set of ids.
025: * @since 3.1
026: */
027: public final class PreferencesUtil {
028:
029: /**
030: * Apply the data to the first page if there is any.
031: * @param data The data to be applied
032: * @param displayedIds The ids to filter to.
033: * @param dialog The dialog to apply to.
034: */
035: private static void applyOptions(Object data,
036: String[] displayedIds, FilteredPreferenceDialog dialog) {
037: if (data != null) {
038: dialog.setPageData(data);
039: IPreferencePage page = dialog.getCurrentPage();
040: if (page instanceof PreferencePage) {
041: ((PreferencePage) page).applyData(data);
042: }
043: }
044:
045: if (displayedIds != null) {
046: dialog.showOnly(displayedIds);
047: }
048: }
049:
050: /**
051: * Creates a workbench preference dialog and selects particular preference page.
052: * If there is already a preference dialog open this dialog is used and its
053: * selection is set to the page with id preferencePageId.
054: * Show the other pages as filtered results using whatever filtering
055: * criteria the search uses. It is the responsibility of the caller to then
056: * call <code>open()</code>. The call to <code>open()</code> will not
057: * return until the dialog closes, so this is the last chance to manipulate
058: * the dialog.
059: *
060: * @param shell
061: * The Shell to parent the dialog off of if it is not
062: * already created. May be <code>null</code>
063: * in which case the active workbench window will be used
064: * if available.
065: * @param preferencePageId
066: * The identifier of the preference page to open; may be
067: * <code>null</code>. If it is <code>null</code>, then the
068: * preference page is not selected or modified in any way.
069: * @param displayedIds
070: * The ids of the other pages to be displayed using the same
071: * filtering criterea as search. If this is <code>null</code>,
072: * then the all preference pages are shown.
073: * @param data
074: * Data that will be passed to all of the preference pages to be
075: * applied as specified within the page as they are created. If
076: * the data is <code>null</code> nothing will be called.
077: *
078: * @return a preference dialog.
079: * @since 3.1
080: * @see PreferenceDialog#PreferenceDialog(Shell, PreferenceManager)
081: */
082: public static final PreferenceDialog createPreferenceDialogOn(
083: Shell shell, String preferencePageId,
084: String[] displayedIds, Object data) {
085: FilteredPreferenceDialog dialog = WorkbenchPreferenceDialog
086: .createDialogOn(shell, preferencePageId);
087:
088: applyOptions(data, displayedIds, dialog);
089:
090: return dialog;
091: }
092:
093: /**
094: * Creates a workbench preference dialog to a particular preference page.
095: * Show the other pages as filtered results using whatever filtering
096: * criteria the search uses. It is the responsibility of the caller to then
097: * call <code>open()</code>. The call to <code>open()</code> will not
098: * return until the dialog closes, so this is the last chance to manipulate
099: * the dialog.
100: *
101: * @param shell
102: * The shell to use to parent the dialog if required.
103: * @param propertyPageId
104: * The identifier of the preference page to open; may be
105: * <code>null</code>. If it is <code>null</code>, then the
106: * dialog is opened with no selected page.
107: * @param element
108: * IAdaptable An adaptable element to open the dialog
109: * on.
110: * @param displayedIds
111: * The ids of the other pages to be displayed using the same
112: * filtering criterea as search. If this is <code>null</code>,
113: * then the all preference pages are shown.
114: * @param data
115: * Data that will be passed to all of the preference pages to be
116: * applied as specified within the page as they are created. If
117: * the data is <code>null</code> nothing will be called.
118: *
119: * @return A preference dialog showing properties for the selection or
120: * <code>null</code> if it could not be created.
121: * @since 3.1
122: */
123: public static final PreferenceDialog createPropertyDialogOn(
124: Shell shell, final IAdaptable element,
125: String propertyPageId, String[] displayedIds, Object data) {
126:
127: FilteredPreferenceDialog dialog = PropertyDialog
128: .createDialogOn(shell, propertyPageId, element);
129:
130: if (dialog == null) {
131: return null;
132: }
133:
134: applyOptions(data, displayedIds, dialog);
135:
136: return dialog;
137:
138: }
139:
140: }
|