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;
16 //import org.eclipse.jface.text.Assert;
17 import org.eclipse.core.runtime.Assert;
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;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.IWorkbenchSite;
27 * Action that dispatches the <code>IAction#run()</code> and the
28 * <code>ISelectionChangedListener#selectionChanged</code> according to the
29 * type of the selection.
32 * <li>if selection is of type <code>ITextSelection</code> then
33 * <code>run(ITextSelection)</code> and
34 * <code>selectionChanged(ITextSelection)</code> is called.</li>
35 * <li>if selection is of type <code>IStructuredSelection</code> then
36 * <code>run(IStructuredSelection)</code> and <code>
37 * selectionChanged(IStructuredSelection)</code>
39 * <li>default is to call <code>run(ISelection)</code> and <code>
40 * selectionChanged(ISelection)</code>.</li>
44 * Note: This class is not intended to be subclassed outside the JDT UI plugin.
49 public abstract class SelectionDispatchAction extends Action implements
50 ISelectionChangedListener {
52 private IWorkbenchSite fSite;
55 * Creates a new action with no text and no image.
57 * Configure the action later using the set methods.
61 * the site this action is working on
63 protected SelectionDispatchAction(IWorkbenchSite site) {
64 Assert.isNotNull(site);
69 * Returns the site owning this action.
71 * @return the site owning this action
73 public IWorkbenchSite getSite() {
78 * Returns the selection provided by the site owning this action.
80 * @return the site's selection
82 public ISelection getSelection() {
83 return getSelectionProvider().getSelection();
87 * Returns the shell provided by the site owning this action.
89 * @return the site's shell
91 public Shell getShell() {
92 return fSite.getShell();
96 * Returns the selection provider managed by the site owning this action.
98 * @return the site's selection provider
100 public ISelectionProvider getSelectionProvider() {
101 return fSite.getSelectionProvider();
105 * Updates the action's enablement state according to the given selection.
106 * This default implementation calls one of the
107 * <code>selectionChanged</code> methods depending on the type of the
111 * the selection this action is working on
113 public void update(ISelection selection) {
114 dispatchSelectionChanged(selection);
118 * Notifies this action that the given structured selection has changed.
119 * This default implementation calls
120 * <code>selectionChanged(ISelection selection)</code>.
125 protected void selectionChanged(IStructuredSelection selection) {
126 selectionChanged((ISelection) selection);
130 * Executes this actions with the given structured selection. This default
131 * implementation calls <code>run(ISelection selection)</code>.
133 protected void run(IStructuredSelection selection) {
134 run((ISelection) selection);
138 * Notifies this action that the given text selection has changed. This
139 * default implementation calls
140 * <code>selectionChanged(ISelection selection)</code>.
145 protected void selectionChanged(ITextSelection selection) {
146 selectionChanged((ISelection) selection);
150 * Executes this actions with the given text selection. This default
151 * implementation calls <code>run(ISelection selection)</code>.
153 protected void run(ITextSelection selection) {
154 run((ISelection) selection);
158 * Notifies this action that the given selection has changed. This default
159 * implementation sets the action's enablement state to <code>false</code>.
164 protected void selectionChanged(ISelection selection) {
169 * Executes this actions with the given selection. This default
170 * implementation does nothing.
172 protected void run(ISelection selection) {
176 * (non-Javadoc) Method declared on IAction.
179 dispatchRun(getSelection());
183 * (non-Javadoc) Method declared on ISelectionChangedListener.
185 public void selectionChanged(SelectionChangedEvent event) {
186 dispatchSelectionChanged(event.getSelection());
189 private void dispatchSelectionChanged(ISelection selection) {
190 if (selection instanceof IStructuredSelection) {
191 selectionChanged((IStructuredSelection) selection);
192 } else if (selection instanceof ITextSelection) {
193 selectionChanged((ITextSelection) selection);
195 selectionChanged(selection);
199 private void dispatchRun(ISelection selection) {
200 if (selection instanceof IStructuredSelection) {
201 run((IStructuredSelection) selection);
202 } else if (selection instanceof ITextSelection) {
203 run((ITextSelection) selection);