1 11 12 package org.eclipse.ui.internal.actions; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 import org.eclipse.core.commands.Command; 19 import org.eclipse.core.commands.CommandEvent; 20 import org.eclipse.core.commands.ICommandListener; 21 import org.eclipse.core.commands.IParameter; 22 import org.eclipse.core.commands.Parameterization; 23 import org.eclipse.core.commands.ParameterizedCommand; 24 import org.eclipse.core.commands.common.NotDefinedException; 25 import org.eclipse.jface.action.Action; 26 import org.eclipse.swt.widgets.Event; 27 import org.eclipse.ui.commands.ICommandService; 28 import org.eclipse.ui.handlers.IHandlerService; 29 import org.eclipse.ui.internal.WorkbenchPlugin; 30 import org.eclipse.ui.services.IServiceLocator; 31 32 45 public class CommandAction extends Action { 46 47 private IHandlerService handlerService = null; 48 49 private ParameterizedCommand parameterizedCommand = null; 50 51 private ICommandListener commandListener; 52 53 protected CommandAction() { 54 55 } 56 57 67 public CommandAction(IServiceLocator serviceLocator, String commandIdIn) { 68 this(serviceLocator, commandIdIn, null); 69 } 70 71 84 public CommandAction(IServiceLocator serviceLocator, String commandIdIn, 85 Map parameterMap) { 86 if (commandIdIn == null) { 87 throw new NullPointerException ("commandIdIn must not be null"); } 89 init(serviceLocator, commandIdIn, parameterMap); 90 } 91 92 protected ICommandListener getCommandListener() { 93 if (commandListener == null) { 94 commandListener = new ICommandListener() { 95 public void commandChanged(CommandEvent commandEvent) { 96 if (commandEvent.isHandledChanged() 97 || commandEvent.isEnabledChanged()) { 98 if (commandEvent.getCommand().isDefined()) { 99 setEnabled(commandEvent.getCommand().isEnabled()); 100 } 101 } 102 } 103 }; 104 } 105 return commandListener; 106 } 107 108 117 private void createCommand(ICommandService commandService, 118 String commandId, Map parameterMap) { 119 try { 120 Command cmd = commandService.getCommand(commandId); 121 if (!cmd.isDefined()) { 122 WorkbenchPlugin.log("Command " + commandId + " is undefined"); return; 124 } 125 126 if (parameterMap == null) { 127 parameterizedCommand = new ParameterizedCommand(cmd, null); 128 return; 129 } 130 131 ArrayList parameters = new ArrayList (); 132 Iterator i = parameterMap.keySet().iterator(); 133 while (i.hasNext()) { 134 String parmName = (String ) i.next(); 135 IParameter parm = cmd.getParameter(parmName); 136 if (parm == null) { 137 WorkbenchPlugin.log("Invalid parameter \'" + parmName + "\' for command " + commandId); return; 140 } 141 parameters.add(new Parameterization(parm, (String ) parameterMap 142 .get(parmName))); 143 } 144 parameterizedCommand = new ParameterizedCommand(cmd, 145 (Parameterization[]) parameters 146 .toArray(new Parameterization[parameters.size()])); 147 } catch (NotDefinedException e) { 148 WorkbenchPlugin.log(e); 149 } 150 } 151 152 public void dispose() { 153 handlerService = null; 155 if (commandListener != null) { 156 parameterizedCommand.getCommand().removeCommandListener( 157 commandListener); 158 commandListener = null; 159 } 160 parameterizedCommand = null; 161 } 162 163 168 public void runWithEvent(Event event) { 169 if (handlerService == null) { 170 String commandId = (parameterizedCommand == null ? "unknownCommand" : parameterizedCommand.getId()); 172 WorkbenchPlugin.log("Cannot run " + commandId + " before command action has been initialized"); return; 175 } 176 try { 177 if (parameterizedCommand != null) { 178 handlerService.executeCommand(parameterizedCommand, event); 179 } 180 } catch (Exception e) { 181 WorkbenchPlugin.log(e); 182 } 183 } 184 185 190 public void run() { 191 runWithEvent(null); 193 } 194 195 protected void init(IServiceLocator serviceLocator, String commandIdIn, 196 Map parameterMap) { 197 if (handlerService != null) { 198 return; 200 } 201 handlerService = (IHandlerService) serviceLocator 202 .getService(IHandlerService.class); 203 ICommandService commandService = (ICommandService) serviceLocator 204 .getService(ICommandService.class); 205 createCommand(commandService, commandIdIn, parameterMap); 206 if (parameterizedCommand != null) { 207 setId(parameterizedCommand.getId()); 208 try { 209 setText(parameterizedCommand.getName()); 210 } catch (NotDefinedException e) { 211 } 213 parameterizedCommand.getCommand().addCommandListener( 214 getCommandListener()); 215 setEnabled(parameterizedCommand.getCommand().isEnabled()); 216 } 217 } 218 219 protected ParameterizedCommand getParameterizedCommand() { 220 return parameterizedCommand; 221 } 222 223 public String getActionDefinitionId() { 224 if (parameterizedCommand != null) { 225 return parameterizedCommand.getId(); 226 } 227 return null; 228 } 229 } 230 | Popular Tags |