KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > context > AbstractDebugContextAction


1 /*******************************************************************************
2  * Copyright (c) 2006 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.context;
13
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.debug.internal.ui.actions.provisional.IBooleanRequestMonitor;
17 import org.eclipse.debug.internal.ui.contexts.DebugContextManager;
18 import org.eclipse.debug.internal.ui.contexts.provisional.IDebugContextListener;
19 import org.eclipse.debug.internal.ui.contexts.provisional.IDebugContextManager;
20 import org.eclipse.jface.action.Action;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.eclipse.swt.widgets.Event;
26 import org.eclipse.ui.IWorkbenchPart;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.PlatformUI;
29
30 public abstract class AbstractDebugContextAction extends Action implements IDebugContextListener {
31
32     private IStructuredSelection fActiveContext;
33
34     private IWorkbenchWindow fWindow;
35
36     private AbstractDebugContextActionDelegate fDelegate;
37
38     public AbstractDebugContextAction() {
39         super();
40         String JavaDoc helpContextId = getHelpContextId();
41         if (helpContextId != null)
42             PlatformUI.getWorkbench().getHelpSystem().setHelp(this, helpContextId);
43         setEnabled(false);
44     }
45    
46     public void setDelegate(AbstractDebugContextActionDelegate delegate) {
47         fDelegate = delegate;
48     }
49
50     protected abstract void doAction(Object JavaDoc target);
51
52     public AbstractDebugContextAction(String JavaDoc text) {
53         super(text);
54     }
55
56     public AbstractDebugContextAction(String JavaDoc text, ImageDescriptor image) {
57         super(text, image);
58     }
59
60     public AbstractDebugContextAction(String JavaDoc text, int style) {
61         super(text, style);
62     }
63
64     /*
65      * (non-Javadoc)
66      * @see org.eclipse.debug.internal.ui.contexts.provisional.IDebugContextListener#contextActivated(org.eclipse.jface.viewers.ISelection, org.eclipse.ui.IWorkbenchPart)
67      */

68     public void contextActivated(ISelection context, IWorkbenchPart part) {
69         fActiveContext = null;
70         update(context);
71     }
72
73     /*
74      * (non-Javadoc)
75      * @see org.eclipse.debug.internal.ui.contexts.provisional.IDebugContextListener#contextChanged(org.eclipse.jface.viewers.ISelection, org.eclipse.ui.IWorkbenchPart)
76      */

77     public void contextChanged(ISelection context, IWorkbenchPart part) {
78         contextActivated(context, part);
79     }
80
81     protected void update(ISelection context) {
82         if (context instanceof IStructuredSelection) {
83             IStructuredSelection ss = (IStructuredSelection) context;
84             updateEnableStateForContext(ss);
85             fActiveContext = (IStructuredSelection) context;
86         } else {
87             setEnabled(false);
88             fActiveContext = StructuredSelection.EMPTY;
89         }
90     }
91
92     /*
93      * (non-Javadoc)
94      * @see org.eclipse.jface.action.Action#setEnabled(boolean)
95      */

96     public void setEnabled(boolean enabled) {
97         super.setEnabled(enabled);
98         if (fDelegate != null) {
99             fDelegate.setEnabled(enabled);
100         }
101     }
102
103     /**
104      * Return whether the action should be enabled or not based on the given
105      * selection.
106      */

107     protected void updateEnableStateForContext(IStructuredSelection selection) {
108         int size = selection.size();
109         BooleanRequestMonitor monitor = new BooleanRequestMonitor(this, size);
110         if (size > 0) {
111             Iterator JavaDoc itr = selection.iterator();
112             while (itr.hasNext()) {
113                 Object JavaDoc element = itr.next();
114                 isEnabledFor(element, monitor);
115             }
116         } else {
117             notSupported(monitor);
118         }
119     }
120
121     protected abstract void isEnabledFor(Object JavaDoc element, IBooleanRequestMonitor monitor);
122     
123     /**
124      * Updates the monitor with a false result. Action should call this method when
125      * updating enablement and the function is not supported.
126      *
127      * @param monitor
128      */

