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 * Brad Reynolds - bug 164653
|
|
11 * Matthew Hall - bug 208332
|
|
12 *******************************************************************************/
|
|
13
|
|
14 module org.eclipse.core.databinding.observable.value.AbstractObservableValue;
|
81
|
15 import org.eclipse.core.databinding.observable.value.ValueDiff;
|
|
16 import org.eclipse.core.databinding.observable.value.IValueChangeListener;
|
|
17 import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
|
|
18 import org.eclipse.core.databinding.observable.value.IObservableValue;
|
78
|
19
|
|
20 import java.lang.all;
|
|
21
|
|
22 import org.eclipse.core.databinding.observable.AbstractObservable;
|
|
23 import org.eclipse.core.databinding.observable.ObservableTracker;
|
|
24 import org.eclipse.core.databinding.observable.Realm;
|
|
25
|
|
26 /**
|
|
27 *
|
|
28 * <p>
|
|
29 * This class is thread safe. All state accessing methods must be invoked from
|
|
30 * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
|
|
31 * listeners may be invoked from any thread.
|
|
32 * </p>
|
|
33 * @since 1.0
|
|
34 *
|
|
35 */
|
|
36 abstract public class AbstractObservableValue : AbstractObservable , IObservableValue {
|
|
37 /**
|
|
38 * Constructs a new instance with the default realm.
|
|
39 */
|
|
40 public this() {
|
|
41 this(Realm.getDefault());
|
|
42 }
|
|
43
|
|
44 /**
|
|
45 * @param realm
|
|
46 */
|
|
47 public this(Realm realm) {
|
|
48 super(realm);
|
|
49 }
|
|
50
|
|
51 public synchronized void addValueChangeListener(IValueChangeListener listener) {
|
|
52 addListener(ValueChangeEvent.TYPE, listener);
|
|
53 }
|
|
54
|
|
55 public synchronized void removeValueChangeListener(IValueChangeListener listener) {
|
|
56 removeListener(ValueChangeEvent.TYPE, listener);
|
|
57 }
|
|
58
|
|
59 final public void setValue(Object value) {
|
|
60 checkRealm();
|
|
61 doSetValue(value);
|
|
62 }
|
|
63
|
|
64 /**
|
|
65 * Template method for setting the value of the observable. By default the
|
|
66 * method throws an {@link UnsupportedOperationException}.
|
|
67 *
|
|
68 * @param value
|
|
69 */
|
|
70 protected void doSetValue(Object value) {
|
|
71 throw new UnsupportedOperationException();
|
|
72 }
|
|
73
|
|
74 protected void fireValueChange(ValueDiff diff) {
|
|
75 // fire general change event first
|
|
76 super.fireChange();
|
|
77 fireEvent(new ValueChangeEvent(this, diff));
|
|
78 }
|
|
79
|
|
80 public final Object getValue() {
|
|
81 getterCalled();
|
|
82 return doGetValue();
|
|
83 }
|
|
84
|
|
85 abstract protected Object doGetValue();
|
|
86
|
|
87 public bool isStale() {
|
|
88 getterCalled();
|
|
89 return false;
|
|
90 }
|
|
91
|
|
92 private void getterCalled() {
|
|
93 ObservableTracker.getterCalled(this);
|
|
94 }
|
|
95
|
|
96 protected void fireChange() {
|
|
97 throw new RuntimeException(
|
|
98 "fireChange should not be called, use fireValueChange() instead"); //$NON-NLS-1$
|
|
99 }
|
|
100
|
|
101 public synchronized void dispose() {
|
|
102 super.dispose();
|
|
103 }
|
|
104 }
|