KickJava   Java API By Example, From Geeks To Geeks.

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


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.value;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashSet JavaDoc;
17
18 import org.eclipse.jface.internal.databinding.provisional.observable.AbstractObservable;
19 import org.eclipse.jface.internal.databinding.provisional.observable.ObservableTracker;
20
21 /**
22  * @since 1.0
23  *
24  */

25 abstract public class AbstractObservableValue extends AbstractObservable
26         implements IObservableValue {
27
28     private Collection JavaDoc valueChangeListeners = null;
29
30     public void addValueChangeListener(IValueChangeListener listener) {
31         if (valueChangeListeners == null) {
32             boolean hadListeners = hasListeners();
33             valueChangeListeners = new ArrayList JavaDoc();
34             valueChangeListeners.add(listener);
35             if (!hadListeners) {
36                 firstListenerAdded();
37             }
38         } else if (valueChangeListeners.size() > 16) {
39             HashSet JavaDoc listenerList = new HashSet JavaDoc();
40             listenerList.addAll(valueChangeListeners);
41             valueChangeListeners = listenerList;
42             valueChangeListeners.add(listener);
43         } else {
44             valueChangeListeners.add(listener);
45         }
46     }
47
48     public void removeValueChangeListener(IValueChangeListener listener) {
49         if (valueChangeListeners == null) {
50             return;
51         }
52         valueChangeListeners.remove(listener);
53         if (valueChangeListeners.size() == 0) {
54             valueChangeListeners = null;
55         }
56         if (!hasListeners()) {
57             lastListenerRemoved();
58         }
59     }
60
61     public void setValue(Object JavaDoc value) {
62         throw new UnsupportedOperationException JavaDoc();
63     }
64
65     protected void fireValueChange(ValueDiff diff) {
66         // fire general change event first
67
super.fireChange();
68         if (valueChangeListeners != null) {
69             IValueChangeListener[] listeners = (IValueChangeListener[]) valueChangeListeners
70                     .toArray(new IValueChangeListener[valueChangeListeners
71                             .size()]);
72             for (int i = 0; i < listeners.length; i++) {
73                 listeners[i].handleValueChange(this, diff);
74             }
75         }
76     }
77
78     public final Object JavaDoc getValue() {
79         ObservableTracker.getterCalled(this);
80         return doGetValue();
81     }
82
83     abstract protected Object JavaDoc doGetValue();
84
85     public boolean isStale() {
86         return false;
87     }
88
89     /**
90      * @return
91      */

92     protected boolean hasListeners() {
93         return super.hasListeners() || valueChangeListeners != null;
94     }
95
96     protected void fireChange() {
97         throw new RuntimeException JavaDoc(
98                 "fireChange should not be called, use fireValueChange() instead"); //$NON-NLS-1$
99
}
100
101     /*
102      * (non-Javadoc)
103      *
104      * @see org.eclipse.jface.provisional.databinding.observable.AbstractObservable#dispose()
105      */

106     public void dispose() {
107         valueChangeListeners = null;
108         super.dispose();
109     }
110 }
111
Popular Tags