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