78
|
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 158687
|
|
11 * Brad Reynolds - bug 164653, 147515
|
|
12 *******************************************************************************/
|
|
13
|
|
14 module org.eclipse.core.databinding.observable.value.WritableValue;
|
81
|
15 import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
|
78
|
16
|
|
17 import java.lang.all;
|
|
18
|
|
19 import org.eclipse.core.databinding.observable.Diffs;
|
|
20 import org.eclipse.core.databinding.observable.Realm;
|
|
21
|
|
22 /**
|
|
23 * Mutable (writable) implementation of {@link IObservableValue} that will maintain a value and fire
|
|
24 * change events when the value changes.
|
|
25 * <p>
|
|
26 * This class is thread safe. All state accessing methods must be invoked from
|
|
27 * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
|
|
28 * listeners may be invoked from any thread.
|
|
29 * </p>
|
|
30 * @since 1.0
|
|
31 */
|
|
32 public class WritableValue : AbstractObservableValue {
|
|
33
|
|
34 private final Object valueType;
|
|
35
|
|
36 /**
|
|
37 * Constructs a new instance with the default realm, a <code>null</code>
|
|
38 * value type, and a <code>null</code> value.
|
|
39 */
|
|
40 public this() {
|
|
41 this(null, null);
|
|
42 }
|
|
43
|
|
44 /**
|
|
45 * Constructs a new instance with the default realm.
|
|
46 *
|
|
47 * @param initialValue
|
|
48 * can be <code>null</code>
|
|
49 * @param valueType
|
|
50 * can be <code>null</code>
|
|
51 */
|
|
52 public this(Object initialValue, Object valueType) {
|
|
53 this(Realm.getDefault(), initialValue, valueType);
|
|
54 }
|
|
55
|
|
56 /**
|
|
57 * Constructs a new instance with the provided <code>realm</code>, a
|
|
58 * <code>null</code> value type, and a <code>null</code> initial value.
|
|
59 *
|
|
60 * @param realm
|
|
61 */
|
|
62 public this(Realm realm) {
|
|
63 this(realm, null, null);
|
|
64 }
|
|
65
|
|
66 /**
|
|
67 * Constructs a new instance.
|
|
68 *
|
|
69 * @param realm
|
|
70 * @param initialValue
|
|
71 * can be <code>null</code>
|
|
72 * @param valueType
|
|
73 * can be <code>null</code>
|
|
74 */
|
|
75 public this(Realm realm, Object initialValue, Object valueType) {
|
|
76 super(realm);
|
|
77 this.valueType = valueType;
|
|
78 this.value = initialValue;
|
|
79 }
|
|
80
|
|
81 private Object value = null;
|
|
82
|
|
83 public Object doGetValue() {
|
|
84 return value;
|
|
85 }
|
|
86
|
|
87 /**
|
|
88 * @param value
|
|
89 * The value to set.
|
|
90 */
|
|
91 public void doSetValue(Object value) {
|
|
92 bool changed = false;
|
|
93
|
|
94 if (this.value is null && value !is null) {
|
|
95 changed = true;
|
|
96 } else if (this.value !is null && !this.value.equals(value)) {
|
|
97 changed = true;
|
|
98 }
|
|
99
|
|
100 if (changed) {
|
|
101 fireValueChange(Diffs.createValueDiff(this.value, this.value = value));
|
|
102 }
|
|
103 }
|
|
104
|
|
105 public Object getValueType() {
|
|
106 return valueType;
|
|
107 }
|
|
108
|
|
109 /**
|
|
110 * @param elementType can be <code>null</code>
|
|
111 * @return new instance with the default realm and a value of <code>null</code>
|
|
112 */
|
|
113 public static WritableValue withValueType(Object elementType) {
|
|
114 return new WritableValue(Realm.getDefault(), null, elementType);
|
|
115 }
|
|
116 }
|