KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > handlers > CommandLegacyActionWrapper


1 /*******************************************************************************
2  * Copyright (c) 2005, 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
12 package org.eclipse.ui.internal.handlers;
13
14 import org.eclipse.core.commands.Command;
15 import org.eclipse.core.commands.CommandEvent;
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.core.commands.ICommandListener;
19 import org.eclipse.core.commands.INamedHandleStateIds;
20 import org.eclipse.core.commands.NotHandledException;
21 import org.eclipse.core.commands.ParameterizedCommand;
22 import org.eclipse.core.commands.State;
23 import org.eclipse.core.commands.common.NotDefinedException;
24 import org.eclipse.jface.action.AbstractAction;
25 import org.eclipse.jface.action.IAction;
26 import org.eclipse.jface.action.IMenuCreator;
27 import org.eclipse.jface.bindings.TriggerSequence;
28 import org.eclipse.jface.bindings.keys.KeySequence;
29 import org.eclipse.jface.bindings.keys.KeyStroke;
30 import org.eclipse.jface.commands.RadioState;
31 import org.eclipse.jface.commands.ToggleState;
32 import org.eclipse.jface.menus.IMenuStateIds;
33 import org.eclipse.jface.menus.TextState;
34 import org.eclipse.jface.resource.ImageDescriptor;
35 import org.eclipse.jface.util.Util;
36 import org.eclipse.swt.events.HelpListener;
37 import org.eclipse.swt.widgets.Event;
38 import org.eclipse.ui.commands.ICommandService;
39 import org.eclipse.ui.internal.commands.CommandImageManager;
40 import org.eclipse.ui.internal.commands.ICommandImageService;
41 import org.eclipse.ui.keys.IBindingService;
42 import org.eclipse.ui.services.IServiceLocator;
43
44 /**
45  * <p>
46  * A wrapper around the new command infrastructure that imitates the old
47  * <code>IAction</code> interface.
48  * </p>
49  * <p>
50  * Clients may instantiate this class, but must not extend.
51  * </p>
52  * <p>
53  * <strong>PROVISIONAL</strong>. This class or interface has been added as
54  * part of a work in progress. There is a guarantee neither that this API will
55  * work nor that it will remain the same. Please do not use this API without
56  * consulting with the Platform/UI team.
57  * </p>
58  * <p>
59  * This class is eventually intended to exist in
60  * <code>org.eclipse.ui.handlers</code>.
61  * </p>
62  *
63  * @since 3.2
64  */

