KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.debug.internal.ui.actions;
13
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.runtime.jobs.Job;
17 import org.eclipse.jface.action.IAction;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.StructuredSelection;
21 import org.eclipse.swt.widgets.Event;
22 import org.eclipse.ui.IActionDelegate2;
23 import org.eclipse.ui.IViewActionDelegate;
24 import org.eclipse.ui.IViewPart;
25 import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
26
27 /**
28  * This class provides a base implementation of a selection action delegate, more specifically a delegate
29  * that uses a selection context to update its underlying <code>IAction</code>.
30  *
31  * This class is intended to be extended by clients
32  *
33  * @see {@link IViewActionDelegate}
34  * @see {@link IActionDelegate2}
35  *
36  */

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

42     private IAction fAction;
43     
44     /**
45      * This action's view part, or <code>null</code> if not installed in a
46      * 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      * Used to schedule jobs, or <code>null</code> if none
57      */

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

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

72     public void dispose() {
73         fSelection = null;
74     }
75
76     /* (non-Javadoc)
77      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
78      */

79     public void selectionChanged(IAction action, ISelection s) {
80         if (s instanceof IStructuredSelection) {
81             IStructuredSelection ss = (IStructuredSelection) s;
82             action.setEnabled(getEnableStateForSelection(ss));
83             setSelection(ss);
84         } else {
85             action.setEnabled(false);
86             setSelection(StructuredSelection.EMPTY);
87         }
88     }
89
90     /**
91      * Returns the String to use as an error dialog message for a failed action.
92      * This message appears as the "Message:" in the error dialog for this
93      * action. Default is to return null.
94      *
95      * @return the message for the error dialog
96      */

97     protected String JavaDoc getErrorDialogMessage() {
98         return null;
99     }
100
101     /**
102      * Returns the String to use as a status message for a failed action. This
103      * message appears as the "Reason:" in the error dialog for this action.
104      * Default is to return the empty String.
105      *
106      * @return the status message
107      */

108     protected String JavaDoc getStatusMessage() {
109         return ""; //$NON-NLS-1$
110
}
111
112     /*
113      * (non-Javadoc)
114      *
115      * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
116      */

117     public void init(IViewPart view) {
118         setView(view);
119         fProgressService = (IWorkbenchSiteProgressService) view.getAdapter(IWorkbenchSiteProgressService.class);
120     }
121
122     /**
123      * Returns this action's view part, or <code>null</code> if not installed
124      * in a view.
125      *
126      * @return the underlying <code>IViewPart</code> or <code>null</code>
127      */

128     protected IViewPart getView() {
129         return fViewPart;
130     }
131
132     /**
133      * Returns the most recent selection
134      *
135      * @return structured selection
136      */

137     protected IStructuredSelection getSelection() {
138         return fSelection;
139     }
140
141     /**
142      * Sets the most recent selection
143      *
144      * @parm selection structured selection
145      */

146     private void setSelection(IStructuredSelection context) {
147         fSelection = context;
148     }
149
150     /**
151      * Allows the underlying <code>IAction</code> to be set to the specified one
152      * @param action the action to set
153      */

154     protected void setAction(IAction action) {
155         fAction = action;
156     }
157
158     /**
159      * Allows access to the underlying <code>IAction</code>
160      * @return the underlying <code>IAction</code>
161      */

162     protected IAction getAction() {
163         return fAction;
164     }
165
166     /**
167      * Allows the underlying <code>IViewPart</code> to be set
168      * @param viewPart the <code>IViewPart</code> to set
169      */

170     protected void setView(IViewPart viewPart) {
171         fViewPart = viewPart;
172     }
173
174     /**
175      * Return whether the action should be enabled or not based on the given
176      * selection.
177      *
178      * @return true if the action should be enabled for the specified selection context
179      * false otherwise
180      */

181     protected boolean getEnableStateForSelection(IStructuredSelection selection) {
182         if (selection.size() == 0) {
183             return false;
184         }
185         Iterator JavaDoc itr = selection.iterator();
186         while (itr.hasNext()) {
187             Object JavaDoc element = itr.next();
188             if (!isEnabledFor(element)) {
189                 return false;
190             }
191         }
192         return true;
193     }
194
195     /**
196      * Returns if the action should be enabled for the specified object context
197      * @param element the object context
198      * @return true if the action should be enabled for the specified object context
199      * false otherwise
200      */

201     protected boolean isEnabledFor(Object JavaDoc element) {
202         return true;
203     }
204
205     /* (non-Javadoc)
206      * @see org.eclipse.ui.IActionDelegate2#runWithEvent(org.eclipse.jface.action.IAction, org.eclipse.swt.widgets.Event)
207      */

208     public void runWithEvent(IAction action, Event event) {
209         run(action);
210     }
211
212     /* (non-Javadoc)
213      * @see org.eclipse.ui.IActionDelegate2#init(org.eclipse.jface.action.IAction)
214      */

215     public void init(IAction action) {
216         setAction(action);
217     }
218
219     /**
220      * Schedules the given job with this action's progress service
221      *
222      * @param job
223      */

224     protected void schedule(Job job) {
225         if (fProgressService == null) {
226             job.schedule();
227         } else {
228             fProgressService.schedule(job);
229         }
230     }
231 }
232
Popular Tags