1 11 package org.eclipse.debug.internal.ui.launchConfigurations; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.Set ; 20 import java.util.StringTokenizer ; 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 49 public class LaunchShortcutExtension implements ILaunchShortcut, IPluginContribution { 50 51 private ImageDescriptor fImageDescriptor = null; 52 private List fPerspectives = null; 53 private ILaunchShortcut fDelegate = null; 54 private Set fModes = null; 55 private Set fAssociatedTypes = null; 56 private Map fDescriptions = null; 57 private IConfigurationElement fContextualLaunchConfigurationElement = null; 58 private Expression fContextualLaunchExpr = null; 59 private Expression fStandardLaunchExpr = null; 60 61 64 private class LaunchCommandHandler extends AbstractHandler { 65 private LaunchShortcutExtension fShortcut; 67 private String fMode; 68 69 74 public LaunchCommandHandler(LaunchShortcutExtension shortcut, String mode) { 75 fShortcut = shortcut; 76 fMode = mode; 77 } 78 79 82 public Object 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 96 private IConfigurationElement fConfig; 97 private List fContextLabels; 98 99 107 public LaunchShortcutExtension(IConfigurationElement element) { 108 setConfigurationElement(element); 109 registerLaunchCommandHandlers(); 110 } 111 112 115 private void registerLaunchCommandHandlers() { 116 Iterator modes = getModes().iterator(); 117 IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getAdapter(IHandlerService.class); 118 while (modes.hasNext()) { 119 String mode = (String ) modes.next(); 120 String id = getId() + "." + mode; IHandler handler = new LaunchCommandHandler(this, mode); 122 handlerService.activateHandler(id, handler); 123 } 124 } 125 126 132 private void setConfigurationElement(IConfigurationElement element) { 133 fConfig = element; 134 } 135 136 143 public IConfigurationElement getConfigurationElement() { 144 return fConfig; 145 } 146 147 153 public String getLabel() { 154 return getConfigurationElement().getAttribute(IConfigurationElementConstants.LABEL); 155 } 156 157 162 public IConfigurationElement getContextualLaunchConfigurationElement() { 163 if (fContextualLaunchConfigurationElement == null) { 164 IConfigurationElement[] elements = getConfigurationElement().getChildren(IConfigurationElementConstants.CONTEXTUAL_LAUNCH); 165 if (elements.length > 0) { 166 fContextualLaunchConfigurationElement = elements[0]; 168 } 169 } 170 return fContextualLaunchConfigurationElement; 171 } 172 188 public String getContextLabel(String mode) { 189 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 (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 Iterator 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 219 public Set getAssociatedConfigurationTypes() { 220 if(fAssociatedTypes == null) { 221 fAssociatedTypes = new HashSet (); 222 IConfigurationElement[] children = fConfig.getChildren(IConfigurationElementConstants.CONFIGURATION_TYPES); 223 String 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 242 public String getShortcutDescription(String mode) { 243 if(mode == null) { 244 return null; 245 } 246 if(fDescriptions == null) { 247 fDescriptions = new HashMap (); 248 String descr = fConfig.getAttribute(IConfigurationElementConstants.DESCRIPTION); 250 String lmode = null; 251 Set modes = getModes(); 252 if(descr != null) { 253 for(Iterator iter = modes.iterator(); iter.hasNext();) { 254 lmode = (String ) iter.next(); 255 fDescriptions.put(lmode, descr); 256 } 257 } 258 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 ) fDescriptions.get(mode); 267 } 268 269 281 public boolean evalEnablementExpression(IEvaluationContext context, Expression exp) throws CoreException { 282 return (exp != null) ? ((exp.evaluate(context)) != EvaluationResult.FALSE) : false; 283 } 284 285 295 public Expression getContextualLaunchEnablementExpression() throws CoreException { 296 if (fContextualLaunchExpr == null) { 298 IConfigurationElement contextualLaunchElement = getContextualLaunchConfigurationElement(); 299 if (contextualLaunchElement == null) { 300 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 322 public Expression getShortcutEnablementExpression() throws CoreException { 323 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 339 public String getId() { 340 return getConfigurationElement().getAttribute(IConfigurationElementConstants.ID); 341 } 342 343 351 public String getHelpContextId() { 352 return getConfigurationElement().getAttribute(IConfigurationElementConstants.HELP_CONTEXT_ID); 353 } 354 355 361 public String getCategory() { 362 return getConfigurationElement().getAttribute(IConfigurationElementConstants.CATEGORY); 363 } 364 365 370 public ImageDescriptor getImageDescriptor() { 371 if (fImageDescriptor == null) { 372 fImageDescriptor = DebugUIPlugin.getImageDescriptor(getConfigurationElement(), "icon"); if (fImageDescriptor == null) { 374 fImageDescriptor = ImageDescriptor.getMissingImageDescriptor(); 375 } 376 } 377 return fImageDescriptor; 378 } 379 380 386 public List getPerspectives() { 387 if (fPerspectives == null) { 388 IConfigurationElement[] perspectives = getConfigurationElement().getChildren(IConfigurationElementConstants.PERSPECTIVE); 389 fPerspectives = new ArrayList (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 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()); } 409 } 410 return fDelegate; 411 } 412 413 416 public void launch(IEditorPart editor, String mode) { 417 ILaunchShortcut shortcut = getDelegate(); 418 if (shortcut != null) { 419 shortcut.launch(editor, mode); 420 } 421 } 422 423 426 public void launch(ISelection selection, String mode) { 427 ILaunchShortcut shortcut = getDelegate(); 428 if (shortcut != null) { 429 shortcut.launch(selection, mode); 430 } 431 } 432 433 438 public Set getModes() { 439 if (fModes == null) { 440 String modes= getConfigurationElement().getAttribute(IConfigurationElementConstants.MODES); 441 if (modes == null) { 442 return new HashSet (0); 443 } 444 StringTokenizer tokenizer= new StringTokenizer (modes, ","); fModes = new HashSet (tokenizer.countTokens()); 446 while (tokenizer.hasMoreTokens()) { 447 fModes.add(tokenizer.nextToken().trim()); 448 } 449 } 450 return fModes; 451 } 452 453 459 public String getMenuPath() { 460 return getConfigurationElement().getAttribute(IConfigurationElementConstants.PATH); 461 } 462 463 467 public String toString() { 468 return getId(); 469 } 470 471 474 public String getLocalId() { 475 return getId(); 476 } 477 478 481 public String getPluginId() { 482 return fConfig.getContributor().getName(); 483 } 484 } 485 486 | Popular Tags |