Mercurial > projects > dwt2
annotate org.eclipse.core.databinding/src/org/eclipse/core/databinding/SetBinding.d @ 88:9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 19 Apr 2009 11:10:09 +0200 |
parents | 6be48cf9f95c |
children |
rev | line source |
---|---|
78 | 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 124684) | |
10 * IBM Corporation - through ListBinding.java | |
11 ******************************************************************************/ | |
12 | |
13 module org.eclipse.core.databinding.SetBinding; | |
81 | 14 import org.eclipse.core.databinding.UpdateSetStrategy; |
15 import org.eclipse.core.databinding.Binding; | |
78 | 16 |
17 import java.lang.all; | |
18 | |
19 import java.util.Collections; | |
20 import java.util.Iterator; | |
21 | |
22 import org.eclipse.core.databinding.observable.Diffs; | |
23 import org.eclipse.core.databinding.observable.set.IObservableSet; | |
24 import org.eclipse.core.databinding.observable.set.ISetChangeListener; | |
25 import org.eclipse.core.databinding.observable.set.SetChangeEvent; | |
26 import org.eclipse.core.databinding.observable.set.SetDiff; | |
27 import org.eclipse.core.databinding.observable.value.IObservableValue; | |
28 import org.eclipse.core.databinding.observable.value.WritableValue; | |
29 import org.eclipse.core.internal.databinding.BindingStatus; | |
30 import org.eclipse.core.runtime.IStatus; | |
31 import org.eclipse.core.runtime.MultiStatus; | |
32 import org.eclipse.core.runtime.Status; | |
33 | |
34 /** | |
35 * @since 1.1 | |
36 * | |
37 */ | |
38 public class SetBinding : Binding { | |
39 | |
40 private UpdateSetStrategy targetToModel; | |
41 private UpdateSetStrategy modelToTarget; | |
42 private IObservableValue validationStatusObservable; | |
43 private bool updatingTarget; | |
44 private bool updatingModel; | |
45 | |
85 | 46 private ISetChangeListener targetChangeListener; |
47 class TargetChangeListener : ISetChangeListener { | |
78 | 48 public void handleSetChange(SetChangeEvent event) { |
49 if (!updatingTarget) { | |
50 doUpdate(cast(IObservableSet) getTarget(), | |
51 cast(IObservableSet) getModel(), event.diff, targetToModel, | |
52 false, false); | |
53 } | |
54 } | |
55 }; | |
56 | |
85 | 57 private ISetChangeListener modelChangeListener; |
58 class ModelChangeListener : ISetChangeListener { | |
78 | 59 public void handleSetChange(SetChangeEvent event) { |
60 if (!updatingModel) { | |
61 doUpdate(cast(IObservableSet) getModel(), | |
62 cast(IObservableSet) getTarget(), event.diff, | |
63 modelToTarget, false, false); | |
64 } | |
65 } | |
66 }; | |
67 | |
68 /** | |
69 * @param target | |
70 * @param model | |
71 * @param modelToTargetStrategy | |
72 * @param targetToModelStrategy | |
73 */ | |
74 public this(IObservableSet target, IObservableSet model, | |
75 UpdateSetStrategy targetToModelStrategy, | |
76 UpdateSetStrategy modelToTargetStrategy) { | |
85 | 77 targetChangeListener = new TargetChangeListener(); |
78 modelChangeListener = new ModelChangeListener(); | |
78 | 79 super(target, model); |
80 this.targetToModel = targetToModelStrategy; | |
81 this.modelToTarget = modelToTargetStrategy; | |
82 if ((targetToModel.getUpdatePolicy() & UpdateSetStrategy.POLICY_UPDATE) !is 0) { | |
83 target.addSetChangeListener(targetChangeListener); | |
84 } else { | |
85 targetChangeListener = null; | |
86 } | |
87 if ((modelToTarget.getUpdatePolicy() & UpdateSetStrategy.POLICY_UPDATE) !is 0) { | |
88 model.addSetChangeListener(modelChangeListener); | |
89 } else { | |
90 modelChangeListener = null; | |
91 } | |
92 } | |
93 | |
94 public IObservableValue getValidationStatus() { | |
95 return validationStatusObservable; | |
96 } | |
97 | |
98 protected void preInit() { | |
99 validationStatusObservable = new WritableValue(context | |
88
9e0ab372d5d8
Revert from TypeInfo/ClassInfo to java.lang.Class
Frank Benoit <benoit@tionex.de>
parents:
85
diff
changeset
|
100 .getValidationRealm(), cast(Object)Status.OK_STATUS, Class.fromType!(IStatus)); |
78 | 101 } |
102 | |
103 protected void postInit() { | |
104 if (modelToTarget.getUpdatePolicy() is UpdateSetStrategy.POLICY_UPDATE) { | |
105 updateModelToTarget(); | |
106 } | |
107 if (targetToModel.getUpdatePolicy() !is UpdateSetStrategy.POLICY_NEVER) { | |
108 validateTargetToModel(); | |
109 } | |
110 } | |
111 | |
112 public void updateModelToTarget() { | |
113 final IObservableSet modelSet = cast(IObservableSet) getModel(); | |
114 modelSet.getRealm().exec(new class() Runnable { | |
115 public void run() { | |
116 SetDiff diff = Diffs.computeSetDiff(Collections.EMPTY_SET, | |
117 modelSet); | |
118 doUpdate(modelSet, cast(IObservableSet) getTarget(), diff, | |
119 modelToTarget, true, true); | |
120 } | |
121 }); | |
122 } | |
123 | |
124 public void updateTargetToModel() { | |
125 final IObservableSet targetSet = cast(IObservableSet) getTarget(); | |
126 targetSet.getRealm().exec(new class() Runnable { | |
127 public void run() { | |
128 SetDiff diff = Diffs.computeSetDiff(Collections.EMPTY_SET, | |
129 targetSet); | |
130 doUpdate(targetSet, cast(IObservableSet) getModel(), diff, | |
131 targetToModel, true, true); | |
132 } | |
133 }); | |
134 } | |
135 | |
136 public void validateModelToTarget() { | |
137 // nothing for now | |
138 } | |
139 | |
140 public void validateTargetToModel() { | |
141 // nothing for now | |
142 } | |
143 | |
144 /* | |
145 * This method may be moved to UpdateSetStrategy in the future if clients | |
146 * need more control over how the two sets are kept in sync. | |
147 */ | |
148 private void doUpdate(IObservableSet source, | |
149 IObservableSet destination, SetDiff diff, | |
150 UpdateSetStrategy updateSetStrategy, bool explicit, | |
151 bool clearDestination) { | |
152 final int policy = updateSetStrategy.getUpdatePolicy(); | |
153 if (policy is UpdateSetStrategy.POLICY_NEVER) | |
154 return; | |
155 if (policy is UpdateSetStrategy.POLICY_ON_REQUEST && !explicit) | |
156 return; | |
157 destination.getRealm().exec(dgRunnable((IObservableSet destination_, SetDiff diff_, UpdateSetStrategy updateSetStrategy_, bool clearDestination_) { | |
158 if (destination_ is getTarget()) { | |
159 updatingTarget = true; | |
160 } else { | |
161 updatingModel = true; | |
162 } | |
163 MultiStatus multiStatus = BindingStatus.ok(); | |
164 | |
165 try { | |
166 if (clearDestination_) { | |
167 destination_.clear(); | |
168 } | |
169 | |
170 for (Iterator iterator = diff_.getRemovals().iterator(); iterator | |
171 .hasNext();) { | |
85 | 172 IStatus setterStatus = updateSetStrategy_.doRemove_package( |
78 | 173 destination_, updateSetStrategy_.convert(iterator |
174 .next())); | |
175 | |
176 mergeStatus(multiStatus, setterStatus); | |
177 // TODO - at this point, the two sets | |
178 // will be out of sync if an error | |
179 // occurred... | |
180 } | |
181 | |
182 for (Iterator iterator = diff_.getAdditions().iterator(); iterator | |
183 .hasNext();) { | |
85 | 184 IStatus setterStatus = updateSetStrategy_.doAdd_package( |
78 | 185 destination_, updateSetStrategy_.convert(iterator |
186 .next())); | |
187 | |
188 mergeStatus(multiStatus, setterStatus); | |
189 // TODO - at this point, the two sets | |
190 // will be out of sync if an error | |
191 // occurred... | |
192 } | |
193 } finally { | |
194 validationStatusObservable.setValue(multiStatus); | |
195 | |
196 if (destination_ is getTarget()) { | |
197 updatingTarget = false; | |
198 } else { | |
199 updatingModel = false; | |
200 } | |
201 } | |
85 | 202 }, destination, diff, updateSetStrategy, clearDestination)); |
78 | 203 } |
204 | |
205 /** | |
206 * Merges the provided <code>newStatus</code> into the | |
207 * <code>multiStatus</code>. | |
208 * | |
209 * @param multiStatus | |
210 * @param newStatus | |
211 */ | |
212 /* package */void mergeStatus(MultiStatus multiStatus, IStatus newStatus) { | |
213 if (!newStatus.isOK()) { | |
214 multiStatus.add(newStatus); | |
215 } | |
216 } | |
217 | |
218 public void dispose() { | |
219 if (targetChangeListener !is null) { | |
220 (cast(IObservableSet) getTarget()) | |
221 .removeSetChangeListener(targetChangeListener); | |
222 targetChangeListener = null; | |
223 } | |
224 if (modelChangeListener !is null) { | |
225 (cast(IObservableSet) getModel()) | |
226 .removeSetChangeListener(modelChangeListener); | |
227 modelChangeListener = null; | |
228 } | |
229 super.dispose(); | |
230 } | |
231 } |