1 11 12 package org.eclipse.ui.internal.commands; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.HashSet ; 17 import java.util.Map ; 18 import java.util.Set ; 19 20 import org.eclipse.core.commands.Category; 21 import org.eclipse.core.commands.Command; 22 import org.eclipse.core.commands.IExecutionListener; 23 import org.eclipse.core.commands.IHandler; 24 import org.eclipse.core.commands.ParameterType; 25 import org.eclipse.core.commands.ParameterizedCommand; 26 import org.eclipse.core.commands.SerializationException; 27 import org.eclipse.core.commands.common.NotDefinedException; 28 import org.eclipse.ui.commands.IElementReference; 29 import org.eclipse.ui.commands.ICommandService; 30 import org.eclipse.ui.menus.UIElement; 31 32 42 public class SlaveCommandService implements ICommandService { 43 44 private Collection fExecutionListeners = new ArrayList (); 45 46 51 private Set fCallbackCache = new HashSet (); 52 53 private ICommandService fParentService; 54 55 61 private String fScopingName; 62 63 69 private Object fScopingValue; 70 71 77 public SlaveCommandService(ICommandService parent, String scopeName, 78 Object scopeValue) { 79 if (parent == null) { 80 throw new NullPointerException ( 81 "The parent command service must not be null"); } 83 fParentService = parent; 84 fScopingName = scopeName; 85 fScopingValue = scopeValue; 86 } 87 88 93 public void addExecutionListener(IExecutionListener listener) { 94 if (!fExecutionListeners.contains(listener)) { 95 fExecutionListeners.add(listener); 96 } 97 fParentService.addExecutionListener(listener); 98 } 99 100 106 public void defineUncategorizedCategory(String name, String description) { 107 fParentService.defineUncategorizedCategory(name, description); 108 } 109 110 115 public ParameterizedCommand deserialize( 116 String serializedParameterizedCommand) throws NotDefinedException, 117 SerializationException { 118 return fParentService.deserialize(serializedParameterizedCommand); 119 } 120 121 126 public void dispose() { 127 if (!fExecutionListeners.isEmpty()) { 128 Object [] array = fExecutionListeners.toArray(); 129 for (int i = 0; i < array.length; i++) { 130 removeExecutionListener((IExecutionListener) array[i]); 131 } 132 fExecutionListeners.clear(); 133 } 134 if (!fCallbackCache.isEmpty()) { 135 Object [] array = fCallbackCache.toArray(); 136 for (int i = 0; i < array.length; i++) { 137 unregisterElement((IElementReference) array[i]); 138 } 139 } 140 } 141 142 147 public Category getCategory(String categoryId) { 148 return fParentService.getCategory(categoryId); 149 } 150 151 156 public Command getCommand(String commandId) { 157 return fParentService.getCommand(commandId); 158 } 159 160 165 public Category[] getDefinedCategories() { 166 return fParentService.getDefinedCategories(); 167 } 168 169 174 public Collection getDefinedCategoryIds() { 175 return fParentService.getDefinedCategoryIds(); 176 } 177 178 183 public Collection getDefinedCommandIds() { 184 return fParentService.getDefinedCommandIds(); 185 } 186 187 192 public Command[] getDefinedCommands() { 193 return fParentService.getDefinedCommands(); 194 } 195 196 201 public Collection getDefinedParameterTypeIds() { 202 return fParentService.getDefinedParameterTypeIds(); 203 } 204 205 210 public ParameterType[] getDefinedParameterTypes() { 211 return fParentService.getDefinedParameterTypes(); 212 } 213 214 public final String getHelpContextId(final Command command) 215 throws NotDefinedException { 216 return fParentService.getHelpContextId(command); 217 } 218 219 public final String getHelpContextId(final String commandId) 220 throws NotDefinedException { 221 return fParentService.getHelpContextId(commandId); 222 } 223 224 229 public ParameterType getParameterType(String parameterTypeId) { 230 return fParentService.getParameterType(parameterTypeId); 231 } 232 233 238 public void readRegistry() { 239 fParentService.readRegistry(); 240 } 241 242 247 public void removeExecutionListener(IExecutionListener listener) { 248 fExecutionListeners.remove(listener); 249 fParentService.removeExecutionListener(listener); 250 } 251 252 public final void setHelpContextId(final IHandler handler, 253 final String helpContextId) { 254 fParentService.setHelpContextId(handler, helpContextId); 255 } 256 257 263 public void refreshElements(String commandId, Map filter) { 264 fParentService.refreshElements(commandId, filter); 265 } 266 267 273 public IElementReference registerElementForCommand( 274 ParameterizedCommand command, UIElement element) 275 throws NotDefinedException { 276 if (!command.getCommand().isDefined()) { 277 throw new NotDefinedException( 278 "Cannot define a callback for undefined command " + command.getCommand().getId()); 280 } 281 if (element == null) { 282 throw new NotDefinedException("No callback defined for command " + command.getCommand().getId()); 284 } 285 286 ElementReference ref = new ElementReference(command.getId(), element, 287 command.getParameterMap()); 288 registerElement(ref); 289 return ref; 290 } 291 292 297 public void registerElement(IElementReference elementReference) { 298 fCallbackCache.add(elementReference); 299 elementReference.getParameters().put(fScopingName, fScopingValue); 300 fParentService.registerElement(elementReference); 301 } 302 303 308 public void unregisterElement(IElementReference elementReference) { 309 fCallbackCache.remove(elementReference); 310 fParentService.unregisterElement(elementReference); 311 } 312 } 313 | Popular Tags |