001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.ui.actions;
011:
012: import org.eclipse.core.runtime.Assert;
013:
014: import org.eclipse.swt.widgets.Shell;
015:
016: import org.eclipse.jface.action.Action;
017: import org.eclipse.jface.viewers.ISelection;
018: import org.eclipse.jface.viewers.ISelectionChangedListener;
019: import org.eclipse.jface.viewers.ISelectionProvider;
020: import org.eclipse.jface.viewers.IStructuredSelection;
021: import org.eclipse.jface.viewers.SelectionChangedEvent;
022:
023: import org.eclipse.jface.text.ITextSelection;
024:
025: import org.eclipse.ui.IWorkbenchSite;
026:
027: import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
028:
029: /**
030: * Action that dispatches the <code>IAction#run()</code> and the
031: * <code>ISelectionChangedListener#selectionChanged</code>
032: * according to the type of the selection.
033: *
034: * <ul>
035: * <li>if selection is of type <code>ITextSelection</code> then
036: * <code>run(ITextSelection)</code> and <code>selectionChanged(ITextSelection)</code>
037: * is called.</li>
038: * <li>if selection is of type <code>IStructuredSelection</code> then
039: * <code>run(IStructuredSelection)</code> and <code>
040: * selectionChanged(IStructuredSelection)</code> is called.</li>
041: * <li>default is to call <code>run(ISelection)</code> and <code>
042: * selectionChanged(ISelection)</code>.</li>
043: * </ul>
044: *
045: * <p>
046: * Note: This class is not intended to be subclassed outside the JDT UI plug-in.
047: * </p>
048: *
049: * @since 2.0
050: */
051: public abstract class SelectionDispatchAction extends Action implements
052: ISelectionChangedListener {
053:
054: private IWorkbenchSite fSite;
055: private ISelectionProvider fSpecialSelectionProvider;
056:
057: /**
058: * Creates a new action with no text and no image.
059: * <p>
060: * Configure the action later using the set methods.
061: * </p>
062: *
063: * @param site the site this action is working on
064: */
065: protected SelectionDispatchAction(IWorkbenchSite site) {
066: Assert.isNotNull(site);
067: fSite = site;
068: }
069:
070: /**
071: * Creates a new action with no text and no image
072: *
073: * <p>
074: * Configure the action later using the set methods.
075: * </p>
076: *
077: * @param site the site this action is working on
078: * @param provider a special selection provider which is used
079: * instead of the site's selection provider or <code>null</code> to use the site's
080: * selection provider. Clients can for example use a {@link ConvertingSelectionProvider}
081: * to first convert a selection before passing it to the action.
082: *
083: * @since 3.2
084: * @deprecated Use {@link #setSpecialSelectionProvider(ISelectionProvider)} instead. This constructor will be
085: * removed after 3.2 M5.
086: */
087: protected SelectionDispatchAction(IWorkbenchSite site,
088: ISelectionProvider provider) {
089: this (site);
090: setSpecialSelectionProvider(provider);
091: }
092:
093: /**
094: * Returns the site owning this action.
095: *
096: * @return the site owning this action
097: */
098: public IWorkbenchSite getSite() {
099: return fSite;
100: }
101:
102: /**
103: * Returns the selection provided by the site owning this action.
104: *
105: * @return the site's selection
106: */
107: public ISelection getSelection() {
108: ISelectionProvider selectionProvider = getSelectionProvider();
109: if (selectionProvider != null)
110: return selectionProvider.getSelection();
111: else
112: return null;
113: }
114:
115: /**
116: * Returns the shell provided by the site owning this action.
117: *
118: * @return the site's shell
119: */
120: public Shell getShell() {
121: return fSite.getShell();
122: }
123:
124: /**
125: * Returns the selection provider managed by the site owning this action or the selection
126: * provider explicitly set in {@link #setSpecialSelectionProvider(ISelectionProvider)}.
127: *
128: * @return the site's selection provider
129: */
130: public ISelectionProvider getSelectionProvider() {
131: if (fSpecialSelectionProvider != null) {
132: return fSpecialSelectionProvider;
133: }
134: return fSite.getSelectionProvider();
135: }
136:
137: /**
138: * Sets a special selection provider which will be used instead of the site's selection provider.
139: * This method should be used directly after constructing the action and before the action is
140: * registered as a selection listener. The invocation will not a perform a selection change notification.
141: *
142: * @param provider a special selection provider which is used
143: * instead of the site's selection provider or <code>null</code> to use the site's
144: * selection provider. Clients can for example use a {@link ConvertingSelectionProvider}
145: * to first convert a selection before passing it to the action.
146: *
147: * @since 3.2
148: */
149: public void setSpecialSelectionProvider(ISelectionProvider provider) {
150: fSpecialSelectionProvider = provider;
151: }
152:
153: /**
154: * Updates the action's enablement state according to the given selection. This
155: * default implementation calls one of the <code>selectionChanged</code>
156: * methods depending on the type of the passed selection.
157: *
158: * @param selection the selection this action is working on
159: */
160: public void update(ISelection selection) {
161: dispatchSelectionChanged(selection);
162: }
163:
164: /**
165: * Notifies this action that the given structured selection has changed. This default
166: * implementation calls <code>selectionChanged(ISelection selection)</code>.
167: *
168: * @param selection the new selection
169: */
170: public void selectionChanged(IStructuredSelection selection) {
171: selectionChanged((ISelection) selection);
172: }
173:
174: /**
175: * Executes this actions with the given structured selection. This default implementation
176: * calls <code>run(ISelection selection)</code>.
177: *
178: * @param selection the selection
179: */
180: public void run(IStructuredSelection selection) {
181: run((ISelection) selection);
182: }
183:
184: /**
185: * Note: This method is for internal use only. Clients should not call this method.
186: *
187: * @param selection the selection
188: */
189: public void selectionChanged(JavaTextSelection selection) {
190: selectionChanged((ITextSelection) selection);
191: }
192:
193: /**
194: * Note: This method is for internal use only. Clients should not call this method.
195: *
196: * @param selection the selection
197: */
198: public void run(JavaTextSelection selection) {
199: run((ITextSelection) selection);
200: }
201:
202: /**
203: * Notifies this action that the given text selection has changed. This default
204: * implementation calls <code>selectionChanged(ISelection selection)</code>.
205: *
206: * @param selection the new selection
207: */
208: public void selectionChanged(ITextSelection selection) {
209: selectionChanged((ISelection) selection);
210: }
211:
212: /**
213: * Executes this actions with the given text selection. This default implementation
214: * calls <code>run(ISelection selection)</code>.
215: *
216: * @param selection the selection
217: */
218: public void run(ITextSelection selection) {
219: run((ISelection) selection);
220: }
221:
222: /**
223: * Notifies this action that the given selection has changed. This default
224: * implementation sets the action's enablement state to <code>false</code>.
225: *
226: * @param selection the new selection
227: */
228: public void selectionChanged(ISelection selection) {
229: setEnabled(false);
230: }
231:
232: /**
233: * Executes this actions with the given selection. This default implementation
234: * does nothing.
235: *
236: * @param selection the selection
237: */
238: public void run(ISelection selection) {
239: }
240:
241: /* (non-Javadoc)
242: * Method declared on IAction.
243: */
244: public void run() {
245: dispatchRun(getSelection());
246: }
247:
248: /* (non-Javadoc)
249: * Method declared on ISelectionChangedListener.
250: */
251: public void selectionChanged(SelectionChangedEvent event) {
252: dispatchSelectionChanged(event.getSelection());
253: }
254:
255: private void dispatchSelectionChanged(ISelection selection) {
256: if (selection instanceof IStructuredSelection) {
257: selectionChanged((IStructuredSelection) selection);
258: } else if (selection instanceof JavaTextSelection) {
259: selectionChanged((JavaTextSelection) selection);
260: } else if (selection instanceof ITextSelection) {
261: selectionChanged((ITextSelection) selection);
262: } else {
263: selectionChanged(selection);
264: }
265: }
266:
267: private void dispatchRun(ISelection selection) {
268: if (selection instanceof IStructuredSelection) {
269: run((IStructuredSelection) selection);
270: } else if (selection instanceof JavaTextSelection) {
271: run((JavaTextSelection) selection);
272: } else if (selection instanceof ITextSelection) {
273: run((ITextSelection) selection);
274: } else {
275: run(selection);
276: }
277: }
278: }
|