KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > commands > MutableCommandManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.commands;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Locale JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.SortedSet JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24 import java.util.TreeMap JavaDoc;
25 import java.util.TreeSet JavaDoc;
26 import java.util.WeakHashMap JavaDoc;
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     /**
46      * Whether commands should print out information about which handlers are
47      * being executed. Change this value if you want console output on command
48      * execution.
49      */

50     public static boolean DEBUG_COMMAND_EXECUTION = false;
51     
52     /**
53      * Whether commands should print out information about handler changes.
54      * Change this value if you want console output when commands change
55      * handlers.
56      */

57     public static boolean DEBUG_HANDLERS = false;
58
59     /**
60      * Which command should print out debugging information. Change this value
61      * if you want to only here when a command with a particular identifier
62      * changes its handler.
63      */

64     public static String JavaDoc DEBUG_HANDLERS_COMMAND_ID = null;
65
66     public final static String JavaDoc SEPARATOR = "_"; //$NON-NLS-1$
67

68     static String JavaDoc[] extend(String JavaDoc[] strings) {
69         String JavaDoc[] strings2 = new String JavaDoc[strings.length + 1];
70         System.arraycopy(strings, 0, strings2, 0, strings.length);
71         return strings2;
72     }
73     static String JavaDoc[] getPath(String JavaDoc string, String JavaDoc separator) {
74         if (string == null || separator == null)
75             return new String JavaDoc[0];
76         List JavaDoc strings = new ArrayList JavaDoc();
77         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
78         string = string.trim();
79         if (string.length() > 0) {
80             StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(string,
81                     separator);
82             while (stringTokenizer.hasMoreElements()) {
83                 if (stringBuffer.length() > 0)
84                     stringBuffer.append(separator);
85                 stringBuffer.append(((String JavaDoc) 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 JavaDoc[]) strings.toArray(new String JavaDoc[strings.size()]);
93     }
94     static boolean isKeyConfigurationDefinitionChildOf(String JavaDoc ancestor,
95             String JavaDoc id, Map JavaDoc keyConfigurationDefinitionsById) {
96         Collection JavaDoc visited = new HashSet JavaDoc();
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 JavaDoc 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 JavaDoc keySequenceBindingDefinitions) {
120         Iterator JavaDoc iterator = keySequenceBindingDefinitions.iterator();
121         while (iterator.hasNext()) {
122             KeySequenceBindingDefinition keySequenceBindingDefinition = (KeySequenceBindingDefinition) iterator
123                     .next();
124             String JavaDoc 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 JavaDoc activeContextIds = new HashMap JavaDoc();
134     // TODO does this have any use anymore?
135
private String JavaDoc activeKeyConfigurationId = null;
136     private String JavaDoc activeLocale = null;
137     private String JavaDoc activePlatform = null;
138     private Map JavaDoc categoriesById = new WeakHashMap JavaDoc();
139     private Set JavaDoc categoriesWithListeners = new HashSet JavaDoc();
140     private Map JavaDoc categoryDefinitionsById = new HashMap JavaDoc();
141     private Map JavaDoc commandDefinitionsById = new HashMap JavaDoc();
142     private List JavaDoc commandManagerListeners;
143     private ICommandRegistry commandRegistry;
144     private Map JavaDoc commandsById = new WeakHashMap JavaDoc();
145     private Set JavaDoc commandsWithListeners = new HashSet JavaDoc();
146     private Set JavaDoc definedCategoryIds = new HashSet JavaDoc();
147     private Set JavaDoc definedCommandIds = new HashSet JavaDoc();
148     private Set JavaDoc definedHandlers = new HashSet JavaDoc();
149     private Set JavaDoc definedKeyConfigurationIds = new HashSet JavaDoc();
150     // TODO review begin
151
private Map JavaDoc handlersByCommandId = new HashMap JavaDoc();
152     private Map JavaDoc keyConfigurationDefinitionsById = new HashMap JavaDoc();
153     private Map JavaDoc keyConfigurationsById = new WeakHashMap JavaDoc();
154     private Set JavaDoc keyConfigurationsWithListeners = new HashSet JavaDoc();
155     private KeySequenceBindingMachine keySequenceBindingMachine = new KeySequenceBindingMachine();
156     private Map JavaDoc keySequenceBindingsByCommandId = new HashMap JavaDoc();
157     private IMutableCommandRegistry mutableCommandRegistry;
158     // TODO review end
159
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 JavaDoc();
168         this.commandRegistry = commandRegistry;
169         this.mutableCommandRegistry = mutableCommandRegistry;
170         String JavaDoc systemLocale = Locale.getDefault().toString();
171         activeLocale = systemLocale != null
172                 ? systemLocale
173                 : Util.ZERO_LENGTH_STRING;
174         String JavaDoc 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 JavaDoc();
198         if (commandManagerListeners == null)
199             commandManagerListeners = new ArrayList JavaDoc();
200         if (!commandManagerListeners.contains(commandManagerListener))
201             commandManagerListeners.add(commandManagerListener);
202     }
203     
204     /**
205      * <p>
206      * Calculates the active key sequence bindings for this command manager. The
207      * active key sequence bindings are a function of the active contexts, the
208      * active key configurations, the active locales and the active platforms
209      * within the system. To ensure that the contexts are considered from the
210      * most specific to the least specific, sorting is applied to the context
211      * identifier array. This sorting takes into account the depth of the
212      * context within the context tree.
213      * </p>
214      * <p>
215      * When this method completes, the
216      * <code>keySequenceBindingsByCommandId</code> will represent an accurate
217      * an update-to-date mapping of key sequence bindings. The key sequence
218      * binding machine (i.e., a utility for computing key sequence bindings)
219      * will also be up-to-date.
220      * </p>
221      */

222     private void calculateKeySequenceBindings() {
223         // Get the current state of the system.
224
final String JavaDoc[] activeKeyConfigurationIds = extend(getKeyConfigurationIds(activeKeyConfigurationId));
225         final String JavaDoc[] activeLocales = extend(getPath(activeLocale, SEPARATOR));
226         final String JavaDoc[] activePlatforms = extend(getPath(activePlatform,
227                 SEPARATOR));
228
229         // Transfer this information to the key sequence binding machine.
230
keySequenceBindingMachine.setActiveContextIds(activeContextIds);
231         keySequenceBindingMachine
232                 .setActiveKeyConfigurationIds(activeKeyConfigurationIds);
233         keySequenceBindingMachine.setActiveLocales(activeLocales);
234         keySequenceBindingMachine.setActivePlatforms(activePlatforms);
235
236         // Allow the machine to compute our key sequences for us.
237
keySequenceBindingsByCommandId = keySequenceBindingMachine
238                 .getKeySequenceBindingsByCommandId();
239     }
240     
241     private void fireCommandManagerChanged(
242             CommandManagerEvent commandManagerEvent) {
243         if (commandManagerEvent == null)
244             throw new NullPointerException JavaDoc();
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 JavaDoc getActiveContextIds() {
251         return activeContextIds.keySet();
252     }
253     public String JavaDoc getActiveKeyConfigurationId() {
254         return activeKeyConfigurationId;
255     }
256     public String JavaDoc getActiveLocale() {
257         return activeLocale;
258     }
259     public String JavaDoc getActivePlatform() {
260         return activePlatform;
261     }
262     public ICategory getCategory(String JavaDoc categoryId) {
263         if (categoryId == null)
264             throw new NullPointerException JavaDoc();
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 JavaDoc commandId) {
274         if (commandId == null)
275             throw new NullPointerException JavaDoc();
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     // TODO public only for test cases. remove?
285
public ICommandRegistry getCommandRegistry() {
286         return commandRegistry;
287     }
288     public Set JavaDoc getDefinedCategoryIds() {
289         return Collections.unmodifiableSet(definedCategoryIds);
290     }
291     public Set JavaDoc getDefinedCommandIds() {
292         return Collections.unmodifiableSet(definedCommandIds);
293     }
294     /**
295      * An accessor for those handlers that have been defined in XML.
296      *
297      * @return The handlers defined in XML; never <code>null</code>, but may
298      * be empty.
299      */

300     public Set JavaDoc getDefinedHandlers() {
301         return Collections.unmodifiableSet(definedHandlers);
302     }
303     public Set JavaDoc getDefinedKeyConfigurationIds() {
304         return Collections.unmodifiableSet(definedKeyConfigurationIds);
305     }
306     public Map JavaDoc getHandlersByCommandId() {
307         return Collections.unmodifiableMap(handlersByCommandId);
308     }
309     public IKeyConfiguration getKeyConfiguration(String JavaDoc keyConfigurationId) {
310         if (keyConfigurationId == null)
311             throw new NullPointerException JavaDoc();
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 JavaDoc[] getKeyConfigurationIds(String JavaDoc keyConfigurationId) {
323         List JavaDoc strings = new ArrayList JavaDoc();
324         while (keyConfigurationId != null) {
325             strings.add(keyConfigurationId);
326             try {
327                 keyConfigurationId = getKeyConfiguration(keyConfigurationId)
328                         .getParentId();
329             } catch (NotDefinedException eNotDefined) {
330                 return new String JavaDoc[0];
331             }
332         }
333         return (String JavaDoc[]) strings.toArray(new String JavaDoc[strings.size()]);
334     }
335     IMutableCommandRegistry getMutableCommandRegistry() {
336         return mutableCommandRegistry;
337     }
338     public Map JavaDoc getPartialMatches(KeySequence keySequence) {
339         Map JavaDoc map = new HashMap JavaDoc();
340         for (Iterator JavaDoc iterator = keySequenceBindingMachine
341                 .getMatchesByKeySequence().entrySet().iterator(); iterator
342                 .hasNext();) {
343             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) 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 JavaDoc 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 JavaDoc iterator = keySequenceBindingMachine
358                 .getMatchesByKeySequence().entrySet().iterator(); iterator
359                 .hasNext();) {
360             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) 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 JavaDoc categoryEventsByCategoryId) {
371         for (Iterator JavaDoc iterator = categoryEventsByCategoryId.entrySet()
372                 .iterator(); iterator.hasNext();) {
373             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
374             String JavaDoc categoryId = (String JavaDoc) 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 JavaDoc commandEventsByCommandId) {
382         for (Iterator JavaDoc iterator = commandEventsByCommandId.entrySet().iterator(); iterator
383                 .hasNext();) {
384             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
385             String JavaDoc commandId = (String JavaDoc) 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 JavaDoc keyConfigurationEventsByKeyConfigurationId) {
394         for (Iterator JavaDoc iterator = keyConfigurationEventsByKeyConfigurationId
395                 .entrySet().iterator(); iterator.hasNext();) {
396             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
397             String JavaDoc keyConfigurationId = (String JavaDoc) 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 JavaDoc categoryDefinitions = new ArrayList JavaDoc();
409         categoryDefinitions.addAll(commandRegistry.getCategoryDefinitions());
410         categoryDefinitions.addAll(mutableCommandRegistry
411                 .getCategoryDefinitions());
412         Map JavaDoc categoryDefinitionsById = new HashMap JavaDoc(CategoryDefinition
413                 .categoryDefinitionsById(categoryDefinitions, false));
414         definedHandlers.addAll(commandRegistry.getHandlers());
415         for (Iterator JavaDoc iterator = categoryDefinitionsById.values().iterator(); iterator
416                 .hasNext();) {
417             CategoryDefinition categoryDefinition = (CategoryDefinition) iterator
418                     .next();
419             String JavaDoc name = categoryDefinition.getName();
420             if (name == null || name.length() == 0)
421                 iterator.remove();
422         }
423         Collection JavaDoc commandDefinitions = new ArrayList JavaDoc();
424         commandDefinitions.addAll(commandRegistry.getCommandDefinitions());
425         commandDefinitions.addAll(mutableCommandRegistry
426                 .getCommandDefinitions());
427         Map JavaDoc commandDefinitionsById = new HashMap JavaDoc(CommandDefinition
428                 .commandDefinitionsById(commandDefinitions, false));
429         for (Iterator JavaDoc iterator = commandDefinitionsById.values().iterator(); iterator
430                 .hasNext();) {
431             CommandDefinition commandDefinition = (CommandDefinition) iterator
432                     .next();
433             String JavaDoc name = commandDefinition.getName();
434             if (name == null || name.length() == 0)
435                 iterator.remove();
436         }
437         Collection JavaDoc keyConfigurationDefinitions = new ArrayList JavaDoc();
438         keyConfigurationDefinitions.addAll(commandRegistry
439                 .getKeyConfigurationDefinitions());
440         keyConfigurationDefinitions.addAll(mutableCommandRegistry
441                 .getKeyConfigurationDefinitions());
442         Map JavaDoc keyConfigurationDefinitionsById = new HashMap JavaDoc(
443                 KeyConfigurationDefinition.keyConfigurationDefinitionsById(
444                         keyConfigurationDefinitions, false));
445         for (Iterator JavaDoc iterator = keyConfigurationDefinitionsById.values()
446                 .iterator(); iterator.hasNext();) {
447             KeyConfigurationDefinition keyConfigurationDefinition = (KeyConfigurationDefinition) iterator
448                     .next();
449             String JavaDoc name = keyConfigurationDefinition.getName();
450             if (name == null || name.length() == 0)
451                 iterator.remove();
452         }
453         // TODO should we really filter these out?
454
for (Iterator JavaDoc iterator = commandDefinitionsById.values().iterator(); iterator
455                 .hasNext();) {
456             CommandDefinition commandDefinition = (CommandDefinition) iterator
457                     .next();
458             String JavaDoc categoryId = commandDefinition.getCategoryId();
459             if (categoryId != null
460                     && !categoryDefinitionsById.containsKey(categoryId))
461                 iterator.remove();
462         }
463         for (Iterator JavaDoc iterator = keyConfigurationDefinitionsById.keySet()
464                 .iterator(); iterator.hasNext();)
465             if (!isKeyConfigurationDefinitionChildOf(null, (String JavaDoc) iterator
466                     .next(), keyConfigurationDefinitionsById))
467                 iterator.remove();
468         // TODO should the active key configuration change if a call to
469
// setContextKeyConfigurationId was explicitly made already?
470
List JavaDoc activeKeyConfigurationDefinitions = new ArrayList JavaDoc();
471         activeKeyConfigurationDefinitions.addAll(commandRegistry
472                 .getActiveKeyConfigurationDefinitions());
473         activeKeyConfigurationDefinitions.addAll(mutableCommandRegistry
474                 .getActiveKeyConfigurationDefinitions());
475         String JavaDoc 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         // TODO - if null, pick the first key configuration in sorted order by
487
// id?
488
if (activeKeyConfigurationId == null
489                 && !keyConfigurationDefinitionsById.isEmpty()) {
490             SortedSet JavaDoc sortedSet = new TreeSet JavaDoc(keyConfigurationDefinitionsById
491                     .keySet());
492             activeKeyConfigurationId = (String JavaDoc) 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 JavaDoc definedCategoryIds = new HashSet JavaDoc(categoryDefinitionsById.keySet());
505         Set JavaDoc 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 JavaDoc definedCommandIds = new HashSet JavaDoc(commandDefinitionsById.keySet());
513         Set JavaDoc 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 JavaDoc definedKeyConfigurationIds = new HashSet JavaDoc(
521                 keyConfigurationDefinitionsById.keySet());
522         Set JavaDoc previouslyDefinedKeyConfigurationIds = null;
523         if (!definedKeyConfigurationIds.equals(this.definedKeyConfigurationIds)) {
524             previouslyDefinedKeyConfigurationIds = this.definedKeyConfigurationIds;
525             this.definedKeyConfigurationIds = definedKeyConfigurationIds;
526             definedKeyConfigurationIdsChanged = true;
527         }
528         List JavaDoc commandRegistryKeySequenceBindingDefinitions = new ArrayList JavaDoc(
529                 commandRegistry.getKeySequenceBindingDefinitions());
530         validateKeySequenceBindingDefinitions(commandRegistryKeySequenceBindingDefinitions);
531         List JavaDoc mutableCommandRegistryKeySequenceBindingDefinitions = new ArrayList JavaDoc(
532                 mutableCommandRegistry.getKeySequenceBindingDefinitions());
533         validateKeySequenceBindingDefinitions(mutableCommandRegistryKeySequenceBindingDefinitions);
534         keySequenceBindingMachine
535                 .setKeySequenceBindings0(mutableCommandRegistryKeySequenceBindingDefinitions);
536         keySequenceBindingMachine
537                 .setKeySequenceBindings1(commandRegistryKeySequenceBindingDefinitions);
538         calculateKeySequenceBindings();
539         Map JavaDoc categoryEventsByCategoryId = updateCategories(categoriesById
540                 .keySet());
541         Map JavaDoc commandEventsByCommandId = updateCommands(commandsById.keySet());
542         Map JavaDoc 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 JavaDoc();
564         if (commandManagerListeners != null)
565             commandManagerListeners.remove(commandManagerListener);
566     }
567     public void setActiveContextIds(Map JavaDoc activeContextIds) {
568         boolean commandManagerChanged = false;
569         Map JavaDoc 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 JavaDoc activeKeyConfigurationId) {
583         boolean commandManagerChanged = false;
584         Map JavaDoc commandEventsByCommandId = null;
585         Map JavaDoc 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 JavaDoc activeLocale) {
604         boolean commandManagerChanged = false;
605         Map JavaDoc 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 JavaDoc activePlatform) {
619         boolean commandManagerChanged = false;
620         Map JavaDoc 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 JavaDoc handlersByCommandId) {
634         handlersByCommandId = Util.safeCopy(handlersByCommandId, String JavaDoc.class,
635                 IHandler.class, false, true);
636         boolean commandManagerChanged = false;
637         Map JavaDoc 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 JavaDoc updateCategories(Collection JavaDoc categoryIds) {
648         Map JavaDoc categoryEventsByCategoryId = new TreeMap JavaDoc();
649         for (Iterator JavaDoc iterator = categoryIds.iterator(); iterator.hasNext();) {
650             String JavaDoc categoryId = (String JavaDoc) 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         // TODO list to sortedset in api?
678
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         // TODO list to sortedset in api?
690
SortedSet JavaDoc keySequenceBindings = (SortedSet JavaDoc) keySequenceBindingsByCommandId
691                 .get(command.getId());
692         // TODO list to sortedset in api?
693
boolean keySequenceBindingsChanged = command
694                 .setKeySequenceBindings(keySequenceBindings != null
695                         ? new ArrayList JavaDoc(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 /* TODO */,
704                     categoryIdChanged, definedChanged,
705                     descriptionChanged, handlerChanged,
706                     keySequenceBindingsChanged, nameChanged, null); // TODO
707
else
708             return null;
709     }
710     private Map JavaDoc updateCommands(Collection JavaDoc commandIds) {
711         Map JavaDoc commandEventsByCommandId = new TreeMap JavaDoc();
712         for (Iterator JavaDoc iterator = commandIds.iterator(); iterator.hasNext();) {
713             String JavaDoc commandId = (String JavaDoc) 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 JavaDoc updateKeyConfigurations(Collection JavaDoc keyConfigurationIds) {
751         Map JavaDoc keyConfigurationEventsByKeyConfigurationId = new TreeMap JavaDoc();
752         for (Iterator JavaDoc iterator = keyConfigurationIds.iterator(); iterator
753                 .hasNext();) {
754             String JavaDoc keyConfigurationId = (String JavaDoc) 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