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