1 11 12 package org.eclipse.ui.internal.quickaccess; 13 14 import org.eclipse.core.commands.Command; 15 import org.eclipse.core.commands.ParameterizedCommand; 16 import org.eclipse.core.commands.common.NotDefinedException; 17 import org.eclipse.jface.resource.ImageDescriptor; 18 import org.eclipse.ui.IWorkbenchWindow; 19 import org.eclipse.ui.PlatformUI; 20 import org.eclipse.ui.handlers.IHandlerService; 21 import org.eclipse.ui.internal.misc.StatusUtil; 22 import org.eclipse.ui.statushandlers.StatusManager; 23 24 28 public class CommandElement extends QuickAccessElement { 29 30 private static final String separator = " - "; 32 private ParameterizedCommand command; 33 34 private String id; 35 36 CommandElement(ParameterizedCommand command, String id, 37 CommandProvider commandProvider) { 38 super(commandProvider); 39 this.id = id; 40 this.command = command; 41 } 42 43 public void execute() { 44 Object o = getProvider(); 45 if (o instanceof CommandProvider) { 46 CommandProvider provider = (CommandProvider) o; 47 if (provider.getRealHandlerService()!=null) { 48 try { 49 provider.getRealHandlerService().executeCommandInContext( 50 command, null, provider.getContextSnapshot()); 51 } catch (Exception ex) { 52 StatusUtil.handleStatus(ex, StatusManager.SHOW 53 | StatusManager.LOG); 54 } 55 return; 56 } 57 } 58 59 IWorkbenchWindow window = PlatformUI.getWorkbench() 61 .getActiveWorkbenchWindow(); 62 if (window != null) { 63 IHandlerService handlerService = (IHandlerService) window 64 .getWorkbench().getService(IHandlerService.class); 65 try { 66 handlerService.executeCommand(command, null); 67 } catch (Exception ex) { 68 StatusUtil.handleStatus(ex, StatusManager.SHOW 69 | StatusManager.LOG); 70 } 71 } 72 } 73 74 public String getId() { 75 return id; 76 } 77 78 public ImageDescriptor getImageDescriptor() { 79 return null; 80 } 81 82 public String getLabel() { 83 try { 84 Command nestedCommand = command.getCommand(); 85 if (nestedCommand != null && nestedCommand.getDescription() != null 86 && nestedCommand.getDescription().length() != 0) { 87 return command.getName() + separator 88 + nestedCommand.getDescription(); 89 } 90 return command.getName(); 91 } catch (NotDefinedException e) { 92 return command.toString(); 93 } 94 } 95 96 public int hashCode() { 97 final int prime = 31; 98 int result = 1; 99 result = prime * result + ((command == null) ? 0 : command.hashCode()); 100 return result; 101 } 102 103 public boolean equals(Object obj) { 104 if (this == obj) 105 return true; 106 if (obj == null) 107 return false; 108 if (getClass() != obj.getClass()) 109 return false; 110 final CommandElement other = (CommandElement) obj; 111 if (command == null) { 112 if (other.command != null) 113 return false; 114 } else if (!command.equals(other.command)) 115 return false; 116 return true; 117 } 118 } 119 | Popular Tags |