KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > commands > common > EventManager


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.core.commands.common;
13
14 import org.eclipse.core.runtime.ListenerList;
15
16 /**
17  * <p>
18  * A manager to which listeners can be attached. This handles the management of
19  * a list of listeners -- optimizing memory and performance. All the methods on
20  * this class are guaranteed to be thread-safe.
21  * </p>
22  * <p>
23  * Clients may extend.
24  * </p>
25  *
26  * @since 3.2
27  */

28 public abstract class EventManager {
29
30     /**
31      * An empty array that can be returned from a call to
32      * {@link #getListeners()} when {@link #listenerList} is <code>null</code>.
33      */

34     private static final Object JavaDoc[] EMPTY_ARRAY = new Object JavaDoc[0];
35
36     /**
37      * A collection of objects listening to changes to this manager. This
38      * collection is <code>null</code> if there are no listeners.
39      */

40     private transient ListenerList listenerList = null;
41
42     /**
43      * Adds a listener to this manager that will be notified when this manager's
44      * state changes.
45      *
46      * @param listener
47      * The listener to be added; must not be <code>null</code>.
48      */

49     protected synchronized final void addListenerObject(final Object JavaDoc listener) {
50         if (listenerList == null) {
51             listenerList = new ListenerList(ListenerList.IDENTITY);
52         }
53
54         listenerList.add(listener);
55     }
56
57     /**
58      * Clears all of the listeners from the listener list.
59      */

60     protected synchronized final void clearListeners() {
61         if (listenerList != null) {
62             listenerList.clear();
63         }
64     }
65
66     /**
67      * Returns the listeners attached to this event manager.
68      *
69      * @return The listeners currently attached; may be empty, but never
70      * <code>null</code>
71      */

72     protected final Object JavaDoc[] getListeners() {
73         final ListenerList list = listenerList;
74         if (list == null) {
75             return EMPTY_ARRAY;
76         }
77
78         return list.getListeners();
79     }
80
81     /**
82      * Whether one or more listeners are attached to the manager.
83      *
84      * @return <code>true</code> if listeners are attached to the manager;
85      * <code>false</code> otherwise.
86      */

87     protected final boolean isListenerAttached() {
88         return listenerList != null;
89     }
90
91     /**
92      * Removes a listener from this manager.
93      *
94      * @param listener
95      * The listener to be removed; must not be <code>null</code>.
96      */

97     protected synchronized final void removeListenerObject(final Object JavaDoc listener) {
98         if (listenerList != null) {
99             listenerList.remove(listener);
100
101             if (listenerList.isEmpty()) {
102                 listenerList = null;
103             }
104         }
105     }
106 }
107
Popular Tags