KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.core.databinding.observable.value;
15
16 import org.eclipse.core.databinding.observable.Diffs;
17 import org.eclipse.core.databinding.observable.Realm;
18
19 /**
20  * Mutable (writable) implementation of {@link IObservableValue} that will maintain a value and fire
21  * change events when the value changes.
22  * <p>
23  * This class is thread safe. All state accessing methods must be invoked from
24  * the {@link Realm#isCurrent() current realm}. Methods for adding and removing
25  * listeners may be invoked from any thread.
26  * </p>
27  * @since 1.0
28  */

29 public class WritableValue extends AbstractObservableValue {
30
31     private final Object JavaDoc valueType;
32
33     /**
34      * Constructs a new instance with the default realm, a <code>null</code>
35      * value type, and a <code>null</code> value.
36      */

37     public WritableValue() {
38         this(null, null);
39     }
40
41     /**
42      * Constructs a new instance with the default realm.
43      *
44      * @param initialValue
45      * can be <code>null</code>
46      * @param valueType
47      * can be <code>null</code>
48      */

49     public WritableValue(Object JavaDoc initialValue, Object JavaDoc valueType) {
50         this(Realm.getDefault(), initialValue, valueType);
51     }
52
53     /**
54      * Constructs a new instance with the provided <code>realm</code>, a
55      * <code>null</code> value type, and a <code>null</code> initial value.
56      *
57      * @param realm
58      */

59     public WritableValue(Realm realm) {
60         this(realm, null, null);
61     }
62
63     /**
64      * Constructs a new instance.
65      *
66      * @param realm
67      * @param initialValue
68      * can be <code>null</code>
69      * @param valueType
70      * can be <code>null</code>
71      */

72     public WritableValue(Realm realm, Object JavaDoc initialValue, Object JavaDoc valueType) {
73         super(realm);
74         this.valueType = valueType;
75         this.value = initialValue;
76     }
77
78     private Object JavaDoc value = null;
79
80     public Object JavaDoc doGetValue() {
81         return value;
82     }
83
84     /**
85      * @param value
86      * The value to set.
87      */

88     public void doSetValue(Object JavaDoc value) {
89         boolean changed = false;
90
91         if (this.value == null && value != null) {
92             changed = true;
93         } else if (this.value != null && !this.value.equals(value)) {
94             changed = true;
95         }
96
97         if (changed) {
98             fireValueChange(Diffs.createValueDiff(this.value, this.value = value));
99         }
100     }
101
102     public Object JavaDoc getValueType() {
103         return valueType;
104     }
105
106     /**
107      * @param elementType can be <code>null</code>
108      * @return new instance with the default realm and a value of <code>null</code>
109      */

110     public static WritableValue withValueType(Object JavaDoc elementType) {
111         return new WritableValue(Realm.getDefault(), null, elementType);
112     }
113 }
114
Popular Tags