1 11 package org.eclipse.ui.internal.commands; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 import org.eclipse.core.commands.Command; 22 import org.eclipse.core.commands.CommandManager; 23 import org.eclipse.core.commands.contexts.ContextManager; 24 import org.eclipse.core.commands.contexts.ContextManagerEvent; 25 import org.eclipse.core.commands.contexts.IContextManagerListener; 26 import org.eclipse.jface.bindings.Binding; 27 import org.eclipse.jface.bindings.BindingManager; 28 import org.eclipse.jface.bindings.BindingManagerEvent; 29 import org.eclipse.jface.bindings.IBindingManagerListener; 30 import org.eclipse.jface.bindings.Scheme; 31 import org.eclipse.jface.bindings.TriggerSequence; 32 import org.eclipse.jface.bindings.keys.ParseException; 33 import org.eclipse.ui.commands.CommandManagerEvent; 34 import org.eclipse.ui.commands.ICategory; 35 import org.eclipse.ui.commands.ICommand; 36 import org.eclipse.ui.commands.ICommandManager; 37 import org.eclipse.ui.commands.ICommandManagerListener; 38 import org.eclipse.ui.commands.IKeyConfiguration; 39 import org.eclipse.ui.internal.handlers.LegacyHandlerWrapper; 40 import org.eclipse.ui.internal.keys.SchemeWrapper; 41 import org.eclipse.ui.internal.util.Util; 42 import org.eclipse.ui.keys.KeySequence; 43 44 49 public final class CommandManagerWrapper implements ICommandManager, 50 org.eclipse.core.commands.ICommandManagerListener, 51 IBindingManagerListener, IContextManagerListener { 52 53 58 public static boolean DEBUG_COMMAND_EXECUTION = false; 59 60 65 public static boolean DEBUG_HANDLERS = false; 66 67 72 public static String DEBUG_HANDLERS_COMMAND_ID = null; 73 74 static boolean validateKeySequence(KeySequence keySequence) { 75 if (keySequence == null) 76 return false; 77 List keyStrokes = keySequence.getKeyStrokes(); 78 int size = keyStrokes.size(); 79 if (size == 0 || size > 4 || !keySequence.isComplete()) 80 return false; 81 return true; 82 } 83 84 91 private final BindingManager bindingManager; 92 93 99 private final CommandManager commandManager; 100 101 private List commandManagerListeners; 102 103 109 private final ContextManager contextManager; 110 111 127 public CommandManagerWrapper(final BindingManager bindingManager, 128 final CommandManager commandManager, 129 final ContextManager contextManager) { 130 if (contextManager == null) { 131 throw new NullPointerException ( 132 "The context manager cannot be null."); } 134 this.bindingManager = bindingManager; 135 this.commandManager = commandManager; 136 this.contextManager = contextManager; 137 } 138 139 public final void addCommandManagerListener( 140 final ICommandManagerListener commandManagerListener) { 141 if (commandManagerListener == null) { 142 throw new NullPointerException ("Cannot add a null listener."); } 144 145 if (commandManagerListeners == null) { 146 commandManagerListeners = new ArrayList (); 147 this.commandManager.addCommandManagerListener(this); 148 this.bindingManager.addBindingManagerListener(this); 149 this.contextManager.addContextManagerListener(this); 150 } 151 152 if (!commandManagerListeners.contains(commandManagerListener)) { 153 commandManagerListeners.add(commandManagerListener); 154 } 155 } 156 157 162 public final void bindingManagerChanged(final BindingManagerEvent event) { 163 final boolean schemeDefinitionsChanged = event.getScheme() != null; 164 final Set previousSchemes; 165 if (schemeDefinitionsChanged) { 166 previousSchemes = new HashSet (); 167 final Scheme scheme = event.getScheme(); 168 final Scheme[] definedSchemes = event.getManager() 169 .getDefinedSchemes(); 170 final int definedSchemesCount = definedSchemes.length; 171 for (int i = 0; i < definedSchemesCount; i++) { 172 final Scheme definedScheme = definedSchemes[0]; 173 if ((definedScheme == scheme) && (event.isSchemeDefined())) { 174 continue; } 176 previousSchemes.add(definedSchemes[0].getId()); 177 } 178 if (!event.isSchemeDefined()) { 179 previousSchemes.add(scheme.getId()); 180 } 181 } else { 182 previousSchemes = null; 183 } 184 185 fireCommandManagerChanged(new CommandManagerEvent(this, false, event 186 .isActiveSchemeChanged(), event.isLocaleChanged(), event 187 .isPlatformChanged(), false, false, schemeDefinitionsChanged, 188 null, null, previousSchemes)); 189 } 190 191 196 public final void commandManagerChanged( 197 final org.eclipse.core.commands.CommandManagerEvent event) { 198 final boolean categoryIdsChanged = event.isCategoryChanged(); 200 final Set previousCategoryIds; 201 if (categoryIdsChanged) { 202 previousCategoryIds = new HashSet (commandManager 203 .getDefinedCategoryIds()); 204 final String categoryId = event.getCategoryId(); 205 if (event.isCategoryDefined()) { 206 previousCategoryIds.remove(categoryId); 207 } else { 208 previousCategoryIds.add(categoryId); 209 } 210 } else { 211 previousCategoryIds = null; 212 } 213 214 final boolean commandIdsChanged = event.isCommandChanged(); 216 final Set previousCommandIds; 217 if (commandIdsChanged) { 218 previousCommandIds = new HashSet (commandManager 219 .getDefinedCommandIds()); 220 final String commandId = event.getCommandId(); 221 if (event.isCommandDefined()) { 222 previousCommandIds.remove(commandId); 223 } else { 224 previousCommandIds.add(commandId); 225 } 226 } else { 227 previousCommandIds = null; 228 } 229 230 fireCommandManagerChanged(new CommandManagerEvent(this, false, false, 231 false, false, categoryIdsChanged, commandIdsChanged, false, 232 previousCategoryIds, previousCommandIds, null)); 233 } 234 235 public final void contextManagerChanged(final ContextManagerEvent event) { 236 fireCommandManagerChanged(new CommandManagerEvent(this, event 237 .isActiveContextsChanged(), false, false, false, false, false, 238 false, null, null, null)); 239 } 240 241 private void fireCommandManagerChanged( 242 CommandManagerEvent commandManagerEvent) { 243 if (commandManagerEvent == null) 244 throw new NullPointerException (); 245 if (commandManagerListeners != null) 246 for (int i = 0; i < commandManagerListeners.size(); i++) 247 ((ICommandManagerListener) commandManagerListeners.get(i)) 248 .commandManagerChanged(commandManagerEvent); 249 } 250 251 public Set getActiveContextIds() { 252 return contextManager.getActiveContextIds(); 253 } 254 255 public String getActiveKeyConfigurationId() { 256 final Scheme scheme = bindingManager.getActiveScheme(); 257 if (scheme != null) { 258 return scheme.getId(); 259 } 260 261 265 return Util.ZERO_LENGTH_STRING; 266 } 267 268 public String getActiveLocale() { 269 return bindingManager.getLocale(); 270 } 271 272 public String getActivePlatform() { 273 return bindingManager.getPlatform(); 274 } 275 276 public ICategory getCategory(String categoryId) { 277 return null; 280 } 281 282 public ICommand getCommand(String commandId) { 283 final Command command = commandManager.getCommand(commandId); 284 return new CommandWrapper(command, bindingManager); 285 } 286 287 292 public Set getDefinedCategoryIds() { 293 return commandManager.getDefinedCategoryIds(); 294 } 295 296 public Set getDefinedCommandIds() { 297 return commandManager.getDefinedCommandIds(); 298 } 299 300 public Set getDefinedKeyConfigurationIds() { 301 final Set definedIds = new HashSet (); 302 final Scheme[] schemes = bindingManager.getDefinedSchemes(); 303 for (int i = 0; i < schemes.length; i++) { 304 definedIds.add(schemes[i].getId()); 305 } 306 return definedIds; 307 } 308 309 public IKeyConfiguration getKeyConfiguration(String keyConfigurationId) { 310 final Scheme scheme = bindingManager.getScheme(keyConfigurationId); 311 return new SchemeWrapper(scheme, bindingManager); 312 } 313 314 public Map getPartialMatches(KeySequence keySequence) { 315 try { 316 final org.eclipse.jface.bindings.keys.KeySequence sequence = org.eclipse.jface.bindings.keys.KeySequence 317 .getInstance(keySequence.toString()); 318 final Map partialMatches = bindingManager 319 .getPartialMatches(sequence); 320 final Map returnValue = new HashMap (); 321 final Iterator matchItr = partialMatches.entrySet().iterator(); 322 while (matchItr.hasNext()) { 323 final Map.Entry entry = (Map.Entry ) matchItr.next(); 324 final TriggerSequence trigger = (TriggerSequence) entry 325 .getKey(); 326 if (trigger instanceof org.eclipse.jface.bindings.keys.KeySequence) { 327 final org.eclipse.jface.bindings.keys.KeySequence triggerKey = (org.eclipse.jface.bindings.keys.KeySequence) trigger; 328 returnValue.put(KeySequence.getInstance(triggerKey 329 .toString()), entry.getValue()); 330 } 331 } 332 return returnValue; 333 } catch (final ParseException e) { 334 return new HashMap (); 335 } catch (final org.eclipse.ui.keys.ParseException e) { 336 return new HashMap (); 337 } 338 } 339 340 public String getPerfectMatch(KeySequence keySequence) { 341 try { 342 final org.eclipse.jface.bindings.keys.KeySequence sequence = org.eclipse.jface.bindings.keys.KeySequence 343 .getInstance(keySequence.toString()); 344 final Binding binding = bindingManager.getPerfectMatch(sequence); 345 if (binding == null) { 346 return null; 347 } 348 349 return binding.getParameterizedCommand().getId(); 350 351 } catch (final ParseException e) { 352 return null; 353 } 354 } 355 356 public boolean isPartialMatch(KeySequence keySequence) { 357 try { 358 final org.eclipse.jface.bindings.keys.KeySequence sequence = org.eclipse.jface.bindings.keys.KeySequence 359 .getInstance(keySequence.toString()); 360 return bindingManager.isPartialMatch(sequence); 361 } catch (final ParseException e) { 362 return false; 363 } 364 } 365 366 public boolean isPerfectMatch(KeySequence keySequence) { 367 try { 368 final org.eclipse.jface.bindings.keys.KeySequence sequence = org.eclipse.jface.bindings.keys.KeySequence 369 .getInstance(keySequence.toString()); 370 return bindingManager.isPerfectMatch(sequence); 371 } catch (final ParseException e) { 372 return false; 373 } 374 } 375 376 public void removeCommandManagerListener( 377 ICommandManagerListener commandManagerListener) { 378 if (commandManagerListener == null) { 379 throw new NullPointerException ("Cannot remove a null listener"); } 381 382 if (commandManagerListeners != null) { 383 commandManagerListeners.remove(commandManagerListener); 384 if (commandManagerListeners.isEmpty()) { 385 commandManagerListeners = null; 386 this.commandManager.removeCommandManagerListener(this); 387 this.bindingManager.removeBindingManagerListener(this); 388 this.contextManager.removeContextManagerListener(this); 389 } 390 } 391 } 392 393 400 public final void setHandlersByCommandId(final Map handlersByCommandId) { 401 final Iterator entryItr = handlersByCommandId.entrySet().iterator(); 403 while (entryItr.hasNext()) { 404 final Map.Entry entry = (Map.Entry ) entryItr.next(); 405 final Object handler = entry.getValue(); 406 if (handler instanceof org.eclipse.ui.commands.IHandler) { 407 final String commandId = (String ) entry.getKey(); 408 handlersByCommandId.put(commandId, new LegacyHandlerWrapper( 409 (org.eclipse.ui.commands.IHandler) handler)); 410 } 411 } 412 413 commandManager.setHandlersByCommandId(handlersByCommandId); 414 } 415 } 416 | Popular Tags |