Skip to main content
summaryrefslogtreecommitdiffstats
blob: cf803bf39ef30125ef07f6cec8fe903383de085e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*******************************************************************************
 * Copyright (c) 2008 Matthew Hall and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Matthew Hall - initial API and implementation (bug 124684)
 *     IBM Corporation - through ListBinding.java
 *     Matthew Hall - bug 271148
 ******************************************************************************/

package org.eclipse.core.databinding;

import java.util.Collections;
import java.util.Iterator;

import org.eclipse.core.databinding.observable.Diffs;
import org.eclipse.core.databinding.observable.set.IObservableSet;
import org.eclipse.core.databinding.observable.set.ISetChangeListener;
import org.eclipse.core.databinding.observable.set.SetChangeEvent;
import org.eclipse.core.databinding.observable.set.SetDiff;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.core.internal.databinding.BindingStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;

/**
 * @since 1.1
 * 
 */
public class SetBinding extends Binding {

	private UpdateSetStrategy targetToModel;
	private UpdateSetStrategy modelToTarget;
	private IObservableValue validationStatusObservable;
	private boolean updatingTarget;
	private boolean updatingModel;

	private ISetChangeListener targetChangeListener = new ISetChangeListener() {
		public void handleSetChange(SetChangeEvent event) {
			if (!updatingTarget) {
				doUpdate((IObservableSet) getTarget(),
						(IObservableSet) getModel(), event.diff, targetToModel,
						false, false);
			}
		}
	};

	private ISetChangeListener modelChangeListener = new ISetChangeListener() {
		public void handleSetChange(SetChangeEvent event) {
			if (!updatingModel) {
				doUpdate((IObservableSet) getModel(),
						(IObservableSet) getTarget(), event.diff,
						modelToTarget, false, false);
			}
		}
	};

	/**
	 * @param target
	 * @param model
	 * @param modelToTargetStrategy
	 * @param targetToModelStrategy
	 */
	public SetBinding(IObservableSet target, IObservableSet model,
			UpdateSetStrategy targetToModelStrategy,
			UpdateSetStrategy modelToTargetStrategy) {
		super(target, model);
		this.targetToModel = targetToModelStrategy;
		this.modelToTarget = modelToTargetStrategy;
		if ((targetToModel.getUpdatePolicy() & UpdateSetStrategy.POLICY_UPDATE) != 0) {
			target.addSetChangeListener(targetChangeListener);
		} else {
			targetChangeListener = null;
		}
		if ((modelToTarget.getUpdatePolicy() & UpdateSetStrategy.POLICY_UPDATE) != 0) {
			model.addSetChangeListener(modelChangeListener);
		} else {
			modelChangeListener = null;
		}
	}

	public IObservableValue getValidationStatus() {
		return validationStatusObservable;
	}

	protected void preInit() {
		validationStatusObservable = new WritableValue(context
				.getValidationRealm(), Status.OK_STATUS, IStatus.class);
	}

	protected void postInit() {
		if (modelToTarget.getUpdatePolicy() == UpdateSetStrategy.POLICY_UPDATE) {
			updateModelToTarget();
		}
		if (targetToModel.getUpdatePolicy() == UpdateSetStrategy.POLICY_UPDATE) {
			validateTargetToModel();
		}
	}

	public void updateModelToTarget() {
		final IObservableSet modelSet = (IObservableSet) getModel();
		modelSet.getRealm().exec(new Runnable() {
			public void run() {
				SetDiff diff = Diffs.computeSetDiff(Collections.EMPTY_SET,
						modelSet);
				doUpdate(modelSet, (IObservableSet) getTarget(), diff,
						modelToTarget, true, true);
			}
		});
	}

	public void updateTargetToModel() {
		final IObservableSet targetSet = (IObservableSet) getTarget();
		targetSet.getRealm().exec(new Runnable() {
			public void run() {
				SetDiff diff = Diffs.computeSetDiff(Collections.EMPTY_SET,
						targetSet);
				doUpdate(targetSet, (IObservableSet) getModel(), diff,
						targetToModel, true, true);
			}
		});
	}

	public void validateModelToTarget() {
		// nothing for now
	}

	public void validateTargetToModel() {
		// nothing for now
	}

	/*
	 * This method may be moved to UpdateSetStrategy in the future if clients
	 * need more control over how the two sets are kept in sync.
	 */
	private void doUpdate(final IObservableSet source,
			final IObservableSet destination, final SetDiff diff,
			final UpdateSetStrategy updateSetStrategy, final boolean explicit,
			final boolean clearDestination) {
		final int policy = updateSetStrategy.getUpdatePolicy();
		if (policy == UpdateSetStrategy.POLICY_NEVER)
			return;
		if (policy == UpdateSetStrategy.POLICY_ON_REQUEST && !explicit)
			return;
		destination.getRealm().exec(new Runnable() {
			public void run() {
				if (destination == getTarget()) {
					updatingTarget = true;
				} else {
					updatingModel = true;
				}
				MultiStatus multiStatus = BindingStatus.ok();

				try {
					if (clearDestination) {
						destination.clear();
					}

					for (Iterator iterator = diff.getRemovals().iterator(); iterator
							.hasNext();) {
						IStatus setterStatus = updateSetStrategy.doRemove(
								destination, updateSetStrategy.convert(iterator
										.next()));

						mergeStatus(multiStatus, setterStatus);
						// TODO - at this point, the two sets
						// will be out of sync if an error
						// occurred...
					}

					for (Iterator iterator = diff.getAdditions().iterator(); iterator
							.hasNext();) {
						IStatus setterStatus = updateSetStrategy.doAdd(
								destination, updateSetStrategy.convert(iterator
										.next()));

						mergeStatus(multiStatus, setterStatus);
						// TODO - at this point, the two sets
						// will be out of sync if an error
						// occurred...
					}
				} finally {
					validationStatusObservable.setValue(multiStatus);

					if (destination == getTarget()) {
						updatingTarget = false;
					} else {
						updatingModel = false;
					}
				}
			}
		});
	}

	/**
	 * Merges the provided <code>newStatus</code> into the
	 * <code>multiStatus</code>.
	 * 
	 * @param multiStatus
	 * @param newStatus
	 */
	/* package */void mergeStatus(MultiStatus multiStatus, IStatus newStatus) {
		if (!newStatus.isOK()) {
			multiStatus.add(newStatus);
		}
	}

	public void dispose() {
		if (targetChangeListener != null) {
			((IObservableSet) getTarget())
					.removeSetChangeListener(targetChangeListener);
			targetChangeListener = null;
		}
		if (modelChangeListener != null) {
			((IObservableSet) getModel())
					.removeSetChangeListener(modelChangeListener);
			modelChangeListener = null;
		}
		super.dispose();
	}
}

Back to the top