Mercurial > projects > dwt2
comparison org.eclipse.core.databinding.observable/src/org/eclipse/core/internal/databinding/observable/ValidatedObservableValue.d @ 95:6208d4f6a277
Added trees for databinding.beans and observable
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 21 Apr 2009 10:55:51 +0200 |
parents | |
children | b74ac5dfcc06 |
comparison
equal
deleted
inserted
replaced
94:1d37a7813832 | 95:6208d4f6a277 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2008 Matthew Hall 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 * Matthew Hall - initial API and implementation (bug 218269) | |
10 ******************************************************************************/ | |
11 | |
12 package org.eclipse.core.internal.databinding.observable; | |
13 | |
14 import org.eclipse.core.databinding.observable.Diffs; | |
15 import org.eclipse.core.databinding.observable.IStaleListener; | |
16 import org.eclipse.core.databinding.observable.ObservableTracker; | |
17 import org.eclipse.core.databinding.observable.StaleEvent; | |
18 import org.eclipse.core.databinding.observable.value.AbstractObservableValue; | |
19 import org.eclipse.core.databinding.observable.value.IObservableValue; | |
20 import org.eclipse.core.databinding.observable.value.IValueChangeListener; | |
21 import org.eclipse.core.databinding.observable.value.IVetoableValue; | |
22 import org.eclipse.core.databinding.observable.value.ValueChangeEvent; | |
23 import org.eclipse.core.databinding.observable.value.ValueChangingEvent; | |
24 import org.eclipse.core.internal.databinding.Util; | |
25 import org.eclipse.core.runtime.Assert; | |
26 import org.eclipse.core.runtime.IStatus; | |
27 | |
28 /** | |
29 * An {@link IObservableValue} wrapper that stays in sync with the target | |
30 * observable as long as a given validation status is valid. | |
31 * <ul> | |
32 * <li>While status is valid, ValidatedObservableValue stays in sync with its | |
33 * target. | |
34 * <li>When status becomes invalid, ValidatedObservableValue will retain the | |
35 * last valid value of its target. | |
36 * <li>While status is invalid, changes in the target observable cause | |
37 * ValidatedObservableValue to fire a stale event, to indicate that changes are | |
38 * pending. | |
39 * <li>When status becomes valid, pending value changes are performed (if any) | |
40 * and synchronization resumes. | |
41 * </ul> | |
42 * <p> | |
43 * Note: | |
44 * <ul> | |
45 * <li>By default, a status is valid if its | |
46 * {@link IStatus#getSeverity() severity} is {@link IStatus#OK OK}, | |
47 * {@link IStatus#INFO INFO}, or {@link IStatus#WARNING WARNING} | |
48 * <li>Calls to {@link #setValuecast(Object)} on the validated observable changes | |
49 * the value regardless of the validation status. | |
50 * <li>This class will not forward {@link ValueChangingEvent} events from a | |
51 * wrapped {@link IVetoableValue}. | |
52 * </ul> | |
53 * | |
54 * @since 1.2 | |
55 */ | |
56 public class ValidatedObservableValue : AbstractObservableValue { | |
57 private IObservableValue target; | |
58 private IObservableValue validationStatus; | |
59 | |
60 private Object cachedValue; | |
61 private bool stale; | |
62 private bool updatingTarget = false; | |
63 | |
64 private IValueChangeListener targetChangeListener = new class() IValueChangeListener { | |
65 public void handleValueChange(ValueChangeEvent event) { | |
66 if (updatingTarget) | |
67 return; | |
68 IStatus status = cast(IStatus) validationStatus.getValue(); | |
69 if (isValid(status)) | |
70 internalSetValue(event.diff.getNewValue(), false); | |
71 else | |
72 makeStale(); | |
73 } | |
74 }; | |
75 | |
76 private static bool isValid(IStatus status) { | |
77 return status.isOK() || status.matches(IStatus.INFO | IStatus.WARNING); | |
78 } | |
79 | |
80 private IStaleListener targetStaleListener = new class() IStaleListener { | |
81 public void handleStale(StaleEvent staleEvent) { | |
82 fireStale(); | |
83 } | |
84 }; | |
85 | |
86 private IValueChangeListener validationStatusChangeListener = new class() IValueChangeListener { | |
87 public void handleValueChange(ValueChangeEvent event) { | |
88 IStatus oldStatus = cast(IStatus) event.diff.getOldValue(); | |
89 IStatus newStatus = cast(IStatus) event.diff.getNewValue(); | |
90 if (stale && !isValid(oldStatus) && isValid(newStatus)) { | |
91 internalSetValue(target.getValue(), false); | |
92 } | |
93 } | |
94 }; | |
95 | |
96 /** | |
97 * Constructs an observable value | |
98 * | |
99 * @param target | |
100 * the observable value to be wrapped | |
101 * @param validationStatus | |
102 * an observable value of type {@link IStatus}.class which | |
103 * contains the current validation status | |
104 */ | |
105 public this(IObservableValue target, | |
106 IObservableValue validationStatus) { | |
107 super(target.getRealm()); | |
108 Assert.isNotNull(validationStatus, | |
109 "Validation status observable cannot be null"); //$NON-NLS-1$ | |
110 Assert | |
111 .isTrue(target.getRealm().equals(validationStatus.getRealm()), | |
112 "Target and validation status observables must be on the same realm"); //$NON-NLS-1$ | |
113 this.target = target; | |
114 this.validationStatus = validationStatus; | |
115 this.cachedValue = target.getValue(); | |
116 | |
117 target.addValueChangeListener(targetChangeListener); | |
118 target.addStaleListener(targetStaleListener); | |
119 validationStatus.addValueChangeListener(validationStatusChangeListener); | |
120 } | |
121 | |
122 private void makeStale() { | |
123 if (!stale) { | |
124 stale = true; | |
125 fireStale(); | |
126 } | |
127 } | |
128 | |
129 public bool isStale() { | |
130 ObservableTracker.getterCalled(this); | |
131 return stale || target.isStale(); | |
132 } | |
133 | |
134 protected Object doGetValue() { | |
135 return cachedValue; | |
136 } | |
137 | |
138 private void internalSetValue(Object value, bool updateTarget) { | |
139 Object oldValue = cachedValue; | |
140 cachedValue = value; | |
141 if (updateTarget) { | |
142 updatingTarget = true; | |
143 try { | |
144 target.setValue(value); | |
145 cachedValue = target.getValue(); | |
146 } finally { | |
147 updatingTarget = false; | |
148 } | |
149 } | |
150 stale = false; | |
151 if (!Util.equals(oldValue, cachedValue)) | |
152 fireValueChange(Diffs.createValueDiff(oldValue, cachedValue)); | |
153 } | |
154 | |
155 protected void doSetValue(Object value) { | |
156 internalSetValue(value, true); | |
157 } | |
158 | |
159 public Object getValueType() { | |
160 return target.getValueType(); | |
161 } | |
162 | |
163 public synchronized void dispose() { | |
164 target.removeValueChangeListener(targetChangeListener); | |
165 target.removeStaleListener(targetStaleListener); | |
166 validationStatus | |
167 .removeValueChangeListener(validationStatusChangeListener); | |
168 super.dispose(); | |
169 } | |
170 } |