KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > PropertyDialogAction


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.dialogs;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.jface.preference.PreferenceDialog;
15 import org.eclipse.jface.viewers.ISelectionProvider;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.jface.window.IShellProvider;
18 import org.eclipse.jface.window.SameShellProvider;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.ui.PlatformUI;
21 import org.eclipse.ui.actions.SelectionProviderAction;
22 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
23 import org.eclipse.ui.internal.WorkbenchMessages;
24 import org.eclipse.ui.internal.dialogs.PropertyDialog;
25 import org.eclipse.ui.internal.dialogs.PropertyPageContributorManager;
26
27 /**
28  * Standard action for opening a Property Pages Dialog on the currently selected
29  * element.
30  * <p>
31  * This class may be instantiated; it is not intended to be subclassed.
32  * </p>
33  * <p>
34  * Generally speaking, this action is useful in pop-up menus because it allows
35  * the user to browse and change properties of selected elements. When
36  * performed, the action will bring up a Property Pages Dialog containing
37  * property pages registered with the workbench for elements of the selected
38  * type.
39  * </p>
40  * <p>
41  * Although the action is capable of calculating if there are any applicable
42  * pages for the current selection, this calculation is costly because it
43  * require searching the workbench registry. Where performance is critical, the
44  * action can simply be added to the pop-up menu. In the event of no applicable
45  * pages, the action will just open an appropriate message dialog.
46  * </p>
47  */

48 public class PropertyDialogAction extends SelectionProviderAction {
49     /**
50      * Provides the shell in which to open the property dialog.
51      */

52     private IShellProvider shellProvider;
53     
54     /**
55      * The id of the page to open up on.
56      */

57     private String JavaDoc initialPageId;
58
59
60     /**
61      * Creates a new action for opening a property dialog on the elements from
62      * the given selection provider.
63      *
64      * @param shell
65      * the shell in which the dialog will open
66      * @param provider
67      * the selection provider whose elements the property dialog will
68      * describe
69      * @deprecated use PropertyDialogAction(IShellProvider, ISelectionProvider)
70      */

71     public PropertyDialogAction(Shell shell, ISelectionProvider provider) {
72         this(new SameShellProvider(shell), provider);
73     }
74     
75     /**
76      * Creates a new action for opening a property dialog on the elements from
77      * the given selection provider.
78      *
79      * @param shell
80      * provides the shell in which the dialog will open
81      * @param provider
82      * the selection provider whose elements the property dialog will
83      * describe
84      * @since 3.1
85      */

86     public PropertyDialogAction(IShellProvider shell, ISelectionProvider provider) {
87         super(provider, WorkbenchMessages.PropertyDialog_text);
88         Assert.isNotNull(shell);
89         this.shellProvider = shell;
90         setToolTipText(WorkbenchMessages.PropertyDialog_toolTip);
91         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
92                 IWorkbenchHelpContextIds.PROPERTY_DIALOG_ACTION);
93     }
94
95     /**
96      * Returns whether the provided object has pages registered in the property
97      * page manager.
98      *
99      * @param object
100      * @return boolean
101      */

102     private boolean hasPropertyPagesFor(Object JavaDoc object) {
103         return PropertyPageContributorManager.getManager().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(IStructuredSelection selection) {
145         return selection.size() == 1 && hasPropertyPagesFor(selection.getFirstElement());
146     }
147
148     
149     /* (non-Javadoc)
150      * @see org.eclipse.jface.action.IAction#run()
151      */

152     public void run() {
153
154         PreferenceDialog dialog = createDialog();
155         if (dialog != null) {
156             dialog.open();
157         }
158     }
159
160     /**
161      * Create the dialog for the receiver. If no pages are found, an informative
162      * message dialog is presented instead.
163      *
164      * @return PreferenceDialog or <code>null</code> if no applicable pages
165      * are found.
166      * @since 3.1
167      */

168     public PreferenceDialog createDialog() {
169
170         Object JavaDoc element = getStructuredSelection().getFirstElement();
171         if (element == null) {
172             return null;
173         }
174         return PropertyDialog
175                 .createDialogOn(shellProvider.getShell(), initialPageId, element);
176     }
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 && selection.getFirstElement() != null);
184     }
185 }
186
Popular Tags