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
|
85
|
46 private IListChangeListener targetChangeListener;
|
|
47 private class TargetChangeListener : IListChangeListener {
|
78
|
48 public void handleListChange(ListChangeEvent event) {
|
|
49 if (!updatingTarget) {
|
|
50 doUpdate(cast(IObservableList) getTarget(),
|
|
51 cast(IObservableList) getModel(), event.diff,
|
|
52 targetToModel, false, false);
|
|
53 }
|
|
54 }
|
85
|
55 }
|
|
56 private IListChangeListener modelChangeListener;
|
|
57 class ModelChangeListener : IListChangeListener {
|
78
|
58 public void handleListChange(ListChangeEvent event) {
|
|
59 if (!updatingModel) {
|
|
60 doUpdate(cast(IObservableList) getModel(),
|
|
61 cast(IObservableList) getTarget(), event.diff,
|
|
62 modelToTarget, false, false);
|
|
63 }
|
|
64 }
|
|
65 };
|
|
66
|
|
67 /**
|
|
68 * @param target
|
|
69 * @param model
|
|
70 * @param modelToTargetStrategy
|
|
71 * @param targetToModelStrategy
|
|
72 */
|
|
73 public this(IObservableList target, IObservableList model,
|
|
74 UpdateListStrategy targetToModelStrategy,
|
|
75 UpdateListStrategy modelToTargetStrategy) {
|
85
|
76 targetChangeListener = new TargetChangeListener();
|
|
77 modelChangeListener = new ModelChangeListener();
|
78
|
78 super(target, model);
|
|
79 this.targetToModel = targetToModelStrategy;
|
|
80 this.modelToTarget = modelToTargetStrategy;
|
|
81 if ((targetToModel.getUpdatePolicy() & UpdateValueStrategy.POLICY_UPDATE) !is 0) {
|
|
82 target.addListChangeListener(targetChangeListener);
|
|
83 } else {
|
|
84 targetChangeListener = null;
|
|
85 }
|
|
86 if ((modelToTarget.getUpdatePolicy() & UpdateValueStrategy.POLICY_UPDATE) !is 0) {
|
|
87 model.addListChangeListener(modelChangeListener);
|
|
88 } else {
|
|
89 modelChangeListener = null;
|
|
90 }
|
|
91 }
|
|
92
|
|
93 public IObservableValue getValidationStatus() {
|
|
94 return validationStatusObservable;
|
|
95 }
|
|
96
|
|
97 protected void preInit() {
|
|
98 validationStatusObservable = new WritableValue(context
|
85
|
99 .getValidationRealm(), cast(Object)Status.OK_STATUS, typeid(IStatus));
|
78
|
100 }
|
|
101
|
|
102 protected void postInit() {
|
|
103 if (modelToTarget.getUpdatePolicy() is UpdateListStrategy.POLICY_UPDATE) {
|
|
104 updateModelToTarget();
|
|
105 }
|
|
106 if (targetToModel.getUpdatePolicy() !is UpdateListStrategy.POLICY_NEVER) {
|
|
107 validateTargetToModel();
|
|
108 }
|
|
109 }
|
|
110
|
|
111 public void updateModelToTarget() {
|
|
112 final IObservableList modelList = cast(IObservableList) getModel();
|
|
113 modelList.getRealm().exec(new class() Runnable {
|
|
114 public void run() {
|
|
115 ListDiff diff = Diffs.computeListDiff(Collections.EMPTY_LIST,
|
|
116 modelList);
|
|
117 doUpdate(modelList, cast(IObservableList) getTarget(), diff,
|
|
118 modelToTarget, true, true);
|
|
119 }
|
|
120 });
|
|
121 }
|
|
122
|
|
123 public void updateTargetToModel() {
|
|
124 final IObservableList targetList = cast(IObservableList) getTarget();
|
|
125 targetList.getRealm().exec(new class() Runnable {
|
|
126 public void run() {
|
|
127 ListDiff diff = Diffs.computeListDiff(Collections.EMPTY_LIST,
|
|
128 targetList);
|
|
129 doUpdate(targetList, cast(IObservableList) getModel(), diff,
|
|
130 targetToModel, true, true);
|
|
131 }
|
|
132 });
|
|
133 }
|
|
134
|
|
135 public void validateModelToTarget() {
|
|
136 // nothing for now
|
|
137 }
|
|
138
|
|
139 public void validateTargetToModel() {
|
|
140 // nothing for now
|
|
141 }
|
|
142
|
|
143 /*
|
|
144 * This method may be moved to UpdateListStrategy in the future if clients
|
|
145 * need more control over how the two lists are kept in sync.
|
|
146 */
|
|
147 private void doUpdate(IObservableList source,
|
|
148 IObservableList destination, ListDiff diff,
|
|
149 UpdateListStrategy updateListStrategy,
|
|
150 bool explicit, bool clearDestination) {
|
|
151 final int policy = updateListStrategy.getUpdatePolicy();
|
|
152 if (policy !is UpdateListStrategy.POLICY_NEVER) {
|
|
153 if (policy !is UpdateListStrategy.POLICY_ON_REQUEST || explicit) {
|
|
154 destination.getRealm().exec(dgRunnable((
|
|
155 IObservableList destination_,
|
|
156 ListDiff diff_,
|
|
157 UpdateListStrategy updateListStrategy_,
|
|
158 bool clearDestination_) {
|
|
159 if (destination_ is getTarget()) {
|
|
160 updatingTarget = true;
|
|
161 } else {
|
|
162 updatingModel = true;
|
|
163 }
|
|
164 MultiStatus multiStatus = BindingStatus.ok();
|
|
165
|
|
166 try {
|
|
167 if (clearDestination_) {
|
|
168 destination_.clear();
|
|
169 }
|
|
170 ListDiffEntry[] diffEntries = diff_.getDifferences();
|
|
171 for (int i = 0; i < diffEntries.length; i++) {
|
|
172 ListDiffEntry listDiffEntry = diffEntries[i];
|
|
173 if (listDiffEntry.isAddition()) {
|
|
174 IStatus setterStatus = updateListStrategy_
|
85
|
175 .doAdd_package(
|
78
|
176 destination_,
|
|
177 updateListStrategy_
|
|
178 .convert(listDiffEntry
|
|
179 .getElement()),
|
|
180 listDiffEntry.getPosition());
|
|
181
|
|
182 mergeStatus(multiStatus, setterStatus);
|
|
183 // TODO - at this point, the two lists
|
|
184 // will be out of sync if an error occurred...
|
|
185 } else {
|
|
186 IStatus setterStatus = updateListStrategy_
|
85
|
187 .doRemove_package(destination_,
|
78
|
188 listDiffEntry.getPosition());
|
|
189
|
|
190 mergeStatus(multiStatus, setterStatus);
|
|
191 // TODO - at this point, the two lists
|
|
192 // will be out of sync if an error occurred...
|
|
193 }
|
|
194 }
|
|
195 } finally {
|
|
196 validationStatusObservable.setValue(multiStatus);
|
|
197
|
|
198 if (destination_ is getTarget()) {
|
|
199 updatingTarget = false;
|
|
200 } else {
|
|
201 updatingModel = false;
|
|
202 }
|
|
203 }
|
|
204 }, destination, diff, updateListStrategy, clearDestination));
|
|
205 }
|
|
206 }
|
|
207 }
|
|
208
|
|
209 /**
|
|
210 * Merges the provided <code>newStatus</code> into the
|
|
211 * <code>multiStatus</code>.
|
|
212 *
|
|
213 * @param multiStatus
|
|
214 * @param newStatus
|
|
215 */
|
|
216 /* package */void mergeStatus(MultiStatus multiStatus, IStatus newStatus) {
|
|
217 if (!newStatus.isOK()) {
|
|
218 multiStatus.add(newStatus);
|
|
219 }
|
|
220 }
|
|
221
|
|
222 public void dispose() {
|
|
223 if (targetChangeListener !is null) {
|
|
224 (cast(IObservableList)getTarget()).removeListChangeListener(targetChangeListener);
|
|
225 targetChangeListener = null;
|
|
226 }
|
|
227 if (modelChangeListener !is null) {
|
|
228 (cast(IObservableList)getModel()).removeListChangeListener(modelChangeListener);
|
|
229 modelChangeListener = null;
|
|
230 }
|
|
231 super.dispose();
|
|
232 }
|
|
233 }
|