KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > 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.jface.commands;
12
13 import org.eclipse.core.commands.AbstractHandler;
14 import org.eclipse.core.commands.ExecutionEvent;
15 import org.eclipse.core.commands.ExecutionException;
16 import org.eclipse.core.commands.HandlerEvent;
17 import org.eclipse.core.commands.IHandlerListener;
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
23 /**
24  * <p>
25  * This class adapts instances of <code>IAction</code> to
26  * <code>IHandler</code>.
27  * </p>
28  *
29  * @since 3.1
30  */

31 public final class ActionHandler extends AbstractHandler {
32
33     /**
34      * The wrapped action. This value is never <code>null</code>.
35      */

36     private final IAction action;
37
38     /**
39      * The property change listener hooked on to the action. This is initialized
40      * when the first listener is attached to this handler, and is removed when
41      * the handler is disposed or the last listener is removed.
42      */

43     private IPropertyChangeListener propertyChangeListener;
44
45     /**
46      * Creates a new instance of this class given an instance of
47      * <code>IAction</code>.
48      *
49      * @param action
50      * the action. Must not be <code>null</code>.
51      */

52     public ActionHandler(final IAction action) {
53         if (action == null) {
54             throw new NullPointerException JavaDoc();
55         }
56
57         this.action = action;
58     }
59
60     public final void addHandlerListener(final IHandlerListener handlerListener) {
61         if (!hasListeners()) {
62             attachListener();
63         }
64
65         super.addHandlerListener(handlerListener);
66     }
67
68     /**
69      * When a listener is attached to this handler, then this registers a
70      * listener with the underlying action.
71      *
72      * @since 3.1
73      */

74     private final void attachListener() {
75         if (propertyChangeListener == null) {
76             propertyChangeListener = new IPropertyChangeListener() {
77                 public final void propertyChange(
78                         final PropertyChangeEvent propertyChangeEvent) {
79                     final String JavaDoc property = propertyChangeEvent.getProperty();
80                     fireHandlerChanged(new HandlerEvent(ActionHandler.this,
81                             IAction.ENABLED.equals(property), IAction.HANDLED
82                                     .equals(property)));
83                 }
84             };
85         }
86
87         this.action.addPropertyChangeListener(propertyChangeListener);
88     }
89
90     /**
91      * When no more listeners are registered, then this is used to removed the
92      * property change listener from the underlying action.
93      */

94     private final void detachListener() {
95         this.action.removePropertyChangeListener(propertyChangeListener);
96         propertyChangeListener = null;
97     }
98
99     /**
100      * Removes the property change listener from the action.
101      *
102      * @see org.eclipse.core.commands.IHandler#dispose()
103      */

104     public final void dispose() {
105         if (hasListeners()) {
106             action.removePropertyChangeListener(propertyChangeListener);
107         }
108     }
109     
110     public final Object JavaDoc execute(final ExecutionEvent event)
111             throws ExecutionException {
112         if ((action.getStyle() == IAction.AS_CHECK_BOX)
113                 || (action.getStyle() == IAction.AS_RADIO_BUTTON)) {
114             action.setChecked(!action.isChecked());
115         }
116         final Object JavaDoc trigger = event.getTrigger();
117         try {
118             if (trigger instanceof Event) {
119                 action.runWithEvent((Event) trigger);
120             } else {
121                 action.runWithEvent(new Event());
122             }
123         } catch (Exception JavaDoc e) {
124             throw new ExecutionException(
125                     "While executing the action, an exception occurred", e); //$NON-NLS-1$
126
}
127         return null;
128     }
129
130     /**
131      * Returns the action associated with this handler
132      *
133      * @return the action associated with this handler (not null)
134      * @since 3.1
135      */

136     public final IAction getAction() {
137         return action;
138     }
139     
140     public final boolean isEnabled() {
141         return action.isEnabled();
142     }
143     
144     public final boolean isHandled() {
145         return action.isHandled();
146     }
147     
148     public final void removeHandlerListener(
149             final IHandlerListener handlerListener) {
150         super.removeHandlerListener(handlerListener);
151
152         if (!hasListeners()) {
153             detachListener();
154         }
155     }
156     
157     public final String JavaDoc toString() {
158         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
159
160         buffer.append("ActionHandler("); //$NON-NLS-1$
161
buffer.append(action);
162         buffer.append(')');
163
164         return buffer.toString();
165     }
166 }
167
Popular Tags