comparison org.eclipse.core.databinding/src/org/eclipse/core/databinding/observable/value/WritableValue.d @ 78:0a55d2d5a946

Added file for databinding
author Frank Benoit <benoit@tionex.de>
date Tue, 14 Apr 2009 11:35:29 +0200
parents
children 383ce7bd736b
comparison
equal deleted inserted replaced
76:f05e6e8b2f2d 78:0a55d2d5a946
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;
15
16 import java.lang.all;
17
18 import org.eclipse.core.databinding.observable.Diffs;
19 import org.eclipse.core.databinding.observable.Realm;
20
21 /**
22 * Mutable (writable) implementation of {@link IObservableValue} that will maintain a value and fire
23 * change events when the value changes.
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 public class WritableValue : AbstractObservableValue {
32
33 private final Object valueType;
34
35 /**
36 * Constructs a new instance with the default realm, a <code>null</code>
37 * value type, and a <code>null</code> value.
38 */
39 public this() {
40 this(null, null);
41 }
42
43 /**
44 * Constructs a new instance with the default realm.
45 *
46 * @param initialValue
47 * can be <code>null</code>
48 * @param valueType
49 * can be <code>null</code>
50 */
51 public this(Object initialValue, Object valueType) {
52 this(Realm.getDefault(), initialValue, valueType);
53 }
54
55 /**
56 * Constructs a new instance with the provided <code>realm</code>, a
57 * <code>null</code> value type, and a <code>null</code> initial value.
58 *
59 * @param realm
60 */
61 public this(Realm realm) {
62 this(realm, null, null);
63 }
64
65 /**
66 * Constructs a new instance.
67 *
68 * @param realm
69 * @param initialValue
70 * can be <code>null</code>
71 * @param valueType
72 * can be <code>null</code>
73 */
74 public this(Realm realm, Object initialValue, Object valueType) {
75 super(realm);
76 this.valueType = valueType;
77 this.value = initialValue;
78 }
79
80 private Object value = null;
81
82 public Object doGetValue() {
83 return value;
84 }
85
86 /**
87 * @param value
88 * The value to set.
89 */
90 public void doSetValue(Object value) {
91 bool changed = false;
92
93 if (this.value is null && value !is null) {
94 changed = true;
95 } else if (this.value !is null && !this.value.equals(value)) {
96 changed = true;
97 }
98
99 if (changed) {
100 fireValueChange(Diffs.createValueDiff(this.value, this.value = value));
101 }
102 }
103
104 public Object getValueType() {
105 return valueType;
106 }
107
108 /**
109 * @param elementType can be <code>null</code>
110 * @return new instance with the default realm and a value of <code>null</code>
111 */
112 public static WritableValue withValueType(Object elementType) {
113 return new WritableValue(Realm.getDefault(), null, elementType);
114 }
115 }