Replace deprecated org.eclipse.jface.text.Assert with org.eclipse.core.runtime.Assert
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / ui / actions / SelectionDispatchAction.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  ******************************************************************************/
11 package net.sourceforge.phpdt.ui.actions;
12
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.text.ITextSelection;
15 //incastrix
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;
25
26 /**
27  * Action that dispatches the <code>IAction#run()</code> and the
28  * <code>ISelectionChangedListener#selectionChanged</code> according to the
29  * type of the selection.
30  * 
31  * <ul>
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>
38  * is called.</li>
39  * <li>default is to call <code>run(ISelection)</code> and <code>
40  *      selectionChanged(ISelection)</code>.</li>
41  * </ul>
42  * 
43  * <p>
44  * Note: This class is not intended to be subclassed outside the JDT UI plugin.
45  * </p>
46  * 
47  * @since 2.0
48  */
49 public abstract class SelectionDispatchAction extends Action implements
50                 ISelectionChangedListener {
51
52         private IWorkbenchSite fSite;
53
54         /**
55          * Creates a new action with no text and no image.
56          * <p>
57          * Configure the action later using the set methods.
58          * </p>
59          * 
60          * @param site
61          *            the site this action is working on
62          */
63         protected SelectionDispatchAction(IWorkbenchSite site) {
64                 Assert.isNotNull(site);
65                 fSite = site;
66         }
67
68         /**
69          * Returns the site owning this action.
70          * 
71          * @return the site owning this action
72          */
73         public IWorkbenchSite getSite() {
74                 return fSite;
75         }
76
77         /**
78          * Returns the selection provided by the site owning this action.
79          * 
80          * @return the site's selection
81          */
82         public ISelection getSelection() {
83                 return getSelectionProvider().getSelection();
84         }
85
86         /**
87          * Returns the shell provided by the site owning this action.
88          * 
89          * @return the site's shell
90          */
91         public Shell getShell() {
92                 return fSite.getShell();
93         }
94
95         /**
96          * Returns the selection provider managed by the site owning this action.
97          * 
98          * @return the site's selection provider
99          */
100         public ISelectionProvider getSelectionProvider() {
101                 return fSite.getSelectionProvider();
102         }
103
104         /**
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
108          * passed selection.
109          * 
110          * @param selection
111          *            the selection this action is working on
112          */
113         public void update(ISelection selection) {
114                 dispatchSelectionChanged(selection);
115         }
116
117         /**
118          * Notifies this action that the given structured selection has changed.
119          * This default implementation calls
120          * <code>selectionChanged(ISelection selection)</code>.
121          * 
122          * @param selection
123          *            the new selection
124          */
125         protected void selectionChanged(IStructuredSelection selection) {
126                 selectionChanged((ISelection) selection);
127         }
128
129         /**
130          * Executes this actions with the given structured selection. This default
131          * implementation calls <code>run(ISelection selection)</code>.
132          */
133         protected void run(IStructuredSelection selection) {
134                 run((ISelection) selection);
135         }
136
137         /**
138          * Notifies this action that the given text selection has changed. This
139          * default implementation calls
140          * <code>selectionChanged(ISelection selection)</code>.
141          * 
142          * @param selection
143          *            the new selection
144          */
145         protected void selectionChanged(ITextSelection selection) {
146                 selectionChanged((ISelection) selection);
147         }
148
149         /**
150          * Executes this actions with the given text selection. This default
151          * implementation calls <code>run(ISelection selection)</code>.
152          */
153         protected void run(ITextSelection selection) {
154                 run((ISelection) selection);
155         }
156
157         /**
158          * Notifies this action that the given selection has changed. This default
159          * implementation sets the action's enablement state to <code>false</code>.
160          * 
161          * @param selection
162          *            the new selection
163          */
164         protected void selectionChanged(ISelection selection) {
165                 setEnabled(false);
166         }
167
168         /**
169          * Executes this actions with the given selection. This default
170          * implementation does nothing.
171          */
172         protected void run(ISelection selection) {
173         }
174
175         /*
176          * (non-Javadoc) Method declared on IAction.
177          */
178         public void run() {
179                 dispatchRun(getSelection());
180         }
181
182         /*
183          * (non-Javadoc) Method declared on ISelectionChangedListener.
184          */
185         public void selectionChanged(SelectionChangedEvent event) {
186                 dispatchSelectionChanged(event.getSelection());
187         }
188
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);
194                 } else {
195                         selectionChanged(selection);
196                 }
197         }
198
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);
204                 } else {
205                         run(selection);
206                 }
207         }
208 }