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