78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 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 ******************************************************************************/
|
|
11
|
|
12 module org.eclipse.core.databinding.ListBinding;
|
81
|
13 import org.eclipse.core.databinding.UpdateListStrategy;
|
|
14 import org.eclipse.core.databinding.UpdateValueStrategy;
|
|
15 import org.eclipse.core.databinding.Binding;
|
78
|
16
|
|
17 import java.lang.all;
|
|
18
|
|
19 import java.util.Collections;
|
|
20
|
|
21 import org.eclipse.core.databinding.observable.Diffs;
|
|
22 import org.eclipse.core.databinding.observable.list.IListChangeListener;
|
|
23 import org.eclipse.core.databinding.observable.list.IObservableList;
|
|
24 import org.eclipse.core.databinding.observable.list.ListChangeEvent;
|
|
25 import org.eclipse.core.databinding.observable.list.ListDiff;
|
|
26 import org.eclipse.core.databinding.observable.list.ListDiffEntry;
|
|
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.0
|
|
36 *
|
|
37 */
|
|
38 public class ListBinding : Binding {
|
|
39
|
|
40 private UpdateListStrategy targetToModel;
|
|
41 private UpdateListStrategy modelToTarget;
|
|
42 private IObservableValue validationStatusObservable;
|
|
43 private bool updatingTarget;
|
|
44 private bool updatingModel;
|
|
45
|
|
46 private IListChangeListener targetChangeListener = new class() IListChangeListener {
|
|
47 public void handleListChange(ListChangeEvent event) {
|
|
48 if (!updatingTarget) {
|
|
49 doUpdate(cast(IObservableList) getTarget(),
|
|
50 cast(IObservableList) getModel(), event.diff,
|
|
51 targetToModel, false, false);
|
|
52 }
|
|
53 }
|
|
54 };
|
|
55 private IListChangeListener modelChangeListener = new class() IListChangeListener {
|
|
56 public void handleListChange(ListChangeEvent event) {
|
|
57 if (!updatingModel) {
|
|
58 doUpdate(cast(IObservableList) getModel(),
|
|
59 cast(IObservableList) getTarget(), event.diff,
|
|
60 modelToTarget, false, false);
|
|
61 }
|
|
62 }
|
|
63 };
|
|
64
|
|
65 /**
|
|
66 * @param target
|
|
67 * @param model
|
|
68 * @param modelToTargetStrategy
|
|
69 * @param targetToModelStrategy
|
|
70 */
|
|
71 public this(IObservableList target, IObservableList model,
|
|
72 UpdateListStrategy targetToModelStrategy,
|
|
73 UpdateListStrategy modelToTargetStrategy) {
|
|
74 super(target, model);
|
|
75 this.targetToModel = targetToModelStrategy;
|
|
76 this.modelToTarget = modelToTargetStrategy;
|
|
77 if ((targetToModel.getUpdatePolicy() & UpdateValueStrategy.POLICY_UPDATE) !is 0) {
|
|
78 target.addListChangeListener(targetChangeListener);
|
|
79 } else {
|
|
80 targetChangeListener = null;
|
|
81 }
|
|
82 if ((modelToTarget.getUpdatePolicy() & UpdateValueStrategy.POLICY_UPDATE) !is 0) {
|
|
83 model.addListChangeListener(modelChangeListener);
|
|
84 } else {
|
|
85 modelChangeListener = null;
|
|
86 }
|
|
87 }
|
|
88
|
|
89 public IObservableValue getValidationStatus() {
|
|
90 return validationStatusObservable;
|
|
91 }
|
|
92
|
|
93 protected void preInit() {
|
|
94 validationStatusObservable = new WritableValue(context
|
|
95 .getValidationRealm(), Status.OK_STATUS, IStatus.classinfo);
|
|
96 }
|
|
97
|
|
98 protected void postInit() {
|
|
99 if (modelToTarget.getUpdatePolicy() is UpdateListStrategy.POLICY_UPDATE) {
|
|
100 updateModelToTarget();
|
|
101 }
|
|
102 if (targetToModel.getUpdatePolicy() !is UpdateListStrategy.POLICY_NEVER) {
|
|
103 validateTargetToModel();
|
|
104 }
|
|
105 }
|
|
106
|
|
107 public void updateModelToTarget() {
|
|
108 final IObservableList modelList = cast(IObservableList) getModel();
|
|
109 modelList.getRealm().exec(new class() Runnable {
|
|
110 public void run() {
|
|
111 ListDiff diff = Diffs.computeListDiff(Collections.EMPTY_LIST,
|
|
112 modelList);
|
|
113 doUpdate(modelList, cast(IObservableList) getTarget(), diff,
|
|
114 modelToTarget, true, true);
|
|
115 }
|
|
116 });
|
|
117 }
|
|
118
|
|
119 public void updateTargetToModel() {
|
|
120 final IObservableList targetList = cast(IObservableList) getTarget();
|
|
121 targetList.getRealm().exec(new class() Runnable {
|
|
122 public void run() {
|
|
123 ListDiff diff = Diffs.computeListDiff(Collections.EMPTY_LIST,
|
|
124 targetList);
|
|
125 doUpdate(targetList, cast(IObservableList) getModel(), diff,
|
|
126 targetToModel, true, true);
|
|
127 }
|
|
128 });
|
|
129 }
|
|
130
|
|
131 public void validateModelToTarget() {
|
|
132 // nothing for now
|
|
133 }
|
|
134
|
|
135 public void validateTargetToModel() {
|
|
136 // nothing for now
|
|
137 }
|
|
138
|
|
139 /*
|
|
140 * This method may be moved to UpdateListStrategy in the future if clients
|
|
141 * need more control over how the two lists are kept in sync.
|
|
142 */
|
|
143 private void doUpdate(IObservableList source,
|
|
144 IObservableList destination, ListDiff diff,
|
|
145 UpdateListStrategy updateListStrategy,
|
|
146 bool explicit, bool clearDestination) {
|
|
147 final int policy = updateListStrategy.getUpdatePolicy();
|
|
148 if (policy !is UpdateListStrategy.POLICY_NEVER) {
|
|
149 if (policy !is UpdateListStrategy.POLICY_ON_REQUEST || explicit) {
|
|
150 destination.getRealm().exec(dgRunnable((
|
|
151 IObservableList destination_,
|
|
152 ListDiff diff_,
|
|
153 UpdateListStrategy updateListStrategy_,
|
|
154 bool clearDestination_) {
|
|
155 if (destination_ is getTarget()) {
|
|
156 updatingTarget = true;
|
|
157 } else {
|
|
158 updatingModel = true;
|
|
159 }
|
|
160 MultiStatus multiStatus = BindingStatus.ok();
|
|
161
|
|
162 try {
|
|
163 if (clearDestination_) {
|
|
164 destination_.clear();
|
|
165 }
|
|
166 ListDiffEntry[] diffEntries = diff_.getDifferences();
|
|
167 for (int i = 0; i < diffEntries.length; i++) {
|
|
168 ListDiffEntry listDiffEntry = diffEntries[i];
|
|
169 if (listDiffEntry.isAddition()) {
|
|
170 IStatus setterStatus = updateListStrategy_
|
|
171 .doAdd(
|
|
172 destination_,
|
|
173 updateListStrategy_
|
|
174 .convert(listDiffEntry
|
|
175 .getElement()),
|
|
176 listDiffEntry.getPosition());
|
|
177
|
|
178 mergeStatus(multiStatus, setterStatus);
|
|
179 // TODO - at this point, the two lists
|
|
180 // will be out of sync if an error occurred...
|
|
181 } else {
|
|
182 IStatus setterStatus = updateListStrategy_
|
|
183 .doRemove(destination_,
|
|
184 listDiffEntry.getPosition());
|
|
185
|
|
186 mergeStatus(multiStatus, setterStatus);
|
|
187 // TODO - at this point, the two lists
|
|
188 // will be out of sync if an error occurred...
|
|
189 }
|
|
190 }
|
|
191 } finally {
|
|
192 validationStatusObservable.setValue(multiStatus);
|
|
193
|
|
194 if (destination_ is getTarget()) {
|
|
195 updatingTarget = false;
|
|
196 } else {
|
|
197 updatingModel = false;
|
|
198 }
|
|
199 }
|
|
200 }, destination, diff, updateListStrategy, clearDestination));
|
|
201 }
|
|
202 }
|
|
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(IObservableList)getTarget()).removeListChangeListener(targetChangeListener);
|
|
221 targetChangeListener = null;
|
|
222 }
|
|
223 if (modelChangeListener !is null) {
|
|
224 (cast(IObservableList)getModel()).removeListChangeListener(modelChangeListener);
|
|
225 modelChangeListener = null;
|
|
226 }
|
|
227 super.dispose();
|
|
228 }
|
|
229 }
|