KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > commands > ws > CommandCallback


1 /*******************************************************************************
2  * Copyright (c) 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.ws;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.jface.action.IAction;
18 import org.eclipse.jface.action.ExternalActionManager.ICallback;
19 import org.eclipse.jface.util.IPropertyChangeListener;
20 import org.eclipse.jface.util.PropertyChangeEvent;
21 import org.eclipse.ui.IWorkbench;
22 import org.eclipse.ui.commands.CommandEvent;
23 import org.eclipse.ui.commands.ICommand;
24 import org.eclipse.ui.commands.ICommandListener;
25 import org.eclipse.ui.commands.ICommandManager;
26 import org.eclipse.ui.commands.IKeySequenceBinding;
27 import org.eclipse.ui.commands.NotDefinedException;
28 import org.eclipse.ui.keys.KeySequence;
29 import org.eclipse.ui.keys.KeyStroke;
30 import org.eclipse.ui.keys.SWTKeySupport;
31
32 /**
33  * @since 3.0
34  */

35 public final class CommandCallback implements ICallback {
36
37     /**
38      * The list of listeners that have registered for property change
39      * notification. This is a map os <code>IPropertyChangeListener</code> to
40      * <code>ICommandListener</code>.
41      */

42     private final Map JavaDoc registeredListeners = new HashMap JavaDoc();
43
44     /**
45      * The workbench to query about command and context information. This value
46      * should never be <code>null</code>.
47      */

48     private final IWorkbench workbench;
49
50     /**
51      * Constructs a new instance of <code>CommandCallback</code> with the
52      * workbench it should be using.
53      *
54      * @param workbenchToUse
55      * The workbench that should be used for resolving command
56      * information; must not be <code>null</code>.
57      */

58     public CommandCallback(final IWorkbench workbenchToUse) {
59         workbench = workbenchToUse;
60     }
61
62     /**
63      * @see org.eclipse.jface.action.ExternalActionManager.ICallback#addPropertyChangeListener(String,
64      * IPropertyChangeListener)
65      */

66     public void addPropertyChangeListener(final String JavaDoc commandId,
67             final IPropertyChangeListener listener) {
68         final ICommand command = workbench.getCommandSupport()
69                 .getCommandManager().getCommand(commandId);
70         final ICommandListener commandListener = new ICommandListener() {
71
72             /*
73              * (non-Javadoc)
74              *
75              * @see org.eclipse.ui.commands.ICommandListener#commandChanged(org.eclipse.ui.commands.CommandEvent)
76              */

77             public void commandChanged(CommandEvent commandEvent) {
78                 // Check if the text has changed.
79
if (commandEvent.hasNameChanged()
80                         || commandEvent.haveKeySequenceBindingsChanged()) {
81                     PropertyChangeEvent event;
82                     try {
83                         event = new PropertyChangeEvent(command, IAction.TEXT,
84                                 null /* TODO Don't have enough information */,
85                                 command.getName());
86                     } catch (final NotDefinedException e) {
87                         event = new PropertyChangeEvent(command, IAction.TEXT,
88                                 null /* TODO Don't have enough information */,
89                                 null /* Couldn't get the name */);
90                     }
91                     listener.propertyChange(event);
92                 }
93
94                 // TODO Add enabled property change.
95
}
96         };
97
98         command.addCommandListener(commandListener);
99         registeredListeners.put(listener, commandListener);
100     }
101
102     /**
103      * @see org.eclipse.jface.action.ExternalActionManager.ICallback#getAccelerator(String)
104      */

105     public final Integer JavaDoc getAccelerator(final String JavaDoc commandId) {
106         final ICommand command = workbench.getCommandSupport()
107                 .getCommandManager().getCommand(commandId);
108         Integer JavaDoc accelerator = null;
109
110         if (command.isDefined()) {
111             List JavaDoc keySequenceBindings = command.getKeySequenceBindings();
112             final int size = keySequenceBindings.size();
113
114             for (int i = 0; i < size; i++) {
115                 IKeySequenceBinding keySequenceBinding = (IKeySequenceBinding) keySequenceBindings
116                         .get(i);
117                 List JavaDoc keyStrokes = keySequenceBinding.getKeySequence()
118                         .getKeyStrokes();
119
120                 if (keyStrokes.size() == 1) {
121                     KeyStroke keyStroke = (KeyStroke) keyStrokes.get(0);
122                     accelerator = new Integer JavaDoc(SWTKeySupport
123                             .convertKeyStrokeToAccelerator(keyStroke));
124                     break;
125                 }
126             }
127         }
128
129         return accelerator;
130     }
131
132     /**
133      * @see org.eclipse.jface.action.ExternalActionManager.ICallback#getAcceleratorText(String)
134      */

135     public final String JavaDoc getAcceleratorText(final String JavaDoc commandId) {
136         final ICommand command = workbench.getCommandSupport()
137                 .getCommandManager().getCommand(commandId);
138         String JavaDoc acceleratorText = null;
139
140         if (command.isDefined()) {
141             List JavaDoc keySequenceBindings = command.getKeySequenceBindings();
142
143             if (!keySequenceBindings.isEmpty()) {
144                 IKeySequenceBinding keySequenceBinding = (IKeySequenceBinding) keySequenceBindings
145                         .get(0);
146                 acceleratorText = keySequenceBinding.getKeySequence().format();
147             }
148         }
149
150         return acceleratorText;
151     }
152
153     /**
154      * @see org.eclipse.jface.action.ExternalActionManager.ICallback#isAcceleratorInUse(int)
155      */

156     public boolean isAcceleratorInUse(int accelerator) {
157         final KeySequence keySequence = KeySequence.getInstance(SWTKeySupport
158                 .convertAcceleratorToKeyStroke(accelerator));
159         final ICommandManager commandManager = workbench.getCommandSupport()
160                 .getCommandManager();
161         return commandManager.isPerfectMatch(keySequence)
162                 || commandManager.isPartialMatch(keySequence);
163     }
164
165     /**
166      * @see org.eclipse.jface.action.ExternalActionManager.ICallback#isActive(String)
167      */

168     public final boolean isActive(final String JavaDoc commandId) {
169         if (commandId != null) {
170             final ICommand command = workbench.getCommandSupport()
171                     .getCommandManager().getCommand(commandId);
172
173             if (command != null)
174                     return command.isDefined()
175                             && workbench.getActivitySupport()
176                                     .getActivityManager().getIdentifier(
177                                             command.getId()).isEnabled();
178         }
179
180         return true;
181     }
182
183     /**
184      * @see org.eclipse.jface.action.ExternalActionManager.ICallback#removePropertyChangeListener(String,
185      * IPropertyChangeListener)
186      */

187     public final void removePropertyChangeListener(final String JavaDoc commandId,
188             final IPropertyChangeListener listener) {
189         final ICommand command = workbench.getCommandSupport()
190                 .getCommandManager().getCommand(commandId);
191         final Object JavaDoc associatedListener = registeredListeners.remove(listener);
192         if (associatedListener instanceof ICommandListener) {
193             final ICommandListener commandListener = (ICommandListener) associatedListener;
194             command.removeCommandListener(commandListener);
195         }
196     }
197 }
Popular Tags