65 public final class CommandLegacyActionWrapper extends AbstractAction {
66
67     /**
68      * Listens to changes to one or more commands, and forwards them out through
69      * the property change event mechanism.
70      */

71     private final class CommandListener implements ICommandListener {
72         public final void commandChanged(final CommandEvent commandEvent) {
73             final Command baseCommand = commandEvent.getCommand();
74
75             // Check if the name changed.
76
if (commandEvent.isNameChanged()) {
77                 String JavaDoc newName = null;
78                 if (baseCommand.isDefined()) {
79                     try {
80                         newName = baseCommand.getName();
81                     } catch (final NotDefinedException e) {
82                         // Not defined, so leave as null.
83
}
84                 }
85                 firePropertyChange(IAction.TEXT, null, newName);
86             }
87
88             // Check if the description changed.
89
if (commandEvent.isDescriptionChanged()) {
90                 String JavaDoc newDescription = null;
91                 if (baseCommand.isDefined()) {
92                     try {
93                         newDescription = baseCommand.getDescription();
94                     } catch (final NotDefinedException e) {
95                         // Not defined, so leave as null.
96
}
97                 }
98                 firePropertyChange(IAction.DESCRIPTION, null, newDescription);
99                 firePropertyChange(IAction.TOOL_TIP_TEXT, null, newDescription);
100             }
101
102             // Check if the handled property changed.
103
if (commandEvent.isHandledChanged()) {
104                 if (baseCommand.isHandled()) {
105                     firePropertyChange(IAction.HANDLED, Boolean.FALSE,
106                             Boolean.TRUE);
107                 } else {
108                     firePropertyChange(IAction.HANDLED, Boolean.TRUE,
109                             Boolean.FALSE);
110                 }
111             }
112         }
113
114     }
115
116     /**
117      * The command with which this action is associated; never <code>null</code>.
118      */

119     private ParameterizedCommand command;
120
121     /**
122      * Listens to changes in a command, and forwards them out through the
123      * property change event mechanism.
124      */

125     private final ICommandListener commandListener = new CommandListener();
126
127     /**
128      * Whether this action has been marked as enabled.
129      */

130     private boolean enabled = true;
131
132     /**
133      * The identifier for the action. This may be <code>null</code>.
134      */

135     private String JavaDoc id;
136
137     /**
138      * A service locator that can be used for retrieving command-based services.
139      * This value is never <code>null</code>.
140      */

141     private final IServiceLocator serviceLocator;
142
143     /**
144      * The image style to use for this action. This value may be
145      * <code>null</code>.
146      */

147     private final String JavaDoc style;
148
149     /**
150      * Constructs a new instance of <code>ActionProxy</code>.
151      *
152      * @param id
153      * The initial action identifier; may be <code>null</code>.
154      * @param command
155      * The command with which this action is associated; must not be
156      * <code>null</code>.
157      * @param style
158      * The image style to use for this action, may be
159      * <code>null</code>.
160      * @param serviceLocator
161      * A service locator that can be used to find various
162      * command-based services; must not be <code>null</code>.
163      */

164     public CommandLegacyActionWrapper(final String JavaDoc id,
165             final ParameterizedCommand command, final String JavaDoc style,
166             final IServiceLocator serviceLocator) {
167         if (command == null) {
168             throw new NullPointerException JavaDoc(
169                     "An action proxy can't be created without a command"); //$NON-NLS-1$
170
}
171
172         if (serviceLocator == null) {
173             throw new NullPointerException JavaDoc(
174                     "An action proxy can't be created without a service locator"); //$NON-NLS-1$
175
}
176
177         this.command = command;
178         this.id = id;
179         this.style = style;
180         this.serviceLocator = serviceLocator;
181
182         // TODO Needs to listen to command, state, binding and image changes.
183
command.getCommand().addCommandListener(commandListener);
184     }
185
186     public final int getAccelerator() {
187         final String JavaDoc commandId = getActionDefinitionId();
188         final IBindingService bindingService = (IBindingService) serviceLocator
189                 .getService(IBindingService.class);
190         final TriggerSequence triggerSequence = bindingService
191                 .getBestActiveBindingFor(commandId);
192         if (triggerSequence instanceof KeySequence) {
193             final KeySequence keySequence = (KeySequence) triggerSequence;
194             final KeyStroke[] keyStrokes = keySequence.getKeyStrokes();
195             if (keyStrokes.length == 1) {
196                 final KeyStroke keyStroke = keyStrokes[0];
197                 return keyStroke.getModifierKeys() | keyStroke.getNaturalKey();
198             }
199         }
200
201         return 0;
202     }
203
204     public final String JavaDoc getActionDefinitionId() {
205         return command.getId();
206     }
207
208     public final String JavaDoc getDescription() {
209         try {
210             return command.getCommand().getDescription();
211         } catch (final NotDefinedException e) {
212             return null;
213         }
214     }
215
216     public final ImageDescriptor getDisabledImageDescriptor() {
217         final String JavaDoc commandId = getActionDefinitionId();
218         final ICommandImageService commandImageService = (ICommandImageService) serviceLocator
219                 .getService(ICommandImageService.class);
220         return commandImageService.getImageDescriptor(commandId,
221                 CommandImageManager.TYPE_DISABLED, style);
222     }
223
224     public final HelpListener getHelpListener() {
225         // TODO Help. Addressing help on commands.
226
return null;
227     }
228
229     public final ImageDescriptor getHoverImageDescriptor() {
230         final String JavaDoc commandId = getActionDefinitionId();
231         final ICommandImageService commandImageService = (ICommandImageService) serviceLocator
232                 .getService(ICommandImageService.class);
233         return commandImageService.getImageDescriptor(commandId,
234                 CommandImageManager.TYPE_HOVER, style);
235     }
236
237     public final String JavaDoc getId() {
238         return id;
239     }
240
241     public final ImageDescriptor getImageDescriptor() {
242         final String JavaDoc commandId = getActionDefinitionId();
243         final ICommandImageService commandImageService = (ICommandImageService) serviceLocator
244                 .getService(ICommandImageService.class);
245         return commandImageService.getImageDescriptor(commandId, style);
246     }
247
248     public final IMenuCreator getMenuCreator() {
249         // TODO Pulldown. What kind of callback is needed here?
250
return null;
251     }
252
253     public final int getStyle() {
254         // TODO Pulldown. This does not currently support the pulldown style.
255
final State state = command.getCommand().getState(IMenuStateIds.STYLE);
256         if (state instanceof RadioState) {
257             return IAction.AS_RADIO_BUTTON;
258         } else if (state instanceof ToggleState) {
259             return IAction.AS_CHECK_BOX;
260         }
261
262         return IAction.AS_PUSH_BUTTON;
263     }
264
265     public final String JavaDoc getText() {
266         try {
267             return command.getName();
268         } catch (final NotDefinedException e) {
269             return null;
270         }
271     }
272
273     public final String JavaDoc getToolTipText() {
274         return getDescription();
275     }
276
277     public final boolean isChecked() {
278         final State state = command.getCommand().getState(IMenuStateIds.STYLE);
279         if (state instanceof ToggleState) {
280             final Boolean JavaDoc currentValue = (Boolean JavaDoc) state.getValue();
281             return currentValue.booleanValue();
282         }
283
284         return false;
285     }
286
287     public final boolean isEnabled() {
288         final Command baseCommand = command.getCommand();
289         return baseCommand.isEnabled() && enabled;
290     }
291
292     /**
293      * Whether this action's local <code>enabled</code> property is set. This
294      * can be used by handlers that are trying to check if
295      * {@link #setEnabled(boolean)} has been called. This is typically used by
296      * legacy action proxies who are trying to avoid a <a
297      * HREF="https://bugs.eclipse.org/bugs/show_bug.cgi?id=117496">stack
298      * overflow</a>.
299      *
300      * @return <code>false</code> if someone has called
301      * {@link #setEnabled(boolean)} with <code>false</code>;
302      * <code>true</code> otherwise.
303      */

304     public final boolean isEnabledDisregardingCommand() {
305         return enabled;
306     }
307
308     public final boolean isHandled() {
309         final Command baseCommand = command.getCommand();
310         return baseCommand.isHandled();
311     }
312
313     public final void run() {
314         runWithEvent(null);
315     }
316
317     public final void runWithEvent(final Event event) {
318         final Command baseCommand = command.getCommand();
319         final ExecutionEvent executionEvent = new ExecutionEvent(command
320                 .getCommand(), command.getParameterMap(), event, null);
321         try {
322             baseCommand.execute(executionEvent);
323             firePropertyChange(IAction.RESULT, null, Boolean.TRUE);
324
325         } catch (final NotHandledException e) {
326             firePropertyChange(IAction.RESULT, null, Boolean.FALSE);
327
328         } catch (final ExecutionException e) {
329             firePropertyChange(IAction.RESULT, null, Boolean.FALSE);
330             // TODO Should this be logged?
331

332         }
333     }
334
335     public final void setAccelerator(final int keycode) {
336         // TODO Binding. This is hopefully not essential.
337
}
338
339     public final void setActionDefinitionId(final String JavaDoc id) {
340         // Get the old values.
341
final boolean oldChecked = isChecked();
342         final String JavaDoc oldDescription = getDescription();
343         final boolean oldEnabled = isEnabled();
344         final boolean oldHandled = isHandled();
345         final ImageDescriptor oldDefaultImage = getImageDescriptor();
346         final ImageDescriptor oldDisabledImage = getDisabledImageDescriptor();
347         final ImageDescriptor oldHoverImage = getHoverImageDescriptor();
348         final String JavaDoc oldText = getText();
349
350         // Update the command.
351
final Command oldBaseCommand = command.getCommand();
352         oldBaseCommand.removeCommandListener(commandListener);
353         final ICommandService commandService = (ICommandService) serviceLocator
354                 .getService(ICommandService.class);
355         final Command newBaseCommand = commandService.getCommand(id);
356         command = new ParameterizedCommand(newBaseCommand, null);
357         newBaseCommand.addCommandListener(commandListener);
358
359         // Get the new values.
360
final boolean newChecked = isChecked();
361         final String JavaDoc newDescription = getDescription();
362         final boolean newEnabled = isEnabled();
363         final boolean newHandled = isHandled();
364         final ImageDescriptor newDefaultImage = getImageDescriptor();
365         final ImageDescriptor newDisabledImage = getDisabledImageDescriptor();
366         final ImageDescriptor newHoverImage = getHoverImageDescriptor();
367         final String JavaDoc newText = getText();
368
369         // Fire property change events, as necessary.
370
if (newChecked != oldChecked) {
371             if (oldChecked) {
372                 firePropertyChange(IAction.CHECKED, Boolean.TRUE, Boolean.FALSE);
373             } else {
374                 firePropertyChange(IAction.CHECKED, Boolean.FALSE, Boolean.TRUE);
375             }
376         }
377
378         if (!Util.equals(oldDescription, newDescription)) {
379             firePropertyChange(IAction.DESCRIPTION, oldDescription,
380                     newDescription);
381             firePropertyChange(IAction.TOOL_TIP_TEXT, oldDescription,
382                     newDescription);
383         }
384
385         if (newEnabled != oldEnabled) {
386             if (oldEnabled) {
387                 firePropertyChange(IAction.ENABLED, Boolean.TRUE, Boolean.FALSE);
388             } else {
389                 firePropertyChange(IAction.ENABLED, Boolean.FALSE, Boolean.TRUE);
390             }
391         }
392
393         if (newHandled != oldHandled) {
394             if (oldHandled) {
395                 firePropertyChange(IAction.HANDLED, Boolean.TRUE, Boolean.FALSE);
396             } else {
397                 firePropertyChange(IAction.HANDLED, Boolean.FALSE, Boolean.TRUE);
398             }
399         }
400
401         if (!Util.equals(oldDefaultImage, newDefaultImage)) {
402             firePropertyChange(IAction.IMAGE, oldDefaultImage, newDefaultImage);
403         }
404
405         if (!Util.equals(oldDisabledImage, newDisabledImage)) {
406             firePropertyChange(IAction.IMAGE, oldDisabledImage,
407                     newDisabledImage);
408         }
409
410         if (!Util.equals(oldHoverImage, newHoverImage)) {
411             firePropertyChange(IAction.IMAGE, oldHoverImage, newHoverImage);
412         }
413
414         if (!Util.equals(oldText, newText)) {
415             firePropertyChange(IAction.TEXT, oldText, newText);
416         }
417     }
418
419     public final void setChecked(final boolean checked) {
420         final State state = command.getCommand().getState(IMenuStateIds.STYLE);
421         if (state instanceof ToggleState) {
422             final Boolean JavaDoc currentValue = (Boolean JavaDoc) state.getValue();
423             if (checked != currentValue.booleanValue()) {
424                 if (checked) {
425                     state.setValue(Boolean.TRUE);
426                 } else {
427                     state.setValue(Boolean.FALSE);
428                 }
429             }
430         }
431     }
432
433     public final void setDescription(final String JavaDoc text) {
434         final State state = command.getCommand().getState(
435                 INamedHandleStateIds.DESCRIPTION);
436         if (state instanceof TextState) {
437             final String JavaDoc currentValue = (String JavaDoc) state.getValue();
438             if (!Util.equals(text, currentValue)) {
439                 state.setValue(text);
440             }
441         }
442     }
443
444     public final void setDisabledImageDescriptor(final ImageDescriptor newImage) {
445         final String JavaDoc commandId = getActionDefinitionId();
446         final int type = CommandImageManager.TYPE_DISABLED;
447         final ICommandImageService commandImageService = (ICommandImageService) serviceLocator
448                 .getService(ICommandImageService.class);
449         commandImageService.bind(commandId, type, style, newImage);
450     }
451
452     public final void setEnabled(final boolean enabled) {
453         if (enabled != this.enabled) {
454             final Boolean JavaDoc oldValue = this.enabled ? Boolean.TRUE
455                     : Boolean.FALSE;
456             final Boolean JavaDoc newValue = enabled ? Boolean.TRUE : Boolean.FALSE;
457             this.enabled = enabled;
458             firePropertyChange(ENABLED, oldValue, newValue);
459         }
460     }
461
462     public final void setHelpListener(final HelpListener listener) {
463         // TODO Help Haven't even started to look at help yet.
464

465     }
466
467     public final void setHoverImageDescriptor(final ImageDescriptor newImage) {
468         final String JavaDoc commandId = getActionDefinitionId();
469         final int type = CommandImageManager.TYPE_HOVER;
470         final ICommandImageService commandImageService = (ICommandImageService) serviceLocator
471                 .getService(ICommandImageService.class);
472         commandImageService.bind(commandId, type, style, newImage);
473     }
474
475     public final void setId(final String JavaDoc id) {
476         this.id = id;
477     }
478
479     public final void setImageDescriptor(final ImageDescriptor newImage) {
480         final String JavaDoc commandId = getActionDefinitionId();
481         final int type = CommandImageManager.TYPE_DEFAULT;
482         final ICommandImageService commandImageService = (ICommandImageService) serviceLocator
483                 .getService(ICommandImageService.class);
484         commandImageService.bind(commandId, type, style, newImage);
485     }
486
487     public final void setMenuCreator(final IMenuCreator creator) {
488         // TODO Pulldown. This is complicated
489
}
490
491     public final void setText(final String JavaDoc text) {
492         final State state = command.getCommand().getState(
493                 INamedHandleStateIds.NAME);
494         if (state instanceof TextState) {
495             final String JavaDoc currentValue = (String JavaDoc) state.getValue();
496             if (!Util.equals(text, currentValue)) {
497                 state.setValue(text);
498             }
499         }
500     }
501
502     public final void setToolTipText(final String JavaDoc text) {
503         setDescription(text);
504     }
505
506 }
507
Popular Tags