KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > 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.core.commands;
12
13 import org.eclipse.core.commands.common.EventManager;
14
15 /**
16  * <p>
17  * This class is a partial implementation of <code>IHandler</code>. This
18  * abstract implementation provides support for handler listeners. You should
19  * subclass from this method unless you want to implement your own listener
20  * support. Subclasses should call
21  * {@link AbstractHandler#fireHandlerChanged(HandlerEvent)}when the handler
22  * changes. Subclasses can also override {@link AbstractHandler#isEnabled()} and
23  * {@link AbstractHandler#isHandled()}.
24  * </p>
25  *
26  * @since 3.1
27  */

28 public abstract class AbstractHandler extends EventManager implements IHandler {
29
30     /**
31      * @see IHandler#addHandlerListener(IHandlerListener)
32      */

33     public void addHandlerListener(final IHandlerListener handlerListener) {
34         addListenerObject(handlerListener);
35     }
36
37     /**
38      * The default implementation does nothing. Subclasses who attach listeners
39      * to other objects are encouraged to detach them in this method.
40      *
41      * @see org.eclipse.core.commands.IHandler#dispose()
42      */

43     public void dispose() {
44         // Do nothing.
45
}
46
47     /**
48      * Fires an event to all registered listeners describing changes to this
49      * instance.
50      * <p>
51      * Subclasses may extend the definition of this method (i.e., if a different
52      * type of listener can be attached to a subclass). This is used primarily
53      * for support of <code>AbstractHandler</code> in
54      * <code>org.eclipse.ui.workbench</code>, and clients should be wary of
55      * overriding this behaviour. If this method is overridden, then the first
56      * line of the method should be "<code>super.fireHandlerChanged(handlerEvent);</code>".
57      * </p>
58      *
59      * @param handlerEvent
60      * the event describing changes to this instance. Must not be
61      * <code>null</code>.
62      */

63     protected void fireHandlerChanged(final HandlerEvent handlerEvent) {
64         if (handlerEvent == null) {
65             throw new NullPointerException JavaDoc();
66         }
67
68         final Object JavaDoc[] listeners = getListeners();
69         for (int i = 0; i < listeners.length; i++) {
70             final IHandlerListener listener = (IHandlerListener) listeners[i];
71             listener.handlerChanged(handlerEvent);
72         }
73     }
74
75     /**
76      * Whether this handler is capable of executing at this time. Subclasses may
77      * override this method.
78      *
79      * @return <code>true</code>
80      */

81     public boolean isEnabled() {
82         return true;
83     }
84
85     /**
86      * Whether this handler is capable of handling delegated responsibilities at
87      * this time. Subclasses may override this method.
88      *
89      * @return <code>true</code>
90      */

91     public boolean isHandled() {
92         return true;
93     }
94
95     /**
96      * <p>
97      * Returns true iff there is one or more IHandlerListeners attached to this
98      * AbstractHandler.
99      * </p>
100      * <p>
101      * Subclasses may extend the definition of this method (i.e., if a different
102      * type of listener can be attached to a subclass). This is used primarily
103      * for support of <code>AbstractHandler</code> in
104      * <code>org.eclipse.ui.workbench</code>, and clients should be wary of
105      * overriding this behaviour. If this method is overridden, then the return
106      * value should include "<code>super.hasListeners() ||</code>".
107      * </p>
108      *
109      * @return true iff there is one or more IHandlerListeners attached to this
110      * AbstractHandler
111      */

112     protected boolean hasListeners() {
113         return isListenerAttached();
114     }
115
116     /**
117      * @see IHandler#removeHandlerListener(IHandlerListener)
118      */

119     public void removeHandlerListener(final IHandlerListener handlerListener) {
120         removeListenerObject(handlerListener);
121     }
122 }
123
Popular Tags