comparison org.eclipse.core.databinding/src/org/eclipse/core/databinding/observable/ChangeManager.d @ 78:0a55d2d5a946

Added file for databinding
author Frank Benoit <benoit@tionex.de>
date Tue, 14 Apr 2009 11:35:29 +0200
parents
children 383ce7bd736b
comparison
equal deleted inserted replaced
76:f05e6e8b2f2d 78:0a55d2d5a946
1 /*******************************************************************************
2 * Copyright (c) 2006, 2008 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 * Matthew Hall - bug 118516
11 *******************************************************************************/
12
13 module org.eclipse.core.databinding.observable.ChangeManager;
14
15 import java.lang.all;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.ListenerList;
19
20 /**
21 * Listener management implementation. Exposed to subclasses in form of
22 * {@link AbstractObservable} and {@link ChangeSupport}.
23 *
24 * @since 1.0
25 *
26 */
27 /* package */ class ChangeManager {
28
29 ListenerList[] listenerLists = null;
30 Object listenerTypes[] = null;
31 private Realm realm;
32
33 /**
34 * @param realm
35 *
36 */
37 /* package */ this(Realm realm) {
38 Assert.isNotNull(realm, "Realm cannot be null"); //$NON-NLS-1$
39 this.realm = realm;
40 }
41
42 /**
43 * @param listenerType
44 * @param listener
45 */
46 protected void addListener(Object listenerType,
47 IObservablesListener listener) {
48 int listenerTypeIndex = findListenerTypeIndex(listenerType);
49 if (listenerTypeIndex is -1) {
50 int length;
51 if (listenerTypes is null) {
52 length = 0;
53 listenerTypes = new Object[1];
54 listenerLists = new ListenerList[1];
55 } else {
56 length = listenerTypes.length;
57 System.arraycopy(listenerTypes, 0,
58 listenerTypes = new Object[length + 1], 0, length);
59 System
60 .arraycopy(listenerLists, 0,
61 listenerLists = new ListenerList[length + 1],
62 0, length);
63 }
64 listenerTypes[length] = listenerType;
65 listenerLists[length] = new ListenerList();
66 bool hadListeners = hasListeners();
67 listenerLists[length].add(listener);
68 if (!hadListeners) {
69 this.firstListenerAdded();
70 }
71 return;
72 }
73 ListenerList listenerList = listenerLists[listenerTypeIndex];
74 bool hadListeners = true;
75 if (listenerList.size() is 0) {
76 hadListeners = hasListeners();
77 }
78 listenerList.add(listener);
79 if (!hadListeners) {
80 firstListenerAdded();
81 }
82 }
83
84 /**
85 * @param listenerType
86 * @param listener
87 */
88 protected void removeListener(Object listenerType,
89 IObservablesListener listener) {
90 int listenerTypeIndex = findListenerTypeIndex(listenerType);
91 if (listenerTypeIndex !is -1) {
92 listenerLists[listenerTypeIndex].remove(listener);
93 if (listenerLists[listenerTypeIndex].size() is 0) {
94 if (!hasListeners()) {
95 this.lastListenerRemoved();
96 }
97 }
98 }
99 }
100
101 protected bool hasListeners() {
102 if (listenerTypes is null) {
103 return false;
104 }
105 for (int i = 0; i < listenerTypes.length; i++) {
106 if (listenerLists[i].size() > 0) {
107 return true;
108 }
109 }
110 return false;
111 }
112
113 private int findListenerTypeIndex(Object listenerType) {
114 if (listenerTypes !is null) {
115 for (int i = 0; i < listenerTypes.length; i++) {
116 if (listenerTypes[i] is listenerType) {
117 return i;
118 }
119 }
120 }
121 return -1;
122 }
123
124 protected void fireEvent(ObservableEvent event) {
125 Object listenerType = event.getListenerType();
126 int listenerTypeIndex = findListenerTypeIndex(listenerType);
127 if (listenerTypeIndex !is -1) {
128 Object[] listeners = listenerLists[listenerTypeIndex]
129 .getListeners();
130 for (int i = 0; i < listeners.length; i++) {
131 event.dispatch(cast(IObservablesListener) listeners[i]);
132 }
133 }
134 }
135
136 /**
137 *
138 */
139 protected void firstListenerAdded() {
140 }
141
142 /**
143 *
144 */
145 protected void lastListenerRemoved() {
146 }
147
148 /**
149 *
150 */
151 public void dispose() {
152 listenerLists = null;
153 listenerTypes = null;
154 realm = null;
155 }
156
157 /**
158 * @return Returns the realm.
159 */
160 public Realm getRealm() {
161 return realm;
162 }
163
164 }