1 11 12 package org.eclipse.ui.internal.handlers; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 import org.eclipse.core.commands.Command; 22 import org.eclipse.core.commands.ExecutionEvent; 23 import org.eclipse.core.commands.ExecutionException; 24 import org.eclipse.core.commands.IHandler; 25 import org.eclipse.core.commands.NotEnabledException; 26 import org.eclipse.core.commands.NotHandledException; 27 import org.eclipse.core.commands.ParameterizedCommand; 28 import org.eclipse.core.commands.common.NotDefinedException; 29 import org.eclipse.core.expressions.Expression; 30 import org.eclipse.core.expressions.IEvaluationContext; 31 import org.eclipse.swt.widgets.Event; 32 import org.eclipse.ui.ISourceProvider; 33 import org.eclipse.ui.handlers.IHandlerActivation; 34 import org.eclipse.ui.handlers.IHandlerService; 35 import org.eclipse.ui.internal.expressions.AndExpression; 36 37 48 public class SlaveHandlerService implements IHandlerService { 49 50 56 protected final Expression defaultExpression; 57 58 63 private Collection fSourceProviders = new ArrayList (); 64 65 71 protected final Map localActivationsToParentActivations = new HashMap (); 72 73 77 protected final IHandlerService parent; 78 79 83 protected final Set parentActivations = new HashSet (); 84 85 96 public SlaveHandlerService(final IHandlerService parentHandlerService, 97 final Expression defaultExpression) { 98 if (parentHandlerService == null) { 99 throw new NullPointerException ( 100 "The parent handler service cannot be null"); } 102 103 this.defaultExpression = defaultExpression; 104 this.parent = parentHandlerService; 105 } 106 107 public final IHandlerActivation activateHandler( 108 final IHandlerActivation childActivation) { 109 final String commandId = childActivation.getCommandId(); 110 final IHandler handler = childActivation.getHandler(); 111 final Expression childExpression = childActivation.getExpression(); 112 final AndExpression expression; 113 if (childExpression instanceof AndExpression) { 114 expression = (AndExpression) childExpression; 115 } else { 116 expression = new AndExpression(); 117 if (childExpression != null) { 118 expression.add(childExpression); 119 } 120 } 121 if (defaultExpression != null) { 122 expression.add(defaultExpression); 123 } 124 final int depth = childActivation.getDepth() + 1; 125 final IHandlerActivation localActivation = new HandlerActivation( 126 commandId, handler, expression, depth, this); 127 128 return doActivation(localActivation); 129 } 130 131 public final IHandlerActivation activateHandler(final String commandId, 132 final IHandler handler) { 133 final IHandlerActivation localActivation = new HandlerActivation( 134 commandId, handler, defaultExpression, 135 IHandlerActivation.ROOT_DEPTH, this); 136 return doActivation(localActivation); 137 } 138 139 public final IHandlerActivation activateHandler(final String commandId, 140 final IHandler handler, final Expression expression) { 141 return activateHandler(commandId, handler, expression, false); 142 } 143 144 public final IHandlerActivation activateHandler(final String commandId, 145 final IHandler handler, final Expression expression, 146 final boolean global) { 147 if (global) { 148 final IHandlerActivation activation = parent.activateHandler( 149 commandId, handler, expression, global); 150 parentActivations.add(activation); 151 return activation; 152 } 153 154 final AndExpression andExpression; 155 if (expression instanceof AndExpression) { 156 andExpression = (AndExpression) expression; 157 } else { 158 andExpression = new AndExpression(); 159 if (expression != null) { 160 andExpression.add(expression); 161 } 162 } 163 if (defaultExpression != null) { 164 andExpression.add(defaultExpression); 165 } 166 final IHandlerActivation localActivation = new HandlerActivation( 167 commandId, handler, andExpression, 168 IHandlerActivation.ROOT_DEPTH, this); 169 return doActivation(localActivation); 170 } 171 172 public final IHandlerActivation activateHandler(final String commandId, 173 final IHandler handler, final Expression expression, 174 final int sourcePriorities) { 175 return activateHandler(commandId, handler, expression); 176 } 177 178 public final void addSourceProvider(final ISourceProvider provider) { 179 if (!fSourceProviders.contains(provider)) { 180 fSourceProviders.add(provider); 181 } 182 parent.addSourceProvider(provider); 183 } 184 185 public final ExecutionEvent createExecutionEvent(final Command command, 186 final Event event) { 187 return parent.createExecutionEvent(command, event); 188 } 189 190 public final ExecutionEvent createExecutionEvent( 191 final ParameterizedCommand command, final Event event) { 192 return parent.createExecutionEvent(command, event); 193 } 194 195 public final void deactivateHandler(final IHandlerActivation activation) { 196 final IHandlerActivation parentActivation; 197 if (localActivationsToParentActivations.containsKey(activation)) { 198 parentActivation = (IHandlerActivation) localActivationsToParentActivations 199 .remove(activation); 200 } else { 201 parentActivation = activation; 202 } 203 204 if (parentActivation != null) { 205 parent.deactivateHandler(parentActivation); 206 parentActivations.remove(parentActivation); 207 } 208 } 209 210 public final void deactivateHandlers(final Collection activations) { 211 Object [] array = activations.toArray(); 212 for (int i = 0; i < array.length; i++) { 213 deactivateHandler((IHandlerActivation) array[i]); 214 array[i] = null; 215 } 216 } 217 218 public final void dispose() { 219 parent.deactivateHandlers(parentActivations); 220 parentActivations.clear(); 221 localActivationsToParentActivations.clear(); 222 223 if (!fSourceProviders.isEmpty()) { 226 Object [] array = fSourceProviders.toArray(); 227 for (int i = 0; i < array.length; i++) { 228 removeSourceProvider((ISourceProvider) array[i]); 229 } 230 fSourceProviders.clear(); 231 } 232 } 233 234 protected IHandlerActivation doActivation( 235 final IHandlerActivation localActivation) { 236 final IHandlerActivation parentActivation; 237 parentActivation = parent.activateHandler(localActivation); 238 parentActivations.add(parentActivation); 239 localActivationsToParentActivations.put(localActivation, 240 parentActivation); 241 return localActivation; 242 } 243 244 public final Object executeCommand(final ParameterizedCommand command, 245 final Event event) throws ExecutionException, NotDefinedException, 246 NotEnabledException, NotHandledException { 247 return parent.executeCommand(command, event); 248 } 249 250 public final Object executeCommand(final String commandId, final Event event) 251 throws ExecutionException, NotDefinedException, 252 NotEnabledException, NotHandledException { 253 return parent.executeCommand(commandId, event); 254 } 255 256 public final IEvaluationContext getCurrentState() { 257 return parent.getCurrentState(); 258 } 259 260 public final void readRegistry() { 261 parent.readRegistry(); 262 } 263 264 public final void removeSourceProvider(final ISourceProvider provider) { 265 fSourceProviders.remove(provider); 266 parent.removeSourceProvider(provider); 267 } 268 269 public final void setHelpContextId(final IHandler handler, 270 final String helpContextId) { 271 parent.setHelpContextId(handler, helpContextId); 272 } 273 274 } 275 276 | Popular Tags |