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>
27 * according to the type of the selection.
30 * <li>if selection is of type <code>ITextSelection</code> then
31 * <code>run(ITextSelection)</code> and <code>selectionChanged(ITextSelection)</code>
33 * <li>if selection is of type <code>IStructuredSelection</code> then
34 * <code>run(IStructuredSelection)</code> and <code>
35 * selectionChanged(IStructuredSelection)</code> is called.</li>
36 * <li>default is to call <code>run(ISelection)</code> and <code>
37 * selectionChanged(ISelection)</code>.</li>
41 * Note: This class is not intended to be subclassed outside the JDT UI plugin.
46 public abstract class SelectionDispatchAction extends Action implements ISelectionChangedListener {
48 private IWorkbenchSite fSite;
51 * Creates a new action with no text and no image.
53 * Configure the action later using the set methods.
56 * @param site the site this action is working on
58 protected SelectionDispatchAction(IWorkbenchSite site) {
59 Assert.isNotNull(site);
64 * Returns the site owning this action.
66 * @return the site owning this action
68 public IWorkbenchSite getSite() {
73 * Returns the selection provided by the site owning this action.
75 * @return the site's selection
77 public ISelection getSelection() {
78 return getSelectionProvider().getSelection();
82 * Returns the shell provided by the site owning this action.
84 * @return the site's shell
86 public Shell getShell() {
87 return fSite.getShell();
91 * Returns the selection provider managed by the site owning this action.
93 * @return the site's selection provider
95 public ISelectionProvider getSelectionProvider() {
96 return fSite.getSelectionProvider();
100 * Updates the action's enablement state according to the given selection. This
101 * default implementation calls one of the <code>selectionChanged</code>
102 * methods depending on the type of the passed selection.
104 * @param selection the selection this action is working on
106 public void update(ISelection selection) {
107 dispatchSelectionChanged(selection);
111 * Notifies this action that the given structured selection has changed. This default
112 * implementation calls <code>selectionChanged(ISelection selection)</code>.
114 * @param selection the new selection
116 protected void selectionChanged(IStructuredSelection selection) {
117 selectionChanged((ISelection)selection);
121 * Executes this actions with the given structured selection. This default implementation
122 * calls <code>run(ISelection selection)</code>.
124 protected void run(IStructuredSelection selection) {
125 run((ISelection)selection);
129 * Notifies this action that the given text selection has changed. This default
130 * implementation calls <code>selectionChanged(ISelection selection)</code>.
132 * @param selection the new selection
134 protected void selectionChanged(ITextSelection selection) {
135 selectionChanged((ISelection)selection);
139 * Executes this actions with the given text selection. This default implementation
140 * calls <code>run(ISelection selection)</code>.
142 protected void run(ITextSelection selection) {
143 run((ISelection)selection);
147 * Notifies this action that the given selection has changed. This default
148 * implementation sets the action's enablement state to <code>false</code>.
150 * @param selection the new selection
152 protected void selectionChanged(ISelection selection) {
157 * Executes this actions with the given selection. This default implementation
160 protected void run(ISelection selection) {
164 * Method declared on IAction.
167 dispatchRun(getSelection());
171 * Method declared on ISelectionChangedListener.
173 public void selectionChanged(SelectionChangedEvent event) {
174 dispatchSelectionChanged(event.getSelection());
177 private void dispatchSelectionChanged(ISelection selection) {
178 if (selection instanceof IStructuredSelection) {
179 selectionChanged((IStructuredSelection)selection);
180 } else if (selection instanceof ITextSelection) {
181 selectionChanged((ITextSelection)selection);
183 selectionChanged(selection);
187 private void dispatchRun(ISelection selection) {
188 if (selection instanceof IStructuredSelection) {
189 run((IStructuredSelection)selection);
190 } else if (selection instanceof ITextSelection) {
191 run((ITextSelection)selection);