KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > commands > ActionHandler


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.commands;
12
13 import java.util.Collections JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.commands.IHandlerAttributes;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.util.IPropertyChangeListener;
20 import org.eclipse.jface.util.PropertyChangeEvent;
21 import org.eclipse.swt.widgets.Event;
22 import org.eclipse.ui.actions.RetargetAction;
23
24 /**
25  * This class adapts instances of <code>IAction</code> to
26  * <code>IHandler</code>.
27  *
28  * @since 3.0
29  * @deprecated Please use the "org.eclipse.core.commands" plug-in instead.
30  * @see org.eclipse.jface.commands.ActionHandler
31  */

32 public final class ActionHandler extends AbstractHandler {
33
34     /**
35      * The attribute name for the checked property of the wrapped action. This
36      * indicates whether the action should be displayed with as a checked check
37      * box.
38      */

39     private final static String JavaDoc ATTRIBUTE_CHECKED = "checked"; //$NON-NLS-1$
40

41     /**
42      * The attribute name for the enabled property of the wrapped action.
43      */

44     private final static String JavaDoc ATTRIBUTE_ENABLED = "enabled"; //$NON-NLS-1$
45

46     /**
47      * <p>
48      * The name of the attribute indicating whether the wrapped instance of
49      * <code>RetargetAction</code> has a handler.
50      * </p>
51      */

52     private final static String JavaDoc ATTRIBUTE_HANDLED = IHandlerAttributes.ATTRIBUTE_HANDLED;
53
54     /**
55      * The attribute name for the identifier of the wrapped action. This is the
56      * action identifier, and not the command identifier.
57      */

58     private final static String JavaDoc ATTRIBUTE_ID = "id"; //$NON-NLS-1$
59

60     /**
61      * The attribute name for the visual style of the wrapped action. The style
62      * can be things like a pull-down menu, a check box, a radio button or a
63      * push button.
64      */

65     private final static String JavaDoc ATTRIBUTE_STYLE = "style"; //$NON-NLS-1$
66

67     /**
68      * The wrapped action. This value is never <code>null</code>.
69      */

70     private final IAction action;
71
72     /**
73      * The map of attributes values. The keys are <code>String</code> values
74      * of the attribute names (given above). The values can be any type of
75      * <code>Object</code>.
76      *
77      * This map is always null if there are no IHandlerListeners registered.
78      *
79      */

80     private Map JavaDoc attributeValuesByName;
81
82     /**
83      * The property change listener hooked on to the action. This is initialized
84      * when the first listener is attached to this handler, and is removed when
85      * the handler is disposed or the last listener is removed.
86      */

87     private IPropertyChangeListener propertyChangeListener;
88
89     /**
90      * Creates a new instance of this class given an instance of
91      * <code>IAction</code>.
92      *
93      * @param action
94      * the action. Must not be <code>null</code>.
95      */

96     public ActionHandler(IAction action) {
97         if (action == null) {
98             throw new NullPointerException JavaDoc();
99         }
100
101         this.action = action;
102     }
103
104     /**
105      * @see org.eclipse.ui.commands.IHandler#addHandlerListener(org.eclipse.ui.commands.IHandlerListener)
106      * @since 3.1
107      */

108     public void addHandlerListener(IHandlerListener handlerListener) {
109         if (!hasListeners()) {
110             attachListener();
111         }
112
113         super.addHandlerListener(handlerListener);
114     }
115
116     /**
117      * When a listener is attached to this handler, then this registers a
118      * listener with the underlying action.
119      *
120      * @since 3.1
121      */

122     private final void attachListener() {
123         if (propertyChangeListener == null) {
124             attributeValuesByName = getAttributeValuesByNameFromAction();
125
126             propertyChangeListener = new IPropertyChangeListener() {
127                 public void propertyChange(
128                         PropertyChangeEvent propertyChangeEvent) {
129                     String JavaDoc property = propertyChangeEvent.getProperty();
130                     if (IAction.ENABLED.equals(property)
131                             || IAction.CHECKED.equals(property)
132                             || IHandlerAttributes.ATTRIBUTE_HANDLED
133                                     .equals(property)) {
134
135                         Map JavaDoc previousAttributeValuesByName = attributeValuesByName;
136                         attributeValuesByName = getAttributeValuesByNameFromAction();
137                         if (!attributeValuesByName
138                                 .equals(previousAttributeValuesByName)) {
139                             fireHandlerChanged(new HandlerEvent(
140                                     ActionHandler.this, true,
141                                     previousAttributeValuesByName));
142                         }
143                     }
144                 }
145             };
146         }
147
148         this.action.addPropertyChangeListener(propertyChangeListener);
149     }
150
151     /**
152      * When no more listeners are registered, then this is used to removed the
153      * property change listener from the underlying action.
154      *
155      * @since 3.1
156      *
157      */

158     private final void detachListener() {
159         this.action.removePropertyChangeListener(propertyChangeListener);
160         propertyChangeListener = null;
161         attributeValuesByName = null;
162     }
163
164     /**
165      * Removes the property change listener from the action.
166      *
167      * @see org.eclipse.ui.commands.IHandler#dispose()
168      */

169     public void dispose() {
170         if (hasListeners()) {
171             action.removePropertyChangeListener(propertyChangeListener);
172         }
173     }
174
175    
176     /* (non-Javadoc)
177      * @see org.eclipse.ui.commands.IHandler#execute(java.util.Map)
178      */

179     public Object JavaDoc execute(Map JavaDoc parameterValuesByName) throws ExecutionException {
180         if ((action.getStyle() == IAction.AS_CHECK_BOX)
181                 || (action.getStyle() == IAction.AS_RADIO_BUTTON)) {
182             action.setChecked(!action.isChecked());
183         }
184         try {
185             action.runWithEvent(new Event());
186         } catch (Exception JavaDoc e) {
187             throw new ExecutionException(
188                     "While executing the action, an exception occurred", e); //$NON-NLS-1$
189
}
190         return null;
191     }
192
193     /**
194      * Returns the action associated with this handler
195      *
196      * @return the action associated with this handler (not null)
197      * @since 3.1
198      */

199     public IAction getAction() {
200         return action;
201     }
202
203     /* (non-Javadoc)
204      * @see org.eclipse.ui.commands.IHandler#getAttributeValuesByName()
205      */

206     public Map JavaDoc getAttributeValuesByName() {
207         if (attributeValuesByName == null) {
208             return getAttributeValuesByNameFromAction();
209         }
210
211         return attributeValuesByName;
212     }
213
214     /**
215      * An accessor for the attribute names from the action. This reads out all
216      * of the attributes from an action into a local map.
217      *
218      * @return A map of the attribute values indexed by the attribute name. The
219      * attributes names are strings, but the values can by any object.
220      *
221      */

222     private Map JavaDoc getAttributeValuesByNameFromAction() {
223         Map JavaDoc map = new HashMap JavaDoc();
224         map.put(ATTRIBUTE_CHECKED, action.isChecked() ? Boolean.TRUE
225                 : Boolean.FALSE);
226         map.put(ATTRIBUTE_ENABLED, action.isEnabled() ? Boolean.TRUE
227                 : Boolean.FALSE);
228         boolean handled = true;
229         if (action instanceof RetargetAction) {
230             RetargetAction retargetAction = (RetargetAction) action;
231             handled = retargetAction.getActionHandler() != null;
232         }
233         map.put(ATTRIBUTE_HANDLED, handled ? Boolean.TRUE : Boolean.FALSE);
234         map.put(ATTRIBUTE_ID, action.getId());
235         map.put(ATTRIBUTE_STYLE, new Integer JavaDoc(action.getStyle()));
236         return Collections.unmodifiableMap(map);
237     }
238
239     /**
240      * @see org.eclipse.ui.commands.IHandler#removeHandlerListener(org.eclipse.ui.commands.IHandlerListener)
241      * @since 3.1
242      */

243     public void removeHandlerListener(IHandlerListener handlerListener) {
244         super.removeHandlerListener(handlerListener);
245
246         if (!hasListeners()) {
247             detachListener();
248         }
249     }
250     
251     /* (non-Javadoc)
252      * @see java.lang.Object#toString()
253      */

254     public final String JavaDoc toString() {
255         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
256
257         buffer.append("ActionHandler(action="); //$NON-NLS-1$
258
buffer.append(action);
259         buffer.append(')');
260
261         return buffer.toString();
262     }
263 }
264
Popular Tags