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