KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > databinding > observable > value > AbstractObservableValue


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  * Brad Reynolds - bug 164653
11  *******************************************************************************/

12
13 package org.eclipse.core.databinding.observable.value;
14
15 import org.eclipse.core.databinding.observable.AbstractObservable;
16 import org.eclipse.core.databinding.observable.ObservableTracker;
17 import org.eclipse.core.databinding.observable.Realm;
18
19 /**
20  *
21  * <p>
22  * This class is thread safe. All state accessing methods must be invoked from
23  * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
24  * listeners may be invoked from any thread.
25  * </p>
26  * @since 1.0
27  *
28  */

29 abstract public class AbstractObservableValue extends AbstractObservable implements IObservableValue {
30     /**
31      * Constructs a new instance with the default realm.
32      */

33     public AbstractObservableValue() {
34         this(Realm.getDefault());
35     }
36
37     /**
38      * @param realm
39      */

40     public AbstractObservableValue(Realm realm) {
41         super(realm);
42     }
43
44     public synchronized void addValueChangeListener(IValueChangeListener listener) {
45         addListener(ValueChangeEvent.TYPE, listener);
46     }
47
48     public synchronized void removeValueChangeListener(IValueChangeListener listener) {
49         removeListener(ValueChangeEvent.TYPE, listener);
50     }
51
52     final public void setValue(Object JavaDoc value) {
53         checkRealm();
54         doSetValue(value);
55     }
56
57     /**
58      * Template method for setting the value of the observable. By default the
59      * method throws an {@link UnsupportedOperationException}.
60      *
61      * @param value
62      */

63     protected void doSetValue(Object JavaDoc value) {
64         throw new UnsupportedOperationException JavaDoc();
65     }
66
67     protected void fireValueChange(ValueDiff diff) {
68         // fire general change event first
69
super.fireChange();
70         fireEvent(new ValueChangeEvent(this, diff));
71     }
72
73     public final Object JavaDoc getValue() {
74         ObservableTracker.getterCalled(this);
75         return doGetValue();
76     }
77
78     abstract protected Object JavaDoc doGetValue();
79
80     public boolean isStale() {
81         return false;
82     }
83
84     protected void fireChange() {
85         throw new RuntimeException JavaDoc(
86                 "fireChange should not be called, use fireValueChange() instead"); //$NON-NLS-1$
87
}
88
89     public synchronized void dispose() {
90         super.dispose();
91     }
92 }
93
Popular Tags