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.dialogs;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.jface.preference.PreferenceDialog;
014: import org.eclipse.jface.viewers.ISelectionProvider;
015: import org.eclipse.jface.viewers.IStructuredSelection;
016: import org.eclipse.jface.window.IShellProvider;
017: import org.eclipse.jface.window.SameShellProvider;
018: import org.eclipse.swt.widgets.Shell;
019: import org.eclipse.ui.PlatformUI;
020: import org.eclipse.ui.actions.SelectionProviderAction;
021: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
022: import org.eclipse.ui.internal.WorkbenchMessages;
023: import org.eclipse.ui.internal.dialogs.PropertyDialog;
024: import org.eclipse.ui.internal.dialogs.PropertyPageContributorManager;
025:
026: /**
027: * Standard action for opening a Property Pages Dialog on the currently selected
028: * element.
029: * <p>
030: * This class may be instantiated; it is not intended to be subclassed.
031: * </p>
032: * <p>
033: * Generally speaking, this action is useful in pop-up menus because it allows
034: * the user to browse and change properties of selected elements. When
035: * performed, the action will bring up a Property Pages Dialog containing
036: * property pages registered with the workbench for elements of the selected
037: * type.
038: * </p>
039: * <p>
040: * Although the action is capable of calculating if there are any applicable
041: * pages for the current selection, this calculation is costly because it
042: * require searching the workbench registry. Where performance is critical, the
043: * action can simply be added to the pop-up menu. In the event of no applicable
044: * pages, the action will just open an appropriate message dialog.
045: * </p>
046: */
047: public class PropertyDialogAction extends SelectionProviderAction {
048: /**
049: * Provides the shell in which to open the property dialog.
050: */
051: private IShellProvider shellProvider;
052:
053: /**
054: * The id of the page to open up on.
055: */
056: private String initialPageId;
057:
058: /**
059: * Creates a new action for opening a property dialog on the elements from
060: * the given selection provider.
061: *
062: * @param shell
063: * the shell in which the dialog will open
064: * @param provider
065: * the selection provider whose elements the property dialog will
066: * describe
067: * @deprecated use PropertyDialogAction(IShellProvider, ISelectionProvider)
068: */
069: public PropertyDialogAction(Shell shell, ISelectionProvider provider) {
070: this (new SameShellProvider(shell), provider);
071: }
072:
073: /**
074: * Creates a new action for opening a property dialog on the elements from
075: * the given selection provider.
076: *
077: * @param shell
078: * provides the shell in which the dialog will open
079: * @param provider
080: * the selection provider whose elements the property dialog will
081: * describe
082: * @since 3.1
083: */
084: public PropertyDialogAction(IShellProvider shell,
085: ISelectionProvider provider) {
086: super (provider, WorkbenchMessages.PropertyDialog_text);
087: Assert.isNotNull(shell);
088: this .shellProvider = shell;
089: setToolTipText(WorkbenchMessages.PropertyDialog_toolTip);
090: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
091: IWorkbenchHelpContextIds.PROPERTY_DIALOG_ACTION);
092: }
093:
094: /**
095: * Returns whether the provided object has pages registered in the property
096: * page manager.
097: *
098: * @param object
099: * @return boolean
100: */
101: private boolean hasPropertyPagesFor(Object object) {
102: return PropertyPageContributorManager.getManager()
103: .hasContributorsFor(object);
104: }
105:
106: /**
107: * Returns whether this action is actually applicable to the current
108: * selection. If this action is disabled, it will return <code>false</code>
109: * without further calculation. If it is enabled, it will check with the
110: * workbench's property page manager to see if there are any property pages
111: * registered for the selected element's type.
112: * <p>
113: * This method is generally too expensive to use when updating the enabled
114: * state of the action on each selection change.
115: * </p>
116: *
117: * @return <code>true</code> if the selection is of size 1 and there are
118: * property pages for the selected element, and <code>false</code>
119: * otherwise
120: */
121: public boolean isApplicableForSelection() {
122: if (!isEnabled()) {
123: return false;
124: }
125: return isApplicableForSelection(getStructuredSelection());
126: }
127:
128: /**
129: * Returns whether this action is applicable to the current selection. This
130: * checks that the selection is of size 1, and checks with the workbench's
131: * property page manager to see if there are any property pages registered
132: * for the selected element's type.
133: * <p>
134: * This method is generally too expensive to use when updating the enabled
135: * state of the action on each selection change.
136: * </p>
137: *
138: * @param selection
139: * The selection to test
140: * @return <code>true</code> if the selection is of size 1 and there are
141: * property pages for the selected element, and <code>false</code>
142: * otherwise
143: */
144: public boolean isApplicableForSelection(
145: IStructuredSelection selection) {
146: return selection.size() == 1
147: && hasPropertyPagesFor(selection.getFirstElement());
148: }
149:
150: /* (non-Javadoc)
151: * @see org.eclipse.jface.action.IAction#run()
152: */
153: public void run() {
154:
155: PreferenceDialog dialog = createDialog();
156: if (dialog != null) {
157: dialog.open();
158: }
159: }
160:
161: /**
162: * Create the dialog for the receiver. If no pages are found, an informative
163: * message dialog is presented instead.
164: *
165: * @return PreferenceDialog or <code>null</code> if no applicable pages
166: * are found.
167: * @since 3.1
168: */
169: public PreferenceDialog createDialog() {
170:
171: Object element = getStructuredSelection().getFirstElement();
172: if (element == null) {
173: return null;
174: }
175: return PropertyDialog.createDialogOn(shellProvider.getShell(),
176: initialPageId, element);
177: }
178:
179: /* (non-Javadoc)
180: * @see org.eclipse.ui.actions.SelectionProviderAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
181: */
182: public void selectionChanged(IStructuredSelection selection) {
183: setEnabled(selection.size() == 1
184: && selection.getFirstElement() != null);
185: }
186: }
|