1 /*******************************************************************************
2 * Copyright (c) 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.actions;
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.text.ITextSelection;
15 import org.eclipse.jface.util.Assert;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.ISelectionChangedListener;
18 import org.eclipse.jface.viewers.ISelectionProvider;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.SelectionChangedEvent;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.ui.IWorkbenchSite;
25 * Action that dispatches the <code>IAction#run()</code> and the
26 * <code>ISelectionChangedListener#selectionChanged</code> according to the
27 * type of the selection.
30 * <li>if selection is of type <code>ITextSelection</code> then
31 * <code>run(ITextSelection)</code> and
32 * <code>selectionChanged(ITextSelection)</code> is called.</li>
33 * <li>if selection is of type <code>IStructuredSelection</code> then
34 * <code>run(IStructuredSelection)</code> and <code>
35 * selectionChanged(IStructuredSelection)</code>
37 * <li>default is to call <code>run(ISelection)</code> and <code>
38 * selectionChanged(ISelection)</code>.</li>
42 * Note: This class is not intended to be subclassed outside the JDT UI plugin.
47 public abstract class SelectionDispatchAction extends Action implements
48 ISelectionChangedListener {
50 private IWorkbenchSite fSite;
53 * Creates a new action with no text and no image.
55 * Configure the action later using the set methods.
59 * the site this action is working on
61 protected SelectionDispatchAction(IWorkbenchSite site) {
62 Assert.isNotNull(site);
67 * Returns the site owning this action.
69 * @return the site owning this action
71 public IWorkbenchSite getSite() {
76 * Returns the selection provided by the site owning this action.
78 * @return the site's selection
80 public ISelection getSelection() {
81 return getSelectionProvider().getSelection();
85 * Returns the shell provided by the site owning this action.
87 * @return the site's shell
89 public Shell getShell() {
90 return fSite.getShell();
94 * Returns the selection provider managed by the site owning this action.
96 * @return the site's selection provider
98 public ISelectionProvider getSelectionProvider() {
99 return fSite.getSelectionProvider();
103 * Updates the action's enablement state according to the given selection.
104 * This default implementation calls one of the
105 * <code>selectionChanged</code> methods depending on the type of the
109 * the selection this action is working on
111 public void update(ISelection selection) {
112 dispatchSelectionChanged(selection);
116 * Notifies this action that the given structured selection has changed.
117 * This default implementation calls
118 * <code>selectionChanged(ISelection selection)</code>.
123 protected void selectionChanged(IStructuredSelection selection) {
124 selectionChanged((ISelection) selection);
128 * Executes this actions with the given structured selection. This default
129 * implementation calls <code>run(ISelection selection)</code>.
131 protected void run(IStructuredSelection selection) {
132 run((ISelection) selection);
136 * Notifies this action that the given text selection has changed. This
137 * default implementation calls
138 * <code>selectionChanged(ISelection selection)</code>.
143 protected void selectionChanged(ITextSelection selection) {
144 selectionChanged((ISelection) selection);
148 * Executes this actions with the given text selection. This default
149 * implementation calls <code>run(ISelection selection)</code>.
151 protected void run(ITextSelection selection) {
152 run((ISelection) selection);
156 * Notifies this action that the given selection has changed. This default
157 * implementation sets the action's enablement state to <code>false</code>.
162 protected void selectionChanged(ISelection selection) {
167 * Executes this actions with the given selection. This default
168 * implementation does nothing.
170 protected void run(ISelection selection) {
174 * (non-Javadoc) Method declared on IAction.
177 dispatchRun(getSelection());
181 * (non-Javadoc) Method declared on ISelectionChangedListener.
183 public void selectionChanged(SelectionChangedEvent event) {
184 dispatchSelectionChanged(event.getSelection());
187 private void dispatchSelectionChanged(ISelection selection) {
188 if (selection instanceof IStructuredSelection) {
189 selectionChanged((IStructuredSelection) selection);
190 } else if (selection instanceof ITextSelection) {
191 selectionChanged((ITextSelection) selection);
193 selectionChanged(selection);
197 private void dispatchRun(ISelection selection) {
198 if (selection instanceof IStructuredSelection) {
199 run((IStructuredSelection) selection);
200 } else if (selection instanceof ITextSelection) {
201 run((ITextSelection) selection);