KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > observable > AbstractObservable


1 /*******************************************************************************
2  * Copyright (c) 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.jface.internal.databinding.provisional.observable;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashSet JavaDoc;
17
18 /**
19  * @since 1.0
20  *
21  */

22 public abstract class AbstractObservable implements IObservable {
23
24     /**
25      * Points to an instance of IChangeListener or a Collection of
26      * IChangeListener
27      */

28     private Object JavaDoc changeListeners = null;
29
30     /**
31      * Points to an instance of IChangeListener or a Collection of
32      * IChangeListener
33      */

34     private Object JavaDoc staleListeners = null;
35
36     public void addChangeListener(IChangeListener listener) {
37         if (changeListeners == null) {
38             boolean hadListeners = hasListeners();
39             changeListeners = listener;
40             if (!hadListeners) {
41                 firstListenerAdded();
42             }
43             return;
44         }
45
46         Collection JavaDoc listenerList;
47         if (changeListeners instanceof IChangeListener) {
48             IChangeListener l = (IChangeListener) changeListeners;
49
50             listenerList = new ArrayList JavaDoc();
51             listenerList.add(l);
52         } else {
53             listenerList = (Collection JavaDoc) changeListeners;
54         }
55
56         if (listenerList.size() > 16) {
57             HashSet JavaDoc listenerSet = new HashSet JavaDoc();
58             listenerSet.addAll(listenerList);
59             changeListeners = listenerList;
60         }
61
62         listenerList.add(listener);
63     }
64
65     public void removeChangeListener(IChangeListener listener) {
66         if (changeListeners == listener) {
67             changeListeners = null;
68             if (!hasListeners()) {
69                 lastListenerRemoved();
70             }
71             return;
72         }
73
74         if (changeListeners instanceof Collection JavaDoc) {
75             Collection JavaDoc listenerList = (Collection JavaDoc) changeListeners;
76             listenerList.remove(listener);
77             if (listenerList.size() == 0) {
78                 changeListeners = null;
79                 if (!hasListeners()) {
80                     lastListenerRemoved();
81                 }
82             }
83         }
84     }
85
86     public void addStaleListener(IStaleListener listener) {
87         if (staleListeners == null) {
88             boolean hadListeners = hasListeners();
89             staleListeners = listener;
90             if (!hadListeners) {
91                 firstListenerAdded();
92             }
93             return;
94         }
95
96         Collection JavaDoc listenerList;
97         if (staleListeners instanceof IStaleListener) {
98             IStaleListener l = (IStaleListener) staleListeners;
99
100             listenerList = new ArrayList JavaDoc();
101             listenerList.add(l);
102         } else {
103             listenerList = (Collection JavaDoc) staleListeners;
104         }
105
106         if (listenerList.size() > 16) {
107             HashSet JavaDoc listenerSet = new HashSet JavaDoc();
108             listenerSet.addAll(listenerList);
109             staleListeners = listenerList;
110         }
111
112         listenerList.add(listener);
113     }
114
115     public void removeStaleListener(IStaleListener listener) {
116         if (staleListeners == listener) {
117             staleListeners = null;
118             if (!hasListeners()) {
119                 lastListenerRemoved();
120             }
121             return;
122         }
123
124         if (staleListeners instanceof Collection JavaDoc) {
125             Collection JavaDoc listenerList = (Collection JavaDoc) staleListeners;
126             listenerList.remove(listener);
127             if (listenerList.size() == 0) {
128                 staleListeners = null;
129                 if (!hasListeners()) {
130                     lastListenerRemoved();
131                 }
132             }
133         }
134     }
135
136     protected void fireChange() {
137         if (changeListeners == null) {
138             return;
139         }
140
141         if (changeListeners instanceof IChangeListener) {
142             ((IChangeListener) changeListeners).handleChange(this);
143             return;
144         }
145
146         Collection JavaDoc changeListenerCollection = (Collection JavaDoc) changeListeners;
147
148         IChangeListener[] listeners = (IChangeListener[]) (changeListenerCollection)
149                 .toArray(new IChangeListener[changeListenerCollection.size()]);
150         for (int i = 0; i < listeners.length; i++) {
151             listeners[i].handleChange(this);
152         }
153     }
154
155     protected void fireStale() {
156         if (staleListeners == null) {
157             return;
158         }
159
160         if (staleListeners instanceof IChangeListener) {
161             ((IChangeListener) staleListeners).handleChange(this);
162             return;
163         }
164
165         Collection JavaDoc changeListenerCollection = (Collection JavaDoc) staleListeners;
166
167         IChangeListener[] listeners = (IChangeListener[]) (changeListenerCollection)
168                 .toArray(new IChangeListener[changeListenerCollection.size()]);
169         for (int i = 0; i < listeners.length; i++) {
170             listeners[i].handleChange(this);
171         }
172     }
173
174     /**
175      * @return true if this observable has listeners
176      */

177     protected boolean hasListeners() {
178         return changeListeners != null || staleListeners != null;
179     }
180
181     /**
182      *
183      */

184     protected void firstListenerAdded() {
185     }
186
187     /**
188      *
189      */

190     protected void lastListenerRemoved() {
191     }
192
193     /**
194      *
195      */

196     public void dispose() {
197         changeListeners = null;
198         staleListeners = null;
199     }
200
201 }
202
Popular Tags