KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > databinding > observable > ChangeManager


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.databinding.observable;
13
14 import org.eclipse.core.runtime.Assert;
15 import org.eclipse.core.runtime.ListenerList;
16
17 /**
18  * Listener management implementation. Exposed to subclasses in form of
19  * {@link AbstractObservable} and {@link ChangeSupport}.
20  *
21  * @since 1.0
22  *
23  */

24 /* package */ class ChangeManager {
25
26     ListenerList[] listenerLists = null;
27     Object JavaDoc listenerTypes[] = null;
28     private Realm realm;
29
30     /**
31      * @param realm
32      *
33      */

34     /* package */ ChangeManager(Realm realm) {
35         Assert.isNotNull(realm);
36         this.realm = realm;
37     }
38
39     /**
40      * @param listenerType
41      * @param listener
42      */

43     protected void addListener(Object JavaDoc listenerType,
44             IObservablesListener listener) {
45         int listenerTypeIndex = findListenerTypeIndex(listenerType);
46         if (listenerTypeIndex == -1) {
47             int length;
48             if (listenerTypes == null) {
49                 length = 0;
50                 listenerTypes = new Object JavaDoc[1];
51                 listenerLists = new ListenerList[1];
52             } else {
53                 length = listenerTypes.length;
54                 System.arraycopy(listenerTypes, 0,
55                         listenerTypes = new Object JavaDoc[length + 1], 0, length);
56                 System
57                         .arraycopy(listenerLists, 0,
58                                 listenerLists = new ListenerList[length + 1],
59                                 0, length);
60             }
61             listenerTypes[length] = listenerType;
62             listenerLists[length] = new ListenerList();
63             boolean hadListeners = hasListeners();
64             listenerLists[length].add(listener);
65             if (!hadListeners) {
66                 this.firstListenerAdded();
67             }
68             return;
69         }
70         ListenerList listenerList = listenerLists[listenerTypeIndex];
71         boolean hadListeners = true;
72         if (listenerList.size() == 0) {
73             hadListeners = hasListeners();
74         }
75         listenerList.add(listener);
76         if (!hadListeners) {
77             firstListenerAdded();
78         }
79     }
80
81     /**
82      * @param listenerType
83      * @param listener
84      */

85     protected void removeListener(Object JavaDoc listenerType,
86             IObservablesListener listener) {
87         int listenerTypeIndex = findListenerTypeIndex(listenerType);
88         if (listenerTypeIndex != -1) {
89             listenerLists[listenerTypeIndex].remove(listener);
90             if (listenerLists[listenerTypeIndex].size() == 0) {
91                 if (!hasListeners()) {
92                     this.lastListenerRemoved();
93                 }
94             }
95         }
96     }
97
98     protected boolean hasListeners() {
99         if (listenerTypes == null) {
100             return false;
101         }
102         for (int i = 0; i < listenerTypes.length; i++) {
103             if (listenerLists[i].size() > 0) {
104                 return true;
105             }
106         }
107         return false;
108     }
109
110     private int findListenerTypeIndex(Object JavaDoc listenerType) {
111         if (listenerTypes != null) {
112             for (int i = 0; i < listenerTypes.length; i++) {
113                 if (listenerTypes[i] == listenerType) {
114                     return i;
115                 }
116             }
117         }
118         return -1;
119     }
120
121     protected void fireEvent(ObservableEvent event) {
122         Object JavaDoc listenerType = event.getListenerType();
123         int listenerTypeIndex = findListenerTypeIndex(listenerType);
124         if (listenerTypeIndex != -1) {
125             Object JavaDoc[] listeners = listenerLists[listenerTypeIndex]
126                     .getListeners();
127             for (int i = 0; i < listeners.length; i++) {
128                 event.dispatch((IObservablesListener) listeners[i]);
129             }
130         }
131     }
132
133     /**
134      *
135      */

136     protected void firstListenerAdded() {
137     }
138
139     /**
140      *
141      */

142     protected void lastListenerRemoved() {
143     }
144
145     /**
146      *
147      */

148     public void dispose() {
149         listenerLists = null;
150         listenerTypes = null;
151         realm = null;
152     }
153
154     /**
155      * @return Returns the realm.
156      */

157     public Realm getRealm() {
158         return realm;
159     }
160
161 }
162
Popular Tags