KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 package org.eclipse.ui.internal.commands;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
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 /**
45  * Provides support for the old <code>ICommandManager</code> interface.
46  *
47  * @since 3.1
48  */

49 public final class CommandManagerLegacyWrapper implements ICommandManager,
50         org.eclipse.core.commands.ICommandManagerListener,
51         IBindingManagerListener, IContextManagerListener {
52
53     /**
54      * Whether commands should print out information about which handlers are
55      * being executed. Change this value if you want console output on command
56      * execution.
57      */

58     public static boolean DEBUG_COMMAND_EXECUTION = false;
59
60     /**
61      * Whether commands should print out information about handler changes.
62      * Change this value if you want console output when commands change
63      * handlers.
64      */

65     public static boolean DEBUG_HANDLERS = false;
66
67     /**
68      * Which command should print out debugging information. Change this value
69      * if you want to only here when a command with a particular identifier
70      * changes its handler.
71      */

72     public static String JavaDoc DEBUG_HANDLERS_COMMAND_ID = null;
73
74     static boolean validateKeySequence(KeySequence keySequence) {
75         if (keySequence == null) {
76             return false;
77         }
78         List JavaDoc 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     /**
87      * The JFace binding machine that provides binding support for this
88      * workbench mutable command manager. This value will never be
89      * <code>null</code>.
90      *
91      * @since 3.1
92      */

93     private final BindingManager bindingManager;
94
95     /**
96      * The command manager that provides functionality for this workbench
97      * command manager. This value will never be <code>null</code>.
98      *
99      * @since 3.1
100      */

101     private final CommandManager commandManager;
102
103     private List JavaDoc commandManagerListeners;
104
105     /**
106      * The context manager that provides functionality for this workbench
107      * command manager. This value will never be <code>null</code>.
108      *
109      * @since 3.1
110      */

111     private final ContextManager contextManager;
112
113     /**
114      * Constructs a new instance of <code>MutableCommandManager</code>. The
115      * binding manager and command manager providing support for this manager
116      * are constructed at this time.
117      *
118      * @param bindingManager
119      * The binding manager providing support for the command manager;
120      * must not be <code>null</code>.
121      * @param commandManager
122      * The command manager providing support for this command
123      * manager; must not be <code>null</code>.
124      * @param contextManager
125      * The context manager to provide context support to this
126      * manager. This value must not be <code>null</code>.
127      *
128      */

129     public CommandManagerLegacyWrapper(final BindingManager bindingManager,
130             final CommandManager commandManager,
131             final ContextManager contextManager) {
132         if (contextManager == null) {
133             throw new NullPointerException JavaDoc(
134                     "The context manager cannot be null."); //$NON-NLS-1$
135
}
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 JavaDoc("Cannot add a null listener."); //$NON-NLS-1$
145
}
146
147         if (commandManagerListeners == null) {
148             commandManagerListeners = new ArrayList JavaDoc();
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     /*
160      * (non-Javadoc)
161      *
162      * @see org.eclipse.jface.bindings.IBindingManagerListener#bindingManagerChanged(org.eclipse.jface.bindings.BindingManagerEvent)
163      */

164     public final void bindingManagerChanged(final BindingManagerEvent event) {
165         final boolean schemeDefinitionsChanged = event.getScheme() != null;
166         final Set JavaDoc previousSchemes;
167         if (schemeDefinitionsChanged) {
168             previousSchemes = new HashSet JavaDoc();
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; // skip this one, it was just defined.
177
}
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     /*
194      * (non-Javadoc)
195      *
196      * @see org.eclipse.commands.ICommandManagerListener#commandManagerChanged(org.eclipse.commands.CommandManagerEvent)
197      */

198     public final void commandManagerChanged(
199             final org.eclipse.core.commands.CommandManagerEvent event) {
200         // Figure out the set of previous category identifiers.
201
final boolean categoryIdsChanged = event.isCategoryChanged();
202         final Set JavaDoc previousCategoryIds;
203         if (categoryIdsChanged) {
204             previousCategoryIds = new HashSet JavaDoc(commandManager
205                     .getDefinedCategoryIds());
206             final String JavaDoc 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         // Figure out the set of previous command identifiers.
217
final boolean commandIdsChanged = event.isCommandChanged();
218         final Set JavaDoc previousCommandIds;
219         if (commandIdsChanged) {
220             previousCommandIds = new HashSet JavaDoc(commandManager
221                     .getDefinedCommandIds());
222             final String JavaDoc 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 JavaDoc();
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 JavaDoc getActiveContextIds() {
257         return contextManager.getActiveContextIds();
258     }
259
260     public String JavaDoc getActiveKeyConfigurationId() {
261         final Scheme scheme = bindingManager.getActiveScheme();
262         if (scheme != null) {
263             return scheme.getId();
264         }
265
266         /*
267          * TODO This is possibly a breaking change. The id should be non-null,
268          * and presumably, a real scheme id.
269          */

270         return Util.ZERO_LENGTH_STRING;
271     }
272
273     public String JavaDoc getActiveLocale() {
274         return bindingManager.getLocale();
275     }
276
277     public String JavaDoc getActivePlatform() {
278         return bindingManager.getPlatform();
279     }
280
281     public ICategory getCategory(String JavaDoc categoryId) {
282         // TODO Provide access to the categories.
283
// return new CategoryWrapper(commandManager.getCategory(categoryId));
284
return null;
285     }
286
287     public ICommand getCommand(String JavaDoc commandId) {
288         final Command command = commandManager.getCommand(commandId);
289         return new CommandLegacyWrapper(command, bindingManager);
290     }
291
292     /*
293      * (non-Javadoc)
294      *
295      * @see org.eclipse.ui.commands.ICommandManager#getDefinedCategoryIds()
296      */

297     public Set JavaDoc getDefinedCategoryIds() {
298         return commandManager.getDefinedCategoryIds();
299     }
300
301     public Set JavaDoc getDefinedCommandIds() {
302         return commandManager.getDefinedCommandIds();
303     }
304
305     public Set JavaDoc getDefinedKeyConfigurationIds() {
306         final Set JavaDoc definedIds = new HashSet JavaDoc();
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 JavaDoc keyConfigurationId) {
315         final Scheme scheme = bindingManager.getScheme(keyConfigurationId);
316         return new SchemeLegacyWrapper(scheme, bindingManager);
317     }
318
319     public Map JavaDoc 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 JavaDoc partialMatches = bindingManager
324                     .getPartialMatches(sequence);
325             final Map JavaDoc returnValue = new HashMap JavaDoc();
326             final Iterator JavaDoc matchItr = partialMatches.entrySet().iterator();
327             while (matchItr.hasNext()) {
328                 final Map.Entry JavaDoc entry = (Map.Entry JavaDoc) 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 JavaDoc();
340         } catch (final org.eclipse.ui.keys.ParseException e) {
341             return new HashMap JavaDoc();
342         }
343     }
344
345     public String JavaDoc 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 JavaDoc("Cannot remove a null listener"); //$NON-NLS-1$
385
}
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     /**
399      * Updates the handlers for a block of commands all at once.
400      *
401      * @param handlersByCommandId
402      * The map of command identifier (<code>String</code>) to
403      * handler (<code>IHandler</code>).
404      */

405     public final void setHandlersByCommandId(final Map JavaDoc handlersByCommandId) {
406         // Wrap legacy handlers so they can be passed to the new API.
407
final Iterator JavaDoc entryItr = handlersByCommandId.entrySet().iterator();
408         while (entryItr.hasNext()) {
409             final Map.Entry JavaDoc entry = (Map.Entry JavaDoc) entryItr.next();
410             final Object JavaDoc handler = entry.getValue();
411             if (handler instanceof org.eclipse.ui.commands.IHandler) {
412                 final String JavaDoc commandId = (String JavaDoc) 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