KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > commands > actions > DebugCommandAction


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.commands.actions;
13
14 import org.eclipse.debug.ui.DebugUITools;
15 import org.eclipse.debug.ui.contexts.DebugContextEvent;
16 import org.eclipse.debug.ui.contexts.IDebugContextListener;
17 import org.eclipse.debug.ui.contexts.IDebugContextService;
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.swt.widgets.Event;
23 import org.eclipse.ui.IWorkbenchPart;
24 import org.eclipse.ui.IWorkbenchWindow;
25 import org.eclipse.ui.PlatformUI;
26
27 /**
28  * Abstract base class for actions performing debug commands
29  *
30  * @since 3.3
31  */

32 public abstract class DebugCommandAction extends Action implements IDebugContextListener {
33
34     /**
35      * The window this action is working for.
36      */

37     private IWorkbenchWindow fWindow;
38     
39     /**
40      * The part this action is working for, or <code>null</code> if global to
41      * a window.
42      */

43     private IWorkbenchPart fPart;
44     
45     /**
46      * Command service.
47      */

48     private DebugCommandService fUpdateService;
49     
50     /**
51      * Delegate this action is working for or <code>null</code> if none.
52      */

53     private DebugCommandActionDelegate fDelegate;
54
55     /**
56      * Constructor
57      */

58     public DebugCommandAction() {
59         super();
60         String JavaDoc helpContextId = getHelpContextId();
61         if (helpContextId != null)
62             PlatformUI.getWorkbench().getHelpSystem().setHelp(this, helpContextId);
63         setEnabled(false);
64     }
65
66     /**
67      * Set the current delegate
68      * @param delegate
69      */

70     public void setDelegate(DebugCommandActionDelegate delegate) {
71         fDelegate = delegate;
72     }
73
74     /**
75      * Executes this action on the given target object
76      *
77      * @param target the target to perform the action on
78      */

79     protected boolean execute(Object JavaDoc[] targets) {
80         return fUpdateService.executeCommand(getCommandType(), targets, getCommandParticipant(targets));
81     }
82     
83     /**
84      * Creates and returns the command participant or <code>null</code>.
85      *
86      * @return command participant to use on command completion
87      */

88     protected ICommandParticipant getCommandParticipant(Object JavaDoc[] targets) {
89         return null;
90     }
91     
92     /**
93      * Returns the command type this action executes.
94      *
95      * @return command class.
96      */

97     abstract protected Class JavaDoc getCommandType();
98
99     /**
100      * @see org.eclipse.debug.ui.contexts.IDebugContextListener#debugContextChanged(org.eclipse.debug.ui.contexts.DebugContextEvent)
101      */

102     public void debugContextChanged(DebugContextEvent event) {
103         fUpdateService.postUpdateCommand(getCommandType(), this);
104     }
105
106     /**
107      * @see org.eclipse.jface.action.Action#setEnabled(boolean)
108      */

109     public void setEnabled(boolean enabled) {
110         super.setEnabled(enabled);
111         if (fDelegate != null) {
112             fDelegate.setEnabled(enabled);
113         }
114     }
115
116     /**
117      * Initializes this action for a specific part.
118      *
119      * @param part workbench part
120      */

121     public void init(IWorkbenchPart part) {
122         fPart = part;
123         fWindow = part.getSite().getWorkbenchWindow();
124         fUpdateService = DebugCommandService.getService(fWindow);
125         IDebugContextService service = getDebugContextService();
126         String JavaDoc partId = part.getSite().getId();
127         service.addDebugContextListener(this, partId);
128         ISelection activeContext = service.getActiveContext(partId);
129         if (activeContext != null) {
130             fUpdateService.updateCommand(getCommandType(), this);
131         } else {
132             setEnabled(getInitialEnablement());
133         }
134     }
135     
136     /**
137      * Initializes the context action
138      * @param window the window
139      */

140     public void init(IWorkbenchWindow window) {
141         fWindow = window;
142         fUpdateService = DebugCommandService.getService(fWindow);
143         IDebugContextService contextService = getDebugContextService();
144         contextService.addDebugContextListener(this);
145         ISelection activeContext = contextService.getActiveContext();
146         if (activeContext != null) {
147             fUpdateService.updateCommand(getCommandType(), this);
148         } else {
149             setEnabled(getInitialEnablement());
150         }
151     }
152     
153     /**
154      * Returns whether this action should be enabled when initialized
155      * and there is no active debug context.
156      *
157      * @return false, by default
158      */

159     protected boolean getInitialEnablement() {
160         return false;
161     }
162
163     /**
164      * Returns the most recent selection
165      *
166      * @return structured selection
167      */

168     protected ISelection getContext() {
169         if (fPart != null) {
170             getDebugContextService().getActiveContext(fPart.getSite().getId());
171         }
172         return getDebugContextService().getActiveContext();
173     }
174
175     /*
176      * (non-Javadoc)
177      * @see org.eclipse.jface.action.Action#run()
178      */

179     public void run() {
180         ISelection selection = getContext();
181         if (selection instanceof IStructuredSelection && isEnabled()) {
182             IStructuredSelection ss = (IStructuredSelection) selection;
183             boolean enabled = execute(ss.toArray());
184             // disable the action according to the command
185
setEnabled(enabled);
186         }
187     }
188
189     /*
190      * (non-Javadoc)
191      * @see org.eclipse.jface.action.Action#runWithEvent(org.eclipse.swt.widgets.Event)
192      */

193     public void runWithEvent(Event event) {
194         run();
195     }
196
197     /**
198      * Clean up when removing
199      */

200     public void dispose() {
201         IDebugContextService service = getDebugContextService();
202         if (fPart != null) {
203             service.removeDebugContextListener(this, fPart.getSite().getId());
204         } else {
205             service.removeDebugContextListener(this);
206         }
207         fWindow = null;
208         fPart = null;
209     }
210     
211     /**
212      * Returns the context service this action linked to.
213      * @return
214      */

215     protected IDebugContextService getDebugContextService() {
216         return DebugUITools.getDebugContextManager().getContextService(fWindow);
217     }
218
219     /**
220      * @return The help context id for this action
221      */

222     public abstract String JavaDoc getHelpContextId();
223
224     /*
225      * (non-Javadoc)
226      * @see org.eclipse.jface.action.Action#getId()
227      */

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

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

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

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

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

258     public abstract ImageDescriptor getImageDescriptor();
259     
260     /**
261      * Returns the delegate associated with this action or <code>null</code>
262      * if none.
263      *
264      * @return delegate or <code>null</code>
265      */

266     protected DebugCommandActionDelegate getDelegate() {
267         return fDelegate;
268     }
269 }
270
Popular Tags