1 11 12 package org.eclipse.ui.internal.handlers; 13 14 import java.util.Collection ; 15 import java.util.Collections ; 16 import java.util.Iterator ; 17 18 import org.eclipse.core.commands.Command; 19 import org.eclipse.core.commands.ExecutionEvent; 20 import org.eclipse.core.commands.ExecutionException; 21 import org.eclipse.core.commands.IHandler; 22 import org.eclipse.core.commands.NotEnabledException; 23 import org.eclipse.core.commands.NotHandledException; 24 import org.eclipse.core.commands.ParameterizedCommand; 25 import org.eclipse.core.commands.common.NotDefinedException; 26 import org.eclipse.core.expressions.Expression; 27 import org.eclipse.core.expressions.IEvaluationContext; 28 import org.eclipse.swt.widgets.Event; 29 import org.eclipse.swt.widgets.Shell; 30 import org.eclipse.ui.ISourceProvider; 31 import org.eclipse.ui.ISources; 32 import org.eclipse.ui.commands.ICommandService; 33 import org.eclipse.ui.handlers.IHandlerActivation; 34 import org.eclipse.ui.handlers.IHandlerService; 35 import org.eclipse.ui.internal.misc.Policy; 36 import org.eclipse.ui.internal.services.IEvaluationService; 37 38 46 public final class HandlerService implements IHandlerService { 47 48 static { 49 Command.DEBUG_HANDLERS = Policy.DEBUG_HANDLERS_VERBOSE; 50 Command.DEBUG_HANDLERS_COMMAND_ID = Policy.DEBUG_HANDLERS_VERBOSE_COMMAND_ID; 51 } 52 53 57 private final ICommandService commandService; 58 59 62 private final HandlerAuthority handlerAuthority; 63 64 67 private final HandlerPersistence handlerPersistence; 68 69 78 public HandlerService(final ICommandService commandService, 79 final IEvaluationService evaluationService) { 80 if (commandService == null) { 81 throw new NullPointerException ( 82 "A handler service requires a command service"); } 84 this.commandService = commandService; 85 this.handlerAuthority = new HandlerAuthority(commandService); 86 this.handlerPersistence = new HandlerPersistence(this, 87 evaluationService); 88 } 89 90 public final IHandlerActivation activateHandler( 91 final IHandlerActivation childActivation) { 92 final String commandId = childActivation.getCommandId(); 93 final IHandler handler = childActivation.getHandler(); 94 final Expression expression = childActivation.getExpression(); 95 final int depth = childActivation.getDepth() + 1; 96 final IHandlerActivation localActivation = new HandlerActivation( 97 commandId, handler, expression, depth, this); 98 handlerAuthority.activateHandler(localActivation); 99 return localActivation; 100 } 101 102 public final IHandlerActivation activateHandler(final String commandId, 103 final IHandler handler) { 104 return activateHandler(commandId, handler, null); 105 } 106 107 public final IHandlerActivation activateHandler(final String commandId, 108 final IHandler handler, final Expression expression) { 109 return activateHandler(commandId, handler, expression, false); 110 } 111 112 public final IHandlerActivation activateHandler(final String commandId, 113 final IHandler handler, final Expression expression, 114 final boolean global) { 115 final IHandlerActivation activation = new HandlerActivation(commandId, 116 handler, expression, IHandlerActivation.ROOT_DEPTH, this); 117 handlerAuthority.activateHandler(activation); 118 return activation; 119 } 120 121 public final IHandlerActivation activateHandler(final String commandId, 122 final IHandler handler, final Expression expression, 123 final int sourcePriority) { 124 return activateHandler(commandId, handler, expression); 125 } 126 127 public final void addSourceProvider(final ISourceProvider provider) { 128 handlerAuthority.addSourceProvider(provider); 129 } 130 131 public final ExecutionEvent createExecutionEvent(final Command command, 132 final Event event) { 133 return new ExecutionEvent(command, null, event, getCurrentState()); 134 } 135 136 public ExecutionEvent createExecutionEvent( 137 final ParameterizedCommand command, final Event event) { 138 return new ExecutionEvent(command.getCommand(), command 139 .getParameterMap(), event, getCurrentState()); 140 } 141 142 public final void deactivateHandler(final IHandlerActivation activation) { 143 if (activation.getHandlerService() == this) { 144 handlerAuthority.deactivateHandler(activation); 145 } 146 } 147 148 public final void deactivateHandlers(final Collection activations) { 149 final Iterator activationItr = activations.iterator(); 150 while (activationItr.hasNext()) { 151 final IHandlerActivation activation = (IHandlerActivation) activationItr 152 .next(); 153 deactivateHandler(activation); 154 } 155 } 156 157 public final void dispose() { 158 handlerAuthority.dispose(); 159 handlerPersistence.dispose(); 160 } 161 162 public final Object executeCommand(final ParameterizedCommand command, 163 final Event trigger) throws ExecutionException, 164 NotDefinedException, NotEnabledException, NotHandledException { 165 return command.executeWithChecks(trigger, getCurrentState()); 166 } 167 168 public final Object executeCommand(final String commandId, 169 final Event trigger) throws ExecutionException, 170 NotDefinedException, NotEnabledException, NotHandledException { 171 final Command command = commandService.getCommand(commandId); 172 final ExecutionEvent event = new ExecutionEvent(command, 173 Collections.EMPTY_MAP, trigger, getCurrentState()); 174 return command.executeWithChecks(event); 175 } 176 177 public final IEvaluationContext getCurrentState() { 178 return handlerAuthority.getCurrentState(); 179 } 180 181 public final void readRegistry() { 182 handlerPersistence.read(); 183 } 184 185 public final void removeSourceProvider(final ISourceProvider provider) { 186 handlerAuthority.removeSourceProvider(provider); 187 } 188 189 public final void setHelpContextId(final IHandler handler, 190 final String helpContextId) { 191 commandService.setHelpContextId(handler, helpContextId); 192 } 193 194 204 public final void updateShellKludge() { 205 handlerAuthority.updateShellKludge(); 206 } 207 208 222 public final void updateShellKludge(final Shell shell) { 223 final Shell currentActiveShell = handlerAuthority.getActiveShell(); 224 if (currentActiveShell != shell) { 225 handlerAuthority.sourceChanged(ISources.ACTIVE_SHELL, 226 ISources.ACTIVE_SHELL_NAME, shell); 227 } 228 } 229 230 242 public final IHandler findHandler(String commandId, 243 IEvaluationContext context) { 244 return handlerAuthority.findHandler(commandId, context); 245 } 246 247 259 public final IEvaluationContext getContextSnapshot() { 260 return handlerAuthority.getContextSnapshot(); 261 } 262 263 274 public final IEvaluationContext getFullContextSnapshot() { 275 return handlerAuthority.getFullContextSnapshot(); 276 } 277 278 303 public final Object executeCommandInContext( 304 final ParameterizedCommand command, final Event trigger, 305 IEvaluationContext context) throws ExecutionException, 306 NotDefinedException, NotEnabledException, NotHandledException { 307 IHandler oldHandler = command.getCommand().getHandler(); 308 309 IHandler handler = findHandler(command.getId(), context); 310 boolean enabled = true; 311 if (handler instanceof HandlerProxy) { 312 enabled = ((HandlerProxy) handler).getProxyEnabled(); 313 } 314 315 try { 316 command.getCommand().setHandler(handler); 317 if (handler instanceof HandlerProxy) { 318 ((HandlerProxy) handler).setEnabledFor(context); 319 } 320 321 return command.executeWithChecks(trigger, context); 322 } finally { 323 if (handler instanceof HandlerProxy) { 324 ((HandlerProxy) handler).setProxyEnabled(enabled); 325 } 326 command.getCommand().setHandler(oldHandler); 327 } 328 } 329 } 330 | Popular Tags |