1 11 package org.eclipse.core.commands; 12 13 import java.util.Collections ; 14 import java.util.Map ; 15 16 import org.eclipse.core.commands.common.NotDefinedException; 17 18 33 public final class ExecutionEvent { 34 35 41 private final Object applicationContext; 42 43 46 private final Command command; 47 48 53 private final Map parameters; 54 55 60 private final Object trigger; 61 62 69 public ExecutionEvent() { 70 this(null, Collections.EMPTY_MAP, null, null); 71 } 72 73 89 public ExecutionEvent(final Map parameters, final Object trigger, 90 final Object applicationContext) { 91 this(null, parameters, trigger, applicationContext); 92 } 93 94 111 public ExecutionEvent(final Command command, final Map parameters, 112 final Object trigger, final Object applicationContext) { 113 if (parameters == null) { 114 throw new NullPointerException ( 115 "An execution event must have a non-null map of parameters"); } 117 118 this.command = command; 119 this.parameters = parameters; 120 this.trigger = trigger; 121 this.applicationContext = applicationContext; 122 } 123 124 130 public final Object getApplicationContext() { 131 return applicationContext; 132 } 133 134 140 public final Command getCommand() { 141 return command; 142 } 143 144 161 public final Object getObjectParameterForExecution(final String parameterId) 162 throws ExecutionException { 163 if (command == null) { 164 throw new ExecutionException( 165 "No command is associated with this execution event"); } 167 168 try { 169 final ParameterType parameterType = command 170 .getParameterType(parameterId); 171 if (parameterType == null) { 172 throw new ExecutionException( 173 "Command does not have a parameter type for the given parameter"); } 175 final AbstractParameterValueConverter valueConverter = parameterType 176 .getValueConverter(); 177 if (valueConverter == null) { 178 throw new ExecutionException( 179 "Command does not have a value converter"); } 181 final String stringValue = getParameter(parameterId); 182 final Object objectValue = valueConverter 183 .convertToObject(stringValue); 184 return objectValue; 185 } catch (final NotDefinedException e) { 186 throw new ExecutionException("Command is not defined", e); } catch (final ParameterValueConversionException e) { 188 throw new ExecutionException( 189 "The parameter string could not be converted to an object", e); } 191 } 192 193 201 public final String getParameter(final String parameterId) { 202 return (String ) parameters.get(parameterId); 203 } 204 205 210 public final Map getParameters() { 211 return parameters; 212 } 213 214 219 public final Object getTrigger() { 220 return trigger; 221 } 222 223 229 public final String toString() { 230 final StringBuffer stringBuffer = new StringBuffer (); 231 stringBuffer.append("ExecutionEvent("); stringBuffer.append(command); 233 stringBuffer.append(','); 234 stringBuffer.append(parameters); 235 stringBuffer.append(','); 236 stringBuffer.append(trigger); 237 stringBuffer.append(','); 238 stringBuffer.append(applicationContext); 239 stringBuffer.append(')'); 240 return stringBuffer.toString(); 241 } 242 } 243 | Popular Tags |