KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > launchConfigurations > LaunchShortcutExtension


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.launchConfigurations;
12  
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21
22 import org.eclipse.core.commands.AbstractHandler;
23 import org.eclipse.core.commands.ExecutionEvent;
24 import org.eclipse.core.commands.ExecutionException;
25 import org.eclipse.core.commands.IHandler;
26 import org.eclipse.core.expressions.EvaluationResult;
27 import org.eclipse.core.expressions.Expression;
28 import org.eclipse.core.expressions.ExpressionConverter;
29 import org.eclipse.core.expressions.ExpressionTagNames;
30 import org.eclipse.core.expressions.IEvaluationContext;
31 import org.eclipse.core.runtime.CoreException;
32 import org.eclipse.core.runtime.IConfigurationElement;
33 import org.eclipse.debug.internal.core.IConfigurationElementConstants;
34 import org.eclipse.debug.internal.ui.DebugUIPlugin;
35 import org.eclipse.debug.internal.ui.Pair;
36 import org.eclipse.debug.internal.ui.actions.LaunchShortcutAction;
37 import org.eclipse.debug.ui.ILaunchShortcut;
38 import org.eclipse.jface.resource.ImageDescriptor;
39 import org.eclipse.jface.viewers.ISelection;
40 import org.eclipse.jface.viewers.StructuredSelection;
41 import org.eclipse.ui.IEditorPart;
42 import org.eclipse.ui.IPluginContribution;
43 import org.eclipse.ui.PlatformUI;
44 import org.eclipse.ui.handlers.IHandlerService;
45
46 /**
47  * Proxy to a launch shortcut extension
48  */

