KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.SchemeWrapper;
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 CommandManagerWrapper 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         List JavaDoc keyStrokes = keySequence.getKeyStrokes();
78         int size = keyStrokes.size();
79         if (size == 0 || size > 4 || !keySequence.isComplete())
80             return false;
81         return true;
82     }
83
84     /**
85      * The JFace binding machine that provides binding support for this
86      * workbench mutable command manager. This value will never be
87      * <code>null</code>.
88      *
89      * @since 3.1
90      */

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

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

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

127     public CommandManagerWrapper(final BindingManager bindingManager,
128             final CommandManager commandManager,
129             final ContextManager contextManager) {
130         if (contextManager == null) {
131             throw new NullPointerException JavaDoc(
132                     "The context manager cannot be null."); //$NON-NLS-1$
133
}
134         this.bindingManager = bindingManager;
135         this.commandManager = commandManager;
136         this.contextManager = contextManager;
137     }
138
139     public final void addCommandManagerListener(
140             final ICommandManagerListener commandManagerListener) {
141         if (commandManagerListener == null) {
142             throw new NullPointerException JavaDoc("Cannot add a null listener."); //$NON-NLS-1$
143
}
144
145         if (commandManagerListeners == null) {
146             commandManagerListeners = new ArrayList JavaDoc();
147             this.commandManager.addCommandManagerListener(this);
148             this.bindingManager.addBindingManagerListener(this);
149             this.contextManager.addContextManagerListener(this);
150         }
151
152         if (!commandManagerListeners.contains(commandManagerListener)) {
153             commandManagerListeners.add(commandManagerListener);
154         }
155     }
156
157     /*
158      * (non-Javadoc)
159      *
160      * @see org.eclipse.jface.bindings.IBindingManagerListener#bindingManagerChanged(org.eclipse.jface.bindings.BindingManagerEvent)
161      */

162     public final void bindingManagerChanged(final BindingManagerEvent event) {
163         final boolean schemeDefinitionsChanged = event.getScheme() != null;
164         final Set JavaDoc previousSchemes;
165         if (schemeDefinitionsChanged) {
166             previousSchemes = new HashSet JavaDoc();
167             final Scheme scheme = event.getScheme();
168             final Scheme[] definedSchemes = event.getManager()
169                     .getDefinedSchemes();
170             final int definedSchemesCount = definedSchemes.length;
171             for (int i = 0; i < definedSchemesCount; i++) {
172                 final Scheme definedScheme = definedSchemes[0];
173                 if ((definedScheme == scheme) && (event.isSchemeDefined())) {
174                     continue; // skip this one, it was just defined.
175
}
176                 previousSchemes.add(definedSchemes[0].getId());
177             }
178             if (!event.isSchemeDefined()) {
179                 previousSchemes.add(scheme.getId());
180             }
181         } else {
182             previousSchemes = null;
183         }
184
185         fireCommandManagerChanged(new CommandManagerEvent(this, false, event
186                 .isActiveSchemeChanged(), event.isLocaleChanged(), event
187                 .isPlatformChanged(), false, false, schemeDefinitionsChanged,
188                 null, null, previousSchemes));
189     }
190
191     /*
192      * (non-Javadoc)
193      *
194      * @see org.eclipse.commands.ICommandManagerListener#commandManagerChanged(org.eclipse.commands.CommandManagerEvent)
195      */

