1 11 package org.eclipse.ui.internal.commands; 12 import java.util.ArrayList ; 13 import java.util.Collection ; 14 import java.util.Collections ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Locale ; 20 import java.util.Map ; 21 import java.util.Set ; 22 import java.util.SortedSet ; 23 import java.util.StringTokenizer ; 24 import java.util.TreeMap ; 25 import java.util.TreeSet ; 26 import java.util.WeakHashMap ; 27 28 import org.eclipse.core.runtime.Platform; 29 import org.eclipse.swt.SWT; 30 import org.eclipse.ui.commands.CategoryEvent; 31 import org.eclipse.ui.commands.CommandEvent; 32 import org.eclipse.ui.commands.CommandManagerEvent; 33 import org.eclipse.ui.commands.ICategory; 34 import org.eclipse.ui.commands.ICommand; 35 import org.eclipse.ui.commands.ICommandManagerListener; 36 import org.eclipse.ui.commands.IHandler; 37 import org.eclipse.ui.commands.IKeyConfiguration; 38 import org.eclipse.ui.commands.KeyConfigurationEvent; 39 import org.eclipse.ui.commands.NotDefinedException; 40 import org.eclipse.ui.internal.WorkbenchPlugin; 41 import org.eclipse.ui.internal.util.Util; 42 import org.eclipse.ui.keys.KeySequence; 43 public final class MutableCommandManager implements IMutableCommandManager { 44 45 50 public static boolean DEBUG_COMMAND_EXECUTION = false; 51 52 57 public static boolean DEBUG_HANDLERS = false; 58 59 64 public static String DEBUG_HANDLERS_COMMAND_ID = null; 65 66 public final static String SEPARATOR = "_"; 68 static String [] extend(String [] strings) { 69 String [] strings2 = new String [strings.length + 1]; 70 System.arraycopy(strings, 0, strings2, 0, strings.length); 71 return strings2; 72 } 73 static String [] getPath(String string, String separator) { 74 if (string == null || separator == null) 75 return new String [0]; 76 List strings = new ArrayList (); 77 StringBuffer stringBuffer = new StringBuffer (); 78 string = string.trim(); 79 if (string.length() > 0) { 80 StringTokenizer stringTokenizer = new StringTokenizer (string, 81 separator); 82 while (stringTokenizer.hasMoreElements()) { 83 if (stringBuffer.length() > 0) 84 stringBuffer.append(separator); 85 stringBuffer.append(((String ) stringTokenizer.nextElement()) 86 .trim()); 87 strings.add(stringBuffer.toString()); 88 } 89 } 90 Collections.reverse(strings); 91 strings.add(Util.ZERO_LENGTH_STRING); 92 return (String []) strings.toArray(new String [strings.size()]); 93 } 94 static boolean isKeyConfigurationDefinitionChildOf(String ancestor, 95 String id, Map keyConfigurationDefinitionsById) { 96 Collection visited = new HashSet (); 97 while (id != null && !visited.contains(id)) { 98 KeyConfigurationDefinition keyConfigurationDefinition = (KeyConfigurationDefinition) keyConfigurationDefinitionsById 99 .get(id); 100 visited.add(id); 101 if (keyConfigurationDefinition != null 102 && Util.equals(id = keyConfigurationDefinition 103 .getParentId(), ancestor)) 104 return true; 105 } 106 return false; 107 } 108 109 static boolean validateKeySequence(KeySequence keySequence) { 110 if (keySequence == null) 111 return false; 112 List keyStrokes = keySequence.getKeyStrokes(); 113 int size = keyStrokes.size(); 114 if (size == 0 || size > 4 || !keySequence.isComplete()) 115 return false; 116 return true; 117 } 118 static void validateKeySequenceBindingDefinitions( 119 Collection keySequenceBindingDefinitions) { 120 Iterator iterator = keySequenceBindingDefinitions.iterator(); 121 while (iterator.hasNext()) { 122 KeySequenceBindingDefinition keySequenceBindingDefinition = (KeySequenceBindingDefinition) iterator 123 .next(); 124 String keyConfigurationId = keySequenceBindingDefinition 125 .getKeyConfigurationId(); 126 KeySequence keySequence = keySequenceBindingDefinition 127 .getKeySequence(); 128 if (keyConfigurationId == null || keySequence == null 129 || !validateKeySequence(keySequence)) 130 iterator.remove(); 131 } 132 } 133 private Map activeContextIds = new HashMap (); 134 private String activeKeyConfigurationId = null; 136 private String activeLocale = null; 137 private String activePlatform = null; 138 private Map categoriesById = new WeakHashMap (); 139 private Set categoriesWithListeners = new HashSet (); 140 private Map categoryDefinitionsById = new HashMap (); 141 private Map commandDefinitionsById = new HashMap (); 142 private List commandManagerListeners; 143 private ICommandRegistry commandRegistry; 144 private Map commandsById = new WeakHashMap (); 145 private Set commandsWithListeners = new HashSet (); 146 private Set definedCategoryIds = new HashSet (); 147 private Set definedCommandIds = new HashSet (); 148 private Set definedHandlers = new HashSet (); 149 private Set definedKeyConfigurationIds = new HashSet (); 150 private Map handlersByCommandId = new HashMap (); 152 private Map keyConfigurationDefinitionsById = new HashMap (); 153 private Map keyConfigurationsById = new WeakHashMap (); 154 private Set keyConfigurationsWithListeners = new HashSet (); 155 private KeySequenceBindingMachine keySequenceBindingMachine = new KeySequenceBindingMachine(); 156 private Map keySequenceBindingsByCommandId = new HashMap (); 157 private IMutableCommandRegistry mutableCommandRegistry; 158 public MutableCommandManager() { 160 this(new ExtensionCommandRegistry(Platform.getExtensionRegistry()), 161 new PreferenceCommandRegistry(WorkbenchPlugin.getDefault() 162 .getPreferenceStore())); 163 } 164 public MutableCommandManager(ICommandRegistry commandRegistry, 165 IMutableCommandRegistry mutableCommandRegistry) { 166 if (commandRegistry == null || mutableCommandRegistry == null) 167 throw new NullPointerException (); 168 this.commandRegistry = commandRegistry; 169 this.mutableCommandRegistry = mutableCommandRegistry; 170 String systemLocale = Locale.getDefault().toString(); 171 activeLocale = systemLocale != null 172 ? systemLocale 173 : Util.ZERO_LENGTH_STRING; 174 String systemPlatform = SWT.getPlatform(); 175 activePlatform = systemPlatform != null 176 ? systemPlatform 177 : Util.ZERO_LENGTH_STRING; 178 this.commandRegistry 179 .addCommandRegistryListener(new ICommandRegistryListener() { 180 public void commandRegistryChanged( 181 CommandRegistryEvent commandRegistryEvent) { 182 readRegistry(); 183 } 184 }); 185 this.mutableCommandRegistry 186 .addCommandRegistryListener(new ICommandRegistryListener() { 187 public void commandRegistryChanged( 188 CommandRegistryEvent commandRegistryEvent) { 189 readRegistry(); 190 } 191 }); 192 readRegistry(); 193 } 194 public void addCommandManagerListener( 195 ICommandManagerListener commandManagerListener) { 196 if (commandManagerListener == null) 197 throw new NullPointerException (); 198 if (commandManagerListeners == null) 199 commandManagerListeners = new ArrayList (); 200 if (!commandManagerListeners.contains(commandManagerListener)) 201 commandManagerListeners.add(commandManagerListener); 202 } 203 204 222 private void calculateKeySequenceBindings() { 223 final String [] activeKeyConfigurationIds = extend(getKeyConfigurationIds(activeKeyConfigurationId)); 225 final String [] activeLocales = extend(getPath(activeLocale, SEPARATOR)); 226 final String [] activePlatforms = extend(getPath(activePlatform, 227 SEPARATOR)); 228 229 keySequenceBindingMachine.setActiveContextIds(activeContextIds); 231 keySequenceBindingMachine 232 .setActiveKeyConfigurationIds(activeKeyConfigurationIds); 233 keySequenceBindingMachine.setActiveLocales(activeLocales); 234 keySequenceBindingMachine.setActivePlatforms(activePlatforms); 235 236 keySequenceBindingsByCommandId = keySequenceBindingMachine 238 .getKeySequenceBindingsByCommandId(); 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 public Set getActiveContextIds() { 251 return activeContextIds.keySet(); 252 } 253 public String getActiveKeyConfigurationId() { 254 return activeKeyConfigurationId; 255 } 256 public String getActiveLocale() { 257 return activeLocale; 258 } 259 public String getActivePlatform() { 260 return activePlatform; 261 } 262 public ICategory getCategory(String categoryId) { 263 if (categoryId == null) 264 throw new NullPointerException (); 265 Category category = (Category) categoriesById.get(categoryId); 266 if (category == null) { 267 category = new Category(categoriesWithListeners, categoryId); 268 updateCategory(category); 269 categoriesById.put(categoryId, category); 270 } 271 return category; 272 } 273 public ICommand getCommand(String commandId) { 274 if (commandId == null) 275 throw new NullPointerException (); 276 Command command = (Command) commandsById.get(commandId); 277 if (command == null) { 278 command = new Command(commandsWithListeners, commandId); 279 updateCommand(command); 280 commandsById.put(commandId, command); 281 } 282 return command; 283 } 284 public ICommandRegistry getCommandRegistry() { 286 return commandRegistry; 287 } 288 public Set getDefinedCategoryIds() { 289 return Collections.unmodifiableSet(definedCategoryIds); 290 } 291 public Set getDefinedCommandIds() { 292 return Collections.unmodifiableSet(definedCommandIds); 293 } 294 300 public Set getDefinedHandlers() { 301 return Collections.unmodifiableSet(definedHandlers); 302 } 303 public Set getDefinedKeyConfigurationIds() { 304 return Collections.unmodifiableSet(definedKeyConfigurationIds); 305 } 306 public Map getHandlersByCommandId() { 307 return Collections.unmodifiableMap(handlersByCommandId); 308 } 309 public IKeyConfiguration getKeyConfiguration(String keyConfigurationId) { 310 if (keyConfigurationId == null) 311 throw new NullPointerException (); 312 KeyConfiguration keyConfiguration = (KeyConfiguration) keyConfigurationsById 313 .get(keyConfigurationId); 314 if (keyConfiguration == null) { 315 keyConfiguration = new KeyConfiguration( 316 keyConfigurationsWithListeners, keyConfigurationId); 317 updateKeyConfiguration(keyConfiguration); 318 keyConfigurationsById.put(keyConfigurationId, keyConfiguration); 319 } 320 return keyConfiguration; 321 } 322 String [] getKeyConfigurationIds(String keyConfigurationId) { 323 List strings = new ArrayList (); 324 while (keyConfigurationId != null) { 325 strings.add(keyConfigurationId); 326 try { 327 keyConfigurationId = getKeyConfiguration(keyConfigurationId) 328 .getParentId(); 329 } catch (NotDefinedException eNotDefined) { 330 return new String [0]; 331 } 332 } 333 return (String []) strings.toArray(new String [strings.size()]); 334 } 335 IMutableCommandRegistry getMutableCommandRegistry() { 336 return mutableCommandRegistry; 337 } 338 public Map getPartialMatches(KeySequence keySequence) { 339 Map map = new HashMap (); 340 for (Iterator iterator = keySequenceBindingMachine 341 .getMatchesByKeySequence().entrySet().iterator(); iterator 342 .hasNext();) { 343 Map.Entry entry = (Map.Entry ) iterator.next(); 344 KeySequence keySequence2 = (KeySequence) entry.getKey(); 345 Match match = (Match) entry.getValue(); 346 if (keySequence2.startsWith(keySequence, false)) 347 map.put(keySequence2, match.getCommandId()); 348 } 349 return Collections.unmodifiableMap(map); 350 } 351 public String getPerfectMatch(KeySequence keySequence) { 352 Match match = (Match) keySequenceBindingMachine 353 .getMatchesByKeySequence().get(keySequence); 354 return match != null ? match.getCommandId() : null; 355 } 356 public boolean isPartialMatch(KeySequence keySequence) { 357 for (Iterator iterator = keySequenceBindingMachine 358 .getMatchesByKeySequence().entrySet().iterator(); iterator 359 .hasNext();) { 360 Map.Entry entry = (Map.Entry ) iterator.next(); 361 KeySequence keySequence2 = (KeySequence) entry.getKey(); 362 if (keySequence2.startsWith(keySequence, false)) 363 return true; 364 } 365 return false; 366 } 367 public boolean isPerfectMatch(KeySequence keySequence) { 368 return getPerfectMatch(keySequence) != null; 369 } 370 private void notifyCategories(Map categoryEventsByCategoryId) { 371 for (Iterator iterator = categoryEventsByCategoryId.entrySet() 372 .iterator(); iterator.hasNext();) { 373 Map.Entry entry = (Map.Entry ) iterator.next(); 374 String categoryId = (String ) entry.getKey(); 375 CategoryEvent categoryEvent = (CategoryEvent) entry.getValue(); 376 Category category = (Category) categoriesById.get(categoryId); 377 if (category != null) 378 category.fireCategoryChanged(categoryEvent); 379 } 380 } 381 private void notifyCommands(Map commandEventsByCommandId) { 382 for (Iterator iterator = commandEventsByCommandId.entrySet().iterator(); iterator 383 .hasNext();) { 384 Map.Entry entry = (Map.Entry ) iterator.next(); 385 String commandId = (String ) entry.getKey(); 386 CommandEvent commandEvent = (CommandEvent) entry.getValue(); 387 Command command = (Command) commandsById.get(commandId); 388 if (command != null) 389 command.fireCommandChanged(commandEvent); 390 } 391 } 392 private void notifyKeyConfigurations( 393 Map keyConfigurationEventsByKeyConfigurationId) { 394 for (Iterator iterator = keyConfigurationEventsByKeyConfigurationId 395 .entrySet().iterator(); iterator.hasNext();) { 396 Map.Entry entry = (Map.Entry ) iterator.next(); 397 String keyConfigurationId = (String ) entry.getKey(); 398 KeyConfigurationEvent keyConfigurationEvent = (KeyConfigurationEvent) entry 399 .getValue(); 400 KeyConfiguration keyConfiguration = (KeyConfiguration) keyConfigurationsById 401 .get(keyConfigurationId); 402 if (keyConfiguration != null) 403 keyConfiguration 404 .fireKeyConfigurationChanged(keyConfigurationEvent); 405 } 406 } 407 private void readRegistry() { 408 Collection categoryDefinitions = new ArrayList (); 409 categoryDefinitions.addAll(commandRegistry.getCategoryDefinitions()); 410 categoryDefinitions.addAll(mutableCommandRegistry 411 .getCategoryDefinitions()); 412 Map categoryDefinitionsById = new HashMap (CategoryDefinition 413 .categoryDefinitionsById(categoryDefinitions, false)); 414 definedHandlers.addAll(commandRegistry.getHandlers()); 415 for (Iterator iterator = categoryDefinitionsById.values().iterator(); iterator 416 .hasNext();) { 417 CategoryDefinition categoryDefinition = (CategoryDefinition) iterator 418 .next(); 419 String name = categoryDefinition.getName(); 420 if (name == null || name.length() == 0) 421 iterator.remove(); 422 } 423 Collection commandDefinitions = new ArrayList (); 424 commandDefinitions.addAll(commandRegistry.getCommandDefinitions()); 425 commandDefinitions.addAll(mutableCommandRegistry 426 .getCommandDefinitions()); 427 Map commandDefinitionsById = new HashMap (CommandDefinition 428 .commandDefinitionsById(commandDefinitions, false)); 429 for (Iterator iterator = commandDefinitionsById.values().iterator(); iterator 430 .hasNext();) { 431 CommandDefinition commandDefinition = (CommandDefinition) iterator 432 .next(); 433 String name = commandDefinition.getName(); 434 if (name == null || name.length() == 0) 435 iterator.remove(); 436 } 437 Collection keyConfigurationDefinitions = new ArrayList (); 438 keyConfigurationDefinitions.addAll(commandRegistry 439 .getKeyConfigurationDefinitions()); 440 keyConfigurationDefinitions.addAll(mutableCommandRegistry 441 .getKeyConfigurationDefinitions()); 442 Map keyConfigurationDefinitionsById = new HashMap ( 443 KeyConfigurationDefinition.keyConfigurationDefinitionsById( 444 keyConfigurationDefinitions, false)); 445 for (Iterator iterator = keyConfigurationDefinitionsById.values() 446 .iterator(); iterator.hasNext();) { 447 KeyConfigurationDefinition keyConfigurationDefinition = (KeyConfigurationDefinition) iterator 448 .next(); 449 String name = keyConfigurationDefinition.getName(); 450 if (name == null || name.length() == 0) 451 iterator.remove(); 452 } 453 for (Iterator iterator = commandDefinitionsById.values().iterator(); iterator 455 .hasNext();) { 456 CommandDefinition commandDefinition = (CommandDefinition) iterator 457 .next(); 458 String categoryId = commandDefinition.getCategoryId(); 459 if (categoryId != null 460 && !categoryDefinitionsById.containsKey(categoryId)) 461 iterator.remove(); 462 } 463 for (Iterator iterator = keyConfigurationDefinitionsById.keySet() 464 .iterator(); iterator.hasNext();) 465 if (!isKeyConfigurationDefinitionChildOf(null, (String ) iterator 466 .next(), keyConfigurationDefinitionsById)) 467 iterator.remove(); 468 List activeKeyConfigurationDefinitions = new ArrayList (); 471 activeKeyConfigurationDefinitions.addAll(commandRegistry 472 .getActiveKeyConfigurationDefinitions()); 473 activeKeyConfigurationDefinitions.addAll(mutableCommandRegistry 474 .getActiveKeyConfigurationDefinitions()); 475 String activeKeyConfigurationId = null; 476 if (!activeKeyConfigurationDefinitions.isEmpty()) { 477 ActiveKeyConfigurationDefinition activeKeyConfigurationDefinition = (ActiveKeyConfigurationDefinition) activeKeyConfigurationDefinitions 478 .get(activeKeyConfigurationDefinitions.size() - 1); 479 activeKeyConfigurationId = activeKeyConfigurationDefinition 480 .getKeyConfigurationId(); 481 if (activeKeyConfigurationId != null 482 && !keyConfigurationDefinitionsById 483 .containsKey(activeKeyConfigurationId)) 484 activeKeyConfigurationId = null; 485 } 486 if (activeKeyConfigurationId == null 489 && !keyConfigurationDefinitionsById.isEmpty()) { 490 SortedSet sortedSet = new TreeSet (keyConfigurationDefinitionsById 491 .keySet()); 492 activeKeyConfigurationId = (String ) sortedSet.first(); 493 } 494 this.categoryDefinitionsById = categoryDefinitionsById; 495 this.commandDefinitionsById = commandDefinitionsById; 496 this.keyConfigurationDefinitionsById = keyConfigurationDefinitionsById; 497 boolean activeKeyConfigurationIdChanged = false; 498 if (!Util.equals(this.activeKeyConfigurationId, 499 activeKeyConfigurationId)) { 500 this.activeKeyConfigurationId = activeKeyConfigurationId; 501 activeKeyConfigurationIdChanged = true; 502 } 503 boolean definedCategoryIdsChanged = false; 504 Set definedCategoryIds = new HashSet (categoryDefinitionsById.keySet()); 505 Set previouslyDefinedCategoryIds = null; 506 if (!definedCategoryIds.equals(this.definedCategoryIds)) { 507 previouslyDefinedCategoryIds = this.definedCategoryIds; 508 this.definedCategoryIds = definedCategoryIds; 509 definedCategoryIdsChanged = true; 510 } 511 boolean definedCommandIdsChanged = false; 512 Set definedCommandIds = new HashSet (commandDefinitionsById.keySet()); 513 Set previouslyDefinedCommandIds = null; 514 if (!definedCommandIds.equals(this.definedCommandIds)) { 515 previouslyDefinedCommandIds = this.definedCommandIds; 516 this.definedCommandIds = definedCommandIds; 517 definedCommandIdsChanged = true; 518 } 519 boolean definedKeyConfigurationIdsChanged = false; 520 Set definedKeyConfigurationIds = new HashSet ( 521 keyConfigurationDefinitionsById.keySet()); 522 Set previouslyDefinedKeyConfigurationIds = null; 523 if (!definedKeyConfigurationIds.equals(this.definedKeyConfigurationIds)) { 524 previouslyDefinedKeyConfigurationIds = this.definedKeyConfigurationIds; 525 this.definedKeyConfigurationIds = definedKeyConfigurationIds; 526 definedKeyConfigurationIdsChanged = true; 527 } 528 List commandRegistryKeySequenceBindingDefinitions = new ArrayList ( 529 commandRegistry.getKeySequenceBindingDefinitions()); 530 validateKeySequenceBindingDefinitions(commandRegistryKeySequenceBindingDefinitions); 531 List mutableCommandRegistryKeySequenceBindingDefinitions = new ArrayList ( 532 mutableCommandRegistry.getKeySequenceBindingDefinitions()); 533 validateKeySequenceBindingDefinitions(mutableCommandRegistryKeySequenceBindingDefinitions); 534 keySequenceBindingMachine 535 .setKeySequenceBindings0(mutableCommandRegistryKeySequenceBindingDefinitions); 536 keySequenceBindingMachine 537 .setKeySequenceBindings1(commandRegistryKeySequenceBindingDefinitions); 538 calculateKeySequenceBindings(); 539 Map categoryEventsByCategoryId = updateCategories(categoriesById 540 .keySet()); 541 Map commandEventsByCommandId = updateCommands(commandsById.keySet()); 542 Map keyConfigurationEventsByKeyConfigurationId = updateKeyConfigurations(keyConfigurationsById 543 .keySet()); 544 if (activeKeyConfigurationIdChanged || definedCategoryIdsChanged 545 || definedCommandIdsChanged 546 || definedKeyConfigurationIdsChanged) 547 fireCommandManagerChanged(new CommandManagerEvent(this, false, 548 activeKeyConfigurationIdChanged, false, false, 549 definedCategoryIdsChanged, definedCommandIdsChanged, 550 definedKeyConfigurationIdsChanged, 551 previouslyDefinedCategoryIds, previouslyDefinedCommandIds, 552 previouslyDefinedKeyConfigurationIds)); 553 if (categoryEventsByCategoryId != null) 554 notifyCategories(categoryEventsByCategoryId); 555 if (commandEventsByCommandId != null) 556 notifyCommands(commandEventsByCommandId); 557 if (keyConfigurationEventsByKeyConfigurationId != null) 558 notifyKeyConfigurations(keyConfigurationEventsByKeyConfigurationId); 559 } 560 public void removeCommandManagerListener( 561 ICommandManagerListener commandManagerListener) { 562 if (commandManagerListener == null) 563 throw new NullPointerException (); 564 if (commandManagerListeners != null) 565 commandManagerListeners.remove(commandManagerListener); 566 } 567 public void setActiveContextIds(Map activeContextIds) { 568 boolean commandManagerChanged = false; 569 Map commandEventsByCommandId = null; 570 if (!this.activeContextIds.equals(activeContextIds)) { 571 this.activeContextIds = activeContextIds; 572 commandManagerChanged = true; 573 calculateKeySequenceBindings(); 574 commandEventsByCommandId = updateCommands(commandsById.keySet()); 575 } 576 if (commandManagerChanged) 577 fireCommandManagerChanged(new CommandManagerEvent(this, true, 578 false, false, false, false, false, false, null, null, null)); 579 if (commandEventsByCommandId != null) 580 notifyCommands(commandEventsByCommandId); 581 } 582 public void setActiveKeyConfigurationId(String activeKeyConfigurationId) { 583 boolean commandManagerChanged = false; 584 Map commandEventsByCommandId = null; 585 Map keyConfigurationEventsByKeyConfigurationId = null; 586 if (!Util.equals(this.activeKeyConfigurationId, 587 activeKeyConfigurationId)) { 588 this.activeKeyConfigurationId = activeKeyConfigurationId; 589 commandManagerChanged = true; 590 calculateKeySequenceBindings(); 591 commandEventsByCommandId = updateCommands(commandsById.keySet()); 592 keyConfigurationEventsByKeyConfigurationId = updateKeyConfigurations(keyConfigurationsById 593 .keySet()); 594 } 595 if (commandManagerChanged) 596 fireCommandManagerChanged(new CommandManagerEvent(this, false, 597 true, false, false, false, false, false, null, null, null)); 598 if (commandEventsByCommandId != null) 599 notifyCommands(commandEventsByCommandId); 600 if (keyConfigurationEventsByKeyConfigurationId != null) 601 notifyKeyConfigurations(keyConfigurationEventsByKeyConfigurationId); 602 } 603 public void setActiveLocale(String activeLocale) { 604 boolean commandManagerChanged = false; 605 Map commandEventsByCommandId = null; 606 if (!Util.equals(this.activeLocale, activeLocale)) { 607 this.activeLocale = activeLocale; 608 commandManagerChanged = true; 609 calculateKeySequenceBindings(); 610 commandEventsByCommandId = updateCommands(commandsById.keySet()); 611 } 612 if (commandManagerChanged) 613 fireCommandManagerChanged(new CommandManagerEvent(this, false, 614 false, true, false, false, false, false, null, null, null)); 615 if (commandEventsByCommandId != null) 616 notifyCommands(commandEventsByCommandId); 617 } 618 public void setActivePlatform(String activePlatform) { 619 boolean commandManagerChanged = false; 620 Map commandEventsByCommandId = null; 621 if (!Util.equals(this.activePlatform, activePlatform)) { 622 this.activePlatform = activePlatform; 623 commandManagerChanged = true; 624 calculateKeySequenceBindings(); 625 commandEventsByCommandId = updateCommands(commandsById.keySet()); 626 } 627 if (commandManagerChanged) 628 fireCommandManagerChanged(new CommandManagerEvent(this, false, 629 false, false, true, false, false, false, null, null, null)); 630 if (commandEventsByCommandId != null) 631 notifyCommands(commandEventsByCommandId); 632 } 633 public void setHandlersByCommandId(Map handlersByCommandId) { 634 handlersByCommandId = Util.safeCopy(handlersByCommandId, String .class, 635 IHandler.class, false, true); 636 boolean commandManagerChanged = false; 637 Map commandEventsByCommandId = null; 638 if (!Util.equals(handlersByCommandId, this.handlersByCommandId)) { 639 this.handlersByCommandId = handlersByCommandId; 640 commandManagerChanged = true; 641 commandEventsByCommandId = updateCommands(commandsById.keySet()); 642 } 643 if (commandEventsByCommandId != null) 644 notifyCommands(commandEventsByCommandId); 645 } 646 647 private Map updateCategories(Collection categoryIds) { 648 Map categoryEventsByCategoryId = new TreeMap (); 649 for (Iterator iterator = categoryIds.iterator(); iterator.hasNext();) { 650 String categoryId = (String ) iterator.next(); 651 Category category = (Category) categoriesById.get(categoryId); 652 if (category != null) { 653 CategoryEvent categoryEvent = updateCategory(category); 654 if (categoryEvent != null) 655 categoryEventsByCategoryId.put(categoryId, categoryEvent); 656 } 657 } 658 return categoryEventsByCategoryId; 659 } 660 private CategoryEvent updateCategory(Category category) { 661 CategoryDefinition categoryDefinition = (CategoryDefinition) categoryDefinitionsById 662 .get(category.getId()); 663 boolean definedChanged = category 664 .setDefined(categoryDefinition != null); 665 boolean descriptionChanged = category 666 .setDescription(categoryDefinition != null ? categoryDefinition 667 .getDescription() : null); 668 boolean nameChanged = category.setName(categoryDefinition != null 669 ? categoryDefinition.getName() 670 : null); 671 if (definedChanged || descriptionChanged || nameChanged) 672 return new CategoryEvent(category, definedChanged, nameChanged); 673 else 674 return null; 675 } 676 private CommandEvent updateCommand(Command command) { 677 CommandDefinition commandDefinition = (CommandDefinition) commandDefinitionsById 679 .get(command.getId()); 680 boolean categoryIdChanged = command 681 .setCategoryId(commandDefinition != null ? commandDefinition 682 .getCategoryId() : null); 683 boolean definedChanged = command.setDefined(commandDefinition != null); 684 boolean descriptionChanged = command 685 .setDescription(commandDefinition != null ? commandDefinition 686 .getDescription() : null); 687 IHandler handler = (IHandler) handlersByCommandId.get(command.getId()); 688 boolean handlerChanged = command.setHandler(handler); 689 SortedSet keySequenceBindings = (SortedSet ) keySequenceBindingsByCommandId 691 .get(command.getId()); 692 boolean keySequenceBindingsChanged = command 694 .setKeySequenceBindings(keySequenceBindings != null 695 ? new ArrayList (keySequenceBindings) 696 : Collections.EMPTY_LIST); 697 boolean nameChanged = command.setName(commandDefinition != null 698 ? commandDefinition.getName() 699 : null); 700 if (categoryIdChanged || definedChanged 701 || descriptionChanged || keySequenceBindingsChanged 702 || nameChanged) 703 return new CommandEvent(command, false , 704 categoryIdChanged, definedChanged, 705 descriptionChanged, handlerChanged, 706 keySequenceBindingsChanged, nameChanged, null); else 708 return null; 709 } 710 private Map updateCommands(Collection commandIds) { 711 Map commandEventsByCommandId = new TreeMap (); 712 for (Iterator iterator = commandIds.iterator(); iterator.hasNext();) { 713 String commandId = (String ) iterator.next(); 714 Command command = (Command) commandsById.get(commandId); 715 if (command != null) { 716 CommandEvent commandEvent = updateCommand(command); 717 if (commandEvent != null) 718 commandEventsByCommandId.put(commandId, commandEvent); 719 } 720 } 721 return commandEventsByCommandId; 722 } 723 private KeyConfigurationEvent updateKeyConfiguration( 724 KeyConfiguration keyConfiguration) { 725 boolean activeChanged = keyConfiguration.setActive(Util.equals( 726 activeKeyConfigurationId, keyConfiguration.getId())); 727 KeyConfigurationDefinition keyConfigurationDefinition = (KeyConfigurationDefinition) keyConfigurationDefinitionsById 728 .get(keyConfiguration.getId()); 729 boolean definedChanged = keyConfiguration 730 .setDefined(keyConfigurationDefinition != null); 731 boolean descriptionChanged = keyConfiguration 732 .setDescription(keyConfigurationDefinition != null 733 ? keyConfigurationDefinition.getDescription() 734 : null); 735 boolean nameChanged = keyConfiguration 736 .setName(keyConfigurationDefinition != null 737 ? keyConfigurationDefinition.getName() 738 : null); 739 boolean parentIdChanged = keyConfiguration 740 .setParentId(keyConfigurationDefinition != null 741 ? keyConfigurationDefinition.getParentId() 742 : null); 743 if (activeChanged || definedChanged || descriptionChanged 744 || nameChanged || parentIdChanged) 745 return new KeyConfigurationEvent(keyConfiguration, activeChanged, 746 definedChanged, nameChanged, parentIdChanged); 747 else 748 return null; 749 } 750 private Map updateKeyConfigurations(Collection keyConfigurationIds) { 751 Map keyConfigurationEventsByKeyConfigurationId = new TreeMap (); 752 for (Iterator iterator = keyConfigurationIds.iterator(); iterator 753 .hasNext();) { 754 String keyConfigurationId = (String ) iterator.next(); 755 KeyConfiguration keyConfiguration = (KeyConfiguration) keyConfigurationsById 756 .get(keyConfigurationId); 757 if (keyConfiguration != null) { 758 KeyConfigurationEvent keyConfigurationEvent = updateKeyConfiguration(keyConfiguration); 759 if (keyConfigurationEvent != null) 760 keyConfigurationEventsByKeyConfigurationId.put( 761 keyConfigurationId, keyConfigurationEvent); 762 } 763 } 764 return keyConfigurationEventsByKeyConfigurationId; 765 } 766 } 767 | Popular Tags |