KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > SelectionDispatchAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.ui.actions;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.swt.widgets.Shell;
16
17 import org.eclipse.jface.action.Action;
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
24 import org.eclipse.jface.text.ITextSelection;
25
26 import org.eclipse.ui.IWorkbenchSite;
27
28 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
29
30 /**
31  * Action that dispatches the <code>IAction#run()</code> and the
32  * <code>ISelectionChangedListener#selectionChanged</code>
33  * according to the type of the selection.
34  *
35  * <ul>
36  * <li>if selection is of type <code>ITextSelection</code> then
37  * <code>run(ITextSelection)</code> and <code>selectionChanged(ITextSelection)</code>
38  * is called.</li>
39  * <li>if selection is of type <code>IStructuredSelection</code> then
40  * <code>run(IStructuredSelection)</code> and <code>
41  * selectionChanged(IStructuredSelection)</code> is called.</li>
42  * <li>default is to call <code>run(ISelection)</code> and <code>
43  * selectionChanged(ISelection)</code>.</li>
44  * </ul>
45  *
46  * <p>
47  * Note: This class is not intended to be subclassed outside the JDT UI plug-in.
48  * </p>
49  *
50  * @since 2.0
51  */

52 public abstract class SelectionDispatchAction extends Action implements ISelectionChangedListener {
53     
54     private IWorkbenchSite fSite;
55     private ISelectionProvider fSpecialSelectionProvider;
56     
57     /**
58      * Creates a new action with no text and no image.
59      * <p>
60      * Configure the action later using the set methods.
61      * </p>
62      *
63      * @param site the site this action is working on
64      */

65     protected SelectionDispatchAction(IWorkbenchSite site) {
66         Assert.isNotNull(site);
67         fSite= site;
68     }
69
70     /**
71      * Creates a new action with no text and no image
72      *
73      * <p>
74      * Configure the action later using the set methods.
75      * </p>
76      *
77      * @param site the site this action is working on
78      * @param provider a special selection provider which is used
79      * instead of the site's selection provider or <code>null</code> to use the site's
80      * selection provider. Clients can for example use a {@link ConvertingSelectionProvider}
81      * to first convert a selection before passing it to the action.
82      *
83      * @since 3.2
84      * @deprecated Use {@link #setSpecialSelectionProvider(ISelectionProvider)} instead. This constructor will be
85      * removed after 3.2 M5.
86      */

87     protected SelectionDispatchAction(IWorkbenchSite site, ISelectionProvider provider) {
88         this(site);
89         setSpecialSelectionProvider(provider);
90     }
91
92     /**
93      * Returns the site owning this action.
94      *
95      * @return the site owning this action
96      */

97     public IWorkbenchSite getSite() {
98         return fSite;
99     }
100
101     /**
102      * Returns the selection provided by the site owning this action.
103      *
104      * @return the site's selection
105      */

106     public ISelection getSelection() {
107         ISelectionProvider selectionProvider= getSelectionProvider();
108         if (selectionProvider != null)
109             return selectionProvider.getSelection();
110         else
111             return null;
112     }
113
114     /**
115      * Returns the shell provided by the site owning this action.
116      *
117      * @return the site's shell
118      */

119     public Shell getShell() {
120         return fSite.getShell();
121     }
122     
123     /**
124      * Returns the selection provider managed by the site owning this action or the selection
125      * provider explicitly set in {@link #setSpecialSelectionProvider(ISelectionProvider)}.
126      *
127      * @return the site's selection provider
128      */

129     public ISelectionProvider getSelectionProvider() {
130         if (fSpecialSelectionProvider != null) {
131             return fSpecialSelectionProvider;
132         }
133         return fSite.getSelectionProvider();
134     }
135     
136     /**
137      * Sets a special selection provider which will be used instead of the site's selection provider.
138      * This method should be used directly after constructing the action and before the action is
139      * registered as a selection listener. The invocation will not a perform a selection change notification.
140      *
141      * @param provider a special selection provider which is used
142      * instead of the site's selection provider or <code>null</code> to use the site's
143      * selection provider. Clients can for example use a {@link ConvertingSelectionProvider}
144      * to first convert a selection before passing it to the action.
145      *
146      * @since 3.2
147      */

148     public void setSpecialSelectionProvider(ISelectionProvider provider) {
149         fSpecialSelectionProvider= provider;
150     }
151
152     /**
153      * Updates the action's enablement state according to the given selection. This
154      * default implementation calls one of the <code>selectionChanged</code>
155      * methods depending on the type of the passed selection.
156      *
157      * @param selection the selection this action is working on
158      */

159     public void update(ISelection selection) {
160         dispatchSelectionChanged(selection);
161     }
162
163     /**
164      * Notifies this action that the given structured selection has changed. This default
165      * implementation calls <code>selectionChanged(ISelection selection)</code>.
166      *
167      * @param selection the new selection
168      */

169     public void selectionChanged(IStructuredSelection selection) {
170         selectionChanged((ISelection)selection);
171     }
172
173     /**
174      * Executes this actions with the given structured selection. This default implementation
175      * calls <code>run(ISelection selection)</code>.
176      *
177      * @param selection the selection
178      */

179     public void run(IStructuredSelection selection) {
180         run((ISelection)selection);
181     }
182     
183     /**
184      * Note: This method is for internal use only. Clients should not call this method.
185      *
186      * @param selection the selection
187      */

188     public void selectionChanged(JavaTextSelection selection) {
189         selectionChanged((ITextSelection)selection);
190     }
191     
192     /**
193      * Note: This method is for internal use only. Clients should not call this method.
194      *
195      * @param selection the selection
196      */

197     public void run(JavaTextSelection selection) {
198         run((ITextSelection)selection);
199     }
200     
201     /**
202      * Notifies this action that the given text selection has changed. This default
203      * implementation calls <code>selectionChanged(ISelection selection)</code>.
204      *
205      * @param selection the new selection
206      */

207     public void selectionChanged(ITextSelection selection) {
208         selectionChanged((ISelection)selection);
209     }
210     
211     /**
212      * Executes this actions with the given text selection. This default implementation
213      * calls <code>run(ISelection selection)</code>.
214      *
215      * @param selection the selection
216      */

217     public void run(ITextSelection selection) {
218         run((ISelection)selection);
219     }
220     
221     /**
222      * Notifies this action that the given selection has changed. This default
223      * implementation sets the action's enablement state to <code>false</code>.
224      *
225      * @param selection the new selection
226      */

227     public void selectionChanged(ISelection selection) {
228         setEnabled(false);
229     }
230     
231     /**
232      * Executes this actions with the given selection. This default implementation
233      * does nothing.
234      *
235      * @param selection the selection
236      */

237     public void run(ISelection selection) {
238     }
239
240     /* (non-Javadoc)
241      * Method declared on IAction.
242      */

243     public void run() {
244         dispatchRun(getSelection());
245     }
246     
247     /* (non-Javadoc)
248      * Method declared on ISelectionChangedListener.
249      */

250     public void selectionChanged(SelectionChangedEvent event) {
251         dispatchSelectionChanged(event.getSelection());
252     }
253
254     private void dispatchSelectionChanged(ISelection selection) {
255         if (selection instanceof IStructuredSelection) {
256             selectionChanged((IStructuredSelection)selection);
257         } else if (selection instanceof JavaTextSelection) {
258             selectionChanged((JavaTextSelection)selection);
259         } else if (selection instanceof ITextSelection) {
260             selectionChanged((ITextSelection)selection);
261         } else {
262             selectionChanged(selection);
263         }
264     }
265
266     private void dispatchRun(ISelection selection) {
267         if (selection instanceof IStructuredSelection) {
268             run((IStructuredSelection)selection);
269         } else if (selection instanceof JavaTextSelection) {
270             run((JavaTextSelection)selection);
271         } else if (selection instanceof ITextSelection) {
272             run((ITextSelection)selection);
273         } else {
274             run(selection);
275         }
276     }
277 }
278
Popular Tags