196     public final void commandManagerChanged(
197             final org.eclipse.core.commands.CommandManagerEvent event) {
198         // Figure out the set of previous category identifiers.
199
final boolean categoryIdsChanged = event.isCategoryChanged();
200         final Set JavaDoc previousCategoryIds;
201         if (categoryIdsChanged) {
202             previousCategoryIds = new HashSet JavaDoc(commandManager
203                     .getDefinedCategoryIds());
204             final String JavaDoc categoryId = event.getCategoryId();
205             if (event.isCategoryDefined()) {
206                 previousCategoryIds.remove(categoryId);
207             } else {
208                 previousCategoryIds.add(categoryId);
209             }
210         } else {
211             previousCategoryIds = null;
212         }
213
214         // Figure out the set of previous command identifiers.
215
final boolean commandIdsChanged = event.isCommandChanged();
216         final Set JavaDoc previousCommandIds;
217         if (commandIdsChanged) {
218             previousCommandIds = new HashSet JavaDoc(commandManager
219                     .getDefinedCommandIds());
220             final String JavaDoc commandId = event.getCommandId();
221             if (event.isCommandDefined()) {
222                 previousCommandIds.remove(commandId);
223             } else {
224                 previousCommandIds.add(commandId);
225             }
226         } else {
227             previousCommandIds = null;
228         }
229
230         fireCommandManagerChanged(new CommandManagerEvent(this, false, false,
231                 false, false, categoryIdsChanged, commandIdsChanged, false,
232                 previousCategoryIds, previousCommandIds, null));
233     }
234
235     public final void contextManagerChanged(final ContextManagerEvent event) {
236         fireCommandManagerChanged(new CommandManagerEvent(this, event
237                 .isActiveContextsChanged(), false, false, false, false, false,
238                 false, null, null, null));
239     }
240
241     private void fireCommandManagerChanged(
242             CommandManagerEvent commandManagerEvent) {
243         if (commandManagerEvent == null)
244             throw new NullPointerException JavaDoc();
245         if (commandManagerListeners != null)
246             for (int i = 0; i < commandManagerListeners.size(); i++)
247                 ((ICommandManagerListener) commandManagerListeners.get(i))
248                         .commandManagerChanged(commandManagerEvent);
249     }
250
251     public Set JavaDoc getActiveContextIds() {
252         return contextManager.getActiveContextIds();
253     }
254
255     public String JavaDoc getActiveKeyConfigurationId() {
256         final Scheme scheme = bindingManager.getActiveScheme();
257         if (scheme != null) {
258             return scheme.getId();
259         }
260
261         /*
262          * TODO This is possibly a breaking change. The id should be non-null,
263          * and presumably, a real scheme id.
264          */

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

292     public Set JavaDoc getDefinedCategoryIds() {
293         return commandManager.getDefinedCategoryIds();
294     }
295
296     public Set JavaDoc getDefinedCommandIds() {
297         return commandManager.getDefinedCommandIds();
298     }
299
300     public Set JavaDoc getDefinedKeyConfigurationIds() {
301         final Set JavaDoc definedIds = new HashSet JavaDoc();
302         final Scheme[] schemes = bindingManager.getDefinedSchemes();
303         for (int i = 0; i < schemes.length; i++) {
304             definedIds.add(schemes[i].getId());
305         }
306         return definedIds;
307     }
308
309     public IKeyConfiguration getKeyConfiguration(String JavaDoc keyConfigurationId) {
310         final Scheme scheme = bindingManager.getScheme(keyConfigurationId);
311         return new SchemeWrapper(scheme, bindingManager);
312     }
313
314     public Map JavaDoc getPartialMatches(KeySequence keySequence) {
315         try {
316             final org.eclipse.jface.bindings.keys.KeySequence sequence = org.eclipse.jface.bindings.keys.KeySequence
317                     .getInstance(keySequence.toString());
318             final Map JavaDoc partialMatches = bindingManager
319                     .getPartialMatches(sequence);
320             final Map JavaDoc returnValue = new HashMap JavaDoc();
321             final Iterator JavaDoc matchItr = partialMatches.entrySet().iterator();
322             while (matchItr.hasNext()) {
323                 final Map.Entry JavaDoc entry = (Map.Entry JavaDoc) matchItr.next();
324                 final TriggerSequence trigger = (TriggerSequence) entry
325                         .getKey();
326                 if (trigger instanceof org.eclipse.jface.bindings.keys.KeySequence) {
327                     final org.eclipse.jface.bindings.keys.KeySequence triggerKey = (org.eclipse.jface.bindings.keys.KeySequence) trigger;
328                     returnValue.put(KeySequence.getInstance(triggerKey
329                             .toString()), entry.getValue());
330                 }
331             }
332             return returnValue;
333         } catch (final ParseException e) {
334             return new HashMap JavaDoc();
335         } catch (final org.eclipse.ui.keys.ParseException e) {
336             return new HashMap JavaDoc();
337         }
338     }
339
340     public String JavaDoc getPerfectMatch(KeySequence keySequence) {
341         try {
342             final org.eclipse.jface.bindings.keys.KeySequence sequence = org.eclipse.jface.bindings.keys.KeySequence
343                     .getInstance(keySequence.toString());
344             final Binding binding = bindingManager.getPerfectMatch(sequence);
345             if (binding == null) {
346                 return null;
347             }
348             
349             return binding.getParameterizedCommand().getId();
350             
351         } catch (final ParseException e) {
352             return null;
353         }
354     }
355
356     public boolean isPartialMatch(KeySequence keySequence) {
357         try {
358             final org.eclipse.jface.bindings.keys.KeySequence sequence = org.eclipse.jface.bindings.keys.KeySequence
359                     .getInstance(keySequence.toString());
360             return bindingManager.isPartialMatch(sequence);
361         } catch (final ParseException e) {
362             return false;
363         }
364     }
365
366     public boolean isPerfectMatch(KeySequence keySequence) {
367         try {
368             final org.eclipse.jface.bindings.keys.KeySequence sequence = org.eclipse.jface.bindings.keys.KeySequence
369                     .getInstance(keySequence.toString());
370             return bindingManager.isPerfectMatch(sequence);
371         } catch (final ParseException e) {
372             return false;
373         }
374     }
375
376     public void removeCommandManagerListener(
377             ICommandManagerListener commandManagerListener) {
378         if (commandManagerListener == null) {
379             throw new NullPointerException JavaDoc("Cannot remove a null listener"); //$NON-NLS-1$
380
}
381
382         if (commandManagerListeners != null) {
383             commandManagerListeners.remove(commandManagerListener);
384             if (commandManagerListeners.isEmpty()) {
385                 commandManagerListeners = null;
386                 this.commandManager.removeCommandManagerListener(this);
387                 this.bindingManager.removeBindingManagerListener(this);
388                 this.contextManager.removeContextManagerListener(this);
389             }
390         }
391     }
392
393     /**
394      * Updates the handlers for a block of commands all at once.
395      *
396      * @param handlersByCommandId
397      * The map of command identifier (<code>String</code>) to
398      * handler (<code>IHandler</code>).
399      */

400     public final void setHandlersByCommandId(final Map JavaDoc handlersByCommandId) {
401         // Wrap legacy handlers so they can be passed to the new API.
402
final Iterator JavaDoc entryItr = handlersByCommandId.entrySet().iterator();
403         while (entryItr.hasNext()) {
404             final Map.Entry JavaDoc entry = (Map.Entry JavaDoc) entryItr.next();
405             final Object JavaDoc handler = entry.getValue();
406             if (handler instanceof org.eclipse.ui.commands.IHandler) {
407                 final String JavaDoc commandId = (String JavaDoc) entry.getKey();
408                 handlersByCommandId.put(commandId, new LegacyHandlerWrapper(
409                         (org.eclipse.ui.commands.IHandler) handler));
410             }
411         }
412
413         commandManager.setHandlersByCommandId(handlersByCommandId);
414     }
415 }
416
Popular Tags