KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.commands.ExecutionEvent;
20 import org.eclipse.core.commands.ExecutionException;
21 import org.eclipse.core.commands.HandlerEvent;
22 import org.eclipse.core.commands.IHandlerAttributes;
23 import org.eclipse.core.commands.IHandlerListener;
24
25 /**
26  * This class is a partial implementation of <code>IHandler</code>. This
27  * abstract implementation provides support for handler listeners. You should
28  * subclass from this method unless you want to implement your own listener
29  * support. Subclasses should call
30  * {@link AbstractHandler#fireHandlerChanged(HandlerEvent)}when the handler
31  * changes. Subclasses should also override
32  * {@link AbstractHandler#getAttributeValuesByName()}if they have any
33  * attributes.
34  *
35  * @since 3.0
36  * @deprecated Please use the "org.eclipse.core.commands" plug-in instead.
37  * @see org.eclipse.core.commands.AbstractHandler
38  */

39 public abstract class AbstractHandler extends
40         org.eclipse.core.commands.AbstractHandler implements IHandler {
41
42     /**
43      * Those interested in hearing about changes to this instance of
44      * <code>IHandler</code>. This member is null iff there are
45      * no listeners attached to this handler. (Most handlers don't
46      * have any listeners, and this optimization saves some memory.)
47      */

48     private List JavaDoc handlerListeners;
49
50     /**
51      * @see IHandler#addHandlerListener(IHandlerListener)
52      */

53     public void addHandlerListener(
54             org.eclipse.ui.commands.IHandlerListener handlerListener) {
55         if (handlerListener == null) {
56             throw new NullPointerException JavaDoc();
57         }
58         if (handlerListeners == null) {
59             handlerListeners = new ArrayList JavaDoc();
60         }
61         if (!handlerListeners.contains(handlerListener)) {
62             handlerListeners.add(handlerListener);
63         }
64     }
65
66     /**
67      * The default implementation does nothing. Subclasses who attach listeners
68      * to other objects are encouraged to detach them in this method.
69      *
70      * @see org.eclipse.ui.commands.IHandler#dispose()
71      */

72     public void dispose() {
73         // Do nothing.
74
}
75
76     public Object JavaDoc execute(final ExecutionEvent event) throws ExecutionException {
77         try {
78             return execute(event.getParameters());
79         } catch (final org.eclipse.ui.commands.ExecutionException e) {
80             throw new ExecutionException(e.getMessage(), e.getCause());
81         }
82     }
83
84     /**
85      * Fires an event to all registered listeners describing changes to this
86      * instance.
87      *
88      * @param handlerEvent
89      * the event describing changes to this instance. Must not be
90      * <code>null</code>.
91      */

92     protected void fireHandlerChanged(HandlerEvent handlerEvent) {
93         super.fireHandlerChanged(handlerEvent);
94         
95         if (handlerListeners != null) {
96             final boolean attributesChanged = handlerEvent.isEnabledChanged()
97                     || handlerEvent.isHandledChanged();
98             final Map JavaDoc previousAttributes;
99             if (attributesChanged) {
100                 previousAttributes = new HashMap JavaDoc();
101                 previousAttributes.putAll(getAttributeValuesByName());
102                 if (handlerEvent.isEnabledChanged()) {
103                     Boolean JavaDoc disabled = !isEnabled() ? Boolean.TRUE: Boolean.FALSE;
104                     previousAttributes
105                             .put("enabled", disabled); //$NON-NLS-1$
106
}
107                 if (handlerEvent.isHandledChanged()) {
108                     Boolean JavaDoc notHandled = !isHandled() ? Boolean.TRUE: Boolean.FALSE;
109                     previousAttributes.put(
110                             IHandlerAttributes.ATTRIBUTE_HANDLED, notHandled);
111                 }
112             } else {
113                 previousAttributes = null;
114             }
115             final org.eclipse.ui.commands.HandlerEvent legacyEvent = new org.eclipse.ui.commands.HandlerEvent(
116                     this, attributesChanged, previousAttributes);
117
118             for (int i = 0; i < handlerListeners.size(); i++) {
119                 ((org.eclipse.ui.commands.IHandlerListener) handlerListeners
120                         .get(i)).handlerChanged(legacyEvent);
121             }
122         }
123     }
124     
125     protected void fireHandlerChanged(
126             final org.eclipse.ui.commands.HandlerEvent handlerEvent) {
127         if (handlerEvent == null) {
128             throw new NullPointerException JavaDoc();
129         }
130
131         if (handlerListeners != null) {
132             for (int i = 0; i < handlerListeners.size(); i++) {
133                 ((org.eclipse.ui.commands.IHandlerListener) handlerListeners
134                         .get(i)).handlerChanged(handlerEvent);
135             }
136         }
137
138         if (super.hasListeners()) {
139             final boolean enabledChanged;
140             final boolean handledChanged;
141             if (handlerEvent.haveAttributeValuesByNameChanged()) {
142                 Map JavaDoc previousAttributes = handlerEvent
143                         .getPreviousAttributeValuesByName();
144
145                 Object JavaDoc attribute = previousAttributes.get("enabled"); //$NON-NLS-1$
146
if (attribute instanceof Boolean JavaDoc) {
147                     enabledChanged = ((Boolean JavaDoc) attribute).booleanValue();
148                 } else {
149                     enabledChanged = false;
150                 }
151
152                 attribute = previousAttributes
153                         .get(IHandlerAttributes.ATTRIBUTE_HANDLED);
154                 if (attribute instanceof Boolean JavaDoc) {
155                     handledChanged = ((Boolean JavaDoc) attribute).booleanValue();
156                 } else {
157                     handledChanged = false;
158                 }
159             } else {
160                 enabledChanged = false;
161                 handledChanged = true;
162             }
163             final HandlerEvent newEvent = new HandlerEvent(this,
164                     enabledChanged, handledChanged);
165             super.fireHandlerChanged(newEvent);
166         }
167     }
168
169     /**
170      * This simply return an empty map. The default implementation has no
171      * attributes.
172      *
173      * @see IHandler#getAttributeValuesByName()
174      */

175     public Map JavaDoc getAttributeValuesByName() {
176         return Collections.EMPTY_MAP;
177     }
178     
179     /**
180      * Returns true iff there is one or more IHandlerListeners attached to this
181      * AbstractHandler.
182      *
183      * @return true iff there is one or more IHandlerListeners attached to this
184      * AbstractHandler
185      * @since 3.1
186      */

187     protected final boolean hasListeners() {
188         return super.hasListeners() || handlerListeners != null;
189     }
190     
191     public boolean isEnabled() {
192         final Object JavaDoc handled = getAttributeValuesByName().get("enabled"); //$NON-NLS-1$
193
if (handled instanceof Boolean JavaDoc) {
194             return ((Boolean JavaDoc) handled).booleanValue();
195         }
196
197         return false;
198     }
199     
200     public boolean isHandled() {
201         final Object JavaDoc handled = getAttributeValuesByName().get(
202                 IHandlerAttributes.ATTRIBUTE_HANDLED);
203         if (handled instanceof Boolean JavaDoc) {
204             return ((Boolean JavaDoc) handled).booleanValue();
205         }
206
207         return false;
208     }
209
210     /**
211      * @see IHandler#removeHandlerListener(IHandlerListener)
212      */

213     public void removeHandlerListener(
214             org.eclipse.ui.commands.IHandlerListener handlerListener) {
215         if (handlerListener == null) {
216             throw new NullPointerException JavaDoc();
217         }
218         if (handlerListeners == null) {
219             return;
220         }
221         
222         if (handlerListeners != null) {
223             handlerListeners.remove(handlerListener);
224         }
225         if (handlerListeners.isEmpty()) {
226             handlerListeners = null;
227         }
228     }
229 }
230
Popular Tags