49 public class LaunchShortcutExtension implements ILaunchShortcut, IPluginContribution {
50     
51     private ImageDescriptor fImageDescriptor = null;
52     private List JavaDoc fPerspectives = null;
53     private ILaunchShortcut fDelegate = null;
54     private Set JavaDoc fModes = null;
55     private Set JavaDoc fAssociatedTypes = null;
56     private Map JavaDoc fDescriptions = null;
57     private IConfigurationElement fContextualLaunchConfigurationElement = null;
58     private Expression fContextualLaunchExpr = null;
59     private Expression fStandardLaunchExpr = null;
60     
61     /**
62      * Command handler for launch shortcut key binding.
63      */

64     private class LaunchCommandHandler extends AbstractHandler {
65         // the shortcut to invoke
66
private LaunchShortcutExtension fShortcut;
67         private String JavaDoc fMode;
68         
69         /**
70          * Constructs a new command handler for the given shortcut
71          *
72          * @param shortcut
73          */

74         public LaunchCommandHandler(LaunchShortcutExtension shortcut, String JavaDoc mode) {
75             fShortcut = shortcut;
76             fMode = mode;
77         }
78
79         /* (non-Javadoc)
80          * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
81          */

82         public Object JavaDoc execute(ExecutionEvent event) throws ExecutionException {
83            LaunchShortcutAction action = new LaunchShortcutAction(fMode, fShortcut);
84             if (action.isEnabled()) {
85                 action.run();
86             } else {
87                 fShortcut.launch(new StructuredSelection(), fMode);
88             }
89             return null;
90         }
91     }
92     
93     /**
94      * The configuration element defining this tab.
95      */

96     private IConfigurationElement fConfig;
97     private /* <Pair> */ List JavaDoc fContextLabels;
98     
99     /**
100      * Constructs a launch configuration tab extension based
101      * on the given configuration element
102      *
103      * @param element the configuration element defining the
104      * attributes of this launch configuration tab extension
105      * @return a new launch configuration tab extension
106      */

107     public LaunchShortcutExtension(IConfigurationElement element) {
108         setConfigurationElement(element);
109         registerLaunchCommandHandlers();
110     }
111     
112     /**
113      * Registers command handlers for launch shortcut key bindings
114      */

115     private void registerLaunchCommandHandlers() {
116         Iterator JavaDoc modes = getModes().iterator();
117         IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getAdapter(IHandlerService.class);
118         while (modes.hasNext()) {
119             String JavaDoc mode = (String JavaDoc) modes.next();
120             String JavaDoc id = getId() + "." + mode; //$NON-NLS-1$
121
IHandler handler = new LaunchCommandHandler(this, mode);
122             handlerService.activateHandler(id, handler);
123         }
124     }
125     
126     /**
127      * Sets the configuration element that defines the attributes
128      * for this extension.
129      *
130      * @param element configuration element
131      */

132     private void setConfigurationElement(IConfigurationElement element) {
133         fConfig = element;
134     }
135     
136     /**
137      * Returns the configuration element that defines the attributes
138      * for this extension.
139      *
140      * @param configuration element that defines the attributes
141      * for this launch configuration tab extension
142      */

143     public IConfigurationElement getConfigurationElement() {
144         return fConfig;
145     }
146     
147     /**
148      * Returns the label of this shortcut
149      *
150      * @return the label of this shortcut, or <code>null</code> if not
151      * specified
152      */

153     public String JavaDoc getLabel() {
154         return getConfigurationElement().getAttribute(IConfigurationElementConstants.LABEL);
155     }
156     
157     /**
158      * Returns the configuration element for the optional Contextual Launch
159      * element of this Launch Configuration description.
160      * @return contextualLaunch element
161      */

162     public IConfigurationElement getContextualLaunchConfigurationElement() {
163         if (fContextualLaunchConfigurationElement == null) {
164             IConfigurationElement[] elements = getConfigurationElement().getChildren(IConfigurationElementConstants.CONTEXTUAL_LAUNCH);
165             if (elements.length > 0) {
166                 // remember so we don't have to hunt again
167
fContextualLaunchConfigurationElement = elements[0];
168             }
169         }
170         return fContextualLaunchConfigurationElement;
171     }
172     /**
173      * Returns the contextual launch label of this shortcut for the named mode.
174      * <p>
175      * <samp>
176      * <launchShortcut...>
177      * <contextualLaunch>
178      * <contextLabel mode="run" label="Run Java Application"/>
179      * <contextLabel mode="debug" label="Debug Java Application"/>
180      * ...
181      * </contextualLaunch>
182      * </launchShortcut>
183      * </samp>
184      *
185      * @return the contextual label of this shortcut, or <code>null</code> if not
186      * specified
187      */

188     public String JavaDoc getContextLabel(String JavaDoc mode) {
189         // remember the list of context labels for this shortcut
190
if (fContextLabels == null) {
191             IConfigurationElement context = getContextualLaunchConfigurationElement();
192             if (context == null) {
193                 return null;
194             }
195             IConfigurationElement[] labels = context.getChildren(IConfigurationElementConstants.CONTEXT_LABEL);
196             fContextLabels = new ArrayList JavaDoc(labels.length);
197             for (int i = 0; i < labels.length; i++) {
198                 fContextLabels.add(new Pair(labels[i].getAttribute(IConfigurationElementConstants.MODE),
199                         labels[i].getAttribute(IConfigurationElementConstants.LABEL)));
200             }
201         }
202         // pick out the first occurance of the "name" bound to "mode"
203
Iterator JavaDoc iter = fContextLabels.iterator();
204         while (iter.hasNext()) {
205             Pair p = (Pair) iter.next();
206             if (p.firstAsString().equals(mode)) {
207                 return p.secondAsString();
208             }
209         }
210         return getLabel();
211     }
212     
213     /**
214      * Returns the set of associated launch configuration type ids.
215      *
216      * @return the set of associated launch configuration type ids
217      * @since 3.3
218      */

219     public Set JavaDoc getAssociatedConfigurationTypes() {
220         if(fAssociatedTypes == null) {
221             fAssociatedTypes = new HashSet JavaDoc();
222             IConfigurationElement[] children = fConfig.getChildren(IConfigurationElementConstants.CONFIGURATION_TYPES);
223             String JavaDoc id = null;
224             for (int i = 0; i < children.length; i++) {
225                 id = children[i].getAttribute(IConfigurationElementConstants.ID);
226                 if(id != null) {
227                     fAssociatedTypes.add(id);
228                 }
229             }
230         }
231         return fAssociatedTypes;
232     }
233     
234     /**
235      * Returns the contributed description of the launch delegate or <code>null</code>
236      * if one has not been provided
237      * @param mode the mode to get the description for
238      * @return the description of the shortcut for that specific mode or <code>null</code> if one was not provided
239      *
240      * @since 3.3
241      */

242     public String JavaDoc getShortcutDescription(String JavaDoc mode) {
243         if(mode == null) {
244             return null;
245         }
246         if(fDescriptions == null) {
247             fDescriptions = new HashMap JavaDoc();
248             //get the description for the main element first
249
String JavaDoc descr = fConfig.getAttribute(IConfigurationElementConstants.DESCRIPTION);
250             String JavaDoc lmode = null;
251             Set JavaDoc modes = getModes();
252             if(descr != null) {
253                 for(Iterator JavaDoc iter = modes.iterator(); iter.hasNext();) {
254                     lmode = (String JavaDoc) iter.next();
255                     fDescriptions.put(lmode, descr);
256                 }
257             }
258             //load descriptions for child description elements
259
IConfigurationElement[] children = fConfig.getChildren(IConfigurationElementConstants.DESCRIPTION);
260             for(int i = 0; i < children.length; i++) {
261                 lmode = children[i].getAttribute(IConfigurationElementConstants.MODE);
262                 descr = children[i].getAttribute(IConfigurationElementConstants.DESCRIPTION);
263                 fDescriptions.put(lmode, descr);
264             }
265         }
266         return (String JavaDoc) fDescriptions.get(mode);
267     }
268     
269     /**
270      * Evaluate the given expression within the given context and return
271      * the result. Returns <code>true</code> iff result is either TRUE or NOT_LOADED.
272      * This allows optimistic inclusion of shortcuts before plugins are loaded.
273      * Returns <code>false</code> if exp is <code>null</code>.
274      *
275      * @param exp the enablement expression to evaluate or <code>null</code>
276      * @param context the context of the evaluation. Usually, the
277      * user's selection.
278      * @return the result of evaluating the expression
279      * @throws CoreException
280      */

281     public boolean evalEnablementExpression(IEvaluationContext context, Expression exp) throws CoreException {
282         return (exp != null) ? ((exp.evaluate(context)) != EvaluationResult.FALSE) : false;
283     }
284     
285     /**
286      * Returns an expression that represents the enablement logic for the
287      * contextual launch element of this launch shortcut description or
288      * <code>null</code> if none.
289      * @return an evaluatable expression or <code>null</code>
290      * @throws CoreException if the configuration element can't be
291      * converted. Reasons include: (a) no handler is available to
292      * cope with a certain configuration element or (b) the XML
293      * expression tree is malformed.
294      */

295     public Expression getContextualLaunchEnablementExpression() throws CoreException {
296         // all of this stuff is optional, so...tedious testing is required
297
if (fContextualLaunchExpr == null) {
298             IConfigurationElement contextualLaunchElement = getContextualLaunchConfigurationElement();
299             if (contextualLaunchElement == null) {
300                 // not available
301
return null;
302             }
303             IConfigurationElement[] elements = contextualLaunchElement.getChildren(ExpressionTagNames.ENABLEMENT);
304             IConfigurationElement enablement = elements.length > 0 ? elements[0] : null;
305
306             if (enablement != null) {
307                 fContextualLaunchExpr= ExpressionConverter.getDefault().perform(enablement);
308             }
309         }
310         return fContextualLaunchExpr;
311     }
312     
313     /**
314      * Returns an expression that represents the enablement logic for the
315      * launch shortcut description or <code>null</code> if none.
316      * @return an evaluatable expression or <code>null</code>
317      * @throws CoreException if the configuration element can't be
318      * converted. Reasons include: (a) no handler is available to
319      * cope with a certain configuration element or (b) the XML
320      * expression tree is malformed.
321      */

322     public Expression getShortcutEnablementExpression() throws CoreException {
323         // all of this stuff is optional, so...tedious testing is required
324
if (fStandardLaunchExpr == null) {
325             IConfigurationElement[] elements = getConfigurationElement().getChildren(ExpressionTagNames.ENABLEMENT);
326             IConfigurationElement enablement = elements.length > 0 ? elements[0] : null;
327             if (enablement != null) {
328                 fStandardLaunchExpr= ExpressionConverter.getDefault().perform(enablement);
329             }
330         }
331         return fStandardLaunchExpr;
332     }
333     
334     /**
335      * Returns the id of this shortcut
336      *
337      * @return the id of this shortcut, or <code>null</code> if not specified
338      */

339     public String JavaDoc getId() {
340         return getConfigurationElement().getAttribute(IConfigurationElementConstants.ID);
341     }
342     
343     /**
344      * Returns the identifier of the help context associated with this launch
345      * shortcut, or <code>null</code> if one was not specified.
346      *
347      * @return the identifier of this launch shortcut's help context or
348      * <code>null</code>
349      * @since 2.1
350      */

351     public String JavaDoc getHelpContextId() {
352         return getConfigurationElement().getAttribute(IConfigurationElementConstants.HELP_CONTEXT_ID);
353     }
354     
355     /**
356      * Returns the category of this shortcut
357      *
358      * @return the category of this shortcut, or <code>null</code> if not
359      * specified
360      */

361     public String JavaDoc getCategory() {
362         return getConfigurationElement().getAttribute(IConfigurationElementConstants.CATEGORY);
363     }
364     
365     /**
366      * Returns the image for this shortcut, or <code>null</code> if none
367      *
368      * @return the image for this shortcut, or <code>null</code> if none
369      */

370     public ImageDescriptor getImageDescriptor() {
371         if (fImageDescriptor == null) {
372             fImageDescriptor = DebugUIPlugin.getImageDescriptor(getConfigurationElement(), "icon"); //$NON-NLS-1$
373
if (fImageDescriptor == null) {
374                 fImageDescriptor = ImageDescriptor.getMissingImageDescriptor();
375             }
376         }
377         return fImageDescriptor;
378     }
379     
380     /**
381      * Returns the perspectives this shortcut is registered for.
382      *
383      * @return list of Strings representing perspective identifiers
384      * @deprecated The use of the perspectives element has been deprecated since 3.1.
385      */

386     public List JavaDoc getPerspectives() {
387         if (fPerspectives == null) {
388             IConfigurationElement[] perspectives = getConfigurationElement().getChildren(IConfigurationElementConstants.PERSPECTIVE);
389             fPerspectives = new ArrayList JavaDoc(perspectives.length);
390             for (int i = 0; i < perspectives.length; i++) {
391                 fPerspectives.add(perspectives[i].getAttribute(IConfigurationElementConstants.ID));
392             }
393         }
394         return fPerspectives;
395     }
396     
397     /**
398      * Returns this shortcut's delegate, or <code>null</code> if none
399      *
400      * @return this shortcut's delegate, or <code>null</code> if none
401      */

402     protected ILaunchShortcut getDelegate() {
403         if (fDelegate == null) {
404             try {
405                 fDelegate = (ILaunchShortcut)fConfig.createExecutableExtension(IConfigurationElementConstants.CLASS);
406             } catch (CoreException e) {
407                 DebugUIPlugin.errorDialog(DebugUIPlugin.getShell(), LaunchConfigurationsMessages.LaunchShortcutExtension_Error_4, LaunchConfigurationsMessages.LaunchShortcutExtension_Unable_to_use_launch_shortcut_5, e.getStatus()); //
408
}
409         }
410         return fDelegate;
411     }
412     
413     /**
414      * @see ILaunchShortcut#launch(IEditorPart, String)
415      */

416     public void launch(IEditorPart editor, String JavaDoc mode) {
417         ILaunchShortcut shortcut = getDelegate();
418         if (shortcut != null) {
419             shortcut.launch(editor, mode);
420         }
421     }
422
423     /**
424      * @see ILaunchShortcut#launch(ISelection, String)
425      */

426     public void launch(ISelection selection, String JavaDoc mode) {
427         ILaunchShortcut shortcut = getDelegate();
428         if (shortcut != null) {
429             shortcut.launch(selection, mode);
430         }
431     }
432     
433     /**
434      * Returns the set of modes this shortcut supports.
435      *
436      * @return the set of modes this shortcut supports
437      */

438     public Set JavaDoc getModes() {
439         if (fModes == null) {
440             String JavaDoc modes= getConfigurationElement().getAttribute(IConfigurationElementConstants.MODES);
441             if (modes == null) {
442                 return new HashSet JavaDoc(0);
443             }
444             StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(modes, ","); //$NON-NLS-1$
445
fModes = new HashSet JavaDoc(tokenizer.countTokens());
446             while (tokenizer.hasMoreTokens()) {
447                 fModes.add(tokenizer.nextToken().trim());
448             }
449         }
450         return fModes;
451     }
452     
453     /**
454      * Returns the menu path attribute this shortcut, or <code>null</code> if none
455      *
456      * @return the menu path attribute this shortcut, or <code>null</code> if none
457      * @since 3.0.1
458      */

459     public String JavaDoc getMenuPath() {
460         return getConfigurationElement().getAttribute(IConfigurationElementConstants.PATH);
461     }
462     
463     /*
464      * Only for debugging
465      * @see java.lang.Object#toString()
466      */

467     public String JavaDoc toString() {
468         return getId();
469     }
470
471     /* (non-Javadoc)
472      * @see org.eclipse.ui.IPluginContribution#getLocalId()
473      */

474     public String JavaDoc getLocalId() {
475         return getId();
476     }
477
478     /* (non-Javadoc)
479      * @see org.eclipse.ui.IPluginContribution#getPluginId()
480      */

481     public String JavaDoc getPluginId() {
482         return fConfig.getContributor().getName();
483     }
484 }
485
486
Popular Tags