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.ui.actions;
13 import org.eclipse.swt.widgets.Shell;
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.jface.text.ITextSelection;
17 import org.eclipse.jface.util.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;
24 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.ui.IWorkbenchSite;
27 import org.eclipse.ui.texteditor.IUpdate;
29 //import org.eclipse.jdt.internal.ui.actions.ActionMessages;
30 import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
33 * Action that dispatches the <code>IAction#run()</code> and the
34 * <code>ISelectionChangedListener#selectionChanged</code>
35 * according to the type of the selection.
38 * <li>if selection is of type <code>ITextSelection</code> then
39 * <code>run(ITextSelection)</code> and <code>selectionChanged(ITextSelection)</code>
41 * <li>if selection is of type <code>IStructuredSelection</code> then
42 * <code>run(IStructuredSelection)</code> and <code>
43 * selectionChanged(IStructuredSelection)</code> is called.</li>
44 * <li>default is to call <code>run(ISelection)</code> and <code>
45 * selectionChanged(ISelection)</code>.</li>
49 * Note: This class is not intended to be subclassed outside the JDT UI plugin.
54 public abstract class SelectionDispatchAction extends Action implements ISelectionChangedListener {
56 private IWorkbenchSite fSite;
59 * Creates a new action with no text and no image.
61 * Configure the action later using the set methods.
64 * @param site the site this action is working on
66 protected SelectionDispatchAction(IWorkbenchSite site) {
67 Assert.isNotNull(site);
72 * Returns the site owning this action.
74 * @return the site owning this action
76 public IWorkbenchSite getSite() {
81 * Returns the selection provided by the site owning this action.
83 * @return the site's selection
85 public ISelection getSelection() {
86 return getSelectionProvider().getSelection();
90 * Returns the shell provided by the site owning this action.
92 * @return the site's shell
94 public Shell getShell() {
95 return fSite.getShell();
99 * Returns the selection provider managed by the site owning this action.
101 * @return the site's selection provider
103 public ISelectionProvider getSelectionProvider() {
104 return fSite.getSelectionProvider();
108 * Updates the action's enablement state according to the given selection. This
109 * default implementation calls one of the <code>selectionChanged</code>
110 * methods depending on the type of the passed selection.
112 * @param selection the selection this action is working on
114 public void update(ISelection selection) {
115 dispatchSelectionChanged(selection);
119 * Notifies this action that the given structured selection has changed. This default
120 * implementation calls <code>selectionChanged(ISelection selection)</code>.
122 * @param selection the new selection
124 protected void selectionChanged(IStructuredSelection selection) {
125 selectionChanged((ISelection)selection);
129 * Executes this actions with the given structured selection. This default implementation
130 * calls <code>run(ISelection selection)</code>.
132 protected void run(IStructuredSelection selection) {
133 run((ISelection)selection);
137 * Notifies this action that the given text selection has changed. This default
138 * implementation calls <code>selectionChanged(ISelection selection)</code>.
140 * @param selection the new selection
142 protected void selectionChanged(ITextSelection selection) {
143 selectionChanged((ISelection)selection);
147 * Executes this actions with the given text selection. This default implementation
148 * calls <code>run(ISelection selection)</code>.
150 protected void run(ITextSelection selection) {
151 run((ISelection)selection);
155 * Notifies this action that the given selection has changed. This default
156 * implementation sets the action's enablement state to <code>false</code>.
158 * @param selection the new selection
160 protected void selectionChanged(ISelection selection) {
165 * Executes this actions with the given selection. This default implementation
168 protected void run(ISelection selection) {
172 * Method declared on IAction.
175 dispatchRun(getSelection());
179 * Method declared on ISelectionChangedListener.
181 public void selectionChanged(SelectionChangedEvent event) {
182 dispatchSelectionChanged(event.getSelection());
185 private void dispatchSelectionChanged(ISelection selection) {
186 if (selection instanceof IStructuredSelection) {
187 selectionChanged((IStructuredSelection)selection);
188 } else if (selection instanceof ITextSelection) {
189 selectionChanged((ITextSelection)selection);
191 selectionChanged(selection);
195 private void dispatchRun(ISelection selection) {
196 if (selection instanceof IStructuredSelection) {
197 run((IStructuredSelection)selection);
198 } else if (selection instanceof ITextSelection) {
199 run((ITextSelection)selection);