]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/ui/actions/SelectionDispatchAction.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / ui / actions / SelectionDispatchAction.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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.swt.widgets.Shell;
14
15 import org.eclipse.core.runtime.Assert;
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  * @noextend This class is not intended to be subclassed by clients.
53  */
54 public abstract class SelectionDispatchAction extends Action implements ISelectionChangedListener {
55
56         private IWorkbenchSite fSite;
57         private ISelectionProvider fSpecialSelectionProvider;
58
59         /**
60          * Creates a new action with no text and no image.
61          * <p>
62          * Configure the action later using the set methods.
63          * </p>
64          *
65          * @param site the site this action is working on
66          */
67         protected SelectionDispatchAction(IWorkbenchSite site) {
68                 Assert.isNotNull(site);
69                 fSite= site;
70         }
71
72         /**
73          * Creates a new action with no text and no image
74          *
75          * <p>
76          * Configure the action later using the set methods.
77          * </p>
78          *
79          * @param site the site this action is working on
80          * @param provider a special selection provider which is used
81          * instead of the site's selection provider or <code>null</code> to use the site's
82          * selection provider. Clients can for example use a {@link ConvertingSelectionProvider}
83          * to first convert a selection before passing it to the action.
84          *
85          * @since 3.2
86          * @deprecated Use {@link #setSpecialSelectionProvider(ISelectionProvider)} instead. This constructor will be
87          * removed after 3.2 M5.
88          */
89         protected SelectionDispatchAction(IWorkbenchSite site, ISelectionProvider provider) {
90                 this(site);
91                 setSpecialSelectionProvider(provider);
92         }
93
94         /**
95          * Returns the site owning this action.
96          *
97          * @return the site owning this action
98          */
99         public IWorkbenchSite getSite() {
100                 return fSite;
101         }
102
103         /**
104          * Returns the selection provided by the site owning this action.
105          *
106          * @return the site's selection
107          */
108         public ISelection getSelection() {
109                 ISelectionProvider selectionProvider= getSelectionProvider();
110                 if (selectionProvider != null)
111                         return selectionProvider.getSelection();
112                 else
113                         return null;
114         }
115
116         /**
117          * Returns the shell provided by the site owning this action.
118          *
119          * @return the site's shell
120          */
121         public Shell getShell() {
122                 return fSite.getShell();
123         }
124
125         /**
126          * Returns the selection provider managed by the site owning this action or the selection
127          * provider explicitly set in {@link #setSpecialSelectionProvider(ISelectionProvider)}.
128          *
129          * @return the site's selection provider
130          */
131         public ISelectionProvider getSelectionProvider() {
132                 if (fSpecialSelectionProvider != null) {
133                         return fSpecialSelectionProvider;
134                 }
135                 return fSite.getSelectionProvider();
136         }
137
138         /**
139          * Sets a special selection provider which will be used instead of the site's selection provider.
140          * This method should be used directly after constructing the action and before the action is
141          * registered as a selection listener. The invocation will not a perform a selection change notification.
142          *
143          * @param provider a special selection provider which is used
144          * instead of the site's selection provider or <code>null</code> to use the site's
145          * selection provider. Clients can for example use a {@link ConvertingSelectionProvider}
146          * to first convert a selection before passing it to the action.
147          *
148          * @since 3.2
149          */
150         public void setSpecialSelectionProvider(ISelectionProvider provider) {
151                 fSpecialSelectionProvider= provider;
152         }
153
154         /**
155          * Updates the action's enablement state according to the given selection. This
156          * default implementation calls one of the <code>selectionChanged</code>
157          * methods depending on the type of the passed selection.
158          *
159          * @param selection the selection this action is working on
160          */
161         public void update(ISelection selection) {
162                 dispatchSelectionChanged(selection);
163         }
164
165         /**
166          * Notifies this action that the given structured selection has changed. This default
167          * implementation calls <code>selectionChanged(ISelection selection)</code>.
168          *
169          * @param selection the new selection
170          */
171         public void selectionChanged(IStructuredSelection selection) {
172                 selectionChanged((ISelection)selection);
173         }
174
175         /**
176          * Executes this actions with the given structured selection. This default implementation
177          * calls <code>run(ISelection selection)</code>.
178          *
179          * @param selection the selection
180          */
181         public void run(IStructuredSelection selection) {
182                 run((ISelection)selection);
183         }
184
185         /**
186          * Note: This method is for internal use only. Clients should not call this method.
187          *
188          * @param selection the selection
189          *
190          * @noreference This method is not intended to be referenced by clients.
191          */
192         public void selectionChanged(JavaTextSelection selection) {
193                 selectionChanged((ITextSelection)selection);
194         }
195
196         /**
197          * Note: This method is for internal use only. Clients should not call this method.
198          *
199          * @param selection the selection
200          *
201          * @noreference This method is not intended to be referenced by clients.
202          */
203         public void run(JavaTextSelection selection) {
204                 run((ITextSelection)selection);
205         }
206
207         /**
208          * Notifies this action that the given text selection has changed.  This default
209          * implementation calls <code>selectionChanged(ISelection selection)</code>.
210          *
211          * @param selection the new selection
212          */
213         public void selectionChanged(ITextSelection selection) {
214                 selectionChanged((ISelection)selection);
215         }
216
217         /**
218          * Executes this actions with the given text selection. This default implementation
219          * calls <code>run(ISelection selection)</code>.
220          *
221          * @param selection the selection
222          */
223         public void run(ITextSelection selection) {
224                 run((ISelection)selection);
225         }
226
227         /**
228          * Notifies this action that the given selection has changed.  This default
229          * implementation sets the action's enablement state to <code>false</code>.
230          *
231          * @param selection the new selection
232          */
233         public void selectionChanged(ISelection selection) {
234                 setEnabled(false);
235         }
236
237         /**
238          * Executes this actions with the given selection. This default implementation
239          * does nothing.
240          *
241          * @param selection the selection
242          */
243         public void run(ISelection selection) {
244         }
245
246         /* (non-Javadoc)
247          * Method declared on IAction.
248          */
249         @Override
250         public void run() {
251                 dispatchRun(getSelection());
252         }
253
254         /* (non-Javadoc)
255          * Method declared on ISelectionChangedListener.
256          */
257         public void selectionChanged(SelectionChangedEvent event) {
258                 dispatchSelectionChanged(event.getSelection());
259         }
260
261         private void dispatchSelectionChanged(ISelection selection) {
262                 if (selection instanceof IStructuredSelection) {
263                         selectionChanged((IStructuredSelection)selection);
264                 } else if (selection instanceof JavaTextSelection) {
265                         selectionChanged((JavaTextSelection)selection);
266                 } else if (selection instanceof ITextSelection) {
267                         selectionChanged((ITextSelection)selection);
268                 } else {
269                         selectionChanged(selection);
270                 }
271         }
272
273         private void dispatchRun(ISelection selection) {
274                 if (selection instanceof IStructuredSelection) {
275                         run((IStructuredSelection)selection);
276                 } else if (selection instanceof JavaTextSelection) {
277                         run((JavaTextSelection)selection);
278             } else if (selection instanceof ITextSelection) {
279                         run((ITextSelection)selection);
280                 } else {
281                         run(selection);
282                 }
283         }
284 }