1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
13 import org.eclipse.swt.widgets.Shell;
15 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.jface.action.Action;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.ISelectionChangedListener;
20 import org.eclipse.jface.viewers.ISelectionProvider;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.SelectionChangedEvent;
24 import org.eclipse.jface.text.ITextSelection;
26 import org.eclipse.ui.IWorkbenchSite;
28 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
31 * Action that dispatches the <code>IAction#run()</code> and the
32 * <code>ISelectionChangedListener#selectionChanged</code>
33 * according to the type of the selection.
36 * <li>if selection is of type <code>ITextSelection</code> then
37 * <code>run(ITextSelection)</code> and <code>selectionChanged(ITextSelection)</code>
39 * <li>if selection is of type <code>IStructuredSelection</code> then
40 * <code>run(IStructuredSelection)</code> and <code>
41 * selectionChanged(IStructuredSelection)</code> is called.</li>
42 * <li>default is to call <code>run(ISelection)</code> and <code>
43 * selectionChanged(ISelection)</code>.</li>
47 * Note: This class is not intended to be subclassed outside the JDT UI plug-in.
52 * @noextend This class is not intended to be subclassed by clients.
54 public abstract class SelectionDispatchAction extends Action implements ISelectionChangedListener {
56 private IWorkbenchSite fSite;
57 private ISelectionProvider fSpecialSelectionProvider;
60 * Creates a new action with no text and no image.
62 * Configure the action later using the set methods.
65 * @param site the site this action is working on
67 protected SelectionDispatchAction(IWorkbenchSite site) {
68 Assert.isNotNull(site);
73 * Creates a new action with no text and no image
76 * Configure the action later using the set methods.
79 * @param site the site this action is working on
80 * @param provider a special selection provider which is used
81 * instead of the site's selection provider or <code>null</code> to use the site's
82 * selection provider. Clients can for example use a {@link ConvertingSelectionProvider}
83 * to first convert a selection before passing it to the action.
86 * @deprecated Use {@link #setSpecialSelectionProvider(ISelectionProvider)} instead. This constructor will be
87 * removed after 3.2 M5.
89 protected SelectionDispatchAction(IWorkbenchSite site, ISelectionProvider provider) {
91 setSpecialSelectionProvider(provider);
95 * Returns the site owning this action.
97 * @return the site owning this action
99 public IWorkbenchSite getSite() {
104 * Returns the selection provided by the site owning this action.
106 * @return the site's selection
108 public ISelection getSelection() {
109 ISelectionProvider selectionProvider= getSelectionProvider();
110 if (selectionProvider != null)
111 return selectionProvider.getSelection();
117 * Returns the shell provided by the site owning this action.
119 * @return the site's shell
121 public Shell getShell() {
122 return fSite.getShell();
126 * Returns the selection provider managed by the site owning this action or the selection
127 * provider explicitly set in {@link #setSpecialSelectionProvider(ISelectionProvider)}.
129 * @return the site's selection provider
131 public ISelectionProvider getSelectionProvider() {
132 if (fSpecialSelectionProvider != null) {
133 return fSpecialSelectionProvider;
135 return fSite.getSelectionProvider();
139 * Sets a special selection provider which will be used instead of the site's selection provider.
140 * This method should be used directly after constructing the action and before the action is
141 * registered as a selection listener. The invocation will not a perform a selection change notification.
143 * @param provider a special selection provider which is used
144 * instead of the site's selection provider or <code>null</code> to use the site's
145 * selection provider. Clients can for example use a {@link ConvertingSelectionProvider}
146 * to first convert a selection before passing it to the action.
150 public void setSpecialSelectionProvider(ISelectionProvider provider) {
151 fSpecialSelectionProvider= provider;
155 * Updates the action's enablement state according to the given selection. This
156 * default implementation calls one of the <code>selectionChanged</code>
157 * methods depending on the type of the passed selection.
159 * @param selection the selection this action is working on
161 public void update(ISelection selection) {
162 dispatchSelectionChanged(selection);
166 * Notifies this action that the given structured selection has changed. This default
167 * implementation calls <code>selectionChanged(ISelection selection)</code>.
169 * @param selection the new selection
171 public void selectionChanged(IStructuredSelection selection) {
172 selectionChanged((ISelection)selection);
176 * Executes this actions with the given structured selection. This default implementation
177 * calls <code>run(ISelection selection)</code>.
179 * @param selection the selection
181 public void run(IStructuredSelection selection) {
182 run((ISelection)selection);
186 * Note: This method is for internal use only. Clients should not call this method.
188 * @param selection the selection
190 * @noreference This method is not intended to be referenced by clients.
192 public void selectionChanged(JavaTextSelection selection) {
193 selectionChanged((ITextSelection)selection);
197 * Note: This method is for internal use only. Clients should not call this method.
199 * @param selection the selection
201 * @noreference This method is not intended to be referenced by clients.
203 public void run(JavaTextSelection selection) {
204 run((ITextSelection)selection);
208 * Notifies this action that the given text selection has changed. This default
209 * implementation calls <code>selectionChanged(ISelection selection)</code>.
211 * @param selection the new selection
213 public void selectionChanged(ITextSelection selection) {
214 selectionChanged((ISelection)selection);
218 * Executes this actions with the given text selection. This default implementation
219 * calls <code>run(ISelection selection)</code>.
221 * @param selection the selection
223 public void run(ITextSelection selection) {
224 run((ISelection)selection);
228 * Notifies this action that the given selection has changed. This default
229 * implementation sets the action's enablement state to <code>false</code>.
231 * @param selection the new selection
233 public void selectionChanged(ISelection selection) {
238 * Executes this actions with the given selection. This default implementation
241 * @param selection the selection
243 public void run(ISelection selection) {
247 * Method declared on IAction.
251 dispatchRun(getSelection());
255 * Method declared on ISelectionChangedListener.
257 public void selectionChanged(SelectionChangedEvent event) {
258 dispatchSelectionChanged(event.getSelection());
261 private void dispatchSelectionChanged(ISelection selection) {
262 if (selection instanceof IStructuredSelection) {
263 selectionChanged((IStructuredSelection)selection);
264 } else if (selection instanceof JavaTextSelection) {
265 selectionChanged((JavaTextSelection)selection);
266 } else if (selection instanceof ITextSelection) {
267 selectionChanged((ITextSelection)selection);
269 selectionChanged(selection);
273 private void dispatchRun(ISelection selection) {
274 if (selection instanceof IStructuredSelection) {
275 run((IStructuredSelection)selection);
276 } else if (selection instanceof JavaTextSelection) {
277 run((JavaTextSelection)selection);
278 } else if (selection instanceof ITextSelection) {
279 run((ITextSelection)selection);