KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > AbstractDebugActionDelegate


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.debug.internal.ui.actions;
12
13
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.runtime.MultiStatus;
17 import org.eclipse.debug.core.DebugException;
18 import org.eclipse.debug.internal.ui.DebugUIPlugin;
19 import org.eclipse.jface.action.IAction;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.swt.custom.BusyIndicator;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.Event;
26 import org.eclipse.ui.IActionDelegate2;
27 import org.eclipse.ui.IViewActionDelegate;
28 import org.eclipse.ui.IViewPart;
29 import org.eclipse.ui.IWorkbenchWindow;
30
31 /**
32  * This class is an abstract implementation of common features for a debug <code>IViewActionDelegate</code>
33  *
34  * This class is intended to be extended by clients
35  * @see {@link IViewActionDelegate}
36  * @see {@link IActionDelegate2}
37  */

38 public abstract class AbstractDebugActionDelegate implements IViewActionDelegate, IActionDelegate2 {
39     
40     /**
41      * The underlying action for this delegate
42      */

43     private IAction fAction;
44     /**
45      * This action's view part, or <code>null</code>
46      * if not installed in a view.
47      */

48     private IViewPart fViewPart;
49     
50     /**
51      * Cache of the most recent selection
52      */

53     private IStructuredSelection fSelection = StructuredSelection.EMPTY;
54     
55     /**
56      * Whether this delegate has been initialized
57      */

58     private boolean fInitialized = false;
59     
60     /**
61      * It's crucial that delegate actions have a zero-argument constructor so that
62      * they can be reflected into existence when referenced in an action set
63      * in the plugin's plugin.xml file.
64      */

65     public AbstractDebugActionDelegate() {}
66     
67     /* (non-Javadoc)
68      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
69      */

70     public void dispose(){
71         fSelection= null;
72     }
73
74     /* (non-Javadoc)
75      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
76      */

77     public void run(IAction action){
78         if (action.isEnabled()) {
79             IStructuredSelection selection = getSelection();
80             // disable the action so it cannot be run again until an event or selection change
81
// updates the enablement
82
action.setEnabled(false);
83             runInForeground(selection);
84         }
85     }
86     
87     /**
88      * Runs this action in the UI thread.
89      */

90     private void runInForeground(final IStructuredSelection selection) {
91         final MultiStatus status=
92             new MultiStatus(DebugUIPlugin.getUniqueIdentifier(), DebugException.REQUEST_FAILED, getStatusMessage(), null);
93         BusyIndicator.showWhile(Display.getCurrent(), new Runnable JavaDoc() {
94             public void run() {
95                 Iterator JavaDoc selectionIter = selection.iterator();
96                 while (selectionIter.hasNext()) {
97                     Object JavaDoc element= selectionIter.next();
98                     try {
99                         // Action's enablement could have been changed since
100
// it was last enabled. Check that the action is still
101
// enabled before running the action.
102
if (isEnabledFor(element))
103                             doAction(element);
104                     } catch (DebugException e) {
105                         status.merge(e.getStatus());
106                     }
107                 }
108             }
109         });
110         reportErrors(status);
111     }
112
113     /**
114      * Reports the specified <code>MultiStatus</code> in an error dialog
115      * @param ms the specified <code>MultiStatus</code>
116      */

117     private void reportErrors(final MultiStatus ms) {
118         if (!ms.isOK()) {
119             IWorkbenchWindow window= DebugUIPlugin.getActiveWorkbenchWindow();
120             if (window != null) {
121                 DebugUIPlugin.errorDialog(window.getShell(), ActionMessages.AbstractDebugActionDelegate_0, getErrorDialogMessage(), ms);
122             } else {
123                 DebugUIPlugin.log(ms);
124             }
125         }
126     }
127
128     /* (non-Javadoc)
129      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
130      */

131     public void selectionChanged(IAction action, ISelection s) {
132         boolean wasInitialized= initialize(action, s);
133         if (!wasInitialized) {
134             if (getView() != null) {
135                 update(action, s);
136             }
137         }
138     }
139     
140     /**
141      * Updates the specified selection based on the selection, as well as setting the selection
142      * for this action
143      * @param action the action to update
144      * @param s the selection
145      */

146     protected void update(IAction action, ISelection s) {
147         if (s instanceof IStructuredSelection) {
148             IStructuredSelection ss = (IStructuredSelection)s;
149             action.setEnabled(getEnableStateForSelection(ss));
150             setSelection(ss);
151         } else {
152             action.setEnabled(false);
153             setSelection(StructuredSelection.EMPTY);
154         }
155     }
156     
157     /**
158      * Performs the specific action on this element.
159      */

160     protected abstract void doAction(Object JavaDoc element) throws DebugException;
161
162     /**
163      * Returns the String to use as an error dialog message for
164      * a failed action. This message appears as the "Message:" in
165      * the error dialog for this action.
166      * Default is to return null.
167      */

168     protected String JavaDoc getErrorDialogMessage(){
169         return null;
170     }
171     /**
172      * Returns the String to use as a status message for
173      * a failed action. This message appears as the "Reason:"
174      * in the error dialog for this action.
175      * Default is to return the empty String.
176      */

177     protected String JavaDoc getStatusMessage(){
178         return ""; //$NON-NLS-1$
179
}
180
181     /* (non-Javadoc)
182      * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
183      */

184     public void init(IViewPart view) {
185         fViewPart = view;
186     }
187     
188     /**
189      * Returns this action's view part, or <code>null</code>
190      * if not installed in a view.
191      *
192      * @return view part or <code>null</code>
193      */

194     protected IViewPart getView() {
195         return fViewPart;
196     }
197
198     /**
199      * Initialize this delegate, updating this delegate's
200      * presentation.
201      * As well, all of the flavors of AbstractDebugActionDelegates need to
202      * have the initial enabled state set with a call to update(IAction, ISelection).
203      *
204      * @param action the presentation for this action
205      * @return whether the action was initialized
206      */

207     protected boolean initialize(IAction action, ISelection selection) {
208         if (!isInitialized()) {
209             setAction(action);
210             update(action, selection);
211             setInitialized(true);
212             return true;
213         }
214         return false;
215     }
216
217     /**
218      * Returns the most recent selection
219      *
220      * @return structured selection
221      */

222     protected IStructuredSelection getSelection() {
223         return fSelection;
224     }
225     
226     /**
227      * Sets the most recent selection
228      *
229      * @parm selection structured selection
230      */

231     private void setSelection(IStructuredSelection selection) {
232         fSelection = selection;
233     }
234     
235     /**
236      * Allows the underlying <code>IAction</code> to be set to the specified <code>IAction</code>
237      * @param action the action to set
238      */

239     protected void setAction(IAction action) {
240         fAction = action;
241     }
242
243     /**
244      * Allows access to the underlying <code>IAction</code>
245      * @return the underlying <code>IAction</code>
246      */

247     protected IAction getAction() {
248         return fAction;
249     }
250     
251     /**
252      * Returns if this action has been initialized or not
253      * @return if this action has been initialized or not
254      */

255     protected boolean isInitialized() {
256         return fInitialized;
257     }
258
259     /**
260      * Sets the initialized state of this action to the specified boolean value
261      * @param initialized the value to set the initialized state to
262      */

263     protected void setInitialized(boolean initialized) {
264         fInitialized = initialized;
265     }
266     
267     /**
268      * Return whether the action should be enabled or not based on the given selection.
269      */

270     protected boolean getEnableStateForSelection(IStructuredSelection selection) {
271         if (selection.size() == 0) {
272             return false;
273         }
274         Iterator JavaDoc itr= selection.iterator();
275         while (itr.hasNext()) {
276             Object JavaDoc element= itr.next();
277             if (!isEnabledFor(element)) {
278                 return false;
279             }
280         }
281         return true;
282     }
283
284     /**
285      * Returns if this action is enabled for the specified object context
286      * @param element the context
287      * @return true if it is, false otherwise
288      */

289     protected boolean isEnabledFor(Object JavaDoc element) {
290         return true;
291     }
292
293     /* (non-Javadoc)
294      * @see org.eclipse.ui.IActionDelegate2#runWithEvent(org.eclipse.jface.action.IAction, org.eclipse.swt.widgets.Event)
295      */

296     public void runWithEvent(IAction action, Event event) {
297         run(action);
298     }
299
300     /* (non-Javadoc)
301      * @see org.eclipse.ui.IActionDelegate2#init(org.eclipse.jface.action.IAction)
302      */

303     public void init(IAction action) {
304         fAction = action;
305     }
306
307 }
308
Popular Tags