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.SchemeLegacyWrapper; 41 import org.eclipse.ui.internal.util.Util; 42 import org.eclipse.ui.keys.KeySequence; 43 44 49 public final class CommandManagerLegacyWrapper 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 } 78 List keyStrokes = keySequence.getKeyStrokes(); 79 int size = keyStrokes.size(); 80 if (size == 0 || size > 4 || !keySequence.isComplete()) { 81 return false; 82 } 83 return true; 84 } 85 86 93 private final BindingManager bindingManager; 94 95 101 private final CommandManager commandManager; 102 103 private List commandManagerListeners; 104 105 111 private final ContextManager contextManager; 112 113 129 public CommandManagerLegacyWrapper(final BindingManager bindingManager, 130 final CommandManager commandManager, 131 final ContextManager contextManager) { 132 if (contextManager == null) { 133 throw new NullPointerException ( 134 "The context manager cannot be null."); } 136 this.bindingManager = bindingManager; 137 this.commandManager = commandManager; 138 this.contextManager = contextManager; 139 } 140 141 public final void addCommandManagerListener( 142 final ICommandManagerListener commandManagerListener) { 143 if (commandManagerListener == null) { 144 throw new NullPointerException ("Cannot add a null listener."); } 146 147 if (commandManagerListeners == null) { 148 commandManagerListeners = new ArrayList (); 149 this.commandManager.addCommandManagerListener(this); 150 this.bindingManager.addBindingManagerListener(this); 151 this.contextManager.addContextManagerListener(this); 152 } 153 154 if (!commandManagerListeners.contains(commandManagerListener)) { 155 commandManagerListeners.add(commandManagerListener); 156 } 157 } 158 159 164 public final void bindingManagerChanged(final BindingManagerEvent event) { 165 final boolean schemeDefinitionsChanged = event.getScheme() != null; 166 final Set previousSchemes; 167 if (schemeDefinitionsChanged) { 168 previousSchemes = new HashSet (); 169 final Scheme scheme = event.getScheme(); 170 final Scheme[] definedSchemes = event.getManager() 171 .getDefinedSchemes(); 172 final int definedSchemesCount = definedSchemes.length; 173 for (int i = 0; i < definedSchemesCount; i++) { 174 final Scheme definedScheme = definedSchemes[0]; 175 if ((definedScheme == scheme) && (event.isSchemeDefined())) { 176 continue; } 178 previousSchemes.add(definedSchemes[0].getId()); 179 } 180 if (!event.isSchemeDefined()) { 181 previousSchemes.add(scheme.getId()); 182 } 183 } else { 184 previousSchemes = null; 185 } 186 187 fireCommandManagerChanged(new CommandManagerEvent(this, false, event 188 .isActiveSchemeChanged(), event.isLocaleChanged(), event 189 .isPlatformChanged(), false, false, schemeDefinitionsChanged, 190 null, null, previousSchemes)); 191 } 192 193 198 public final void commandManagerChanged( 199 final org.eclipse.core.commands.CommandManagerEvent event) { 200 final boolean categoryIdsChanged = event.isCategoryChanged(); 202 final Set previousCategoryIds; 203 if (categoryIdsChanged) { 204 previousCategoryIds = new HashSet (commandManager 205 .getDefinedCategoryIds()); 206 final String categoryId = event.getCategoryId(); 207 if (event.isCategoryDefined()) { 208 previousCategoryIds.remove(categoryId); 209 } else { 210 previousCategoryIds.add(categoryId); 211 } 212 } else { 213 previousCategoryIds = null; 214 } 215 216 final boolean commandIdsChanged = event.isCommandChanged(); 218 final Set previousCommandIds; 219 if (commandIdsChanged) { 220 previousCommandIds = new HashSet (commandManager 221 .getDefinedCommandIds()); 222 final String commandId = event.getCommandId(); 223 if (event.isCommandDefined()) { 224 previousCommandIds.remove(commandId); 225 } else { 226 previousCommandIds.add(commandId); 227 } 228 } else { 229 previousCommandIds = null; 230 } 231 232 fireCommandManagerChanged(new CommandManagerEvent(this, false, false, 233 false, false, categoryIdsChanged, commandIdsChanged, false, 234 previousCategoryIds, previousCommandIds, null)); 235 } 236 237 public final void contextManagerChanged(final ContextManagerEvent event) { 238 fireCommandManagerChanged(new CommandManagerEvent(this, event 239 .isActiveContextsChanged(), false, false, false, false, false, 240 false, null, null, null)); 241 } 242 243 private void fireCommandManagerChanged( 244 CommandManagerEvent commandManagerEvent) { 245 if (commandManagerEvent == null) { 246 throw new NullPointerException (); 247 } 248 if (commandManagerListeners != null) { 249 for (int i = 0; i < commandManagerListeners.size(); i++) { 250 ((ICommandManagerListener) commandManagerListeners.get(i)) 251 .commandManagerChanged(commandManagerEvent); 252 } 253 } 254 } 255 256 public Set getActiveContextIds() { 257 return contextManager.getActiveContextIds(); 258 } 259 260 public String getActiveKeyConfigurationId() { 261 final Scheme scheme = bindingManager.getActiveScheme(); 262 if (scheme != null) { 263 return scheme.getId(); 264 } 265 266 270 return Util.ZERO_LENGTH_STRING; 271 } 272 273 public String getActiveLocale() { 274 return bindingManager.getLocale(); 275 } 276 277 public String getActivePlatform() { 278 return bindingManager.getPlatform(); 279 } 280 281 public ICategory getCategory(String categoryId) { 282 return null; 285 } 286 287 public ICommand getCommand(String commandId) { 288 final Command command = commandManager.getCommand(commandId); 289 return new CommandLegacyWrapper(command, bindingManager); 290 } 291 292 297 public Set getDefinedCategoryIds() { 298 return commandManager.getDefinedCategoryIds(); 299 } 300 301 public Set getDefinedCommandIds() { 302 return commandManager.getDefinedCommandIds(); 303 } 304 305 public Set getDefinedKeyConfigurationIds() { 306 final Set definedIds = new HashSet (); 307 final Scheme[] schemes = bindingManager.getDefinedSchemes(); 308 for (int i = 0; i < schemes.length; i++) { 309 definedIds.add(schemes[i].getId()); 310 } 311 return definedIds; 312 } 313 314 public IKeyConfiguration getKeyConfiguration(String keyConfigurationId) { 315 final Scheme scheme = bindingManager.getScheme(keyConfigurationId); 316 return new SchemeLegacyWrapper(scheme, bindingManager); 317 } 318 319 public Map getPartialMatches(KeySequence keySequence) { 320 try { 321 final org.eclipse.jface.bindings.keys.KeySequence sequence = org.eclipse.jface.bindings.keys.KeySequence 322 .getInstance(keySequence.toString()); 323 final Map partialMatches = bindingManager 324 .getPartialMatches(sequence); 325 final Map returnValue = new HashMap (); 326 final Iterator matchItr = partialMatches.entrySet().iterator(); 327 while (matchItr.hasNext()) { 328 final Map.Entry entry = (Map.Entry ) matchItr.next(); 329 final TriggerSequence trigger = (TriggerSequence) entry 330 .getKey(); 331 if (trigger instanceof org.eclipse.jface.bindings.keys.KeySequence) { 332 final org.eclipse.jface.bindings.keys.KeySequence triggerKey = (org.eclipse.jface.bindings.keys.KeySequence) trigger; 333 returnValue.put(KeySequence.getInstance(triggerKey 334 .toString()), entry.getValue()); 335 } 336 } 337 return returnValue; 338 } catch (final ParseException e) { 339 return new HashMap (); 340 } catch (final org.eclipse.ui.keys.ParseException e) { 341 return new HashMap (); 342 } 343 } 344 345 public String getPerfectMatch(KeySequence keySequence) { 346 try { 347 final org.eclipse.jface.bindings.keys.KeySequence sequence = org.eclipse.jface.bindings.keys.KeySequence 348 .getInstance(keySequence.toString()); 349 final Binding binding = bindingManager.getPerfectMatch(sequence); 350 if (binding == null) { 351 return null; 352 } 353 354 return binding.getParameterizedCommand().getId(); 355 356 } catch (final ParseException e) { 357 return null; 358 } 359 } 360 361 public boolean isPartialMatch(KeySequence keySequence) { 362 try { 363 final org.eclipse.jface.bindings.keys.KeySequence sequence = org.eclipse.jface.bindings.keys.KeySequence 364 .getInstance(keySequence.toString()); 365 return bindingManager.isPartialMatch(sequence); 366 } catch (final ParseException e) { 367 return false; 368 } 369 } 370 371 public boolean isPerfectMatch(KeySequence keySequence) { 372 try { 373 final org.eclipse.jface.bindings.keys.KeySequence sequence = org.eclipse.jface.bindings.keys.KeySequence 374 .getInstance(keySequence.toString()); 375 return bindingManager.isPerfectMatch(sequence); 376 } catch (final ParseException e) { 377 return false; 378 } 379 } 380 381 public void removeCommandManagerListener( 382 ICommandManagerListener commandManagerListener) { 383 if (commandManagerListener == null) { 384 throw new NullPointerException ("Cannot remove a null listener"); } 386 387 if (commandManagerListeners != null) { 388 commandManagerListeners.remove(commandManagerListener); 389 if (commandManagerListeners.isEmpty()) { 390 commandManagerListeners = null; 391 this.commandManager.removeCommandManagerListener(this); 392 this.bindingManager.removeBindingManagerListener(this); 393 this.contextManager.removeContextManagerListener(this); 394 } 395 } 396 } 397 398 405 public final void setHandlersByCommandId(final Map handlersByCommandId) { 406 final Iterator entryItr = handlersByCommandId.entrySet().iterator(); 408 while (entryItr.hasNext()) { 409 final Map.Entry entry = (Map.Entry ) entryItr.next(); 410 final Object handler = entry.getValue(); 411 if (handler instanceof org.eclipse.ui.commands.IHandler) { 412 final String commandId = (String ) entry.getKey(); 413 handlersByCommandId.put(commandId, new LegacyHandlerWrapper( 414 (org.eclipse.ui.commands.IHandler) handler)); 415 } 416 } 417 418 commandManager.setHandlersByCommandId(handlersByCommandId); 419 } 420 } 421 | Popular Tags |