129     protected void notSupported(IBooleanRequestMonitor monitor) {
130         monitor.setResult(false);
131         monitor.done();
132     }
133
134     public void init(IWorkbenchWindow window) {
135         setWindow(window);
136         IDebugContextManager manager = DebugContextManager.getDefault();
137         manager.addDebugContextListener(this, window);
138         ISelection activeContext = manager.getActiveContext(window);
139         if (activeContext != null) {
140             contextActivated(activeContext, null);
141         } else {
142             setEnabled(getInitialEnablement());
143         }
144     }
145     
146     /**
147      * Returns whether this action should be enabled when initialized
148      * and there is no active debug context.
149      *
150      * @return false, by default
151      */

152     protected boolean getInitialEnablement() {
153         return false;
154     }
155
156     protected void setWindow(IWorkbenchWindow window) {
157         fWindow = window;
158     }
159
160     /**
161      * Returns the most recent selection
162      *
163      * @return structured selection
164      */

165     protected IStructuredSelection getContext() {
166         return fActiveContext;
167     }
168
169     /*
170      * (non-Javadoc)
171      * @see org.eclipse.jface.action.Action#run()
172      */

173     public void run() {
174         IStructuredSelection selection = getContext();
175         if (selection != null && isEnabled()) {
176             // disable the action so it cannot be run again until an event or
177
// selection change updates the enablement
178
setEnabled(false);
179             for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
180                 Object JavaDoc element = iter.next();
181                 doAction(element);
182             }
183         }
184     }
185
186     /*
187      * (non-Javadoc)
188      * @see org.eclipse.jface.action.Action#runWithEvent(org.eclipse.swt.widgets.Event)
189      */

190     public void runWithEvent(Event event) {
191         run();
192     }
193
194     public void dispose() {
195         IWorkbenchWindow window = getWindow();
196         if (getWindow() != null) {
197             DebugContextManager.getDefault().removeDebugContextListener(this, window);
198         }
199     }
200
201     protected IWorkbenchWindow getWindow() {
202         return fWindow;
203     }
204
205     /**
206      * Returns the String to use as an error dialog message for a failed action.
207      * This message appears as the "Message:" in the error dialog for this
208      * action. Default is to return null.
209      */

210     protected String JavaDoc getErrorDialogMessage() {
211         return null;
212     }
213
214     /**
215      * Returns the String to use as a status message for a failed action. This
216      * message appears as the "Reason:" in the error dialog for this action.
217      * Default is to return the empty String.
218      */

219     protected String JavaDoc getStatusMessage() {
220         return ""; //$NON-NLS-1$
221
}
222
223     public abstract String JavaDoc getHelpContextId();
224
225     /*
226      * (non-Javadoc)
227      * @see org.eclipse.jface.action.Action#getId()
228      */

229     public abstract String JavaDoc getId();
230
231     /*
232      * (non-Javadoc)
233      * @see org.eclipse.jface.action.Action#getText()
234      */

235     public abstract String JavaDoc getText();
236
237     /*
238      * (non-Javadoc)
239      * @see org.eclipse.jface.action.Action#getToolTipText()
240      */

241     public abstract String JavaDoc getToolTipText();
242
243     /*
244      * (non-Javadoc)
245      * @see org.eclipse.jface.action.Action#getDisabledImageDescriptor()
246      */

247     public abstract ImageDescriptor getDisabledImageDescriptor();
248
249     /*
250      * (non-Javadoc)
251      * @see org.eclipse.jface.action.Action#getHoverImageDescriptor()
252      */

253     public abstract ImageDescriptor getHoverImageDescriptor();
254
255     /*
256      * (non-Javadoc)
257      * @see org.eclipse.jface.action.Action#getImageDescriptor()
258      */

259     public abstract ImageDescriptor getImageDescriptor();
260 }
261
Popular Tags