Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 194734 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/core/internal/databinding/beans/JavaBeanObservableMap.java (-193 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006, 2008 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
 *     Brad Reynolds - bug 171616
11
 *     Matthew hall - bugs 223164, 241585, 226289, 246103
12
 *******************************************************************************/
13
14
package org.eclipse.core.internal.databinding.beans;
15
16
import java.beans.PropertyChangeListener;
17
import java.beans.PropertyDescriptor;
18
import java.lang.reflect.Method;
19
import java.util.HashMap;
20
import java.util.Map;
21
22
import org.eclipse.core.databinding.beans.IBeanObservable;
23
import org.eclipse.core.databinding.observable.Diffs;
24
import org.eclipse.core.databinding.observable.map.ComputedObservableMap;
25
import org.eclipse.core.databinding.observable.set.IObservableSet;
26
import org.eclipse.core.databinding.util.Policy;
27
import org.eclipse.core.internal.databinding.Util;
28
import org.eclipse.core.runtime.IStatus;
29
import org.eclipse.core.runtime.Status;
30
31
/**
32
 * @since 1.0
33
 * 
34
 */
35
public class JavaBeanObservableMap extends ComputedObservableMap implements
36
		IBeanObservable {
37
38
	private PropertyDescriptor propertyDescriptor;
39
	
40
	private PropertyChangeListener elementListener = new PropertyChangeListener() {
41
		public void propertyChange(final java.beans.PropertyChangeEvent event) {
42
			if (!updating) {
43
				getRealm().exec(new Runnable() {
44
					public void run() {
45
						Object source = event.getSource();
46
						Object oldValue = event.getOldValue();
47
						Object newValue = event.getNewValue();
48
						if (oldValue == null && newValue == null) {
49
							oldValue = cachedValues.get(new IdentityWrapper(
50
									source));
51
							newValue = doGet(source);
52
						}
53
						cachedValues.put(new IdentityWrapper(source), newValue);
54
						if (!Util.equals(oldValue, newValue)) {
55
							fireMapChange(Diffs.createMapDiffSingleChange(
56
									source, oldValue, newValue));
57
						}
58
					}
59
				});
60
			}
61
		}
62
	};
63
64
	private ListenerSupport listenerSupport;
65
66
	private boolean updating = false;
67
68
	private boolean attachListeners;
69
70
	// Applicable only while hasListeners() == true
71
	private Map cachedValues;
72
73
	/**
74
	 * @param domain
75
	 * @param propertyDescriptor
76
	 */
77
	public JavaBeanObservableMap(IObservableSet domain,
78
			PropertyDescriptor propertyDescriptor) {
79
		this(domain, propertyDescriptor, true);
80
	}
81
82
	/**
83
	 * @param domain
84
	 * @param propertyDescriptor
85
	 * @param attachListeners
86
	 */
87
	public JavaBeanObservableMap(IObservableSet domain,
88
			PropertyDescriptor propertyDescriptor, boolean attachListeners) {
89
		super(domain, propertyDescriptor.getPropertyType());
90
91
		this.propertyDescriptor = propertyDescriptor;
92
		this.attachListeners = attachListeners;
93
		if (attachListeners) {
94
			this.listenerSupport = new ListenerSupport(elementListener,
95
					propertyDescriptor.getName());
96
		}
97
	}
98
99
	protected void firstListenerAdded() {
100
		if (attachListeners) {
101
			cachedValues = new HashMap();
102
		}
103
		super.firstListenerAdded();
104
	}
105
106
	protected void lastListenerRemoved() {
107
		super.lastListenerRemoved();
108
		if (attachListeners) {
109
			cachedValues = null;
110
		}
111
	}
112
113
	protected void hookListener(Object domainElement) {
114
		if (attachListeners && domainElement != null) {
115
			listenerSupport.hookListener(domainElement);
116
			cachedValues.put(new IdentityWrapper(domainElement),
117
					doGet(domainElement));
118
		}
119
	}
120
121
	protected void unhookListener(Object domainElement) {
122
		if (attachListeners && domainElement != null) {
123
			cachedValues.remove(new IdentityWrapper(domainElement));
124
			listenerSupport.unhookListener(domainElement);
125
		}
126
	}
127
128
	protected Object doGet(Object key) {
129
		if (key == null) {
130
			return null;
131
		}
132
		try {
133
			Method readMethod = propertyDescriptor.getReadMethod();
134
			if (!readMethod.isAccessible()) {
135
				readMethod.setAccessible(true);
136
			}
137
			return readMethod.invoke(key, new Object[0]);
138
		} catch (Exception e) {
139
			Policy.getLog().log(
140
					new Status(IStatus.ERROR, Policy.JFACE_DATABINDING,
141
							IStatus.ERROR, "cannot get value", e)); //$NON-NLS-1$
142
			throw new RuntimeException(e);
143
		}
144
	}
145
146
	protected Object doPut(Object key, Object value) {
147
		try {
148
			Object oldValue = get(key);
149
			if (!Util.equals(oldValue, value)) {
150
				Method writeMethod = propertyDescriptor.getWriteMethod();
151
				if (!writeMethod.isAccessible()) {
152
					writeMethod.setAccessible(true);
153
				}
154
				writeMethod.invoke(key, new Object[] { value });
155
			}
156
157
			if (hasListeners()) {
158
				// oldValue contains the live value which may be different from
159
				// the cached value if the bean does not have listener API or
160
				// does not fire events properly. For consistency we want to
161
				// provide the cached value as the old value, rather than the
162
				// live value so that consumers that hook/unhook listeners can
163
				// do so without maintaining caches of their own.
164
				Object newValue = doGet(key);
165
				oldValue = cachedValues.put(new IdentityWrapper(key), newValue);
166
167
				if (!Util.equals(oldValue, newValue)) {
168
					fireSingleChange(key, oldValue, newValue);
169
				}
170
			}
171
			return oldValue;
172
		} catch (Exception e) {
173
			Policy.getLog().log(
174
					new Status(IStatus.ERROR, Policy.JFACE_DATABINDING,
175
							IStatus.ERROR, "cannot set value", e)); //$NON-NLS-1$
176
			throw new RuntimeException(e);
177
		}
178
	}
179
180
	/* (non-Javadoc)
181
	 * @see org.eclipse.core.databinding.beans.IBeanObservable#getObserved()
182
	 */
183
	public Object getObserved() {
184
		return keySet();
185
	}
186
187
	/* (non-Javadoc)
188
	 * @see org.eclipse.core.databinding.beans.IBeanObservable#getPropertyDescriptor()
189
	 */
190
	public PropertyDescriptor getPropertyDescriptor() {
191
		return propertyDescriptor;
192
	}
193
}
(-)src/org/eclipse/core/internal/databinding/beans/JavaBeanObservableList.java (-399 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006-2008 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
 *     Brad Reynolds - bug 171616
11
 *     Matthew Hall - bugs 208858, 221351, 213145, 223164, 244098
12
 *     Mike Evans - bug 217558
13
 *******************************************************************************/
14
15
package org.eclipse.core.internal.databinding.beans;
16
17
import java.beans.PropertyChangeListener;
18
import java.beans.PropertyDescriptor;
19
import java.lang.reflect.Array;
20
import java.lang.reflect.InvocationTargetException;
21
import java.lang.reflect.Method;
22
import java.util.ArrayList;
23
import java.util.Arrays;
24
import java.util.Collection;
25
import java.util.Iterator;
26
import java.util.List;
27
28
import org.eclipse.core.databinding.BindingException;
29
import org.eclipse.core.databinding.beans.IBeanObservable;
30
import org.eclipse.core.databinding.observable.Diffs;
31
import org.eclipse.core.databinding.observable.Realm;
32
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
33
import org.eclipse.core.databinding.observable.list.ObservableList;
34
35
/**
36
 * @since 1.0
37
 * 
38
 */
39
public class JavaBeanObservableList extends ObservableList implements
40
		IBeanObservable {
41
42
	private final Object object;
43
44
	private boolean updating = false;
45
46
	private PropertyDescriptor descriptor;
47
48
	private ListenerSupport listenerSupport;
49
50
	/**
51
	 * @param realm
52
	 * @param object
53
	 * @param descriptor
54
	 * @param elementType
55
	 */
56
	public JavaBeanObservableList(Realm realm, Object object,
57
			PropertyDescriptor descriptor, Class elementType) {
58
		this(realm, object, descriptor, elementType, true);
59
	}
60
61
	/**
62
	 * @param realm
63
	 * @param object
64
	 * @param descriptor
65
	 * @param elementType
66
	 * @param attachListeners
67
	 */
68
	public JavaBeanObservableList(Realm realm, Object object,
69
			PropertyDescriptor descriptor, Class elementType,
70
			boolean attachListeners) {
71
72
		super(realm, new ArrayList(), elementType);
73
		this.object = object;
74
		this.descriptor = descriptor;
75
76
		if (attachListeners) {
77
			PropertyChangeListener listener = new PropertyChangeListener() {
78
				public void propertyChange(java.beans.PropertyChangeEvent event) {
79
					if (!updating) {
80
						getRealm().exec(new Runnable() {
81
							public void run() {
82
								updateWrappedList(new ArrayList(Arrays
83
										.asList(getValues())));
84
							}
85
						});
86
					}
87
				}
88
			};
89
			this.listenerSupport = new ListenerSupport(listener,
90
					descriptor.getName());
91
			listenerSupport.hookListener(this.object);
92
		}
93
94
		// initialize list without firing events
95
		wrappedList.addAll(Arrays.asList(getValues()));
96
	}
97
98
	public void dispose() {
99
		if (listenerSupport != null) {
100
			listenerSupport.dispose();
101
			listenerSupport = null;
102
		}
103
		super.dispose();
104
	}
105
106
	private Object primGetValues() {
107
		Exception ex = null;
108
		try {
109
			Method readMethod = descriptor.getReadMethod();
110
			if (!readMethod.isAccessible()) {
111
				readMethod.setAccessible(true);
112
			}
113
			return readMethod.invoke(object, new Object[0]);
114
		} catch (IllegalArgumentException e) {
115
			ex = e;
116
		} catch (IllegalAccessException e) {
117
			ex = e;
118
		} catch (InvocationTargetException e) {
119
			ex = e;
120
		}
121
		throw new BindingException("Could not read collection values", ex); //$NON-NLS-1$
122
	}
123
124
	private Object[] getValues() {
125
		Object[] values = null;
126
127
		Object result = primGetValues();
128
		if (descriptor.getPropertyType().isArray())
129
			values = (Object[]) result;
130
		else {
131
			// TODO add jUnit for POJO (var. SettableValue) collections
132
			Collection list = (Collection) result;
133
			if (list != null) {
134
				values = list.toArray();
135
			}
136
		}
137
		if (values == null)
138
			values = new Object[0];
139
		return values;
140
	}
141
142
	public Object getObserved() {
143
		return object;
144
	}
145
146
	public PropertyDescriptor getPropertyDescriptor() {
147
		return descriptor;
148
	}
149
150
	private void setValues() {
151
		if (descriptor.getPropertyType().isArray()) {
152
			Class componentType = descriptor.getPropertyType()
153
					.getComponentType();
154
			Object[] newArray = (Object[]) Array.newInstance(componentType,
155
					wrappedList.size());
156
			wrappedList.toArray(newArray);
157
			primSetValues(newArray);
158
		} else {
159
			// assume that it is a java.util.List
160
			primSetValues(new ArrayList(wrappedList));
161
		}
162
	}
163
164
	private void primSetValues(Object newValue) {
165
		Exception ex = null;
166
		try {
167
			Method writeMethod = descriptor.getWriteMethod();
168
			if (!writeMethod.isAccessible()) {
169
				writeMethod.setAccessible(true);
170
			}
171
			writeMethod.invoke(object, new Object[] { newValue });
172
			return;
173
		} catch (IllegalArgumentException e) {
174
			ex = e;
175
		} catch (IllegalAccessException e) {
176
			ex = e;
177
		} catch (InvocationTargetException e) {
178
			ex = e;
179
		}
180
		throw new BindingException("Could not write collection values", ex); //$NON-NLS-1$
181
	}
182
183
	public Object set(int index, Object element) {
184
		getterCalled();
185
		updating = true;
186
		try {
187
			Object oldElement = wrappedList.set(index, element);
188
			setValues();
189
			fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
190
					index, false, oldElement), Diffs.createListDiffEntry(index,
191
					true, element)));
192
			return oldElement;
193
		} finally {
194
			updating = false;
195
		}
196
	}
197
198
	public Object move(int oldIndex, int newIndex) {
199
		getterCalled();
200
		updating = true;
201
		try {
202
			int size = wrappedList.size();
203
			if (oldIndex < 0 || oldIndex >= size)
204
				throw new IndexOutOfBoundsException(
205
						"oldIndex: " + oldIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$
206
			if (newIndex < 0 || newIndex >= size)
207
				throw new IndexOutOfBoundsException(
208
						"newIndex: " + newIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$
209
			if (oldIndex == newIndex)
210
				return wrappedList.get(oldIndex);
211
			Object element = wrappedList.remove(oldIndex);
212
			wrappedList.add(newIndex, element);
213
			setValues();
214
			fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
215
					oldIndex, false, element), Diffs.createListDiffEntry(
216
					newIndex, true, element)));
217
			return element;
218
		} finally {
219
			updating = false;
220
		}
221
	}
222
223
	public Object remove(int index) {
224
		getterCalled();
225
		updating = true;
226
		try {
227
			Object oldElement = wrappedList.remove(index);
228
			setValues();
229
			fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
230
					index, false, oldElement)));
231
			return oldElement;
232
		} finally {
233
			updating = false;
234
		}
235
	}
236
237
	public boolean add(Object element) {
238
		updating = true;
239
		try {
240
			int index = wrappedList.size();
241
			boolean result = wrappedList.add(element);
242
			setValues();
243
			fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
244
					index, true, element)));
245
			return result;
246
		} finally {
247
			updating = false;
248
		}
249
	}
250
251
	public void add(int index, Object element) {
252
		updating = true;
253
		try {
254
			wrappedList.add(index, element);
255
			setValues();
256
			fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
257
					index, true, element)));
258
		} finally {
259
			updating = false;
260
		}
261
	}
262
263
	public boolean addAll(Collection c) {
264
		if (c.isEmpty()) {
265
			return false;
266
		}
267
		updating = true;
268
		try {
269
			int index = wrappedList.size();
270
			boolean result = wrappedList.addAll(c);
271
			setValues();
272
			ListDiffEntry[] entries = new ListDiffEntry[c.size()];
273
			int i = 0;
274
			for (Iterator it = c.iterator(); it.hasNext();) {
275
				Object o = it.next();
276
				entries[i++] = Diffs.createListDiffEntry(index++, true, o);
277
			}
278
			fireListChange(Diffs.createListDiff(entries));
279
			return result;
280
		} finally {
281
			updating = false;
282
		}
283
	}
284
285
	public boolean addAll(int index, Collection c) {
286
		if (c.isEmpty()) {
287
			return false;
288
		}
289
		updating = true;
290
		try {
291
			boolean result = wrappedList.addAll(index, c);
292
			setValues();
293
			ListDiffEntry[] entries = new ListDiffEntry[c.size()];
294
			int i = 0;
295
			for (Iterator it = c.iterator(); it.hasNext();) {
296
				Object o = it.next();
297
				entries[i++] = Diffs.createListDiffEntry(index++, true, o);
298
			}
299
			fireListChange(Diffs.createListDiff(entries));
300
			return result;
301
		} finally {
302
			updating = false;
303
		}
304
	}
305
306
	public boolean remove(Object o) {
307
		getterCalled();
308
		int index = wrappedList.indexOf(o);
309
		if (index == -1) {
310
			return false;
311
		}
312
		updating = true;
313
		try {
314
			Object oldElement = wrappedList.remove(index);
315
			setValues();
316
			fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
317
					index, false, oldElement)));
318
			return true;
319
		} finally {
320
			updating = false;
321
		}
322
	}
323
324
	public boolean removeAll(Collection c) {
325
		getterCalled();
326
		boolean changed = false;
327
		updating = true;
328
		try {
329
			List diffEntries = new ArrayList();
330
			for (Iterator it = c.iterator(); it.hasNext();) {
331
				Object o = it.next();
332
				int index = wrappedList.indexOf(o);
333
				if (index != -1) {
334
					changed = true;
335
					Object oldElement = wrappedList.remove(index);
336
					diffEntries.add(Diffs.createListDiffEntry(index, false,
337
							oldElement));
338
				}
339
			}
340
			if (changed) {
341
				setValues();
342
				fireListChange(Diffs
343
						.createListDiff((ListDiffEntry[]) diffEntries
344
								.toArray(new ListDiffEntry[diffEntries.size()])));
345
			}
346
			return changed;
347
		} finally {
348
			updating = false;
349
		}
350
	}
351
352
	public boolean retainAll(Collection c) {
353
		getterCalled();
354
		boolean changed = false;
355
		updating = true;
356
		try {
357
			List diffEntries = new ArrayList();
358
			int index = 0;
359
			for (Iterator it = wrappedList.iterator(); it.hasNext();) {
360
				Object o = it.next();
361
				boolean retain = c.contains(o);
362
				if (retain) {
363
					index++;
364
				} else {
365
					changed = true;
366
					it.remove();
367
					diffEntries.add(Diffs.createListDiffEntry(index, false, o));
368
				}
369
			}
370
			if (changed) {
371
				setValues();
372
				fireListChange(Diffs
373
						.createListDiff((ListDiffEntry[]) diffEntries
374
								.toArray(new ListDiffEntry[diffEntries.size()])));
375
			}
376
			return changed;
377
		} finally {
378
			updating = false;
379
		}
380
	}
381
382
	public void clear() {
383
		updating = true;
384
		try {
385
			List diffEntries = new ArrayList();
386
			for (Iterator it = wrappedList.iterator(); it.hasNext();) {
387
				Object o = it.next();
388
				diffEntries.add(Diffs.createListDiffEntry(0, false, o));
389
			}
390
			wrappedList.clear();
391
			setValues();
392
			fireListChange(Diffs.createListDiff((ListDiffEntry[]) diffEntries
393
					.toArray(new ListDiffEntry[diffEntries.size()])));
394
		} finally {
395
			updating = false;
396
		}
397
	}
398
399
}
(-)src/org/eclipse/core/internal/databinding/beans/JavaBeanPropertyObservableMap.java (-263 lines)
Removed Link Here
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 221704)
10
 *     Matthew Hall - bug 223164, 226289, 244098, 246103
11
 *******************************************************************************/
12
13
package org.eclipse.core.internal.databinding.beans;
14
15
import java.beans.PropertyChangeEvent;
16
import java.beans.PropertyChangeListener;
17
import java.beans.PropertyDescriptor;
18
import java.lang.reflect.InvocationTargetException;
19
import java.lang.reflect.Method;
20
import java.util.Collections;
21
import java.util.HashMap;
22
import java.util.HashSet;
23
import java.util.Iterator;
24
import java.util.Map;
25
import java.util.Set;
26
27
import org.eclipse.core.databinding.BindingException;
28
import org.eclipse.core.databinding.beans.IBeanObservable;
29
import org.eclipse.core.databinding.observable.Diffs;
30
import org.eclipse.core.databinding.observable.Realm;
31
import org.eclipse.core.databinding.observable.map.ObservableMap;
32
import org.eclipse.core.internal.databinding.Util;
33
import org.eclipse.core.runtime.Assert;
34
35
/**
36
 * @since 1.0
37
 * 
38
 */
39
public class JavaBeanPropertyObservableMap extends ObservableMap implements
40
		IBeanObservable {
41
42
	private final Object object;
43
44
	private Object keyType;
45
	private Object valueType;
46
47
	private boolean updating = false;
48
49
	private PropertyDescriptor descriptor;
50
51
	private ListenerSupport listenerSupport;
52
53
	/**
54
	 * @param realm
55
	 * @param object
56
	 * @param descriptor
57
	 * @param keyType
58
	 * @param valueType
59
	 */
60
	public JavaBeanPropertyObservableMap(Realm realm, Object object,
61
			PropertyDescriptor descriptor, Object keyType, Object valueType) {
62
		this(realm, object, descriptor, keyType, valueType, true);
63
	}
64
65
	/**
66
	 * @param realm
67
	 * @param object
68
	 * @param descriptor
69
	 * @param keyType
70
	 * @param valueType
71
	 * @param attachListeners
72
	 */
73
	public JavaBeanPropertyObservableMap(Realm realm, Object object,
74
			PropertyDescriptor descriptor, Object keyType, Object valueType,
75
			boolean attachListeners) {
76
		super(realm, new HashMap());
77
		this.object = object;
78
		this.descriptor = descriptor;
79
		this.keyType = keyType;
80
		this.valueType = valueType;
81
		if (attachListeners) {
82
			PropertyChangeListener listener = new PropertyChangeListener() {
83
				public void propertyChange(final PropertyChangeEvent event) {
84
					if (!updating) {
85
						getRealm().exec(new Runnable() {
86
							public void run() {
87
								Map oldMap = (Map) event.getOldValue();
88
								Map newMap = (Map) event.getNewValue();
89
								if (oldMap == null && newMap == null) {
90
									oldMap = wrappedMap;
91
									newMap = getMap();
92
								}
93
94
								if (!Util.equals(oldMap, newMap)) {
95
									wrappedMap = new HashMap(newMap);
96
									fireMapChange(Diffs.computeMapDiff(oldMap,
97
											newMap));
98
								}
99
							}
100
						});
101
					}
102
				}
103
			};
104
105
			listenerSupport = new ListenerSupport(listener,
106
					descriptor.getName());
107
			listenerSupport.hookListener(this.object);
108
		}
109
110
		wrappedMap.putAll(getMap());
111
	}
112
113
	public Object getKeyType() {
114
		return keyType;
115
	}
116
117
	public Object getValueType() {
118
		return valueType;
119
	}
120
121
	private Object primGetMap() {
122
		try {
123
			Method readMethod = descriptor.getReadMethod();
124
			if (!readMethod.isAccessible()) {
125
				readMethod.setAccessible(true);
126
			}
127
			return readMethod.invoke(object, new Object[0]);
128
		} catch (IllegalArgumentException e) {
129
		} catch (IllegalAccessException e) {
130
		} catch (InvocationTargetException e) {
131
		}
132
		Assert.isTrue(false, "Could not read collection values"); //$NON-NLS-1$
133
		return null;
134
	}
135
136
	private void primSetMap(Object newValue) {
137
		Exception ex = null;
138
		try {
139
			Method writeMethod = descriptor.getWriteMethod();
140
			if (!writeMethod.isAccessible()) {
141
				writeMethod.setAccessible(true);
142
			}
143
			writeMethod.invoke(object, new Object[] { newValue });
144
			return;
145
		} catch (IllegalArgumentException e) {
146
			ex = e;
147
		} catch (IllegalAccessException e) {
148
			ex = e;
149
		} catch (InvocationTargetException e) {
150
			ex = e;
151
		}
152
		throw new BindingException("Could not write collection values", ex); //$NON-NLS-1$
153
	}
154
155
	private Map getMap() {
156
		Map result = (Map) primGetMap();
157
158
		if (result == null)
159
			result = new HashMap();
160
		return result;
161
	}
162
163
	private void setMap() {
164
		primSetMap(new HashMap(wrappedMap));
165
	}
166
167
	public Object put(Object key, Object value) {
168
		checkRealm();
169
		updating = true;
170
		try {
171
			boolean add = !wrappedMap.containsKey(key);
172
			Object result = wrappedMap.put(key, value);
173
			if (!Util.equals(result, value)) {
174
				setMap();
175
				if (add) {
176
					fireMapChange(Diffs.createMapDiffSingleAdd(key, value));
177
				} else {
178
					fireMapChange(Diffs.createMapDiffSingleChange(key, result,
179
							value));
180
				}
181
			}
182
			return result;
183
		} finally {
184
			updating = false;
185
		}
186
	}
187
188
	public void putAll(Map map) {
189
		checkRealm();
190
		updating = true;
191
		try {
192
			Set addedKeys = new HashSet(map.size());
193
			Map changes = new HashMap(map.size());
194
			for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
195
				Map.Entry entry = (Entry) it.next();
196
				Object key = entry.getKey();
197
				Object newValue = entry.getValue();
198
				boolean add = !wrappedMap.containsKey(key);
199
				Object oldValue = wrappedMap.put(key, newValue);
200
				if (add) {
201
					addedKeys.add(key);
202
				} else if (!Util.equals(oldValue, newValue)) {
203
					changes.put(key, oldValue);
204
				}
205
			}
206
			if (!addedKeys.isEmpty() || !changes.isEmpty()) {
207
				setMap();
208
				fireMapChange(Diffs.createMapDiff(addedKeys,
209
						Collections.EMPTY_SET, changes.keySet(), changes,
210
						wrappedMap));
211
			}
212
		} finally {
213
			updating = false;
214
		}
215
	}
216
217
	public Object remove(Object key) {
218
		checkRealm();
219
		if (!wrappedMap.containsKey(key)) {
220
			return null;
221
		}
222
		updating = true;
223
		try {
224
			Object result = wrappedMap.remove(key);
225
			setMap();
226
			fireMapChange(Diffs.createMapDiffSingleRemove(key, result));
227
			return result;
228
		} finally {
229
			updating = false;
230
		}
231
	}
232
233
	public void clear() {
234
		checkRealm();
235
		if (wrappedMap.isEmpty())
236
			return;
237
		updating = true;
238
		try {
239
			Map oldMap = wrappedMap;
240
			wrappedMap = new HashMap();
241
			setMap();
242
			fireMapChange(Diffs.computeMapDiff(oldMap, Collections.EMPTY_MAP));
243
		} finally {
244
			updating = false;
245
		}
246
	}
247
248
	public Object getObserved() {
249
		return object;
250
	}
251
252
	public PropertyDescriptor getPropertyDescriptor() {
253
		return descriptor;
254
	}
255
256
	public synchronized void dispose() {
257
		if (listenerSupport != null) {
258
			listenerSupport.dispose();
259
			listenerSupport = null;
260
		}
261
		super.dispose();
262
	}
263
}
(-)src/org/eclipse/core/internal/databinding/beans/ListenerSupport.java (-216 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 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
 *     Matthew Hall - bug 118516
11
 *******************************************************************************/
12
package org.eclipse.core.internal.databinding.beans;
13
14
import java.beans.PropertyChangeEvent;
15
import java.beans.PropertyChangeListener;
16
import java.lang.reflect.InvocationTargetException;
17
import java.lang.reflect.Method;
18
import java.util.HashSet;
19
import java.util.Iterator;
20
import java.util.Set;
21
22
import org.eclipse.core.databinding.beans.BeansObservables;
23
import org.eclipse.core.databinding.util.Policy;
24
import org.eclipse.core.runtime.Assert;
25
import org.eclipse.core.runtime.IStatus;
26
import org.eclipse.core.runtime.Status;
27
28
/**
29
 * This is a helper that will hook up and listen for <code>PropertyChangeEvent</code> events
30
 * for a set of target JavaBeans
31
 * 
32
 * @since 1.0
33
 */
34
public class ListenerSupport {
35
36
	private Set elementsListenedTo = new HashSet();
37
	
38
	private PropertyChangeListener listener;
39
40
	private String propertyName;
41
42
	/**
43
	 * Constructs a new instance.
44
	 * 
45
	 * @param listener is the callback that will be called
46
	 * 		when a <code>PropertyChangeEvent</code> is fired on any
47
	 * 		of the target objects.  Will only receive change events 
48
	 * 		when the provided <code>propertyName</code> changes.
49
	 * @param propertyName
50
	 */
51
	public ListenerSupport(final PropertyChangeListener listener,
52
			final String propertyName) {
53
		Assert.isNotNull(listener, "Listener cannot be null"); //$NON-NLS-1$
54
		Assert.isNotNull(propertyName, "Property name cannot be null"); //$NON-NLS-1$
55
56
		this.propertyName = propertyName;
57
		this.listener = new PropertyChangeListener() {
58
			public void propertyChange(PropertyChangeEvent evt) {
59
				if (propertyName.equals(evt.getPropertyName())) {
60
					listener.propertyChange(evt);
61
				}
62
			}
63
		};
64
	}
65
66
	/**
67
	 * Start listen to target (if it supports the JavaBean property change listener pattern)
68
	 * 
69
	 * @param target
70
	 */
71
	public void hookListener(Object target) {
72
		if (processListener(
73
				"addPropertyChangeListener", "Could not attach listener to ", target)) { //$NON-NLS-1$ //$NON-NLS-2$
74
			elementsListenedTo.add(new IdentityWrapper(target));
75
		}
76
	}
77
		
78
	/**
79
	 * Add listeners for new targets (those this instance of<code>ListenerSupport</code> does not 
80
	 * already listen to),
81
	 * Stop to listen to those object that this instance listen to and is one of the object in targets 
82
	 * 
83
	 * @param targets 
84
	 */
85
	public void setHookTargets(Object[] targets) {		
86
		Set elementsToUnhook = new HashSet(elementsListenedTo);
87
		if (targets!=null) {
88
			for (int i = 0; i < targets.length; i++) {
89
				Object newValue = targets[i];
90
				IdentityWrapper identityWrapper = new IdentityWrapper(newValue);
91
				if(!elementsToUnhook.remove(identityWrapper)) 				
92
					hookListener(newValue);
93
			}
94
		}
95
			
96
		for (Iterator it = elementsToUnhook.iterator(); it.hasNext();) {
97
			Object o = it.next();
98
			if (o.getClass()!=IdentityWrapper.class)
99
				o = new IdentityWrapper(o);
100
			elementsListenedTo.remove(o);
101
			unhookListener(o);
102
		}							
103
	}
104
	
105
	/**
106
	 * Stop listen to target
107
	 * 
108
	 * @param target
109
	 */
110
	public void unhookListener(Object target) {
111
		if (target.getClass() == IdentityWrapper.class)
112
			target = ((IdentityWrapper) target).unwrap();
113
114
		if (processListener(
115
				"removePropertyChangeListener", "Cound not remove listener from ", target)) { //$NON-NLS-1$//$NON-NLS-2$
116
			elementsListenedTo.remove(new IdentityWrapper(target));
117
		}
118
	}
119
	
120
	
121
	/**
122
	 * 
123
	 */
124
	public void dispose() {
125
		if (elementsListenedTo!=null) {
126
			Object[] targets = elementsListenedTo.toArray();		
127
			for (int i = 0; i < targets.length; i++) {		
128
				unhookListener(targets[i]);
129
			}			
130
			elementsListenedTo=null;
131
			listener=null;
132
		}
133
	}
134
	
135
	/**
136
	 * @return elements that were registred to
137
	 */
138
	public Object[] getHookedTargets() {
139
		Object[] targets = null;
140
		if (elementsListenedTo!=null && elementsListenedTo.size()>0) {
141
			Object[] identityList = elementsListenedTo.toArray();
142
			targets = new Object[identityList.length];
143
			for (int i = 0; i < identityList.length; i++) 
144
				targets[i]=((IdentityWrapper)identityList[i]).unwrap();							
145
		}
146
		return targets;
147
	}
148
149
	/**
150
	 * Invokes the method for the provided <code>methodName</code> attempting
151
	 * to first use the method with the property name and then the unnamed
152
	 * version.
153
	 * 
154
	 * @param methodName
155
	 *            either addPropertyChangeListener or
156
	 *            removePropertyChangeListener
157
	 * @param message
158
	 *            string that will be prefixed to the target in an error message
159
	 * @param target
160
	 *            object to invoke the method on
161
	 * @return <code>true</code> if the method was invoked successfully
162
	 */
163
	private boolean processListener(String methodName, String message,
164
			Object target) {
165
		Method method = null;
166
		Object[] parameters = null;
167
168
		try {
169
			try {
170
				method = target.getClass().getMethod(
171
						methodName,
172
						new Class[] { String.class,
173
								PropertyChangeListener.class });
174
175
				parameters = new Object[] { propertyName, listener };
176
			} catch (NoSuchMethodException e) {
177
				method = target.getClass().getMethod(methodName,
178
						new Class[] { PropertyChangeListener.class });
179
180
				parameters = new Object[] { listener };
181
			}
182
		} catch (SecurityException e) {
183
			// ignore
184
		} catch (NoSuchMethodException e) {
185
			log(IStatus.WARNING, message + target, e);
186
		}
187
188
		if (method != null) {
189
			if (!method.isAccessible()) {
190
				method.setAccessible(true);
191
			}
192
			try {
193
				method.invoke(target, parameters);
194
				return true;
195
			} catch (IllegalArgumentException e) {
196
				log(IStatus.WARNING, message + target, e);
197
			} catch (IllegalAccessException e) {
198
				log(IStatus.WARNING, message + target, e);
199
			} catch (InvocationTargetException e) {
200
				log(IStatus.WARNING, message + target, e);
201
			}
202
		}
203
		return false;
204
	}
205
206
	/**
207
	 * Logs a message to the Data Binding logger.
208
	 */
209
	private void log(int severity, String message, Throwable throwable) {
210
		if (BeansObservables.DEBUG) {
211
			Policy.getLog().log(
212
					new Status(severity, Policy.JFACE_DATABINDING, IStatus.OK,
213
							message, throwable));
214
		}
215
	}
216
}
(-)src/org/eclipse/core/internal/databinding/beans/JavaBeanObservableSet.java (-301 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006-2008 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
 *     Brad Reynolds - bug 171616
11
 *     Matthew Hall - bugs 221351, 223164, 244098
12
 *******************************************************************************/
13
14
package org.eclipse.core.internal.databinding.beans;
15
16
import java.beans.PropertyChangeListener;
17
import java.beans.PropertyDescriptor;
18
import java.lang.reflect.Array;
19
import java.lang.reflect.InvocationTargetException;
20
import java.lang.reflect.Method;
21
import java.util.Arrays;
22
import java.util.Collection;
23
import java.util.Collections;
24
import java.util.HashSet;
25
import java.util.Iterator;
26
import java.util.Set;
27
28
import org.eclipse.core.databinding.BindingException;
29
import org.eclipse.core.databinding.beans.IBeanObservable;
30
import org.eclipse.core.databinding.observable.Diffs;
31
import org.eclipse.core.databinding.observable.Realm;
32
import org.eclipse.core.databinding.observable.set.ObservableSet;
33
import org.eclipse.core.runtime.Assert;
34
35
/**
36
 * @since 1.0
37
 * 
38
 */
39
public class JavaBeanObservableSet extends ObservableSet implements IBeanObservable {
40
41
	private final Object object;
42
43
	private boolean updating = false;
44
45
	private PropertyDescriptor descriptor;
46
47
	private ListenerSupport listenerSupport;
48
49
	/**
50
	 * @param realm
51
	 * @param object
52
	 * @param descriptor
53
	 * @param elementType
54
	 */
55
	public JavaBeanObservableSet(Realm realm, Object object,
56
			PropertyDescriptor descriptor, Class elementType) {
57
		this(realm, object, descriptor, elementType, true);
58
	}
59
60
	/**
61
	 * @param realm
62
	 * @param object
63
	 * @param descriptor
64
	 * @param elementType
65
	 * @param attachListeners
66
	 */
67
	public JavaBeanObservableSet(Realm realm, Object object,
68
			PropertyDescriptor descriptor, Class elementType,
69
			boolean attachListeners) {
70
		super(realm, new HashSet(), elementType);
71
		this.object = object;
72
		this.descriptor = descriptor;
73
		if (attachListeners) {
74
			PropertyChangeListener listener = new PropertyChangeListener() {
75
				public void propertyChange(java.beans.PropertyChangeEvent event) {
76
					if (!updating) {
77
						getRealm().exec(new Runnable() {
78
							public void run() {
79
								Set newElements = new HashSet(Arrays
80
										.asList(getValues()));
81
								Set addedElements = new HashSet(newElements);
82
								Set removedElements = new HashSet(wrappedSet);
83
								// remove all new elements from old elements to
84
								// compute
85
								// the removed elements
86
								removedElements.removeAll(newElements);
87
								addedElements.removeAll(wrappedSet);
88
								wrappedSet = newElements;
89
								fireSetChange(Diffs.createSetDiff(
90
										addedElements, removedElements));
91
							}
92
						});
93
					}
94
				}
95
			};
96
			this.listenerSupport = new ListenerSupport(listener, descriptor
97
					.getName());
98
			listenerSupport.hookListener(this.object);
99
		}
100
101
		wrappedSet.addAll(Arrays.asList(getValues()));
102
	}
103
104
	private Object primGetValues() {
105
		try {
106
			Method readMethod = descriptor.getReadMethod();
107
			if (!readMethod.isAccessible()) {
108
				readMethod.setAccessible(true);
109
			}
110
			return readMethod.invoke(object, new Object[0]);
111
		} catch (IllegalArgumentException e) {
112
		} catch (IllegalAccessException e) {
113
		} catch (InvocationTargetException e) {
114
		}
115
		Assert.isTrue(false, "Could not read collection values"); //$NON-NLS-1$
116
		return null;
117
	}
118
119
	private Object[] getValues() {
120
		Object[] values = null;
121
122
		Object result = primGetValues();
123
		if (descriptor.getPropertyType().isArray())
124
			values = (Object[]) result;
125
		else {
126
			// TODO add jUnit for POJO (var. SettableValue) collections
127
			Collection list = (Collection) result;
128
			if (list != null)
129
				values = list.toArray();
130
		}
131
		if (values == null)
132
			values = new Object[0];
133
		return values;
134
	}
135
136
	private void setValues() {
137
		if (descriptor.getPropertyType().isArray()) {
138
			Class componentType = descriptor.getPropertyType()
139
					.getComponentType();
140
			Object[] newArray = (Object[]) Array.newInstance(componentType,
141
					wrappedSet.size());
142
			wrappedSet.toArray(newArray);
143
			primSetValues(newArray);
144
		} else {
145
			// assume that it is a java.util.Set
146
			primSetValues(new HashSet(wrappedSet));
147
		}
148
	}
149
150
	public boolean add(Object o) {
151
		getterCalled();
152
		updating = true;
153
		try {
154
			boolean added = wrappedSet.add(o);
155
			if (added) {
156
				setValues();
157
				fireSetChange(Diffs.createSetDiff(Collections.singleton(o),
158
						Collections.EMPTY_SET));
159
			}
160
			return added;
161
		} finally {
162
			updating = false;
163
		}
164
	}
165
166
	public boolean remove(Object o) {
167
		getterCalled();
168
		updating = true;
169
		try {
170
			boolean removed = wrappedSet.remove(o);
171
			if (removed) {
172
				setValues();
173
				fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET,
174
						Collections.singleton(o)));
175
			}
176
			return removed;
177
		} finally {
178
			updating = false;
179
		}
180
	}
181
182
	public boolean addAll(Collection c) {
183
		getterCalled();
184
		updating = true;
185
		try {
186
			Set additions = new HashSet();
187
			for (Iterator iterator = c.iterator(); iterator.hasNext();) {
188
				Object element = iterator.next();
189
				if (wrappedSet.add(element))
190
					additions.add(element);
191
			}
192
			boolean changed = !additions.isEmpty();
193
			if (changed) {
194
				setValues();
195
				fireSetChange(Diffs.createSetDiff(additions,
196
						Collections.EMPTY_SET));
197
			}
198
			return changed;
199
		} finally {
200
			updating = false;
201
		}
202
	}
203
204
	public boolean removeAll(Collection c) {
205
		getterCalled();
206
		updating = true;
207
		try {
208
			Set removals = new HashSet();
209
			for (Iterator iterator = c.iterator(); iterator.hasNext();) {
210
				Object element = iterator.next();
211
				if (wrappedSet.remove(element))
212
					removals.add(element);
213
			}
214
			boolean changed = !removals.isEmpty();
215
			if (changed) {
216
				setValues();
217
				fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET,
218
						removals));
219
			}
220
			return changed;
221
		} finally {
222
			updating = false;
223
		}
224
	}
225
226
	public boolean retainAll(Collection c) {
227
		getterCalled();
228
		updating = true;
229
		try {
230
			Set removals = new HashSet();
231
			for (Iterator iterator = wrappedSet.iterator(); iterator.hasNext();) {
232
				Object element = iterator.next();
233
				if (!c.contains(element)) {
234
					iterator.remove();
235
					removals.add(element);
236
				}
237
			}
238
			boolean changed = !removals.isEmpty();
239
			if (changed) {
240
				setValues();
241
				fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET,
242
						removals));
243
			}
244
			return changed;
245
		} finally {
246
			updating = false;
247
		}
248
	}
249
250
	public void clear() {
251
		getterCalled();
252
		if (wrappedSet.isEmpty())
253
			return;
254
255
		updating = true;
256
		try {
257
			Set removals = new HashSet(wrappedSet);
258
			wrappedSet.clear();
259
			setValues();
260
			fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removals));
261
		} finally {
262
			updating = false;
263
		}
264
	}
265
266
	private void primSetValues(Object newValue) {
267
		Exception ex = null;
268
		try {
269
			Method writeMethod = descriptor.getWriteMethod();
270
			if (!writeMethod.isAccessible()) {
271
				writeMethod.setAccessible(true);
272
			}
273
			writeMethod.invoke(object, new Object[] { newValue });
274
			return;
275
		} catch (IllegalArgumentException e) {
276
			ex = e;
277
		} catch (IllegalAccessException e) {
278
			ex = e;
279
		} catch (InvocationTargetException e) {
280
			ex = e;
281
		}
282
		throw new BindingException("Could not write collection values", ex); //$NON-NLS-1$
283
	}
284
285
	public Object getObserved() {
286
		return object;
287
	}
288
289
	public PropertyDescriptor getPropertyDescriptor() {
290
		return descriptor;
291
	}
292
293
	public synchronized void dispose() {
294
		if (listenerSupport != null) {
295
			listenerSupport.dispose();
296
			listenerSupport = null;
297
		}
298
299
		super.dispose();
300
	}
301
}
(-)src/org/eclipse/core/databinding/beans/BeansObservables.java (-116 / +64 lines)
Lines 8-24 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Brad Reynolds - bugs 164268, 171616, 147515
10
 *     Brad Reynolds - bugs 164268, 171616, 147515
11
 *     Matthew Hall - bug 221704, 234686, 246625, 226289, 246782
11
 *     Matthew Hall - bug 221704, 234686, 246625, 226289, 246782, 194734
12
 *     Thomas Kratz - bug 213787
12
 *     Thomas Kratz - bug 213787
13
 *******************************************************************************/
13
 *******************************************************************************/
14
package org.eclipse.core.databinding.beans;
14
package org.eclipse.core.databinding.beans;
15
15
16
import java.beans.BeanInfo;
17
import java.beans.IntrospectionException;
18
import java.beans.Introspector;
19
import java.beans.PropertyDescriptor;
16
import java.beans.PropertyDescriptor;
20
17
21
import org.eclipse.core.databinding.BindingException;
22
import org.eclipse.core.databinding.observable.IObservable;
18
import org.eclipse.core.databinding.observable.IObservable;
23
import org.eclipse.core.databinding.observable.Realm;
19
import org.eclipse.core.databinding.observable.Realm;
24
import org.eclipse.core.databinding.observable.list.IObservableList;
20
import org.eclipse.core.databinding.observable.list.IObservableList;
Lines 27-43 Link Here
27
import org.eclipse.core.databinding.observable.masterdetail.MasterDetailObservables;
23
import org.eclipse.core.databinding.observable.masterdetail.MasterDetailObservables;
28
import org.eclipse.core.databinding.observable.set.IObservableSet;
24
import org.eclipse.core.databinding.observable.set.IObservableSet;
29
import org.eclipse.core.databinding.observable.value.IObservableValue;
25
import org.eclipse.core.databinding.observable.value.IObservableValue;
26
import org.eclipse.core.databinding.property.list.IListProperty;
27
import org.eclipse.core.databinding.property.map.IMapProperty;
28
import org.eclipse.core.databinding.property.set.ISetProperty;
29
import org.eclipse.core.databinding.property.value.IValueProperty;
30
import org.eclipse.core.databinding.util.Policy;
30
import org.eclipse.core.databinding.util.Policy;
31
import org.eclipse.core.internal.databinding.Util;
31
import org.eclipse.core.internal.databinding.Util;
32
import org.eclipse.core.internal.databinding.beans.BeanObservableListDecorator;
32
import org.eclipse.core.internal.databinding.beans.BeanObservableListDecorator;
33
import org.eclipse.core.internal.databinding.beans.BeanObservableMapDecorator;
33
import org.eclipse.core.internal.databinding.beans.BeanObservableMapDecorator;
34
import org.eclipse.core.internal.databinding.beans.BeanObservableSetDecorator;
34
import org.eclipse.core.internal.databinding.beans.BeanObservableSetDecorator;
35
import org.eclipse.core.internal.databinding.beans.BeanObservableValueDecorator;
35
import org.eclipse.core.internal.databinding.beans.BeanObservableValueDecorator;
36
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableList;
36
import org.eclipse.core.internal.databinding.beans.BeanPropertyHelper;
37
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableMap;
38
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableSet;
39
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableValue;
40
import org.eclipse.core.internal.databinding.beans.JavaBeanPropertyObservableMap;
41
import org.eclipse.core.runtime.Assert;
37
import org.eclipse.core.runtime.Assert;
42
import org.eclipse.core.runtime.IStatus;
38
import org.eclipse.core.runtime.IStatus;
43
import org.eclipse.core.runtime.Status;
39
import org.eclipse.core.runtime.Status;
Lines 87-95 Link Here
87
	 */
83
	 */
88
	public static IObservableValue observeValue(Realm realm, Object bean,
84
	public static IObservableValue observeValue(Realm realm, Object bean,
89
			String propertyName) {
85
			String propertyName) {
90
		PropertyDescriptor descriptor = getPropertyDescriptor(bean.getClass(),
86
		IValueProperty property = BeanProperties.valueProperty(bean.getClass(),
91
				propertyName);
87
				propertyName);
92
		return new JavaBeanObservableValue(realm, bean, descriptor);
88
		PropertyDescriptor propertyDescriptor = ((IBeanProperty) property)
89
				.getPropertyDescriptor();
90
		return new BeanObservableValueDecorator(property.observeValue(realm,
91
				bean), propertyDescriptor);
93
	}
92
	}
94
93
95
	/**
94
	/**
Lines 107-115 Link Here
107
	 */
106
	 */
108
	public static IObservableMap observeMap(IObservableSet domain,
107
	public static IObservableMap observeMap(IObservableSet domain,
109
			Class beanClass, String propertyName) {
108
			Class beanClass, String propertyName) {
110
		PropertyDescriptor descriptor = getPropertyDescriptor(beanClass,
109
		IValueProperty property = BeanProperties.valueProperty(beanClass,
111
				propertyName);
110
				propertyName);
112
		return new JavaBeanObservableMap(domain, descriptor);
111
		PropertyDescriptor propertyDescriptor = ((IBeanProperty) property)
112
				.getPropertyDescriptor();
113
		return new BeanObservableMapDecorator(property
114
				.observeDetailValues(domain), propertyDescriptor);
113
	}
115
	}
114
116
115
	/**
117
	/**
Lines 153-162 Link Here
153
	 */
155
	 */
154
	public static IObservableMap observeMap(Realm realm, Object bean,
156
	public static IObservableMap observeMap(Realm realm, Object bean,
155
			String propertyName, Class keyType, Class valueType) {
157
			String propertyName, Class keyType, Class valueType) {
156
		PropertyDescriptor descriptor = getPropertyDescriptor(bean.getClass(),
158
		IMapProperty property = BeanProperties.mapProperty(bean.getClass(),
157
				propertyName);
159
				propertyName, keyType, valueType);
158
		return new JavaBeanPropertyObservableMap(realm, bean, descriptor,
160
		PropertyDescriptor propertyDescriptor = ((IBeanProperty) property)
159
				keyType, valueType);
161
				.getPropertyDescriptor();
162
		return new BeanObservableMapDecorator(property.observeMap(realm, bean),
163
				propertyDescriptor);
160
	}
164
	}
161
165
162
	/**
166
	/**
Lines 193-221 Link Here
193
	 *         given bean object
197
	 *         given bean object
194
	 * @since 1.2
198
	 * @since 1.2
195
	 */
199
	 */
196
	public static IObservableMap observeMap(Object bean, String propertyName, Class keyType, Class valueType) {
200
	public static IObservableMap observeMap(Object bean, String propertyName,
197
		return observeMap(Realm.getDefault(), bean, propertyName, keyType, valueType);
201
			Class keyType, Class valueType) {
198
	}
202
		return observeMap(Realm.getDefault(), bean, propertyName, keyType,
199
203
				valueType);
200
	/*package*/ static PropertyDescriptor getPropertyDescriptor(Class beanClass,
201
			String propertyName) {
202
		BeanInfo beanInfo;
203
		try {
204
			beanInfo = Introspector.getBeanInfo(beanClass);
205
		} catch (IntrospectionException e) {
206
			// cannot introspect, give up
207
			return null;
208
		}
209
		PropertyDescriptor[] propertyDescriptors = beanInfo
210
				.getPropertyDescriptors();
211
		for (int i = 0; i < propertyDescriptors.length; i++) {
212
			PropertyDescriptor descriptor = propertyDescriptors[i];
213
			if (descriptor.getName().equals(propertyName)) {
214
				return descriptor;
215
			}
216
		}
217
		throw new BindingException(
218
				"Could not find property with name " + propertyName + " in class " + beanClass); //$NON-NLS-1$ //$NON-NLS-2$
219
	}
204
	}
220
205
221
	/**
206
	/**
Lines 283-291 Link Here
283
	 * collection-typed named property of the given bean object. The returned
268
	 * collection-typed named property of the given bean object. The returned
284
	 * list is mutable. When an item is added or removed the setter is invoked
269
	 * list is mutable. When an item is added or removed the setter is invoked
285
	 * for the list on the parent bean to provide notification to other
270
	 * for the list on the parent bean to provide notification to other
286
	 * listeners via <code>PropertyChangeEvents</code>. This is done to
271
	 * listeners via <code>PropertyChangeEvents</code>. This is done to provide
287
	 * provide the same behavior as is expected from arrays as specified in the
272
	 * the same behavior as is expected from arrays as specified in the bean
288
	 * bean spec in section 7.2.
273
	 * spec in section 7.2.
289
	 * 
274
	 * 
290
	 * @param realm
275
	 * @param realm
291
	 *            the realm
276
	 *            the realm
Lines 294-301 Link Here
294
	 * @param propertyName
279
	 * @param propertyName
295
	 *            the name of the property
280
	 *            the name of the property
296
	 * @param elementType
281
	 * @param elementType
297
	 *            type of the elements in the list. If <code>null</code> and
282
	 *            type of the elements in the list. If <code>null</code> and the
298
	 *            the property is an array the type will be inferred. If
283
	 *            property is an array the type will be inferred. If
299
	 *            <code>null</code> and the property type cannot be inferred
284
	 *            <code>null</code> and the property type cannot be inferred
300
	 *            element type will be <code>null</code>.
285
	 *            element type will be <code>null</code>.
301
	 * @return an observable list tracking the collection-typed named property
286
	 * @return an observable list tracking the collection-typed named property
Lines 303-314 Link Here
303
	 */
288
	 */
304
	public static IObservableList observeList(Realm realm, Object bean,
289
	public static IObservableList observeList(Realm realm, Object bean,
305
			String propertyName, Class elementType) {
290
			String propertyName, Class elementType) {
306
		PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean
291
		IListProperty property = BeanProperties.listProperty(bean.getClass(),
307
				.getClass(), propertyName);
292
				propertyName, elementType);
308
		elementType = getCollectionElementType(elementType, propertyDescriptor);
293
		PropertyDescriptor propertyDescriptor = ((IBeanProperty) property)
309
294
				.getPropertyDescriptor();
310
		return new JavaBeanObservableList(realm, bean, propertyDescriptor,
295
		return new BeanObservableListDecorator(property.observeList(
311
				elementType);
296
				realm, bean), propertyDescriptor);
312
	}
297
	}
313
298
314
	/**
299
	/**
Lines 495-504 Link Here
495
480
496
		IObservableValue value = MasterDetailObservables.detailValue(master,
481
		IObservableValue value = MasterDetailObservables.detailValue(master,
497
				valueFactory(realm, propertyName), propertyType);
482
				valueFactory(realm, propertyName), propertyType);
498
		BeanObservableValueDecorator decorator = new BeanObservableValueDecorator(
483
		return new BeanObservableValueDecorator(value, BeanPropertyHelper
499
				value, getValueTypePropertyDescriptor(master, propertyName));
484
				.getValueTypePropertyDescriptor(master, propertyName));
500
501
		return decorator;
502
	}
485
	}
503
486
504
	/* package */static void warnIfDifferentRealms(Realm detailRealm,
487
	/* package */static void warnIfDifferentRealms(Realm detailRealm,
Lines 538-547 Link Here
538
	/**
521
	/**
539
	 * Helper method for
522
	 * Helper method for
540
	 * <code>MasterDetailObservables.detailValue(master, valueFactory(realm,
523
	 * <code>MasterDetailObservables.detailValue(master, valueFactory(realm,
541
	 * propertyName), propertyType)</code>.
524
	 * propertyName), propertyType)</code>. This method returns an
542
	 * This method returns an {@link IBeanObservable} with a
525
	 * {@link IBeanObservable} with a {@link PropertyDescriptor} based on the
543
	 * {@link PropertyDescriptor} based on the given master type and property
526
	 * given master type and property name.
544
	 * name.
545
	 * 
527
	 * 
546
	 * @param realm
528
	 * @param realm
547
	 *            the realm
529
	 *            the realm
Lines 564-578 Link Here
564
	 *             instead.
546
	 *             instead.
565
	 */
547
	 */
566
	public static IObservableValue observeDetailValue(Realm realm,
548
	public static IObservableValue observeDetailValue(Realm realm,
567
			IObservableValue master, Class masterType, String propertyName, Class propertyType) {
549
			IObservableValue master, Class masterType, String propertyName,
550
			Class propertyType) {
568
		warnIfDifferentRealms(realm, master.getRealm());
551
		warnIfDifferentRealms(realm, master.getRealm());
569
		Assert.isNotNull(masterType, "masterType cannot be null"); //$NON-NLS-1$
552
		Assert.isNotNull(masterType, "masterType cannot be null"); //$NON-NLS-1$
570
		IObservableValue value = MasterDetailObservables.detailValue(master,
553
		IObservableValue value = MasterDetailObservables.detailValue(master,
571
				valueFactory(realm, propertyName), propertyType);
554
				valueFactory(realm, propertyName), propertyType);
572
		BeanObservableValueDecorator decorator = new BeanObservableValueDecorator(
555
		return new BeanObservableValueDecorator(value, BeanPropertyHelper
573
				value, getPropertyDescriptor(masterType, propertyName));
556
				.getPropertyDescriptor(masterType, propertyName));
574
575
		return decorator;
576
	}
557
	}
577
558
578
	/**
559
	/**
Lines 627-637 Link Here
627
		IObservableList observableList = MasterDetailObservables.detailList(
608
		IObservableList observableList = MasterDetailObservables.detailList(
628
				master, listFactory(realm, propertyName, propertyType),
609
				master, listFactory(realm, propertyName, propertyType),
629
				propertyType);
610
				propertyType);
630
		BeanObservableListDecorator decorator = new BeanObservableListDecorator(
611
		return new BeanObservableListDecorator(observableList,
631
				observableList, getValueTypePropertyDescriptor(master,
612
				BeanPropertyHelper.getValueTypePropertyDescriptor(master,
632
						propertyName));
613
						propertyName));
633
634
		return decorator;
635
	}
614
	}
636
615
637
	/**
616
	/**
Lines 679-689 Link Here
679
		IObservableSet observableSet = MasterDetailObservables.detailSet(
658
		IObservableSet observableSet = MasterDetailObservables.detailSet(
680
				master, setFactory(realm, propertyName, propertyType),
659
				master, setFactory(realm, propertyName, propertyType),
681
				propertyType);
660
				propertyType);
682
		BeanObservableSetDecorator decorator = new BeanObservableSetDecorator(
661
		return new BeanObservableSetDecorator(observableSet, BeanPropertyHelper
683
				observableSet, getValueTypePropertyDescriptor(master,
662
				.getValueTypePropertyDescriptor(master, propertyName));
684
						propertyName));
685
686
		return decorator;
687
	}
663
	}
688
664
689
	/**
665
	/**
Lines 725-734 Link Here
725
		warnIfDifferentRealms(realm, master.getRealm());
701
		warnIfDifferentRealms(realm, master.getRealm());
726
		IObservableMap observableMap = MasterDetailObservables.detailMap(
702
		IObservableMap observableMap = MasterDetailObservables.detailMap(
727
				master, mapPropertyFactory(realm, propertyName));
703
				master, mapPropertyFactory(realm, propertyName));
728
		BeanObservableMapDecorator decorator = new BeanObservableMapDecorator(
704
		return new BeanObservableMapDecorator(observableMap, BeanPropertyHelper
729
				observableMap, getValueTypePropertyDescriptor(master,
705
				.getValueTypePropertyDescriptor(master, propertyName));
730
						propertyName));
731
		return decorator;
732
	}
706
	}
733
707
734
	/**
708
	/**
Lines 771-782 Link Here
771
	 */
745
	 */
772
	public static IObservableSet observeSet(Realm realm, Object bean,
746
	public static IObservableSet observeSet(Realm realm, Object bean,
773
			String propertyName, Class elementType) {
747
			String propertyName, Class elementType) {
774
		PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean
748
		ISetProperty property = BeanProperties.setProperty(bean.getClass(),
775
				.getClass(), propertyName);
749
				propertyName, elementType);
776
		elementType = getCollectionElementType(elementType, propertyDescriptor);
750
		PropertyDescriptor propertyDescriptor = ((IBeanProperty) property)
777
751
				.getPropertyDescriptor();
778
		return new JavaBeanObservableSet(realm, bean, propertyDescriptor,
752
		return new BeanObservableSetDecorator(property.observeSet(
779
				elementType);
753
				realm, bean), propertyDescriptor);
780
	}
754
	}
781
755
782
	/**
756
	/**
Lines 863-879 Link Here
863
	 * @param propertyName
837
	 * @param propertyName
864
	 *            the name of the property
838
	 *            the name of the property
865
	 * @return a factory for creating {@link IObservableMap} objects
839
	 * @return a factory for creating {@link IObservableMap} objects
866
	 *
840
	 * 
867
	 * @since 1.1
841
	 * @since 1.1
868
	 */
842
	 */
869
	public static IObservableFactory setToMapFactory(final Class beanClass, final String propertyName) {
843
	public static IObservableFactory setToMapFactory(final Class beanClass,
844
			final String propertyName) {
870
		return new IObservableFactory() {
845
		return new IObservableFactory() {
871
			public IObservable createObservable(Object target) {
846
			public IObservable createObservable(Object target) {
872
				return observeMap((IObservableSet) target, beanClass, propertyName);
847
				return observeMap((IObservableSet) target, beanClass,
848
						propertyName);
873
			}
849
			}
874
		};
850
		};
875
	}
851
	}
876
	
852
877
	/**
853
	/**
878
	 * Returns a factory for creating an observable map. The factory, when
854
	 * Returns a factory for creating an observable map. The factory, when
879
	 * provided with a bean object, will create an {@link IObservableMap} in the
855
	 * provided with a bean object, will create an {@link IObservableMap} in the
Lines 911-942 Link Here
911
	public static IObservableFactory mapPropertyFactory(String propertyName) {
887
	public static IObservableFactory mapPropertyFactory(String propertyName) {
912
		return mapPropertyFactory(Realm.getDefault(), propertyName);
888
		return mapPropertyFactory(Realm.getDefault(), propertyName);
913
	}
889
	}
914
915
	/**
916
	 * @param elementType
917
	 *            can be <code>null</code>
918
	 * @param propertyDescriptor
919
	 * @return type of the items in a collection/array property
920
	 */
921
	/*package*/ static Class getCollectionElementType(Class elementType,
922
			PropertyDescriptor propertyDescriptor) {
923
		if (elementType == null) {
924
			Class propertyType = propertyDescriptor.getPropertyType();
925
			elementType = propertyType.isArray() ? propertyType
926
					.getComponentType() : Object.class;
927
		}
928
929
		return elementType;
930
	}
931
932
	/**
933
	 * @param observable
934
	 * @param propertyName
935
	 * @return property descriptor or <code>null</code>
936
	 */
937
	/* package*/ static PropertyDescriptor getValueTypePropertyDescriptor(
938
			IObservableValue observable, String propertyName) {
939
		return (observable.getValueType() != null) ? getPropertyDescriptor(
940
				(Class) observable.getValueType(), propertyName) : null;
941
	}
942
}
890
}
(-)src/org/eclipse/core/internal/databinding/beans/BeanMapProperty.java (+135 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.beans;
13
14
import java.beans.PropertyChangeEvent;
15
import java.beans.PropertyChangeListener;
16
import java.beans.PropertyDescriptor;
17
import java.util.Collections;
18
import java.util.HashMap;
19
import java.util.Map;
20
21
import org.eclipse.core.databinding.beans.IBeanProperty;
22
import org.eclipse.core.databinding.observable.Diffs;
23
import org.eclipse.core.databinding.observable.map.MapDiff;
24
import org.eclipse.core.databinding.property.INativePropertyListener;
25
import org.eclipse.core.databinding.property.map.IMapPropertyChangeListener;
26
import org.eclipse.core.databinding.property.map.MapPropertyChangeEvent;
27
import org.eclipse.core.databinding.property.map.SimpleMapProperty;
28
29
/**
30
 * @since 3.3
31
 * 
32
 */
33
public class BeanMapProperty extends SimpleMapProperty implements IBeanProperty {
34
	private PropertyDescriptor propertyDescriptor;
35
	private Object keyType;
36
	private Object valueType;
37
38
	/**
39
	 * @param propertyDescriptor
40
	 * @param keyType
41
	 * @param valueType
42
	 */
43
	public BeanMapProperty(PropertyDescriptor propertyDescriptor,
44
			Object keyType, Object valueType) {
45
		this.propertyDescriptor = propertyDescriptor;
46
		this.keyType = keyType;
47
		this.valueType = valueType;
48
	}
49
50
	protected Map doGetMap(Object source) {
51
		if (source == null)
52
			return Collections.EMPTY_MAP;
53
		Object propertyValue = BeanPropertyHelper.readProperty(source,
54
				propertyDescriptor);
55
		return asMap(propertyValue);
56
	}
57
58
	private Map asMap(Object propertyValue) {
59
		if (propertyValue == null)
60
			return new HashMap();
61
		return (Map) propertyValue;
62
	}
63
64
	protected void setMap(Object source, Map map, MapDiff diff) {
65
		if (source != null) {
66
			BeanPropertyHelper.writeProperty(source, propertyDescriptor, map);
67
		}
68
	}
69
70
	public PropertyDescriptor getPropertyDescriptor() {
71
		return propertyDescriptor;
72
	}
73
74
	public Object getKeyType() {
75
		return keyType;
76
	}
77
78
	public Object getValueType() {
79
		return valueType;
80
	}
81
82
	public INativePropertyListener adaptListener(
83
			final IMapPropertyChangeListener listener) {
84
		return new Listener(listener);
85
	}
86
87
	private class Listener implements INativePropertyListener,
88
			PropertyChangeListener {
89
		private final IMapPropertyChangeListener listener;
90
91
		private Listener(IMapPropertyChangeListener listener) {
92
			this.listener = listener;
93
		}
94
95
		public void propertyChange(PropertyChangeEvent evt) {
96
			if (propertyDescriptor.getName().equals(evt.getPropertyName())) {
97
				Object oldValue = evt.getOldValue();
98
				Object newValue = evt.getNewValue();
99
100
				MapDiff diff;
101
				if (oldValue == null && newValue == null) {
102
					diff = null; // unknown change
103
				} else {
104
					diff = Diffs.computeMapDiff(asMap(oldValue),
105
							asMap(newValue));
106
				}
107
108
				listener.handleMapPropertyChange(new MapPropertyChangeEvent(evt
109
						.getSource(), BeanMapProperty.this, diff));
110
			}
111
		}
112
	}
113
114
	public void addListener(Object source, INativePropertyListener listener) {
115
		BeanPropertyListenerSupport.hookListener(source, propertyDescriptor
116
				.getName(), (PropertyChangeListener) listener);
117
	}
118
119
	public void removeListener(Object source, INativePropertyListener listener) {
120
		BeanPropertyListenerSupport.unhookListener(source, propertyDescriptor
121
				.getName(), (PropertyChangeListener) listener);
122
	}
123
124
	public String toString() {
125
		Class beanClass = propertyDescriptor.getReadMethod()
126
				.getDeclaringClass();
127
		String propertyName = propertyDescriptor.getName();
128
		String s = beanClass.getName() + "." + propertyName + "{:}"; //$NON-NLS-1$ //$NON-NLS-2$
129
130
		if (keyType != null || valueType != null) {
131
			s += " <" + keyType + ", " + valueType + ">"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
132
		}
133
		return s;
134
	}
135
}
(-)src/org/eclipse/core/internal/databinding/beans/PojoMapProperty.java (+100 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.beans;
13
14
import java.beans.PropertyDescriptor;
15
import java.util.Collections;
16
import java.util.HashMap;
17
import java.util.Map;
18
19
import org.eclipse.core.databinding.beans.IBeanProperty;
20
import org.eclipse.core.databinding.observable.map.MapDiff;
21
import org.eclipse.core.databinding.property.INativePropertyListener;
22
import org.eclipse.core.databinding.property.map.IMapPropertyChangeListener;
23
import org.eclipse.core.databinding.property.map.SimpleMapProperty;
24
25
/**
26
 * @since 3.3
27
 * 
28
 */
29
public class PojoMapProperty extends SimpleMapProperty implements IBeanProperty {
30
	private PropertyDescriptor propertyDescriptor;
31
	private Object keyType;
32
	private Object valueType;
33
34
	/**
35
	 * @param propertyDescriptor
36
	 * @param keyType
37
	 * @param valueType
38
	 */
39
	public PojoMapProperty(PropertyDescriptor propertyDescriptor,
40
			Object keyType, Object valueType) {
41
		this.propertyDescriptor = propertyDescriptor;
42
		this.keyType = keyType;
43
		this.valueType = valueType;
44
	}
45
46
	protected Map doGetMap(Object source) {
47
		if (source == null)
48
			return Collections.EMPTY_MAP;
49
		Object propertyValue = BeanPropertyHelper.readProperty(source,
50
				propertyDescriptor);
51
		return asMap(propertyValue);
52
	}
53
54
	private Map asMap(Object propertyValue) {
55
		if (propertyValue == null)
56
			return new HashMap();
57
		return (Map) propertyValue;
58
	}
59
60
	protected void setMap(Object source, Map map, MapDiff diff) {
61
		if (source != null) {
62
			BeanPropertyHelper.writeProperty(source, propertyDescriptor, map);
63
		}
64
	}
65
66
	public PropertyDescriptor getPropertyDescriptor() {
67
		return propertyDescriptor;
68
	}
69
70
	public Object getKeyType() {
71
		return keyType;
72
	}
73
74
	public Object getValueType() {
75
		return valueType;
76
	}
77
78
	public INativePropertyListener adaptListener(
79
			IMapPropertyChangeListener listener) {
80
		return null;
81
	}
82
83
	public void addListener(Object source, INativePropertyListener listener) {
84
	}
85
86
	public void removeListener(Object source, INativePropertyListener listener) {
87
	}
88
89
	public String toString() {
90
		Class beanClass = propertyDescriptor.getReadMethod()
91
				.getDeclaringClass();
92
		String propertyName = propertyDescriptor.getName();
93
		String s = beanClass.getName() + "." + propertyName + "{:}"; //$NON-NLS-1$ //$NON-NLS-2$
94
95
		if (keyType != null || valueType != null) {
96
			s += " <" + keyType + ", " + valueType + ">"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
97
		}
98
		return s;
99
	}
100
}
(-)src/org/eclipse/core/databinding/beans/PojoProperties.java (+159 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.beans;
13
14
import java.beans.PropertyChangeEvent;
15
16
import org.eclipse.core.databinding.property.list.IListProperty;
17
import org.eclipse.core.databinding.property.map.IMapProperty;
18
import org.eclipse.core.databinding.property.set.ISetProperty;
19
import org.eclipse.core.databinding.property.value.IValueProperty;
20
import org.eclipse.core.internal.databinding.beans.BeanPropertyHelper;
21
import org.eclipse.core.internal.databinding.beans.PojoListProperty;
22
import org.eclipse.core.internal.databinding.beans.PojoMapProperty;
23
import org.eclipse.core.internal.databinding.beans.PojoSetProperty;
24
import org.eclipse.core.internal.databinding.beans.PojoValueProperty;
25
26
/**
27
 * A factory for creating properties for POJOs (plain old java objects) that
28
 * conform to idea of an object with getters and setters but does not provide
29
 * {@link PropertyChangeEvent property change events} on change. This factory is
30
 * identical to {@link BeanProperties} except for this fact.
31
 * 
32
 * @since 1.2
33
 */
34
public class PojoProperties {
35
	/**
36
	 * Returns a value property for the given property name of the given bean
37
	 * class.
38
	 * 
39
	 * @param beanClass
40
	 *            the bean class
41
	 * @param propertyName
42
	 *            the property name
43
	 * @return a value property for the given property name of the given bean
44
	 *         class.
45
	 */
46
	public static IValueProperty valueProperty(Class beanClass,
47
			String propertyName) {
48
		return valueProperty(beanClass, propertyName, null);
49
	}
50
51
	/**
52
	 * Returns a value property for the given property name of the given bean
53
	 * class.
54
	 * 
55
	 * @param beanClass
56
	 *            the bean class
57
	 * @param propertyName
58
	 *            the property name
59
	 * @param valueType
60
	 *            the value type of the returned value property
61
	 * @return a value property for the given property name of the given bean
62
	 *         class.
63
	 */
64
	public static IValueProperty valueProperty(Class beanClass,
65
			String propertyName, Class valueType) {
66
		return new PojoValueProperty(BeanPropertyHelper.getPropertyDescriptor(
67
				beanClass, propertyName), valueType);
68
	}
69
70
	/**
71
	 * Returns a set property for the given property name of the given bean
72
	 * class.
73
	 * 
74
	 * @param beanClass
75
	 *            the bean class
76
	 * @param propertyName
77
	 *            the property name
78
	 * @return a set property for the given property name of the given bean
79
	 *         class.
80
	 */
81
	public static ISetProperty setProperty(Class beanClass, String propertyName) {
82
		return setProperty(beanClass, propertyName, null);
83
	}
84
85
	/**
86
	 * Returns a set property for the given property name of the given bean
87
	 * class.
88
	 * 
89
	 * @param beanClass
90
	 *            the bean class
91
	 * @param propertyName
92
	 *            the property name
93
	 * @param elementType
94
	 *            the element type of the returned set property
95
	 * @return a set property for the given property name of the given bean
96
	 *         class.
97
	 */
98
	public static ISetProperty setProperty(Class beanClass,
99
			String propertyName, Class elementType) {
100
		return new PojoSetProperty(BeanPropertyHelper.getPropertyDescriptor(
101
				beanClass, propertyName), elementType);
102
	}
103
104
	/**
105
	 * Returns a list property for the given property name of the given bean
106
	 * class.
107
	 * 
108
	 * @param beanClass
109
	 *            the bean class
110
	 * @param propertyName
111
	 *            the property name
112
	 * @return a list property for the given property name of the given bean
113
	 *         class.
114
	 */
115
	public static IListProperty listProperty(Class beanClass,
116
			String propertyName) {
117
		return listProperty(beanClass, propertyName, null);
118
	}
119
120
	/**
121
	 * Returns a list property for the given property name of the given bean
122
	 * class.
123
	 * 
124
	 * @param beanClass
125
	 *            the bean class
126
	 * @param propertyName
127
	 *            the property name
128
	 * @param elementType
129
	 *            the element type of the returned list property
130
	 * @return a list property for the given property name of the given bean
131
	 *         class.
132
	 */
133
	public static IListProperty listProperty(Class beanClass,
134
			String propertyName, Class elementType) {
135
		return new PojoListProperty(BeanPropertyHelper.getPropertyDescriptor(
136
				beanClass, propertyName), elementType);
137
	}
138
139
	/**
140
	 * Returns a map property for the given property name of the given bean
141
	 * class.
142
	 * 
143
	 * @param beanClass
144
	 *            the bean class
145
	 * @param propertyName
146
	 *            the property name
147
	 * @param keyType
148
	 *            the key type of the returned map property
149
	 * @param valueType
150
	 *            the value type of the returned map property
151
	 * @return a map property for the given property name of the given bean
152
	 *         class.
153
	 */
154
	public static IMapProperty mapProperty(Class beanClass,
155
			String propertyName, Object keyType, Object valueType) {
156
		return new PojoMapProperty(BeanPropertyHelper.getPropertyDescriptor(
157
				beanClass, propertyName), keyType, valueType);
158
	}
159
}
(-)src/org/eclipse/core/internal/databinding/beans/BeanListProperty.java (+148 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.beans;
13
14
import java.beans.PropertyChangeEvent;
15
import java.beans.PropertyChangeListener;
16
import java.beans.PropertyDescriptor;
17
import java.lang.reflect.Array;
18
import java.util.ArrayList;
19
import java.util.Arrays;
20
import java.util.Collections;
21
import java.util.List;
22
23
import org.eclipse.core.databinding.beans.IBeanProperty;
24
import org.eclipse.core.databinding.observable.Diffs;
25
import org.eclipse.core.databinding.observable.list.ListDiff;
26
import org.eclipse.core.databinding.property.INativePropertyListener;
27
import org.eclipse.core.databinding.property.list.IListPropertyChangeListener;
28
import org.eclipse.core.databinding.property.list.ListPropertyChangeEvent;
29
import org.eclipse.core.databinding.property.list.SimpleListProperty;
30
31
/**
32
 * @since 3.3
33
 * 
34
 */
35
public class BeanListProperty extends SimpleListProperty implements
36
		IBeanProperty {
37
	private final PropertyDescriptor propertyDescriptor;
38
	private final Class elementType;
39
40
	/**
41
	 * @param propertyDescriptor
42
	 * @param elementType
43
	 */
44
	public BeanListProperty(PropertyDescriptor propertyDescriptor,
45
			Class elementType) {
46
		this.propertyDescriptor = propertyDescriptor;
47
		this.elementType = elementType == null ? BeanPropertyHelper
48
				.getCollectionPropertyElementType(propertyDescriptor)
49
				: elementType;
50
	}
51
52
	public Object getElementType() {
53
		return elementType;
54
	}
55
56
	protected List doGetList(Object source) {
57
		if (source == null)
58
			return Collections.EMPTY_LIST;
59
		Object propertyValue = BeanPropertyHelper.readProperty(source,
60
				propertyDescriptor);
61
		return asList(propertyValue);
62
	}
63
64
	private List asList(Object propertyValue) {
65
		if (propertyValue == null)
66
			return new ArrayList();
67
		if (propertyDescriptor.getPropertyType().isArray())
68
			return new ArrayList(Arrays.asList((Object[]) propertyValue));
69
		return (List) propertyValue;
70
	}
71
72
	protected void setList(Object source, List list, ListDiff diff) {
73
		if (source != null) {
74
			BeanPropertyHelper.writeProperty(source, propertyDescriptor,
75
					convertListToBeanPropertyType(list));
76
		}
77
	}
78
79
	private Object convertListToBeanPropertyType(List list) {
80
		Object propertyValue = list;
81
		if (propertyDescriptor.getPropertyType().isArray()) {
82
			Class componentType = propertyDescriptor.getPropertyType()
83
					.getComponentType();
84
			Object[] array = (Object[]) Array.newInstance(componentType, list
85
					.size());
86
			list.toArray(array);
87
			propertyValue = array;
88
		}
89
		return propertyValue;
90
	}
91
92
	public PropertyDescriptor getPropertyDescriptor() {
93
		return propertyDescriptor;
94
	}
95
96
	public INativePropertyListener adaptListener(
97
			final IListPropertyChangeListener listener) {
98
		return new Listener(listener);
99
	}
100
101
	private class Listener implements INativePropertyListener,
102
			PropertyChangeListener {
103
		private final IListPropertyChangeListener listener;
104
105
		private Listener(IListPropertyChangeListener listener) {
106
			this.listener = listener;
107
		}
108
109
		public void propertyChange(PropertyChangeEvent evt) {
110
			if (propertyDescriptor.getName().equals(evt.getPropertyName())) {
111
				Object oldValue = evt.getOldValue();
112
				Object newValue = evt.getNewValue();
113
114
				ListDiff diff;
115
				if (oldValue == null && newValue == null) {
116
					diff = null; // unknown change
117
				} else {
118
					diff = Diffs.computeListDiff(asList(oldValue),
119
							asList(newValue));
120
				}
121
122
				listener.handleListPropertyChange(new ListPropertyChangeEvent(
123
						evt.getSource(), BeanListProperty.this, diff));
124
			}
125
		}
126
	}
127
128
	public void addListener(Object source, INativePropertyListener listener) {
129
		BeanPropertyListenerSupport.hookListener(source, propertyDescriptor
130
				.getName(), (PropertyChangeListener) listener);
131
	}
132
133
	public void removeListener(Object source, INativePropertyListener listener) {
134
		BeanPropertyListenerSupport.unhookListener(source, propertyDescriptor
135
				.getName(), (PropertyChangeListener) listener);
136
	}
137
138
	public String toString() {
139
		Class beanClass = propertyDescriptor.getReadMethod()
140
				.getDeclaringClass();
141
		String propertyName = propertyDescriptor.getName();
142
		String s = beanClass.getName() + "." + propertyName + "[]"; //$NON-NLS-1$ //$NON-NLS-2$
143
144
		if (elementType != null)
145
			s += " <" + elementType.getName() + ">"; //$NON-NLS-1$//$NON-NLS-2$
146
		return s;
147
	}
148
}
(-)src/org/eclipse/core/internal/databinding/beans/BeanSetProperty.java (+146 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.beans;
13
14
import java.beans.PropertyChangeEvent;
15
import java.beans.PropertyChangeListener;
16
import java.beans.PropertyDescriptor;
17
import java.lang.reflect.Array;
18
import java.util.Arrays;
19
import java.util.Collections;
20
import java.util.HashSet;
21
import java.util.Set;
22
23
import org.eclipse.core.databinding.beans.IBeanProperty;
24
import org.eclipse.core.databinding.observable.Diffs;
25
import org.eclipse.core.databinding.observable.set.SetDiff;
26
import org.eclipse.core.databinding.property.INativePropertyListener;
27
import org.eclipse.core.databinding.property.set.ISetPropertyChangeListener;
28
import org.eclipse.core.databinding.property.set.SetPropertyChangeEvent;
29
import org.eclipse.core.databinding.property.set.SimpleSetProperty;
30
31
/**
32
 * @since 3.3
33
 * 
34
 */
35
public class BeanSetProperty extends SimpleSetProperty implements IBeanProperty {
36
	private PropertyDescriptor propertyDescriptor;
37
	private Class elementType;
38
39
	/**
40
	 * @param propertyDescriptor
41
	 * @param elementType
42
	 */
43
	public BeanSetProperty(PropertyDescriptor propertyDescriptor,
44
			Class elementType) {
45
		this.propertyDescriptor = propertyDescriptor;
46
		this.elementType = elementType == null ? BeanPropertyHelper
47
				.getCollectionPropertyElementType(propertyDescriptor)
48
				: elementType;
49
	}
50
51
	public Object getElementType() {
52
		return elementType;
53
	}
54
55
	protected Set doGetSet(Object source) {
56
		if (source == null)
57
			return Collections.EMPTY_SET;
58
		Object propertyValue = BeanPropertyHelper.readProperty(source,
59
				propertyDescriptor);
60
		return asSet(propertyValue);
61
	}
62
63
	private Set asSet(Object propertyValue) {
64
		if (propertyValue == null)
65
			return Collections.EMPTY_SET;
66
		if (propertyDescriptor.getPropertyType().isArray())
67
			return new HashSet(Arrays.asList((Object[]) propertyValue));
68
		return (Set) propertyValue;
69
	}
70
71
	protected void setSet(Object source, Set set, SetDiff diff) {
72
		if (source != null) {
73
			BeanPropertyHelper.writeProperty(source, propertyDescriptor,
74
					convertSetToBeanPropertyType(set));
75
		}
76
	}
77
78
	private Object convertSetToBeanPropertyType(Set set) {
79
		Object propertyValue = set;
80
		if (propertyDescriptor.getPropertyType().isArray()) {
81
			Class componentType = propertyDescriptor.getPropertyType()
82
					.getComponentType();
83
			Object[] array = (Object[]) Array.newInstance(componentType, set
84
					.size());
85
			propertyValue = set.toArray(array);
86
		}
87
		return propertyValue;
88
	}
89
90
	public PropertyDescriptor getPropertyDescriptor() {
91
		return propertyDescriptor;
92
	}
93
94
	public INativePropertyListener adaptListener(
95
			final ISetPropertyChangeListener listener) {
96
		return new Listener(listener);
97
	}
98
99
	private class Listener implements INativePropertyListener,
100
			PropertyChangeListener {
101
		private final ISetPropertyChangeListener listener;
102
103
		private Listener(ISetPropertyChangeListener listener) {
104
			this.listener = listener;
105
		}
106
107
		public void propertyChange(PropertyChangeEvent evt) {
108
			if (propertyDescriptor.getName().equals(evt.getPropertyName())) {
109
				Object oldValue = evt.getOldValue();
110
				Object newValue = evt.getNewValue();
111
112
				SetDiff diff;
113
				if (oldValue == null && newValue == null) {
114
					diff = null; // unknown change
115
				} else {
116
					diff = Diffs.computeSetDiff(asSet(oldValue),
117
							asSet(newValue));
118
				}
119
120
				listener.handleSetPropertyChange(new SetPropertyChangeEvent(evt
121
						.getSource(), BeanSetProperty.this, diff));
122
			}
123
		}
124
	}
125
126
	public void addListener(Object source, INativePropertyListener listener) {
127
		BeanPropertyListenerSupport.hookListener(source, propertyDescriptor
128
				.getName(), (PropertyChangeListener) listener);
129
	}
130
131
	public void removeListener(Object source, INativePropertyListener listener) {
132
		BeanPropertyListenerSupport.unhookListener(source, propertyDescriptor
133
				.getName(), (PropertyChangeListener) listener);
134
	}
135
136
	public String toString() {
137
		Class beanClass = propertyDescriptor.getReadMethod()
138
				.getDeclaringClass();
139
		String propertyName = propertyDescriptor.getName();
140
		String s = beanClass.getName() + "." + propertyName + "{}"; //$NON-NLS-1$ //$NON-NLS-2$
141
142
		if (elementType != null)
143
			s += " <" + elementType.getName() + ">"; //$NON-NLS-1$//$NON-NLS-2$
144
		return s;
145
	}
146
}
(-)src/org/eclipse/core/internal/databinding/beans/PojoListProperty.java (+113 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.beans;
13
14
import java.beans.PropertyDescriptor;
15
import java.lang.reflect.Array;
16
import java.util.ArrayList;
17
import java.util.Arrays;
18
import java.util.Collections;
19
import java.util.List;
20
21
import org.eclipse.core.databinding.beans.IBeanProperty;
22
import org.eclipse.core.databinding.observable.list.ListDiff;
23
import org.eclipse.core.databinding.property.INativePropertyListener;
24
import org.eclipse.core.databinding.property.list.IListPropertyChangeListener;
25
import org.eclipse.core.databinding.property.list.SimpleListProperty;
26
27
/**
28
 * @since 3.3
29
 * 
30
 */
31
public class PojoListProperty extends SimpleListProperty implements
32
		IBeanProperty {
33
	private PropertyDescriptor propertyDescriptor;
34
	private Class elementType;
35
36
	/**
37
	 * @param propertyDescriptor
38
	 * @param elementType
39
	 */
40
	public PojoListProperty(PropertyDescriptor propertyDescriptor,
41
			Class elementType) {
42
		this.propertyDescriptor = propertyDescriptor;
43
		this.elementType = elementType == null ? BeanPropertyHelper
44
				.getCollectionPropertyElementType(propertyDescriptor)
45
				: elementType;
46
	}
47
48
	public Object getElementType() {
49
		return elementType;
50
	}
51
52
	protected List doGetList(Object source) {
53
		if (source == null)
54
			return Collections.EMPTY_LIST;
55
		Object propertyValue = BeanPropertyHelper.readProperty(source,
56
				propertyDescriptor);
57
		return asList(propertyValue);
58
	}
59
60
	private List asList(Object propertyValue) {
61
		if (propertyValue == null)
62
			return new ArrayList();
63
		if (propertyDescriptor.getPropertyType().isArray())
64
			return new ArrayList(Arrays.asList((Object[]) propertyValue));
65
		return (List) propertyValue;
66
	}
67
68
	protected void setList(Object source, List list, ListDiff diff) {
69
		if (source != null) {
70
			BeanPropertyHelper.writeProperty(source, propertyDescriptor,
71
					convertListToBeanPropertyType(list));
72
		}
73
	}
74
75
	private Object convertListToBeanPropertyType(List list) {
76
		Object propertyValue = list;
77
		if (propertyDescriptor.getPropertyType().isArray()) {
78
			Class componentType = propertyDescriptor.getPropertyType()
79
					.getComponentType();
80
			Object[] array = (Object[]) Array.newInstance(componentType, list
81
					.size());
82
			list.toArray(array);
83
			propertyValue = array;
84
		}
85
		return propertyValue;
86
	}
87
88
	public PropertyDescriptor getPropertyDescriptor() {
89
		return propertyDescriptor;
90
	}
91
92
	public INativePropertyListener adaptListener(
93
			IListPropertyChangeListener listener) {
94
		return null;
95
	}
96
97
	public void addListener(Object source, INativePropertyListener listener) {
98
	}
99
100
	public void removeListener(Object source, INativePropertyListener listener) {
101
	}
102
103
	public String toString() {
104
		Class beanClass = propertyDescriptor.getReadMethod()
105
				.getDeclaringClass();
106
		String propertyName = propertyDescriptor.getName();
107
		String s = beanClass.getName() + "." + propertyName + "[]"; //$NON-NLS-1$ //$NON-NLS-2$
108
109
		if (elementType != null)
110
			s += " <" + elementType.getName() + ">"; //$NON-NLS-1$//$NON-NLS-2$
111
		return s;
112
	}
113
}
(-)src/org/eclipse/core/internal/databinding/beans/BeanPropertyHelper.java (+169 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.beans;
13
14
import java.beans.BeanInfo;
15
import java.beans.IntrospectionException;
16
import java.beans.Introspector;
17
import java.beans.PropertyDescriptor;
18
import java.lang.reflect.InvocationTargetException;
19
import java.lang.reflect.Method;
20
21
import org.eclipse.core.databinding.BindingException;
22
import org.eclipse.core.databinding.beans.BeansObservables;
23
import org.eclipse.core.databinding.observable.value.IObservableValue;
24
import org.eclipse.core.databinding.util.Policy;
25
import org.eclipse.core.runtime.IStatus;
26
import org.eclipse.core.runtime.Status;
27
28
/**
29
 * @since 1.2
30
 * 
31
 */
32
public class BeanPropertyHelper {
33
	/**
34
	 * Sets the contents of the given property on the given source object to the
35
	 * given value.
36
	 * 
37
	 * @param source
38
	 *            the source object which has the property being updated
39
	 * @param propertyDescriptor
40
	 *            the property being changed
41
	 * @param value
42
	 *            the new value of the property
43
	 */
44
	public static void writeProperty(Object source,
45
			PropertyDescriptor propertyDescriptor, Object value) {
46
		try {
47
			Method writeMethod = propertyDescriptor.getWriteMethod();
48
			if (!writeMethod.isAccessible()) {
49
				writeMethod.setAccessible(true);
50
			}
51
			writeMethod.invoke(source, new Object[] { value });
52
		} catch (InvocationTargetException e) {
53
			/*
54
			 * InvocationTargetException wraps any exception thrown by the
55
			 * invoked method.
56
			 */
57
			throw new RuntimeException(e.getCause());
58
		} catch (Exception e) {
59
			if (BeansObservables.DEBUG) {
60
				Policy
61
						.getLog()
62
						.log(
63
								new Status(
64
										IStatus.WARNING,
65
										Policy.JFACE_DATABINDING,
66
										IStatus.OK,
67
										"Could not change value of " + source + "." + propertyDescriptor.getName(), e)); //$NON-NLS-1$ //$NON-NLS-2$
68
			}
69
		}
70
	}
71
72
	/**
73
	 * Returns the contents of the given property for the given bean.
74
	 * 
75
	 * @param source
76
	 *            the source bean
77
	 * @param propertyDescriptor
78
	 *            the property to retrieve
79
	 * @return the contents of the given property for the given bean.
80
	 */
81
	public static Object readProperty(Object source,
82
			PropertyDescriptor propertyDescriptor) {
83
		try {
84
			Method readMethod = propertyDescriptor.getReadMethod();
85
			if (readMethod == null) {
86
				throw new BindingException(propertyDescriptor.getName()
87
						+ " property does not have a read method."); //$NON-NLS-1$
88
			}
89
			if (!readMethod.isAccessible()) {
90
				readMethod.setAccessible(true);
91
			}
92
			return readMethod.invoke(source, null);
93
		} catch (InvocationTargetException e) {
94
			/*
95
			 * InvocationTargetException wraps any exception thrown by the
96
			 * invoked method.
97
			 */
98
			throw new RuntimeException(e.getCause());
99
		} catch (Exception e) {
100
			if (BeansObservables.DEBUG) {
101
				Policy
102
						.getLog()
103
						.log(
104
								new Status(
105
										IStatus.WARNING,
106
										Policy.JFACE_DATABINDING,
107
										IStatus.OK,
108
										"Could not read value of " + source + "." + propertyDescriptor.getName(), e)); //$NON-NLS-1$ //$NON-NLS-2$
109
			}
110
			return null;
111
		}
112
	}
113
114
	/**
115
	 * Returns the element type of the given collection-typed property for the
116
	 * given bean.
117
	 * 
118
	 * @param descriptor
119
	 *            the property being inspected
120
	 * @return the element type of the given collection-typed property if it is
121
	 *         an array property, or Object.class otherwise.
122
	 */
123
	public static Class getCollectionPropertyElementType(
124
			PropertyDescriptor descriptor) {
125
		Class propertyType = descriptor.getPropertyType();
126
		return propertyType.isArray() ? propertyType.getComponentType()
127
				: Object.class;
128
	}
129
130
	/**
131
	 * @param beanClass
132
	 * @param propertyName
133
	 * @return the PropertyDescriptor for the named property on the given bean
134
	 *         class
135
	 */
136
	public static PropertyDescriptor getPropertyDescriptor(Class beanClass,
137
			String propertyName) {
138
		BeanInfo beanInfo;
139
		try {
140
			beanInfo = Introspector.getBeanInfo(beanClass);
141
		} catch (IntrospectionException e) {
142
			// cannot introspect, give up
143
			return null;
144
		}
145
		PropertyDescriptor[] propertyDescriptors = beanInfo
146
				.getPropertyDescriptors();
147
		for (int i = 0; i < propertyDescriptors.length; i++) {
148
			PropertyDescriptor descriptor = propertyDescriptors[i];
149
			if (descriptor.getName().equals(propertyName)) {
150
				return descriptor;
151
			}
152
		}
153
		throw new BindingException(
154
				"Could not find property with name " + propertyName + " in class " + beanClass); //$NON-NLS-1$ //$NON-NLS-2$
155
	}
156
157
	/**
158
	 * @param observable
159
	 * @param propertyName
160
	 * @return property descriptor or <code>null</code>
161
	 */
162
	/* package */public static PropertyDescriptor getValueTypePropertyDescriptor(
163
			IObservableValue observable, String propertyName) {
164
		if (observable.getValueType() != null)
165
			return getPropertyDescriptor((Class) observable.getValueType(),
166
					propertyName);
167
		return null;
168
	}
169
}
(-)src/org/eclipse/jface/internal/databinding/viewers/ViewerSingleSelectionObservableValue.java (-45 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 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
 *     Brad Reynolds - bug 137877
11
 *     Brad Reynolds - bug 164653
12
 *     Brad Reynolds - bug 147515
13
 *     Ashley Cambrell - bug 198906
14
 *******************************************************************************/
15
16
package org.eclipse.jface.internal.databinding.viewers;
17
18
import org.eclipse.core.databinding.observable.Realm;
19
import org.eclipse.jface.databinding.viewers.IViewerObservableValue;
20
import org.eclipse.jface.viewers.Viewer;
21
22
/**
23
 * Observes single selection of a <code>Viewer</code>.
24
 * 
25
 * @since 1.2
26
 */
27
public class ViewerSingleSelectionObservableValue extends
28
		SelectionProviderSingleSelectionObservableValue implements
29
		IViewerObservableValue {
30
31
	private Viewer viewer;
32
33
	/**
34
	 * @param realm
35
	 * @param viewer
36
	 */
37
	public ViewerSingleSelectionObservableValue(Realm realm, Viewer viewer) {
38
		super(realm, viewer);
39
		this.viewer = viewer;
40
	}
41
42
	public Viewer getViewer() {
43
		return viewer;
44
	}
45
}
(-)src/org/eclipse/jface/internal/databinding/viewers/ViewerMultipleSelectionObservableList.java (-47 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 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
 *     Brad Reynolds - bug 137877
11
 *     Brad Reynolds - bug 164653
12
 *     Brad Reynolds - bug 147515
13
 *     Ashley Cambrell - bug 198906
14
 *******************************************************************************/
15
16
package org.eclipse.jface.internal.databinding.viewers;
17
18
import org.eclipse.core.databinding.observable.Realm;
19
import org.eclipse.jface.databinding.viewers.IViewerObservableList;
20
import org.eclipse.jface.viewers.Viewer;
21
22
/**
23
 * Observes single selection of a <code>Viewer</code>.
24
 * 
25
 * @since 1.2
26
 */
27
public class ViewerMultipleSelectionObservableList extends
28
		SelectionProviderMultipleSelectionObservableList implements
29
		IViewerObservableList {
30
31
	private Viewer viewer;
32
33
	/**
34
	 * @param realm
35
	 * @param viewer
36
	 * @param elementType
37
	 */
38
	public ViewerMultipleSelectionObservableList(Realm realm, Viewer viewer,
39
			Object elementType) {
40
		super(realm, viewer, elementType);
41
		this.viewer = viewer;
42
	}
43
44
	public Viewer getViewer() {
45
		return viewer;
46
	}
47
}
(-)src/org/eclipse/jface/internal/databinding/viewers/CheckboxViewerCheckedElementsObservableSet.java (-90 lines)
Removed Link Here
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
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.viewers;
13
14
import java.util.Arrays;
15
import java.util.Set;
16
17
import org.eclipse.core.databinding.observable.Realm;
18
import org.eclipse.jface.databinding.viewers.IViewerObservableSet;
19
import org.eclipse.jface.viewers.CheckboxTableViewer;
20
import org.eclipse.jface.viewers.CheckboxTreeViewer;
21
import org.eclipse.jface.viewers.StructuredViewer;
22
import org.eclipse.jface.viewers.Viewer;
23
24
/**
25
 * An observable set that tracks the checked elements in a CheckboxTableViewer
26
 * or CheckboxTreeViewer
27
 * 
28
 * @since 1.2
29
 */
30
public class CheckboxViewerCheckedElementsObservableSet extends
31
		CheckableCheckedElementsObservableSet implements IViewerObservableSet {
32
	private StructuredViewer viewer;
33
34
	/**
35
	 * Constructs a new instance on the given realm and checkable.
36
	 * 
37
	 * @param realm
38
	 *            the observable's realm
39
	 * @param viewer
40
	 *            the CheckboxTableViewer viewer to track.
41
	 * @param elementType
42
	 *            type of elements in the set
43
	 */
44
	public CheckboxViewerCheckedElementsObservableSet(Realm realm,
45
			CheckboxTableViewer viewer, Object elementType) {
46
		super(realm, viewer, elementType, createElementSet(viewer));
47
		this.viewer = viewer;
48
	}
49
50
	/**
51
	 * Constructs a new instance on the given realm and checkable.
52
	 * 
53
	 * @param realm
54
	 *            the observable's realm
55
	 * @param viewer
56
	 *            the CheckboxTreeViewer viewer to track.
57
	 * @param elementType
58
	 *            type of elements in the set
59
	 */
60
	public CheckboxViewerCheckedElementsObservableSet(Realm realm,
61
			CheckboxTreeViewer viewer, Object elementType) {
62
		super(realm, viewer, elementType, createElementSet(viewer));
63
		this.viewer = viewer;
64
	}
65
66
	Set createDiffSet() {
67
		return ViewerElementSet.withComparer(viewer.getComparer());
68
	}
69
70
	private static Set createElementSet(CheckboxTableViewer viewer) {
71
		Set set = ViewerElementSet.withComparer(viewer.getComparer());
72
		set.addAll(Arrays.asList(viewer.getCheckedElements()));
73
		return set;
74
	}
75
76
	private static Set createElementSet(CheckboxTreeViewer viewer) {
77
		Set set = ViewerElementSet.withComparer(viewer.getComparer());
78
		set.addAll(Arrays.asList(viewer.getCheckedElements()));
79
		return set;
80
	}
81
82
	public Viewer getViewer() {
83
		return viewer;
84
	}
85
86
	public synchronized void dispose() {
87
		viewer = null;
88
		super.dispose();
89
	}
90
}
(-)src/org/eclipse/jface/internal/databinding/viewers/SelectionProviderSingleSelectionObservableValue.java (-147 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 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
 *     Brad Reynolds - bug 137877
11
 *     Brad Reynolds - bug 164653
12
 *     Brad Reynolds - bug 147515
13
 *     Ashley Cambrell - bug 198906
14
 *******************************************************************************/
15
16
package org.eclipse.jface.internal.databinding.viewers;
17
18
import org.eclipse.core.databinding.observable.Diffs;
19
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
21
import org.eclipse.jface.util.Util;
22
import org.eclipse.jface.viewers.ISelection;
23
import org.eclipse.jface.viewers.ISelectionChangedListener;
24
import org.eclipse.jface.viewers.ISelectionProvider;
25
import org.eclipse.jface.viewers.IStructuredSelection;
26
import org.eclipse.jface.viewers.SelectionChangedEvent;
27
import org.eclipse.jface.viewers.StructuredSelection;
28
29
/**
30
 * Observes single selection of an <code>ISelectionProvider</code>.
31
 *
32
 * @since 1.1
33
 */
34
public class SelectionProviderSingleSelectionObservableValue extends
35
		AbstractObservableValue {
36
37
	private final ISelectionProvider selectionProvider;
38
39
	private boolean updating = false;
40
41
	private Object currentSelection;
42
43
	private ISelectionChangedListener selectionChangedListener;
44
45
	/**
46
	 * Constructs a new instance associated with the provided
47
	 * <code>selectionProvider</code>. In order to initialize itself properly
48
	 * the constructor invokes {@link #doGetValue()}. This could be dangerous
49
	 * for subclasses, see {@link #doGetValue()} for an explanation.
50
	 *
51
	 * @param realm
52
	 *
53
	 * @param selectionProvider
54
	 * @see #doGetValue()
55
	 */
56
	public SelectionProviderSingleSelectionObservableValue(Realm realm,
57
			ISelectionProvider selectionProvider) {
58
		super(realm);
59
		if (selectionProvider == null) {
60
			throw new IllegalArgumentException(
61
					"The 'selectionProvider' parameter is null."); //$NON-NLS-1$
62
		}
63
64
		this.selectionProvider = selectionProvider;
65
		this.currentSelection = doGetValue();
66
67
		selectionChangedListener = new ISelectionChangedListener() {
68
			public void selectionChanged(SelectionChangedEvent event) {
69
				if (!updating) {
70
					Object oldSelection = currentSelection;
71
					currentSelection = doGetValue();
72
					fireValueChange(Diffs.createValueDiff(oldSelection,
73
							currentSelection));
74
				}
75
			}
76
		};
77
		selectionProvider.addSelectionChangedListener(selectionChangedListener);
78
	}
79
80
	/**
81
	 * Sets the selection to the provided <code>value</code>. Value change
82
	 * events are fired after selection is set in the selection provider.
83
	 *
84
	 * @param value
85
	 *            object to set as selected, <code>null</code> if wanting to
86
	 *            remove selection
87
	 */
88
	public void doSetValue(final Object value) {
89
		try {
90
			updating = true;
91
92
			Object oldSelection = currentSelection;
93
			selectionProvider
94
					.setSelection(value == null ? StructuredSelection.EMPTY
95
							: new StructuredSelection(value));
96
			currentSelection = doGetValue();
97
			if (!Util.equals(oldSelection, currentSelection)) {
98
				fireValueChange(Diffs.createValueDiff(oldSelection,
99
						currentSelection));
100
			}
101
		} finally {
102
			updating = false;
103
		}
104
	}
105
106
	/**
107
	 * Retrieves the current selection.
108
	 * <p>
109
	 * If a subclass overrides this method it must not depend upon the subclass
110
	 * to have been fully initialized before this method is invoked.
111
	 * <code>doGetValue()</code> is invoked by the
112
	 * {@link #SelectionProviderSingleSelectionObservableValue(Realm, ISelectionProvider) constructor}
113
	 * which means the subclass's constructor will not have fully executed
114
	 * before this method is invoked.
115
	 * </p>
116
	 *
117
	 * @return selection will be an instance of
118
	 *         <code>IStructuredSelection</code> if a selection exists,
119
	 *         <code>null</code> if no selection
120
	 * @see #SelectionProviderSingleSelectionObservableValue(Realm,
121
	 *      ISelectionProvider)
122
	 */
123
	protected Object doGetValue() {
124
		ISelection selection = selectionProvider.getSelection();
125
		if (selection instanceof IStructuredSelection) {
126
			IStructuredSelection sel = (IStructuredSelection) selection;
127
			return sel.getFirstElement();
128
		}
129
130
		return null;
131
	}
132
133
	public Object getValueType() {
134
		return null;
135
	}
136
137
	/*
138
	 * (non-Javadoc)
139
	 *
140
	 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
141
	 */
142
	public synchronized void dispose() {
143
		selectionProvider
144
				.removeSelectionChangedListener(selectionChangedListener);
145
		super.dispose();
146
	}
147
}
(-)src/org/eclipse/jface/internal/databinding/viewers/CheckableCheckedElementsObservableSet.java (-219 lines)
Removed Link Here
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
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.viewers;
13
14
import java.util.Collection;
15
import java.util.Collections;
16
import java.util.HashSet;
17
import java.util.Iterator;
18
import java.util.Set;
19
20
import org.eclipse.core.databinding.observable.Diffs;
21
import org.eclipse.core.databinding.observable.Realm;
22
import org.eclipse.core.databinding.observable.set.AbstractObservableSet;
23
import org.eclipse.core.runtime.Assert;
24
import org.eclipse.jface.viewers.CheckStateChangedEvent;
25
import org.eclipse.jface.viewers.ICheckStateListener;
26
import org.eclipse.jface.viewers.ICheckable;
27
28
/**
29
 * 
30
 * @since 1.2
31
 */
32
public class CheckableCheckedElementsObservableSet extends
33
		AbstractObservableSet {
34
	private ICheckable checkable;
35
	private Set wrappedSet;
36
	private Object elementType;
37
	private ICheckStateListener listener;
38
39
	/**
40
	 * Constructs a new instance on the given realm and checkable.
41
	 * 
42
	 * @param realm
43
	 *            the observable's realm
44
	 * @param checkable
45
	 *            the ICheckable to track
46
	 * @param elementType
47
	 *            type of elements in the set
48
	 */
49
	public CheckableCheckedElementsObservableSet(Realm realm,
50
			ICheckable checkable, Object elementType) {
51
		this(realm, checkable, elementType, new HashSet());
52
	}
53
54
	/**
55
	 * Constructs a new instance of the given realm, and checkable,
56
	 * 
57
	 * @param realm
58
	 *            the observable's realm
59
	 * @param checkable
60
	 *            the ICheckable to track
61
	 * @param elementType
62
	 *            type of elements in the set
63
	 * @param wrappedSet
64
	 *            the set being wrapped
65
	 */
66
	public CheckableCheckedElementsObservableSet(Realm realm,
67
			ICheckable checkable, Object elementType, final Set wrappedSet) {
68
		super(realm);
69
		Assert.isNotNull(checkable, "Checkable cannot be null"); //$NON-NLS-1$
70
		Assert.isNotNull(wrappedSet, "Wrapped set cannot be null"); //$NON-NLS-1$
71
		this.checkable = checkable;
72
		this.wrappedSet = wrappedSet;
73
		this.elementType = elementType;
74
75
		listener = new ICheckStateListener() {
76
			public void checkStateChanged(CheckStateChangedEvent event) {
77
				Object element = event.getElement();
78
				if (event.getChecked()) {
79
					if (wrappedSet.add(element))
80
						fireSetChange(Diffs.createSetDiff(Collections
81
								.singleton(element), Collections.EMPTY_SET));
82
				} else {
83
					if (wrappedSet.remove(element))
84
						fireSetChange(Diffs.createSetDiff(
85
								Collections.EMPTY_SET, Collections
86
										.singleton(element)));
87
				}
88
			}
89
		};
90
		checkable.addCheckStateListener(listener);
91
	}
92
93
	protected Set getWrappedSet() {
94
		return wrappedSet;
95
	}
96
97
	Set createDiffSet() {
98
		return new HashSet();
99
	}
100
101
	public Object getElementType() {
102
		return elementType;
103
	}
104
105
	public boolean add(Object o) {
106
		getterCalled();
107
		boolean added = wrappedSet.add(o);
108
		if (added) {
109
			checkable.setChecked(o, true);
110
			fireSetChange(Diffs.createSetDiff(Collections.singleton(o),
111
					Collections.EMPTY_SET));
112
		}
113
		return added;
114
	}
115
116
	public boolean remove(Object o) {
117
		getterCalled();
118
		boolean removed = wrappedSet.remove(o);
119
		if (removed) {
120
			checkable.setChecked(o, false);
121
			fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET,
122
					Collections.singleton(o)));
123
		}
124
		return removed;
125
	}
126
127
	public boolean addAll(Collection c) {
128
		getterCalled();
129
		Set additions = createDiffSet();
130
		for (Iterator iterator = c.iterator(); iterator.hasNext();) {
131
			Object element = iterator.next();
132
			if (wrappedSet.add(element)) {
133
				checkable.setChecked(element, true);
134
				additions.add(element);
135
			}
136
		}
137
		boolean changed = !additions.isEmpty();
138
		if (changed)
139
			fireSetChange(Diffs.createSetDiff(additions, Collections.EMPTY_SET));
140
		return changed;
141
	}
142
143
	public boolean removeAll(Collection c) {
144
		getterCalled();
145
		Set removals = createDiffSet();
146
		for (Iterator iterator = c.iterator(); iterator.hasNext();) {
147
			Object element = iterator.next();
148
			if (wrappedSet.remove(element)) {
149
				checkable.setChecked(element, false);
150
				removals.add(element);
151
			}
152
		}
153
		boolean changed = !removals.isEmpty();
154
		if (changed)
155
			fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removals));
156
		return changed;
157
	}
158
159
	public boolean retainAll(Collection c) {
160
		getterCalled();
161
162
		// To ensure that elements are compared correctly, e.g. ViewerElementSet
163
		Set toRetain = createDiffSet();
164
		toRetain.addAll(c);
165
166
		Set removals = createDiffSet();
167
		for (Iterator iterator = wrappedSet.iterator(); iterator.hasNext();) {
168
			Object element = iterator.next();
169
			if (!toRetain.contains(element)) {
170
				iterator.remove();
171
				checkable.setChecked(element, false);
172
				removals.add(element);
173
			}
174
		}
175
		boolean changed = !removals.isEmpty();
176
		if (changed)
177
			fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removals));
178
		return changed;
179
	}
180
181
	public void clear() {
182
		removeAll(wrappedSet);
183
	}
184
185
	public Iterator iterator() {
186
		getterCalled();
187
		final Iterator wrappedIterator = wrappedSet.iterator();
188
		return new Iterator() {
189
			private Object last = null;
190
191
			public boolean hasNext() {
192
				getterCalled();
193
				return wrappedIterator.hasNext();
194
			}
195
196
			public Object next() {
197
				getterCalled();
198
				return last = wrappedIterator.next();
199
			}
200
201
			public void remove() {
202
				getterCalled();
203
				wrappedIterator.remove();
204
				checkable.setChecked(last, false);
205
				fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET,
206
						Collections.singleton(last)));
207
			}
208
		};
209
	}
210
211
	public synchronized void dispose() {
212
		if (checkable != null) {
213
			checkable.removeCheckStateListener(listener);
214
			checkable = null;
215
			listener = null;
216
		}
217
		super.dispose();
218
	}
219
}
(-)src/org/eclipse/jface/internal/databinding/provisional/swt/AbstractSWTObservableValue.java (-69 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 The Pampered Chef, Inc. 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
 *     The Pampered Chef, Inc. - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.provisional.swt;
13
14
import org.eclipse.core.databinding.observable.Realm;
15
import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
16
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
17
import org.eclipse.jface.databinding.swt.SWTObservables;
18
import org.eclipse.swt.events.DisposeEvent;
19
import org.eclipse.swt.events.DisposeListener;
20
import org.eclipse.swt.widgets.Widget;
21
22
/**
23
 * NON-API - An abstract superclass for observable values that gurantees that the 
24
 * observable will be disposed when the control to which it is attached is
25
 * disposed.
26
 * 
27
 * @since 1.1
28
 */
29
public abstract class AbstractSWTObservableValue extends AbstractObservableValue implements ISWTObservableValue {
30
31
	private final Widget widget;
32
33
	/**
34
	 * Standard constructor for an SWT ObservableValue.  Makes sure that
35
	 * the observable gets disposed when the SWT widget is disposed.
36
	 * 
37
	 * @param widget
38
	 */
39
	protected AbstractSWTObservableValue(Widget widget) {
40
		this(SWTObservables.getRealm(widget.getDisplay()), widget);
41
	}
42
	
43
	/**
44
	 * Constructor that allows for the setting of the realm. Makes sure that the
45
	 * observable gets disposed when the SWT widget is disposed.
46
	 * 
47
	 * @param realm
48
	 * @param widget
49
	 * @since 1.2
50
	 */
51
	protected AbstractSWTObservableValue(Realm realm, Widget widget) {
52
		super(realm);
53
		this.widget = widget;
54
		widget.addDisposeListener(disposeListener);
55
	}
56
	
57
	private DisposeListener disposeListener = new DisposeListener() {
58
		public void widgetDisposed(DisposeEvent e) {
59
			AbstractSWTObservableValue.this.dispose();
60
		}
61
	};
62
63
	/**
64
	 * @return Returns the widget.
65
	 */
66
	public Widget getWidget() {
67
		return widget;
68
	}
69
}
(-)src/org/eclipse/jface/databinding/swt/SWTObservables.java (-114 / +181 lines)
Lines 9-17 Link Here
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Matt Carter - bug 170668
10
 *     Matt Carter - bug 170668
11
 *     Brad Reynolds - bug 170848
11
 *     Brad Reynolds - bug 170848
12
 *     Matthew Hall - bugs 180746, 207844, 245647, 248621, 232917
12
 *     Matthew Hall - bugs 180746, 207844, 245647, 248621, 232917, 194734
13
 *     Michael Krauter - bug 180223
13
 *     Michael Krauter - bug 180223
14
 *     Boris Bokowski - bug 245647
14
 *     Boris Bokowski - bug 245647
15
 *     Tom Schindl - bug 246462
15
 *******************************************************************************/
16
 *******************************************************************************/
16
package org.eclipse.jface.databinding.swt;
17
package org.eclipse.jface.databinding.swt;
17
18
Lines 24-57 Link Here
24
import org.eclipse.core.databinding.observable.value.IObservableValue;
25
import org.eclipse.core.databinding.observable.value.IObservableValue;
25
import org.eclipse.core.databinding.observable.value.IVetoableValue;
26
import org.eclipse.core.databinding.observable.value.IVetoableValue;
26
import org.eclipse.core.databinding.observable.value.ValueChangingEvent;
27
import org.eclipse.core.databinding.observable.value.ValueChangingEvent;
27
import org.eclipse.jface.internal.databinding.internal.swt.LinkObservableValue;
28
import org.eclipse.core.databinding.property.list.IListProperty;
28
import org.eclipse.jface.internal.databinding.swt.ButtonObservableValue;
29
import org.eclipse.core.databinding.property.value.IValueProperty;
29
import org.eclipse.jface.internal.databinding.swt.CComboObservableList;
30
import org.eclipse.jface.internal.databinding.swt.CComboObservableValue;
31
import org.eclipse.jface.internal.databinding.swt.CComboSingleSelectionObservableValue;
32
import org.eclipse.jface.internal.databinding.swt.CLabelObservableValue;
33
import org.eclipse.jface.internal.databinding.swt.ComboObservableList;
34
import org.eclipse.jface.internal.databinding.swt.ComboObservableValue;
35
import org.eclipse.jface.internal.databinding.swt.ComboSingleSelectionObservableValue;
36
import org.eclipse.jface.internal.databinding.swt.ControlObservableValue;
37
import org.eclipse.jface.internal.databinding.swt.ItemObservableValue;
38
import org.eclipse.jface.internal.databinding.swt.ItemTooltipObservableValue;
39
import org.eclipse.jface.internal.databinding.swt.LabelObservableValue;
40
import org.eclipse.jface.internal.databinding.swt.ListObservableList;
41
import org.eclipse.jface.internal.databinding.swt.ListObservableValue;
42
import org.eclipse.jface.internal.databinding.swt.ListSingleSelectionObservableValue;
43
import org.eclipse.jface.internal.databinding.swt.SWTDelayedObservableValueDecorator;
30
import org.eclipse.jface.internal.databinding.swt.SWTDelayedObservableValueDecorator;
44
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
31
import org.eclipse.jface.internal.databinding.swt.SWTObservableListDecorator;
45
import org.eclipse.jface.internal.databinding.swt.ScaleObservableValue;
32
import org.eclipse.jface.internal.databinding.swt.SWTObservableValueDecorator;
46
import org.eclipse.jface.internal.databinding.swt.ShellObservableValue;
33
import org.eclipse.jface.internal.databinding.swt.SWTVetoableValueDecorator;
47
import org.eclipse.jface.internal.databinding.swt.SpinnerObservableValue;
48
import org.eclipse.jface.internal.databinding.swt.TableSingleSelectionObservableValue;
49
import org.eclipse.jface.internal.databinding.swt.TextEditableObservableValue;
50
import org.eclipse.jface.internal.databinding.swt.TextObservableValue;
51
import org.eclipse.swt.SWT;
34
import org.eclipse.swt.SWT;
52
import org.eclipse.swt.custom.CCombo;
35
import org.eclipse.swt.custom.CCombo;
53
import org.eclipse.swt.custom.CLabel;
36
import org.eclipse.swt.custom.CLabel;
54
import org.eclipse.swt.custom.CTabItem;
37
import org.eclipse.swt.custom.CTabItem;
38
import org.eclipse.swt.custom.StyledText;
55
import org.eclipse.swt.widgets.Button;
39
import org.eclipse.swt.widgets.Button;
56
import org.eclipse.swt.widgets.Combo;
40
import org.eclipse.swt.widgets.Combo;
57
import org.eclipse.swt.widgets.Control;
41
import org.eclipse.swt.widgets.Control;
Lines 139-164 Link Here
139
				.observeDelayedValue(delay, observable), observable.getWidget());
123
				.observeDelayedValue(delay, observable), observable.getWidget());
140
	}
124
	}
141
125
126
	private static ISWTObservableValue observeWidgetProperty(Widget widget,
127
			IValueProperty property) {
128
		return new SWTObservableValueDecorator(property.observeValue(
129
				getRealm(widget.getDisplay()), widget), widget);
130
	}
131
142
	/**
132
	/**
133
	 * Returns an observable value tracking the enabled state of the given
134
	 * control
135
	 * 
143
	 * @param control
136
	 * @param control
137
	 *            the control to observe
144
	 * @return an observable value tracking the enabled state of the given
138
	 * @return an observable value tracking the enabled state of the given
145
	 *         control
139
	 *         control
146
	 */
140
	 */
147
	public static ISWTObservableValue observeEnabled(Control control) {
141
	public static ISWTObservableValue observeEnabled(Control control) {
148
		return new ControlObservableValue(control, SWTProperties.ENABLED);
142
		return observeWidgetProperty(control, ControlProperties.enabled());
149
	}
143
	}
150
144
151
	/**
145
	/**
146
	 * Returns an observable value tracking the visible state of the given
147
	 * control
148
	 * 
152
	 * @param control
149
	 * @param control
150
	 *            the control to observe
153
	 * @return an observable value tracking the visible state of the given
151
	 * @return an observable value tracking the visible state of the given
154
	 *         control
152
	 *         control
155
	 */
153
	 */
156
	public static ISWTObservableValue observeVisible(Control control) {
154
	public static ISWTObservableValue observeVisible(Control control) {
157
		return new ControlObservableValue(control, SWTProperties.VISIBLE);
155
		return observeWidgetProperty(control, ControlProperties.visible());
158
	}
156
	}
159
157
160
	/**
158
	/**
161
	 * Returns an observable tracking the tooltip text of the given item. The supported types are:
159
	 * Returns an observable tracking the tooltip text of the given item. The
160
	 * supported types are:
162
	 * <ul>
161
	 * <ul>
163
	 * <li>org.eclipse.swt.widgets.Control</li>
162
	 * <li>org.eclipse.swt.widgets.Control</li>
164
	 * <li>org.eclipse.swt.custom.CTabItem</li>
163
	 * <li>org.eclipse.swt.custom.CTabItem</li>
Lines 168-202 Link Here
168
	 * <li>org.eclipse.swt.widgets.TrayItem</li>
167
	 * <li>org.eclipse.swt.widgets.TrayItem</li>
169
	 * <li>org.eclipse.swt.widgets.TreeColumn</li>
168
	 * <li>org.eclipse.swt.widgets.TreeColumn</li>
170
	 * </ul>
169
	 * </ul>
170
	 * 
171
	 * @param widget
171
	 * @param widget
172
	 * @return an observable value tracking the tooltip text of the given
172
	 * @return an observable value tracking the tooltip text of the given item
173
	 *         item
174
	 * 
173
	 * 
175
	 * @since 1.3
174
	 * @since 1.3
176
	 */
175
	 */
177
	public static ISWTObservableValue observeTooltipText(Widget widget) {
176
	public static ISWTObservableValue observeTooltipText(Widget widget) {
178
		if (widget instanceof Control) {
177
		if (widget instanceof Control) {
179
			return new ControlObservableValue((Control)widget, SWTProperties.TOOLTIP_TEXT);
178
			return observeTooltipText((Control) widget);
180
		} else if (widget instanceof CTabItem
181
				|| widget instanceof TabItem
182
				|| widget instanceof TableColumn
183
				|| widget instanceof ToolItem
184
				|| widget instanceof TrayItem
185
				|| widget instanceof TreeColumn) {
186
			return new ItemTooltipObservableValue((Item) widget);
187
		}
179
		}
188
		
180
189
		throw new IllegalArgumentException(
181
		IValueProperty property;
190
				"Item [" + widget.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
182
		if (widget instanceof CTabItem) {
183
			property = CTabItemProperties.tooltipText();
184
		} else if (widget instanceof TabItem) {
185
			property = TabItemProperties.tooltipText();
186
		} else if (widget instanceof TableColumn) {
187
			property = TableColumnProperties.tooltipText();
188
		} else if (widget instanceof ToolItem) {
189
			property = ToolItemProperties.tooltipText();
190
		} else if (widget instanceof TrayItem) {
191
			property = TrayItemProperties.tooltipText();
192
		} else if (widget instanceof TreeColumn) {
193
			property = TreeColumnProperties.tooltipText();
194
		} else {
195
			throw new IllegalArgumentException(
196
					"Item [" + widget.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
197
		}
198
199
		return observeWidgetProperty(widget, property);
191
	}
200
	}
192
201
193
	/**
202
	/**
203
	 * Returns an observable value tracking the tooltip text of the given
204
	 * control
205
	 * 
194
	 * @param control
206
	 * @param control
207
	 *            the control to observe
195
	 * @return an observable value tracking the tooltip text of the given
208
	 * @return an observable value tracking the tooltip text of the given
196
	 *         control
209
	 *         control
197
	 */
210
	 */
198
	public static ISWTObservableValue observeTooltipText(Control control) {
211
	public static ISWTObservableValue observeTooltipText(Control control) {
199
		return observeTooltipText((Widget) control);
212
		return observeWidgetProperty(control, ControlProperties.toolTipText());
200
	}
213
	}
201
214
202
	/**
215
	/**
Lines 217-242 Link Here
217
	 *             if <code>control</code> type is unsupported
230
	 *             if <code>control</code> type is unsupported
218
	 */
231
	 */
219
	public static ISWTObservableValue observeSelection(Control control) {
232
	public static ISWTObservableValue observeSelection(Control control) {
233
		IValueProperty property;
220
		if (control instanceof Spinner) {
234
		if (control instanceof Spinner) {
221
			return new SpinnerObservableValue((Spinner) control,
235
			property = SpinnerProperties.selection();
222
					SWTProperties.SELECTION);
223
		} else if (control instanceof Button) {
236
		} else if (control instanceof Button) {
224
			return new ButtonObservableValue((Button) control);
237
			property = ButtonProperties.selection();
225
		} else if (control instanceof Combo) {
238
		} else if (control instanceof Combo) {
226
			return new ComboObservableValue((Combo) control,
239
			property = ComboProperties.selection();
227
					SWTProperties.SELECTION);
228
		} else if (control instanceof CCombo) {
240
		} else if (control instanceof CCombo) {
229
			return new CComboObservableValue((CCombo) control,
241
			property = CComboProperties.selection();
230
					SWTProperties.SELECTION);
231
		} else if (control instanceof List) {
242
		} else if (control instanceof List) {
232
			return new ListObservableValue((List) control);
243
			property = ListProperties.selection();
233
		} else if (control instanceof Scale) {
244
		} else if (control instanceof Scale) {
234
			return new ScaleObservableValue((Scale) control,
245
			property = ScaleProperties.selection();
235
					SWTProperties.SELECTION);
246
		} else {
247
			throw new IllegalArgumentException(
248
					"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
236
		}
249
		}
237
250
238
		throw new IllegalArgumentException(
251
		return observeWidgetProperty(control, property);
239
				"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
240
	}
252
	}
241
253
242
	/**
254
	/**
Lines 253-267 Link Here
253
	 *             if <code>control</code> type is unsupported
265
	 *             if <code>control</code> type is unsupported
254
	 */
266
	 */
255
	public static ISWTObservableValue observeMin(Control control) {
267
	public static ISWTObservableValue observeMin(Control control) {
268
		IValueProperty property;
256
		if (control instanceof Spinner) {
269
		if (control instanceof Spinner) {
257
			return new SpinnerObservableValue((Spinner) control,
270
			property = SpinnerProperties.minimum();
258
					SWTProperties.MIN);
259
		} else if (control instanceof Scale) {
271
		} else if (control instanceof Scale) {
260
			return new ScaleObservableValue((Scale) control, SWTProperties.MIN);
272
			property = ScaleProperties.minimum();
273
		} else {
274
			throw new IllegalArgumentException(
275
					"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
261
		}
276
		}
262
277
263
		throw new IllegalArgumentException(
278
		return observeWidgetProperty(control, property);
264
				"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
265
	}
279
	}
266
280
267
	/**
281
	/**
Lines 278-292 Link Here
278
	 *             if <code>control</code> type is unsupported
292
	 *             if <code>control</code> type is unsupported
279
	 */
293
	 */
280
	public static ISWTObservableValue observeMax(Control control) {
294
	public static ISWTObservableValue observeMax(Control control) {
295
		IValueProperty property;
281
		if (control instanceof Spinner) {
296
		if (control instanceof Spinner) {
282
			return new SpinnerObservableValue((Spinner) control,
297
			property = SpinnerProperties.maximum();
283
					SWTProperties.MAX);
284
		} else if (control instanceof Scale) {
298
		} else if (control instanceof Scale) {
285
			return new ScaleObservableValue((Scale) control, SWTProperties.MAX);
299
			property = ScaleProperties.maximum();
300
		} else {
301
			throw new IllegalArgumentException(
302
					"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
286
		}
303
		}
287
304
288
		throw new IllegalArgumentException(
305
		return observeWidgetProperty(control, property);
289
				"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
290
	}
306
	}
291
307
292
	/**
308
	/**
Lines 294-314 Link Here
294
	 * <code>control</code>. The supported types are:
310
	 * <code>control</code>. The supported types are:
295
	 * <ul>
311
	 * <ul>
296
	 * <li>org.eclipse.swt.widgets.Text</li>
312
	 * <li>org.eclipse.swt.widgets.Text</li>
313
	 * <li>org.eclipse.swt.custom.StyledText (as of 1.3)</li>
297
	 * </ul>
314
	 * </ul>
298
	 * 
315
	 * 
299
	 * @param control
316
	 * @param control
300
	 * @param event event type to register for change events
317
	 * @param event
318
	 *            event type to register for change events
301
	 * @return observable value
319
	 * @return observable value
302
	 * @throws IllegalArgumentException
320
	 * @throws IllegalArgumentException
303
	 *             if <code>control</code> type is unsupported
321
	 *             if <code>control</code> type is unsupported
304
	 */
322
	 */
305
	public static ISWTObservableValue observeText(Control control, int event) {
323
	public static ISWTObservableValue observeText(Control control, int event) {
324
		IValueProperty property;
306
		if (control instanceof Text) {
325
		if (control instanceof Text) {
307
			return new TextObservableValue((Text) control, event);
326
			property = TextProperties.text(event);
327
		} else if (control instanceof StyledText) {
328
			property = StyledTextProperties.text(event);
329
		} else {
330
			throw new IllegalArgumentException(
331
					"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
308
		}
332
		}
309
333
310
		throw new IllegalArgumentException(
334
		return new SWTVetoableValueDecorator(property.observeValue(
311
				"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
335
				getRealm(control.getDisplay()), control), control);
312
	}
336
	}
313
337
314
	/**
338
	/**
Lines 329-355 Link Here
329
	 * @return observable value
353
	 * @return observable value
330
	 * @throws IllegalArgumentException
354
	 * @throws IllegalArgumentException
331
	 *             if the type of <code>widget</code> is unsupported
355
	 *             if the type of <code>widget</code> is unsupported
332
	 *             
356
	 * 
333
	 * @since 1.3
357
	 * @since 1.3
334
	 */
358
	 */
335
	public static ISWTObservableValue observeText(Widget widget) {
359
	public static ISWTObservableValue observeText(Widget widget) {
336
		if (widget instanceof Label) {
360
		if (widget instanceof Control) {
337
			return new LabelObservableValue((Label) widget);
361
			return observeText((Control) widget);
338
		} else if (widget instanceof Link) {
339
			return new LinkObservableValue((Link) widget);
340
		} else if (widget instanceof CLabel) {
341
			return new CLabelObservableValue((CLabel) widget);
342
		} else if (widget instanceof Combo) {
343
			return new ComboObservableValue((Combo) widget, SWTProperties.TEXT);
344
		} else if (widget instanceof CCombo) {
345
			return new CComboObservableValue((CCombo) widget,
346
					SWTProperties.TEXT);
347
		} else if (widget instanceof Shell) {
348
			return new ShellObservableValue((Shell) widget);
349
		} else if (widget instanceof Text) {
350
			return new TextObservableValue((Text) widget, SWT.None);
351
		} else if (widget instanceof Item) {
362
		} else if (widget instanceof Item) {
352
			return new ItemObservableValue((Item)widget);
363
			return observeWidgetProperty(widget, ItemProperties.text());
353
		}
364
		}
354
365
355
		throw new IllegalArgumentException(
366
		throw new IllegalArgumentException(
Lines 367-372 Link Here
367
	 * <li>org.eclipse.swt.custom.CCombo</li>
378
	 * <li>org.eclipse.swt.custom.CCombo</li>
368
	 * <li>org.eclipse.swt.widgets.Shell</li>
379
	 * <li>org.eclipse.swt.widgets.Shell</li>
369
	 * <li>org.eclipse.swt.widgets.Text (as of 1.3)</li>
380
	 * <li>org.eclipse.swt.widgets.Text (as of 1.3)</li>
381
	 * <li>org.eclipse.swt.custom.StyledText (as of 1.3)</li>
370
	 * </ul>
382
	 * </ul>
371
	 * 
383
	 * 
372
	 * @param control
384
	 * @param control
Lines 375-381 Link Here
375
	 *             if <code>control</code> type is unsupported
387
	 *             if <code>control</code> type is unsupported
376
	 */
388
	 */
377
	public static ISWTObservableValue observeText(Control control) {
389
	public static ISWTObservableValue observeText(Control control) {
378
		return observeText((Widget) control);
390
		if (control instanceof Text || control instanceof StyledText) {
391
			return observeText(control, SWT.None);
392
		}
393
394
		IValueProperty property;
395
		if (control instanceof Label) {
396
			property = LabelProperties.text();
397
		} else if (control instanceof Link) {
398
			property = LinkProperties.text();
399
		} else if (control instanceof CLabel) {
400
			property = CLabelProperties.text();
401
		} else if (control instanceof Combo) {
402
			property = ComboProperties.text();
403
		} else if (control instanceof CCombo) {
404
			property = CComboProperties.text();
405
		} else if (control instanceof Shell) {
406
			property = ShellProperties.text();
407
		} else {
408
			throw new IllegalArgumentException(
409
					"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
410
		}
411
412
		return observeWidgetProperty(control, property);
379
	}
413
	}
380
414
381
	/**
415
	/**
Lines 393-408 Link Here
393
	 *             if <code>control</code> type is unsupported
427
	 *             if <code>control</code> type is unsupported
394
	 */
428
	 */
395
	public static IObservableList observeItems(Control control) {
429
	public static IObservableList observeItems(Control control) {
430
		IListProperty property;
396
		if (control instanceof Combo) {
431
		if (control instanceof Combo) {
397
			return new ComboObservableList((Combo) control);
432
			property = ComboProperties.items();
398
		} else if (control instanceof CCombo) {
433
		} else if (control instanceof CCombo) {
399
			return new CComboObservableList((CCombo) control);
434
			property = CComboProperties.items();
400
		} else if (control instanceof List) {
435
		} else if (control instanceof List) {
401
			return new ListObservableList((List) control);
436
			property = ListProperties.items();
437
		} else {
438
			throw new IllegalArgumentException(
439
					"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
402
		}
440
		}
403
441
404
		throw new IllegalArgumentException(
442
		return new SWTObservableListDecorator(property.observeList(
405
				"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
443
				getRealm(control.getDisplay()), control), control);
406
	}
444
	}
407
445
408
	/**
446
	/**
Lines 422-506 Link Here
422
	 */
460
	 */
423
	public static ISWTObservableValue observeSingleSelectionIndex(
461
	public static ISWTObservableValue observeSingleSelectionIndex(
424
			Control control) {
462
			Control control) {
463
		IValueProperty property;
425
		if (control instanceof Table) {
464
		if (control instanceof Table) {
426
			return new TableSingleSelectionObservableValue((Table) control);
465
			property = TableProperties.singleSelectionIndex();
427
		} else if (control instanceof Combo) {
466
		} else if (control instanceof Combo) {
428
			return new ComboSingleSelectionObservableValue((Combo) control);
467
			property = ComboProperties.singleSelectionIndex();
429
		} else if (control instanceof CCombo) {
468
		} else if (control instanceof CCombo) {
430
			return new CComboSingleSelectionObservableValue((CCombo) control);
469
			property = CComboProperties.singleSelectionIndex();
431
		} else if (control instanceof List) {
470
		} else if (control instanceof List) {
432
			return new ListSingleSelectionObservableValue((List) control);
471
			property = ListProperties.singleSelectionIndex();
472
		} else {
473
			throw new IllegalArgumentException(
474
					"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
433
		}
475
		}
434
476
435
		throw new IllegalArgumentException(
477
		return observeWidgetProperty(control, property);
436
				"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
437
	}
478
	}
438
479
439
	/**
480
	/**
481
	 * Returns an observable value tracking the foreground color of the given
482
	 * control
483
	 * 
440
	 * @param control
484
	 * @param control
485
	 *            the control to observe
441
	 * @return an observable value tracking the foreground color of the given
486
	 * @return an observable value tracking the foreground color of the given
442
	 *         control
487
	 *         control
443
	 */
488
	 */
444
	public static ISWTObservableValue observeForeground(Control control) {
489
	public static ISWTObservableValue observeForeground(Control control) {
445
		return new ControlObservableValue(control, SWTProperties.FOREGROUND);
490
		return observeWidgetProperty(control, ControlProperties.foreground());
446
	}
491
	}
447
492
448
	/**
493
	/**
494
	 * Returns an observable value tracking the background color of the given
495
	 * control
496
	 * 
449
	 * @param control
497
	 * @param control
498
	 *            the control to observe
450
	 * @return an observable value tracking the background color of the given
499
	 * @return an observable value tracking the background color of the given
451
	 *         control
500
	 *         control
452
	 */
501
	 */
453
	public static ISWTObservableValue observeBackground(Control control) {
502
	public static ISWTObservableValue observeBackground(Control control) {
454
		return new ControlObservableValue(control, SWTProperties.BACKGROUND);
503
		return observeWidgetProperty(control, ControlProperties.background());
455
	}
504
	}
456
505
457
	/**
506
	/**
507
	 * Returns an observable value tracking the font of the given control.
508
	 * 
458
	 * @param control
509
	 * @param control
510
	 *            the control to observe
459
	 * @return an observable value tracking the font of the given control
511
	 * @return an observable value tracking the font of the given control
460
	 */
512
	 */
461
	public static ISWTObservableValue observeFont(Control control) {
513
	public static ISWTObservableValue observeFont(Control control) {
462
		return new ControlObservableValue(control, SWTProperties.FONT);
514
		return observeWidgetProperty(control, ControlProperties.font());
463
	}
515
	}
464
	
516
465
	/**
517
	/**
518
	 * Returns an observable value tracking the size of the given control.
519
	 * 
466
	 * @param control
520
	 * @param control
521
	 *            the control to observe
467
	 * @return an observable value tracking the size of the given control
522
	 * @return an observable value tracking the size of the given control
468
	 * @since 1.3
523
	 * @since 1.3
469
	 */
524
	 */
470
	public static ISWTObservableValue observeSize(Control control) {
525
	public static ISWTObservableValue observeSize(Control control) {
471
		return new ControlObservableValue(control,SWTProperties.SIZE);
526
		return observeWidgetProperty(control, ControlProperties.size());
472
	}
527
	}
473
	
528
474
	/**
529
	/**
530
	 * Returns an observable value tracking the location of the given control.
531
	 * 
475
	 * @param control
532
	 * @param control
533
	 *            the control to observe
476
	 * @return an observable value tracking the location of the given control
534
	 * @return an observable value tracking the location of the given control
477
	 * @since 1.3
535
	 * @since 1.3
478
	 */
536
	 */
479
	public static ISWTObservableValue observeLocation(Control control) {
537
	public static ISWTObservableValue observeLocation(Control control) {
480
		return new ControlObservableValue(control,SWTProperties.LOCATION);
538
		return observeWidgetProperty(control, ControlProperties.location());
481
	}
539
	}
482
	
540
483
	/**
541
	/**
542
	 * Returns an observable value tracking the focus of the given control.
543
	 * 
484
	 * @param control
544
	 * @param control
545
	 *            the control to observe
485
	 * @return an observable value tracking the focus of the given control
546
	 * @return an observable value tracking the focus of the given control
486
	 * @since 1.3
547
	 * @since 1.3
487
	 */
548
	 */
488
	public static ISWTObservableValue observeFocus(Control control) {
549
	public static ISWTObservableValue observeFocus(Control control) {
489
		return new ControlObservableValue(control,SWTProperties.FOCUS);
550
		return observeWidgetProperty(control, ControlProperties.focused());
490
	}
551
	}
491
	
552
492
	/**
553
	/**
554
	 * Returns an observable value tracking the bounds of the given control.
555
	 * 
493
	 * @param control
556
	 * @param control
557
	 *            the control to observe
494
	 * @return an observable value tracking the bounds of the given control
558
	 * @return an observable value tracking the bounds of the given control
495
	 * @since 1.3
559
	 * @since 1.3
496
	 */
560
	 */
497
	public static ISWTObservableValue observeBounds(Control control) {
561
	public static ISWTObservableValue observeBounds(Control control) {
498
		return new ControlObservableValue(control,SWTProperties.BOUNDS);
562
		return observeWidgetProperty(control, ControlProperties.bounds());
499
	}
563
	}
500
	
564
501
	/**
565
	/**
502
	 * Returns an observable observing the editable attribute of
566
	 * Returns an observable observing the editable attribute of the provided
503
	 * the provided <code>control</code>. The supported types are:
567
	 * <code>control</code>. The supported types are:
504
	 * <ul>
568
	 * <ul>
505
	 * <li>org.eclipse.swt.widgets.Text</li>
569
	 * <li>org.eclipse.swt.widgets.Text</li>
506
	 * </ul>
570
	 * </ul>
Lines 511-522 Link Here
511
	 *             if <code>control</code> type is unsupported
575
	 *             if <code>control</code> type is unsupported
512
	 */
576
	 */
513
	public static ISWTObservableValue observeEditable(Control control) {
577
	public static ISWTObservableValue observeEditable(Control control) {
578
		IValueProperty property;
514
		if (control instanceof Text) {
579
		if (control instanceof Text) {
515
			return new TextEditableObservableValue((Text) control);
580
			property = TextProperties.editable();
581
		} else {
582
			throw new IllegalArgumentException(
583
					"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
516
		}
584
		}
517
		
585
518
		throw new IllegalArgumentException(
586
		return observeWidgetProperty(control, property);
519
				"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
520
	}
587
	}
521
588
522
	private static class DisplayRealm extends Realm {
589
	private static class DisplayRealm extends Realm {
(-)src/org/eclipse/jface/internal/databinding/swt/ScaleObservableValue.java (-150 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 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
 *     Peter Centgraf - bug 175763
11
 *******************************************************************************/
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.core.databinding.observable.Diffs;
15
import org.eclipse.core.databinding.observable.Realm;
16
import org.eclipse.core.runtime.Assert;
17
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
18
import org.eclipse.swt.events.SelectionAdapter;
19
import org.eclipse.swt.events.SelectionEvent;
20
import org.eclipse.swt.events.SelectionListener;
21
import org.eclipse.swt.widgets.Scale;
22
23
/**
24
 * @since 1.0
25
 * 
26
 */
27
public class ScaleObservableValue extends AbstractSWTObservableValue {
28
29
	private final Scale scale;
30
31
	private final String attribute;
32
33
	private boolean updating = false;
34
35
	private int currentSelection;
36
	
37
	private SelectionListener listener;
38
39
	/**
40
	 * @param scale
41
	 * @param attribute
42
	 */
43
	public ScaleObservableValue(Scale scale, String attribute) {
44
		super(scale);
45
		this.scale = scale;
46
		this.attribute = attribute;
47
		init();
48
	}
49
	
50
	/**
51
	 * @param realm
52
	 * @param scale
53
	 * @param attribute
54
	 */
55
	public ScaleObservableValue(Realm realm, Scale scale, String attribute) {
56
		super(realm, scale);
57
		this.scale = scale;
58
		this.attribute = attribute;
59
		init();
60
	}
61
	
62
	private void init() {		
63
		if (attribute.equals(SWTProperties.SELECTION)) {
64
			currentSelection = scale.getSelection();
65
			scale.addSelectionListener(listener = new SelectionAdapter() {
66
				public void widgetSelected(SelectionEvent e) {
67
					if (!updating) {
68
						int newSelection = ScaleObservableValue.this.scale
69
						.getSelection();
70
						notifyIfChanged(currentSelection, newSelection);
71
						currentSelection = newSelection;
72
					}
73
				}
74
			});
75
		} else if (!attribute.equals(SWTProperties.MIN)
76
				&& !attribute.equals(SWTProperties.MAX)) {
77
			throw new IllegalArgumentException(
78
					"Attribute name not valid: " + attribute); //$NON-NLS-1$
79
		}
80
	}
81
82
	public void doSetValue(final Object value) {
83
		int oldValue;
84
		int newValue;
85
		try {
86
			updating = true;
87
			newValue = ((Integer) value).intValue();
88
			if (attribute.equals(SWTProperties.SELECTION)) {
89
				oldValue = scale.getSelection();
90
				scale.setSelection(newValue);
91
				currentSelection = newValue;
92
			} else if (attribute.equals(SWTProperties.MIN)) {
93
				oldValue = scale.getMinimum();
94
				scale.setMinimum(newValue);
95
			} else if (attribute.equals(SWTProperties.MAX)) {
96
				oldValue = scale.getMaximum();
97
				scale.setMaximum(newValue);
98
			} else {
99
				Assert.isTrue(false, "invalid attribute name:" + attribute); //$NON-NLS-1$
100
				return;
101
			}
102
			
103
			notifyIfChanged(oldValue, newValue);
104
		} finally {
105
			updating = false;
106
		}
107
	}
108
109
	public Object doGetValue() {
110
		int value = 0;
111
		if (attribute.equals(SWTProperties.SELECTION)) {
112
			value = scale.getSelection();
113
		} else if (attribute.equals(SWTProperties.MIN)) {
114
			value = scale.getMinimum();
115
		} else if (attribute.equals(SWTProperties.MAX)) {
116
			value = scale.getMaximum();
117
		}
118
		return new Integer(value);
119
	}
120
121
	public Object getValueType() {
122
		return Integer.TYPE;
123
	}
124
125
	/**
126
	 * @return attribute being observed
127
	 */
128
	public String getAttribute() {
129
		return attribute;
130
	}
131
	
132
	/* (non-Javadoc)
133
	 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
134
	 */
135
	public synchronized void dispose() {
136
		super.dispose();
137
		
138
		if (listener != null && !scale.isDisposed()) {
139
			scale.removeSelectionListener(listener);
140
		}
141
		listener = null;
142
	}
143
	
144
	private void notifyIfChanged(int oldValue, int newValue) {
145
		if (oldValue != newValue) {
146
			fireValueChange(Diffs.createValueDiff(new Integer(oldValue),
147
					new Integer(newValue)));
148
		}
149
	}
150
}
(-)src/org/eclipse/jface/internal/databinding/swt/CComboObservableValue.java (-166 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 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
 *     Brad Reynolds - bug 164653
11
 *     Ashley Cambrell - bug 198904
12
 *     Matthew Hall - bug 118516
13
 *     Eric Rizzo - bug 134884
14
 *******************************************************************************/
15
package org.eclipse.jface.internal.databinding.swt;
16
17
import org.eclipse.core.databinding.observable.Diffs;
18
import org.eclipse.core.databinding.observable.Realm;
19
import org.eclipse.core.runtime.Assert;
20
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
21
import org.eclipse.swt.custom.CCombo;
22
import org.eclipse.swt.events.ModifyEvent;
23
import org.eclipse.swt.events.ModifyListener;
24
25
/**
26
 * @since 3.2
27
 * 
28
 */
29
public class CComboObservableValue extends AbstractSWTObservableValue {
30
31
	/**
32
	 * 
33
	 */
34
35
	private final CCombo ccombo;
36
37
	private final String attribute;
38
39
	private boolean updating = false;
40
41
	private String currentValue;
42
43
	private ModifyListener modifyListener;
44
45
	/**
46
	 * @param ccombo
47
	 * @param attribute
48
	 */
49
	public CComboObservableValue(CCombo ccombo, String attribute) {
50
		super(ccombo);
51
		this.ccombo = ccombo;
52
		this.attribute = attribute;
53
		init();
54
	}
55
56
	/**
57
	 * @param realm
58
	 * @param ccombo
59
	 * @param attribute
60
	 */
61
	public CComboObservableValue(Realm realm, CCombo ccombo, String attribute) {
62
		super(realm, ccombo);
63
		this.ccombo = ccombo;
64
		this.attribute = attribute;
65
		init();
66
	}
67
	
68
	private void init() {		
69
		if (attribute.equals(SWTProperties.SELECTION)
70
				|| attribute.equals(SWTProperties.TEXT)) {
71
			this.currentValue = ccombo.getText();
72
			modifyListener = new ModifyListener() {
73
74
				public void modifyText(ModifyEvent e) {
75
					if (!updating) {
76
						String oldValue = currentValue;
77
						currentValue = CComboObservableValue.this.ccombo
78
								.getText();
79
						
80
						notifyIfChanged(oldValue, currentValue);
81
					}
82
				}
83
			};
84
			ccombo.addModifyListener(modifyListener);
85
		} else
86
			throw new IllegalArgumentException();
87
	}
88
89
	public void doSetValue(final Object value) {
90
		String oldValue = ccombo.getText();
91
		try {
92
			updating = true;
93
			if (attribute.equals(SWTProperties.TEXT)) {
94
				String stringValue = value != null ? value.toString() : ""; //$NON-NLS-1$
95
				ccombo.setText(stringValue);
96
			} else if (attribute.equals(SWTProperties.SELECTION)) {
97
				String items[] = ccombo.getItems();
98
				int index = -1;
99
				if (value == null) {
100
					ccombo.select(-1);
101
				} else if (items != null) {
102
					for (int i = 0; i < items.length; i++) {
103
						if (value.equals(items[i])) {
104
							index = i;
105
							break;
106
						}
107
					}
108
					if (index == -1) {
109
						ccombo.setText((String) value);
110
					} else {
111
						ccombo.select(index); // -1 will not "unselect"
112
					}
113
				}
114
			}
115
		} finally {
116
			updating = false;
117
			currentValue = ccombo.getText();
118
		}
119
		
120
		notifyIfChanged(oldValue, currentValue);
121
	}
122
123
	public Object doGetValue() {
124
		if (attribute.equals(SWTProperties.TEXT))
125
			return ccombo.getText();
126
127
		Assert.isTrue(attribute.equals(SWTProperties.SELECTION),
128
				"unexpected attribute: " + attribute); //$NON-NLS-1$
129
		// The problem with a ccombo, is that it changes the text and
130
		// fires before it update its selection index
131
		return ccombo.getText();
132
	}
133
134
	public Object getValueType() {
135
		Assert.isTrue(attribute.equals(SWTProperties.TEXT)
136
				|| attribute.equals(SWTProperties.SELECTION),
137
				"unexpected attribute: " + attribute); //$NON-NLS-1$
138
		return String.class;
139
	}
140
141
	/**
142
	 * @return attribute being observed
143
	 */
144
	public String getAttribute() {
145
		return attribute;
146
	}
147
148
	/*
149
	 * (non-Javadoc)
150
	 *
151
	 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
152
	 */
153
	public synchronized void dispose() {
154
		super.dispose();
155
156
		if (modifyListener != null && !ccombo.isDisposed()) {
157
			ccombo.removeModifyListener(modifyListener);
158
		}
159
	}
160
	
161
	private void notifyIfChanged(String oldValue, String newValue) {
162
		if (!oldValue.equals(newValue)) {
163
			fireValueChange(Diffs.createValueDiff(oldValue, ccombo.getText()));			
164
		}
165
	}
166
}
(-)src/org/eclipse/jface/internal/databinding/swt/CLabelObservableValue.java (-62 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 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
 *     Brad Reynolds - bug 164653
11
 *******************************************************************************/
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.core.databinding.observable.Diffs;
15
import org.eclipse.core.databinding.observable.Realm;
16
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
17
import org.eclipse.swt.custom.CLabel;
18
19
/**
20
 * @since 1.0
21
 * 
22
 */
23
public class CLabelObservableValue extends AbstractSWTObservableValue {
24
25
	private final CLabel label;
26
27
	/**
28
	 * @param label
29
	 */
30
	public CLabelObservableValue(CLabel label) {
31
		super(label);
32
		this.label = label;
33
	}
34
	
35
	/**
36
	 * @param realm
37
	 * @param label
38
	 */
39
	public CLabelObservableValue(Realm realm, CLabel label) {
40
		super(realm, label);
41
		this.label = label;
42
	}
43
44
	public void doSetValue(final Object value) {
45
		String oldValue = label.getText();
46
		String newValue = value == null ? "" : value.toString();  //$NON-NLS-1$
47
		label.setText(newValue);
48
49
		if (!newValue.equals(oldValue)) {
50
			fireValueChange(Diffs.createValueDiff(oldValue, newValue));
51
		}
52
	}
53
54
	public Object doGetValue() {
55
		return label.getText();
56
	}
57
58
	public Object getValueType() {
59
		return String.class;
60
	}
61
62
}
(-)src/org/eclipse/jface/internal/databinding/swt/ComboObservableValue.java (-155 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 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
 *     Brad Reynolds - bug 164653
11
 *     Ashley Cambrell - bug 198904
12
 *     Matthew Hall - bug 118516
13
 *******************************************************************************/
14
package org.eclipse.jface.internal.databinding.swt;
15
16
import org.eclipse.core.databinding.observable.Diffs;
17
import org.eclipse.core.databinding.observable.Realm;
18
import org.eclipse.core.runtime.Assert;
19
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
20
import org.eclipse.swt.events.ModifyEvent;
21
import org.eclipse.swt.events.ModifyListener;
22
import org.eclipse.swt.widgets.Combo;
23
24
/**
25
 * @since 3.2
26
 * 
27
 */
28
public class ComboObservableValue extends AbstractSWTObservableValue {
29
30
	private final Combo combo;
31
	private final String attribute;
32
	private boolean updating = false;
33
	private String currentValue;
34
	private ModifyListener modifyListener;
35
36
	/**
37
	 * @param combo
38
	 * @param attribute
39
	 */
40
	public ComboObservableValue(Combo combo, String attribute) {
41
		super(combo);
42
		this.combo = combo;
43
		this.attribute = attribute;
44
		init();
45
	}
46
		
47
	/**
48
	 * @param realm
49
	 * @param combo
50
	 * @param attribute
51
	 */
52
	public ComboObservableValue(Realm realm, Combo combo, String attribute) {
53
		super(realm, combo);
54
		this.combo = combo;
55
		this.attribute = attribute;
56
		init();
57
	}
58
	
59
	private void init() {		
60
		if (attribute.equals(SWTProperties.SELECTION)
61
				|| attribute.equals(SWTProperties.TEXT)) {
62
			this.currentValue = combo.getText();
63
			modifyListener = new ModifyListener() {
64
65
				public void modifyText(ModifyEvent e) {
66
					if (!updating) {
67
						String oldValue = currentValue;
68
						currentValue = ComboObservableValue.this.combo
69
								.getText();
70
						
71
						notifyIfChanged(oldValue, currentValue);
72
					}
73
				}
74
			};
75
			combo.addModifyListener(modifyListener);
76
		} else
77
			throw new IllegalArgumentException();
78
	}
79
80
	public void doSetValue(final Object value) {
81
		String oldValue = combo.getText();
82
		try {
83
			updating = true;
84
			if (attribute.equals(SWTProperties.TEXT)) {
85
				String stringValue = value != null ? value.toString() : ""; //$NON-NLS-1$
86
				combo.setText(stringValue);
87
			} else if (attribute.equals(SWTProperties.SELECTION)) {
88
				String items[] = combo.getItems();
89
				int index = -1;
90
				if (items != null && value != null) {
91
					for (int i = 0; i < items.length; i++) {
92
						if (value.equals(items[i])) {
93
							index = i;
94
							break;
95
						}
96
					}
97
					if (index == -1) {
98
						combo.setText((String) value);
99
					} else {
100
						combo.select(index); // -1 will not "unselect"
101
					}
102
				}
103
			}
104
		} finally {
105
			updating = false;
106
			currentValue = combo.getText();
107
		}
108
		
109
		notifyIfChanged(oldValue, currentValue);
110
	}
111
112
	public Object doGetValue() {
113
		if (attribute.equals(SWTProperties.TEXT))
114
			return combo.getText();
115
116
		Assert.isTrue(attribute.equals(SWTProperties.SELECTION),
117
				"unexpected attribute: " + attribute); //$NON-NLS-1$
118
		// The problem with a ccombo, is that it changes the text and
119
		// fires before it update its selection index
120
		return combo.getText();
121
	}
122
123
	public Object getValueType() {
124
		Assert.isTrue(attribute.equals(SWTProperties.TEXT)
125
				|| attribute.equals(SWTProperties.SELECTION),
126
				"unexpected attribute: " + attribute); //$NON-NLS-1$
127
		return String.class;
128
	}
129
130
	/**
131
	 * @return attribute being observed
132
	 */
133
	public String getAttribute() {
134
		return attribute;
135
	}
136
137
	/*
138
	 * (non-Javadoc)
139
	 *
140
	 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
141
	 */
142
	public synchronized void dispose() {
143
		super.dispose();
144
145
		if (modifyListener != null && !combo.isDisposed()) {
146
			combo.removeModifyListener(modifyListener);
147
		}
148
	}
149
	
150
	private void notifyIfChanged(String oldValue, String newValue) {
151
		if (!oldValue.equals(newValue)) {
152
			fireValueChange(Diffs.createValueDiff(oldValue, newValue));
153
		}
154
	}
155
}
(-)src/org/eclipse/jface/internal/databinding/swt/LabelObservableValue.java (-62 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 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
 *     Brad Reynolds - bug 164653
11
 *******************************************************************************/
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.core.databinding.observable.Diffs;
15
import org.eclipse.core.databinding.observable.Realm;
16
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
17
import org.eclipse.swt.widgets.Label;
18
19
/**
20
 * @since 3.3
21
 * 
22
 */
23
public class LabelObservableValue extends AbstractSWTObservableValue {
24
25
	private final Label label;
26
27
	/**
28
	 * @param label
29
	 */
30
	public LabelObservableValue(Label label) {
31
		super(label);
32
		this.label = label;
33
	}
34
	
35
	/**
36
	 * @param realm
37
	 * @param label
38
	 */
39
	public LabelObservableValue(Realm realm, Label label) {
40
		super(realm, label);
41
		this.label = label;
42
	}
43
44
	public void doSetValue(final Object value) {
45
		String oldValue = label.getText();
46
		String newValue = value == null ? "" : value.toString(); //$NON-NLS-1$
47
		label.setText(newValue);
48
		
49
		if (!newValue.equals(oldValue)) {
50
			fireValueChange(Diffs.createValueDiff(oldValue, newValue));
51
		}
52
	}
53
54
	public Object doGetValue() {
55
		return label.getText();
56
	}
57
58
	public Object getValueType() {
59
		return String.class;
60
	}
61
62
}
(-)src/org/eclipse/jface/internal/databinding/swt/ComboObservableList.java (-51 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 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
package org.eclipse.jface.internal.databinding.swt;
12
13
import org.eclipse.jface.databinding.swt.SWTObservables;
14
import org.eclipse.swt.widgets.Combo;
15
16
/**
17
 * @since 3.2
18
 * 
19
 */
20
public class ComboObservableList extends SWTObservableList {
21
22
	private final Combo combo;
23
24
	/**
25
	 * @param combo
26
	 */
27
	public ComboObservableList(Combo combo) {
28
		super(SWTObservables.getRealm(combo.getDisplay()));
29
		this.combo = combo;
30
	}
31
32
	protected int getItemCount() {
33
		return combo.getItemCount();
34
	}
35
36
	protected void setItems(String[] newItems) {
37
		combo.setItems(newItems);
38
	}
39
40
	protected String[] getItems() {
41
		return combo.getItems();
42
	}
43
44
	protected String getItem(int index) {
45
		return combo.getItem(index);
46
	}
47
48
	protected void setItem(int index, String string) {
49
		combo.setItem(index, string);
50
	}
51
}
(-)src/org/eclipse/jface/internal/databinding/swt/ComboSingleSelectionObservableValue.java (-71 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 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
 *     Brad Reynolds - bug 164653
11
 *     Ashley Cambrell - bugs 198903, 198904
12
 *******************************************************************************/
13
package org.eclipse.jface.internal.databinding.swt;
14
15
import org.eclipse.swt.events.SelectionEvent;
16
import org.eclipse.swt.events.SelectionListener;
17
import org.eclipse.swt.widgets.Combo;
18
19
/**
20
 * @since 1.0
21
 *
22
 */
23
public class ComboSingleSelectionObservableValue extends
24
		SingleSelectionObservableValue {
25
26
	private SelectionListener selectionListener;
27
28
	/**
29
	 * @param combo
30
	 */
31
	public ComboSingleSelectionObservableValue(Combo combo) {
32
		super(combo);
33
	}
34
35
	private Combo getCombo() {
36
		return (Combo) getWidget();
37
	}
38
39
	protected void doAddSelectionListener(final Runnable runnable) {
40
		selectionListener = new SelectionListener() {
41
			public void widgetDefaultSelected(SelectionEvent e) {
42
				runnable.run();
43
			}
44
45
			public void widgetSelected(SelectionEvent e) {
46
				runnable.run();
47
			}
48
		};
49
		getCombo().addSelectionListener(selectionListener);
50
	}
51
52
	protected int doGetSelectionIndex() {
53
		return getCombo().getSelectionIndex();
54
	}
55
56
	protected void doSetSelectionIndex(int index) {
57
		getCombo().select(index);
58
	}
59
60
	/*
61
	 * (non-Javadoc)
62
	 *
63
	 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
64
	 */
65
	public synchronized void dispose() {
66
		super.dispose();
67
		if (selectionListener != null && !getCombo().isDisposed()) {
68
			getCombo().removeSelectionListener(selectionListener);
69
		}
70
	}
71
}
(-)src/org/eclipse/jface/internal/databinding/swt/ItemObservableValue.java (-61 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 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
package org.eclipse.jface.internal.databinding.swt;
12
13
import org.eclipse.core.databinding.observable.Diffs;
14
import org.eclipse.core.databinding.observable.Realm;
15
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
16
import org.eclipse.swt.widgets.Item;
17
18
/**
19
 * @since 3.5
20
 * 
21
 */
22
public class ItemObservableValue extends AbstractSWTObservableValue {
23
24
	private final Item item;
25
26
	/**
27
	 * @param item
28
	 */
29
	public ItemObservableValue(Item item) {
30
		super(item);
31
		this.item = item;
32
	}
33
	
34
	/**
35
	 * @param realm
36
	 * @param item
37
	 */
38
	public ItemObservableValue(Realm realm, Item item) {
39
		super(realm, item);
40
		this.item = item;
41
	}
42
43
	public void doSetValue(final Object value) {
44
		String oldValue = item.getText();
45
		String newValue = value == null ? "" : value.toString(); //$NON-NLS-1$
46
		item.setText(newValue);
47
		
48
		if (!newValue.equals(oldValue)) {
49
			fireValueChange(Diffs.createValueDiff(oldValue, newValue));
50
		}
51
	}
52
53
	public Object doGetValue() {
54
		return item.getText();
55
	}
56
57
	public Object getValueType() {
58
		return String.class;
59
	}
60
61
}
(-)src/org/eclipse/jface/internal/databinding/swt/ListObservableList.java (-51 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 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
package org.eclipse.jface.internal.databinding.swt;
12
13
import org.eclipse.jface.databinding.swt.SWTObservables;
14
import org.eclipse.swt.widgets.List;
15
16
/**
17
 * @since 3.2
18
 * 
19
 */
20
public class ListObservableList extends SWTObservableList {
21
22
	private final List list;
23
24
	/**
25
	 * @param list
26
	 */
27
	public ListObservableList(List list) {
28
		super(SWTObservables.getRealm(list.getDisplay()));
29
		this.list = list;
30
	}
31
32
	protected int getItemCount() {
33
		return list.getItemCount();
34
	}
35
36
	protected void setItems(String[] newItems) {
37
		list.setItems(newItems);
38
	}
39
40
	protected String[] getItems() {
41
		return list.getItems();
42
	}
43
44
	protected String getItem(int index) {
45
		return list.getItem(index);
46
	}
47
48
	protected void setItem(int index, String string) {
49
		list.setItem(index, string);
50
	}
51
}
(-)src/org/eclipse/jface/databinding/swt/ButtonProperties.java (+31 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.value.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.ButtonSelectionProperty;
16
17
/**
18
 * A factory for creating properties of SWT Buttons.
19
 * 
20
 * @since 1.3
21
 */
22
public class ButtonProperties {
23
	/**
24
	 * Returns a value property for the selection state of a SWT Button.
25
	 * 
26
	 * @return a value property for the selection state of a SWT Button.
27
	 */
28
	public static IValueProperty selection() {
29
		return new ButtonSelectionProperty();
30
	}
31
}
(-)src/org/eclipse/jface/databinding/viewers/CheckboxTableViewerProperties.java (+36 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.viewers;
13
14
import org.eclipse.core.databinding.property.set.ISetProperty;
15
import org.eclipse.jface.internal.databinding.viewers.CheckboxTableViewerCheckedElementsProperty;
16
17
/**
18
 * A factory for creating properties of JFace CheckboxTableViewer
19
 * 
20
 * @since 1.3
21
 */
22
public class CheckboxTableViewerProperties {
23
	/**
24
	 * Returns a set property for the checked elements of a JFace
25
	 * CheckboxTableViewer.
26
	 * 
27
	 * @param elementType
28
	 *            the element type of the returned property
29
	 * 
30
	 * @return a set property for the checked elements of a JFace
31
	 *         CheckboxTableViewer.
32
	 */
33
	public static ISetProperty checkedElements(Object elementType) {
34
		return new CheckboxTableViewerCheckedElementsProperty(elementType);
35
	}
36
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlEnabledProperty.java (+32 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
16
/**
17
 * @since 3.3
18
 * 
19
 */
20
public class ControlEnabledProperty extends WidgetBooleanValueProperty {
21
	public boolean doGetBooleanValue(Object source) {
22
		return ((Control) source).getEnabled();
23
	}
24
25
	void doSetBooleanValue(Object source, boolean value) {
26
		((Control) source).setEnabled(value);
27
	}
28
29
	public String toString() {
30
		return "Control.enabled <boolean>"; //$NON-NLS-1$
31
	}
32
}
(-)src/org/eclipse/jface/databinding/swt/ScaleProperties.java (+51 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.value.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.ScaleMaximumProperty;
16
import org.eclipse.jface.internal.databinding.swt.ScaleMinimumProperty;
17
import org.eclipse.jface.internal.databinding.swt.ScaleSelectionProperty;
18
19
/**
20
 * A factory for creating properties of SWT Scales.
21
 * 
22
 * @since 1.3
23
 */
24
public class ScaleProperties {
25
	/**
26
	 * Returns a value property for the selected value of a SWT Scale.
27
	 * 
28
	 * @return a value property for the selected value of a SWT Scale.
29
	 */
30
	public static IValueProperty selection() {
31
		return new ScaleSelectionProperty();
32
	}
33
34
	/**
35
	 * Returns a value property for the minimum value of a SWT Scale.
36
	 * 
37
	 * @return a value property for the minimum value of a SWT Scale.
38
	 */
39
	public static IValueProperty minimum() {
40
		return new ScaleMinimumProperty();
41
	}
42
43
	/**
44
	 * Returns a value property for the maximum value of a SWT Scale.
45
	 * 
46
	 * @return a value property for the maximum value of a SWT Scale.
47
	 */
48
	public static IValueProperty maximum() {
49
		return new ScaleMaximumProperty();
50
	}
51
}
(-)src/org/eclipse/jface/internal/databinding/swt/ButtonSelectionProperty.java (+40 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.Button;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class ButtonSelectionProperty extends WidgetBooleanValueProperty {
22
	/**
23
	 * 
24
	 */
25
	public ButtonSelectionProperty() {
26
		super(SWT.Selection);
27
	}
28
29
	boolean doGetBooleanValue(Object source) {
30
		return ((Button) source).getSelection();
31
	}
32
33
	void doSetBooleanValue(Object source, boolean value) {
34
		((Button) source).setSelection(value);
35
	}
36
37
	public String toString() {
38
		return "Button.selection <Boolean>"; //$NON-NLS-1$
39
	}
40
}
(-)src/org/eclipse/jface/internal/databinding/swt/ScaleMaximumProperty.java (+32 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Scale;
15
16
/**
17
 * @since 3.3
18
 * 
19
 */
20
public class ScaleMaximumProperty extends WidgetIntValueProperty {
21
	int doGetIntValue(Object source) {
22
		return ((Scale) source).getMaximum();
23
	}
24
25
	void doSetIntValue(Object source, int value) {
26
		((Scale) source).setMaximum(value);
27
	}
28
29
	public String toString() {
30
		return "Scale.maximum <int>"; //$NON-NLS-1$
31
	}
32
}
(-)src/org/eclipse/jface/internal/databinding/swt/ListSelectionProperty.java (+55 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.List;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class ListSelectionProperty extends WidgetStringValueProperty {
22
	/**
23
	 * 
24
	 */
25
	public ListSelectionProperty() {
26
		super(SWT.Selection);
27
	}
28
29
	String doGetStringValue(Object source) {
30
		List list = (List) source;
31
		int index = list.getSelectionIndex();
32
		if (index >= 0)
33
			return list.getItem(index);
34
		return null;
35
	}
36
37
	void doSetStringValue(Object source, String value) {
38
		List list = (List) source;
39
		String items[] = list.getItems();
40
		int index = -1;
41
		if (items != null && value != null) {
42
			for (int i = 0; i < items.length; i++) {
43
				if (value.equals(items[i])) {
44
					index = i;
45
					break;
46
				}
47
			}
48
			list.select(index);
49
		}
50
	}
51
52
	public String toString() {
53
		return "List.selection <String>"; //$NON-NLS-1$
54
	}
55
}
(-)src/org/eclipse/jface/internal/databinding/swt/ItemTextProperty.java (+32 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Item;
15
16
/**
17
 * @since 3.3
18
 * 
19
 */
20
public class ItemTextProperty extends WidgetStringValueProperty {
21
	String doGetStringValue(Object source) {
22
		return ((Item) source).getText();
23
	}
24
25
	void doSetStringValue(Object source, String value) {
26
		((Item) source).setText(value == null ? "" : value); //$NON-NLS-1$
27
	}
28
29
	public String toString() {
30
		return "Item.text <String>"; //$NON-NLS-1$
31
	}
32
}
(-)src/org/eclipse/jface/internal/databinding/swt/LinkTextProperty.java (+32 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Link;
15
16
/**
17
 * @since 3.3
18
 * 
19
 */
20
public class LinkTextProperty extends WidgetStringValueProperty {
21
	String doGetStringValue(Object source) {
22
		return ((Link) source).getText();
23
	}
24
25
	void doSetStringValue(Object source, String value) {
26
		((Link) source).setText(value == null ? "" : value); //$NON-NLS-1$
27
	}
28
29
	public String toString() {
30
		return "Link.text <String>"; //$NON-NLS-1$
31
	}
32
}
(-)src/org/eclipse/jface/databinding/swt/ControlProperties.java (+121 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.value.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.ControlBackgroundProperty;
16
import org.eclipse.jface.internal.databinding.swt.ControlBoundsProperty;
17
import org.eclipse.jface.internal.databinding.swt.ControlEnabledProperty;
18
import org.eclipse.jface.internal.databinding.swt.ControlFocusedProperty;
19
import org.eclipse.jface.internal.databinding.swt.ControlFontProperty;
20
import org.eclipse.jface.internal.databinding.swt.ControlForegroundProperty;
21
import org.eclipse.jface.internal.databinding.swt.ControlLocationProperty;
22
import org.eclipse.jface.internal.databinding.swt.ControlSizeProperty;
23
import org.eclipse.jface.internal.databinding.swt.ControlTooltipTextProperty;
24
import org.eclipse.jface.internal.databinding.swt.ControlVisibleProperty;
25
26
/**
27
 * A factory for creating properties of SWT controls.
28
 * 
29
 * @since 1.3
30
 */
31
public class ControlProperties {
32
	/**
33
	 * Returns a value property for the enablement state of a SWT Control.
34
	 * 
35
	 * @return a value property for the enablement state of a SWT Control.
36
	 */
37
	public static IValueProperty enabled() {
38
		return new ControlEnabledProperty();
39
	}
40
41
	/**
42
	 * Returns a value property for the visibility state of a SWT Control.
43
	 * 
44
	 * @return a value property for the visibility state of a SWT Control.
45
	 */
46
	public static IValueProperty visible() {
47
		return new ControlVisibleProperty();
48
	}
49
50
	/**
51
	 * Returns a value property for the tooltip text of a SWT Control.
52
	 * 
53
	 * @return a value property for the tooltip text of a SWT Control.
54
	 */
55
	public static IValueProperty toolTipText() {
56
		return new ControlTooltipTextProperty();
57
	}
58
59
	/**
60
	 * Returns a value property for the foreground color of a SWT Control.
61
	 * 
62
	 * @return a value property for the foreground color of a SWT Control.
63
	 */
64
	public static IValueProperty foreground() {
65
		return new ControlForegroundProperty();
66
	}
67
68
	/**
69
	 * Returns a value property for the background color of a SWT Control.
70
	 * 
71
	 * @return a value property for the background color of a SWT Control.
72
	 */
73
	public static IValueProperty background() {
74
		return new ControlBackgroundProperty();
75
	}
76
77
	/**
78
	 * Returns a value property for the font of a SWT Control.
79
	 * 
80
	 * @return a value property for the font of a SWT Control.
81
	 */
82
	public static IValueProperty font() {
83
		return new ControlFontProperty();
84
	}
85
86
	/**
87
	 * Returns a value property for the size of a SWT Control.
88
	 * 
89
	 * @return a value property for the size of a SWT Control.
90
	 */
91
	public static IValueProperty size() {
92
		return new ControlSizeProperty();
93
	}
94
95
	/**
96
	 * Returns a value property for the location of a SWT Control.
97
	 * 
98
	 * @return a value property for the location of a SWT Control.
99
	 */
100
	public static IValueProperty location() {
101
		return new ControlLocationProperty();
102
	}
103
104
	/**
105
	 * Returns a value property for the bounds of a SWT Control.
106
	 * 
107
	 * @return a value property for the bounds of a SWT Control.
108
	 */
109
	public static IValueProperty bounds() {
110
		return new ControlBoundsProperty();
111
	}
112
113
	/**
114
	 * Returns a value property for the focus state of a SWT Control.
115
	 * 
116
	 * @return a value property for the focus state of a SWT Control.
117
	 */
118
	public static IValueProperty focused() {
119
		return new ControlFocusedProperty();
120
	}
121
}
(-)src/org/eclipse/jface/databinding/viewers/CheckboxTreeViewerProperties.java (+36 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.viewers;
13
14
import org.eclipse.core.databinding.property.set.ISetProperty;
15
import org.eclipse.jface.internal.databinding.viewers.CheckboxTreeViewerCheckedElementsProperty;
16
17
/**
18
 * A factory for creating properties of JFace CheckboxTreeViewer
19
 * 
20
 * @since 1.3
21
 */
22
public class CheckboxTreeViewerProperties {
23
	/**
24
	 * Returns a set property for the checked elements of a JFace
25
	 * CheckboxTreeViewer.
26
	 * 
27
	 * @param elementType
28
	 *            the element type of the returned property
29
	 * 
30
	 * @return a set property for the checked elements of a JFace
31
	 *         CheckboxTreeViewer.
32
	 */
33
	public static ISetProperty checkedElements(Object elementType) {
34
		return new CheckboxTreeViewerCheckedElementsProperty(elementType);
35
	}
36
}
(-)src/org/eclipse/jface/internal/databinding/swt/TableColumnTooltipTextProperty.java (+32 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.TableColumn;
15
16
/**
17
 * @since 3.3
18
 * 
19
 */
20
public class TableColumnTooltipTextProperty extends WidgetStringValueProperty {
21
	String doGetStringValue(Object source) {
22
		return ((TableColumn) source).getText();
23
	}
24
25
	void doSetStringValue(Object source, String value) {
26
		((TableColumn) source).setText(value == null ? "" : value); //$NON-NLS-1$
27
	}
28
29
	public String toString() {
30
		return "TableColumn.tooltipText <String>"; //$NON-NLS-1$
31
	}
32
}
(-)src/org/eclipse/jface/internal/databinding/swt/ComboSelectionProperty.java (+55 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.Combo;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class ComboSelectionProperty extends WidgetStringValueProperty {
22
	/**
23
	 * 
24
	 */
25
	public ComboSelectionProperty() {
26
		super(SWT.Modify);
27
	}
28
29
	String doGetStringValue(Object source) {
30
		return ((Combo) source).getText();
31
	}
32
33
	void doSetStringValue(Object source, String value) {
34
		Combo combo = (Combo) source;
35
		String items[] = combo.getItems();
36
		int index = -1;
37
		if (items != null && value != null) {
38
			for (int i = 0; i < items.length; i++) {
39
				if (value.equals(items[i])) {
40
					index = i;
41
					break;
42
				}
43
			}
44
			if (index == -1) {
45
				combo.setText(value);
46
			} else {
47
				combo.select(index); // -1 will not "unselect"
48
			}
49
		}
50
	}
51
52
	public String toString() {
53
		return "Combo.selection <String>"; //$NON-NLS-1$
54
	}
55
}
(-)src/org/eclipse/jface/internal/databinding/viewers/CheckboxTreeViewerCheckedElementsProperty.java (+57 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.viewers;
13
14
import java.util.Arrays;
15
import java.util.Set;
16
17
import org.eclipse.core.databinding.observable.set.SetDiff;
18
import org.eclipse.jface.viewers.CheckboxTreeViewer;
19
import org.eclipse.jface.viewers.ICheckable;
20
21
/**
22
 * @since 3.3
23
 * 
24
 */
25
public class CheckboxTreeViewerCheckedElementsProperty extends
26
		CheckableCheckedElementsProperty {
27
	/**
28
	 * @param elementType
29
	 */
30
	public CheckboxTreeViewerCheckedElementsProperty(Object elementType) {
31
		super(elementType);
32
	}
33
34
	protected Set createElementSet(ICheckable checkable) {
35
		return ViewerElementSet.withComparer(((CheckboxTreeViewer) checkable)
36
				.getComparer());
37
	}
38
39
	protected Set doGetSet(ICheckable checkable) {
40
		CheckboxTreeViewer viewer = (CheckboxTreeViewer) checkable;
41
		Set set = createElementSet(viewer);
42
		set.addAll(Arrays.asList(viewer.getCheckedElements()));
43
		return set;
44
	}
45
46
	protected void doSetSet(Object source, Set set, SetDiff diff) {
47
		CheckboxTreeViewer viewer = (CheckboxTreeViewer) source;
48
		viewer.setCheckedElements(set.toArray());
49
	}
50
51
	public String toString() {
52
		String s = "CheckboxTreeViewer.checkedElements{}"; //$NON-NLS-1$
53
		if (getElementType() != null)
54
			s += " <" + getElementType() + ">"; //$NON-NLS-1$//$NON-NLS-2$
55
		return s;
56
	}
57
}
(-)src/org/eclipse/jface/internal/databinding/swt/CLabelTextProperty.java (+32 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.custom.CLabel;
15
16
/**
17
 * @since 3.3
18
 * 
19
 */
20
public class CLabelTextProperty extends WidgetStringValueProperty {
21
	String doGetStringValue(Object source) {
22
		return ((CLabel) source).getText();
23
	}
24
25
	void doSetStringValue(Object source, String value) {
26
		((CLabel) source).setText(value == null ? "" : value); //$NON-NLS-1$
27
	}
28
29
	public String toString() {
30
		return "CLabel.text <String>"; //$NON-NLS-1$
31
	}
32
}
(-)src/org/eclipse/jface/internal/databinding/viewers/ViewerObservableSetDecorator.java (+40 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.viewers;
13
14
import org.eclipse.core.databinding.observable.set.DecoratingObservableSet;
15
import org.eclipse.core.databinding.observable.set.IObservableSet;
16
import org.eclipse.jface.databinding.viewers.IViewerObservableSet;
17
import org.eclipse.jface.viewers.Viewer;
18
19
/**
20
 * @since 3.3
21
 * 
22
 */
23
public class ViewerObservableSetDecorator extends DecoratingObservableSet
24
		implements IViewerObservableSet {
25
	private final Viewer viewer;
26
27
	/**
28
	 * @param decorated
29
	 * @param viewer
30
	 */
31
	public ViewerObservableSetDecorator(IObservableSet decorated, Viewer viewer) {
32
		super(decorated, true);
33
		this.viewer = viewer;
34
	}
35
36
	public Viewer getViewer() {
37
		return viewer;
38
	}
39
40
}
(-)src/org/eclipse/jface/internal/databinding/swt/CComboItemsProperty.java (+33 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.custom.CCombo;
15
import org.eclipse.swt.widgets.Control;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class CComboItemsProperty extends ControlStringListProperty {
22
	protected void doSetStringList(Control control, String[] list) {
23
		((CCombo) control).setItems(list);
24
	}
25
26
	public String[] doGetStringList(Control control) {
27
		return ((CCombo) control).getItems();
28
	}
29
30
	public String toString() {
31
		return "CCombo.items[] <String>"; //$NON-NLS-1$
32
	}
33
}
(-)src/org/eclipse/jface/internal/databinding/swt/ComboSingleSelectionIndexProperty.java (+40 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.Combo;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class ComboSingleSelectionIndexProperty extends WidgetIntValueProperty {
22
	/**
23
	 * 
24
	 */
25
	public ComboSingleSelectionIndexProperty() {
26
		super(new int[] { SWT.Selection, SWT.DefaultSelection });
27
	}
28
29
	int doGetIntValue(Object source) {
30
		return ((Combo) source).getSelectionIndex();
31
	}
32
33
	void doSetIntValue(Object source, int value) {
34
		((Combo) source).select(value);
35
	}
36
37
	public String toString() {
38
		return "Combo.selectionIndex <int>"; //$NON-NLS-1$
39
	}
40
}
(-)src/org/eclipse/jface/internal/databinding/swt/ToolItemTooltipTextProperty.java (+32 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.ToolItem;
15
16
/**
17
 * @since 3.3
18
 * 
19
 */
20
public class ToolItemTooltipTextProperty extends WidgetStringValueProperty {
21
	String doGetStringValue(Object source) {
22
		return ((ToolItem) source).getText();
23
	}
24
25
	void doSetStringValue(Object source, String value) {
26
		((ToolItem) source).setText(value == null ? "" : value); //$NON-NLS-1$
27
	}
28
29
	public String toString() {
30
		return "ToolItem.tooltipText <String>"; //$NON-NLS-1$
31
	}
32
}
(-)src/org/eclipse/jface/databinding/swt/TextProperties.java (+45 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.value.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.TextEditableProperty;
16
import org.eclipse.jface.internal.databinding.swt.TextTextProperty;
17
import org.eclipse.swt.SWT;
18
19
/**
20
 * A factory for creating properties of SWT Texts.
21
 * 
22
 * @since 1.3
23
 */
24
public class TextProperties {
25
	/**
26
	 * Returns a value property for the text of a SWT Text.
27
	 * 
28
	 * @param event
29
	 *            the SWT event type to register for change events. May be
30
	 *            {@link SWT#None}, {@link SWT#Modify} or {@link SWT#FocusOut}.
31
	 * 
32
	 * @return a value property for the text of a SWT Text.
33
	 */
34
	public static IValueProperty text(int event) {
35
		return new TextTextProperty(event);
36
	}
37
38
	/**
39
	 * Returns a value property for the editable state of a SWT Text.
40
	 * @return a value property for the editable state of a SWT Text.
41
	 */
42
	public static IValueProperty editable() {
43
		return new TextEditableProperty();
44
	}
45
}
(-)src/org/eclipse/jface/internal/databinding/swt/SWTVetoableValueDecorator.java (+141 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.core.databinding.observable.Diffs;
15
import org.eclipse.core.databinding.observable.IDecoratingObservable;
16
import org.eclipse.core.databinding.observable.IObservable;
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.AbstractVetoableValue;
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.ValueChangeEvent;
24
import org.eclipse.core.runtime.Assert;
25
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
26
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.widgets.Event;
28
import org.eclipse.swt.widgets.Listener;
29
import org.eclipse.swt.widgets.Widget;
30
31
/**
32
 * @since 3.3
33
 * 
34
 */
35
public class SWTVetoableValueDecorator extends AbstractVetoableValue implements
36
		ISWTObservableValue, IDecoratingObservable {
37
38
	private IObservableValue decorated;
39
	private Widget widget;
40
41
	private IValueChangeListener valueListener = new IValueChangeListener() {
42
		public void handleValueChange(ValueChangeEvent event) {
43
			fireValueChange(event.diff);
44
		}
45
	};
46
47
	private IStaleListener staleListener = new IStaleListener() {
48
		public void handleStale(StaleEvent staleEvent) {
49
			fireStale();
50
		}
51
	};
52
53
	private Listener verifyListener = new Listener() {
54
		public void handleEvent(Event event) {
55
			String currentText = (String) decorated.getValue();
56
			String newText = currentText.substring(0, event.start) + event.text
57
					+ currentText.substring(event.end);
58
			if (!fireValueChanging(Diffs.createValueDiff(currentText, newText))) {
59
				event.doit = false;
60
			}
61
		}
62
	};
63
64
	private Listener disposeListener = new Listener() {
65
		public void handleEvent(Event event) {
66
			SWTVetoableValueDecorator.this.dispose();
67
		}
68
	};
69
70
	/**
71
	 * @param decorated
72
	 * @param widget
73
	 */
74
	public SWTVetoableValueDecorator(IObservableValue decorated, Widget widget) {
75
		super(decorated.getRealm());
76
		this.decorated = decorated;
77
		this.widget = widget;
78
		Assert
79
				.isTrue(decorated.getValueType().equals(String.class),
80
						"SWTVetoableValueDecorator can only decorate observable values of String type"); //$NON-NLS-1$
81
		widget.addListener(SWT.Dispose, disposeListener);
82
	}
83
84
	private void getterCalled() {
85
		ObservableTracker.getterCalled(this);
86
	}
87
88
	protected void firstListenerAdded() {
89
		decorated.addValueChangeListener(valueListener);
90
		decorated.addStaleListener(staleListener);
91
		widget.addListener(SWT.Verify, verifyListener);
92
	}
93
94
	protected void lastListenerRemoved() {
95
		if (decorated != null) {
96
			decorated.removeValueChangeListener(valueListener);
97
			decorated.removeStaleListener(staleListener);
98
		}
99
		if (widget != null && !widget.isDisposed())
100
			widget.removeListener(SWT.Verify, verifyListener);
101
	}
102
103
	protected void doSetApprovedValue(Object value) {
104
		checkRealm();
105
		decorated.setValue(value);
106
	}
107
108
	protected Object doGetValue() {
109
		getterCalled();
110
		return decorated.getValue();
111
	}
112
113
	public Object getValueType() {
114
		return decorated.getValueType();
115
	}
116
117
	public boolean isStale() {
118
		getterCalled();
119
		return decorated.isStale();
120
	}
121
122
	public void dispose() {
123
		if (decorated != null) {
124
			decorated.dispose();
125
			decorated = null;
126
		}
127
		if (widget != null && !widget.isDisposed()) {
128
			widget.removeListener(SWT.Verify, verifyListener);
129
		}
130
		this.widget = null;
131
		super.dispose();
132
	}
133
134
	public Widget getWidget() {
135
		return widget;
136
	}
137
138
	public IObservable getDecorated() {
139
		return decorated;
140
	}
141
}
(-)src/org/eclipse/jface/databinding/swt/ShellProperties.java (+31 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.value.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.ShellTextProperty;
16
17
/**
18
 * A factory for creating properties of SWT Shells.
19
 * 
20
 * @since 1.3
21
 */
22
public class ShellProperties {
23
	/**
24
	 * Returns a value property for the text of a SWT Shell.
25
	 * 
26
	 * @return a value property for the text of a SWT Shell.
27
	 */
28
	public static IValueProperty text() {
29
		return new ShellTextProperty();
30
	}
31
}
(-)src/org/eclipse/jface/databinding/swt/LinkProperties.java (+31 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.value.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.LinkTextProperty;
16
17
/**
18
 * A factory for creating properties of SWT Links.
19
 * 
20
 * @since 1.3
21
 */
22
public class LinkProperties {
23
	/**
24
	 * Returns a value property for the text of a SWT Link.
25
	 * 
26
	 * @return a value property for the text of a SWT Link.
27
	 */
28
	public static IValueProperty text() {
29
		return new LinkTextProperty();
30
	}
31
}
(-)src/org/eclipse/jface/databinding/swt/TabItemProperties.java (+31 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.value.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.TabItemTooltipTextProperty;
16
17
/**
18
 * A factory for creating properties of SWT TabItems
19
 * 
20
 * @since 1.3
21
 */
22
public class TabItemProperties {
23
	/**
24
	 * Returns a value property for the tooltip text of a SWT TabItem.
25
	 * 
26
	 * @return a value property for the tooltip text of a SWT TabItem.
27
	 */
28
	public static IValueProperty tooltipText() {
29
		return new TabItemTooltipTextProperty();
30
	}
31
}
(-)src/org/eclipse/jface/internal/databinding/viewers/CheckableCheckedElementsProperty.java (+119 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.viewers;
13
14
import java.util.Collections;
15
import java.util.HashSet;
16
import java.util.Iterator;
17
import java.util.Set;
18
19
import org.eclipse.core.databinding.observable.Diffs;
20
import org.eclipse.core.databinding.observable.set.SetDiff;
21
import org.eclipse.core.databinding.property.INativePropertyListener;
22
import org.eclipse.core.databinding.property.set.ISetPropertyChangeListener;
23
import org.eclipse.core.databinding.property.set.SetPropertyChangeEvent;
24
import org.eclipse.core.databinding.property.set.SimpleSetProperty;
25
import org.eclipse.jface.viewers.CheckStateChangedEvent;
26
import org.eclipse.jface.viewers.ICheckStateListener;
27
import org.eclipse.jface.viewers.ICheckable;
28
29
/**
30
 * @since 3.3
31
 * 
32
 */
33
public class CheckableCheckedElementsProperty extends SimpleSetProperty {
34
	private Object elementType;
35
36
	/**
37
	 * @param elementType
38
	 */
39
	public CheckableCheckedElementsProperty(Object elementType) {
40
		this.elementType = elementType;
41
	}
42
43
	public Object getElementType() {
44
		return elementType;
45
	}
46
47
	protected Set doGetSet(Object source) {
48
		ICheckable checkable = (ICheckable) source;
49
50
		Set set = doGetSet(checkable);
51
		if (set == null) {
52
			set = createElementSet(checkable);
53
		}
54
55
		return set;
56
	}
57
58
	protected Set doGetSet(ICheckable checkable) {
59
		return null; // overridden by viewer-specific subclasses
60
	}
61
62
	protected Set createElementSet(ICheckable checkable) {
63
		return new HashSet();
64
	}
65
66
	protected void setSet(Object source, Set set, SetDiff diff) {
67
		ICheckable checkable = (ICheckable) source;
68
		for (Iterator it = diff.getAdditions().iterator(); it.hasNext();) {
69
			checkable.setChecked(it.next(), true);
70
		}
71
		for (Iterator it = diff.getRemovals().iterator(); it.hasNext();) {
72
			checkable.setChecked(it.next(), false);
73
		}
74
	}
75
76
	public INativePropertyListener adaptListener(ISetPropertyChangeListener listener) {
77
		return new CheckStateListener(listener);
78
	}
79
80
	public void addListener(Object source, INativePropertyListener listener) {
81
		((ICheckable) source)
82
				.addCheckStateListener((ICheckStateListener) listener);
83
	}
84
85
	public void removeListener(Object source, INativePropertyListener listener) {
86
		((ICheckable) source)
87
				.removeCheckStateListener((ICheckStateListener) listener);
88
	}
89
90
	private class CheckStateListener implements INativePropertyListener,
91
			ICheckStateListener {
92
		private ISetPropertyChangeListener listener;
93
94
		private CheckStateListener(ISetPropertyChangeListener listener) {
95
			this.listener = listener;
96
		}
97
98
		public void checkStateChanged(CheckStateChangedEvent event) {
99
			Object element = event.getElement();
100
			SetDiff diff;
101
			if (event.getChecked()) {
102
				diff = Diffs.createSetDiff(Collections.singleton(element),
103
						Collections.EMPTY_SET);
104
			} else {
105
				diff = Diffs.createSetDiff(Collections.EMPTY_SET, Collections
106
						.singleton(element));
107
			}
108
			listener.handleSetPropertyChange(new SetPropertyChangeEvent(event
109
					.getSource(), CheckableCheckedElementsProperty.this, diff));
110
		}
111
	}
112
113
	public String toString() {
114
		String s = "ICheckable.checkedElements{}"; //$NON-NLS-1$
115
		if (getElementType() != null)
116
			s += " <" + getElementType() + ">"; //$NON-NLS-1$//$NON-NLS-2$
117
		return s;
118
	}
119
}
(-)src/org/eclipse/jface/internal/databinding/swt/WidgetBooleanValueProperty.java (+44 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
/**
15
 * @since 3.3
16
 * 
17
 */
18
public abstract class WidgetBooleanValueProperty extends WidgetValueProperty {
19
	WidgetBooleanValueProperty(int[] events) {
20
		super(events, Boolean.TYPE);
21
	}
22
23
	WidgetBooleanValueProperty(int event) {
24
		super(event, Boolean.TYPE);
25
	}
26
27
	WidgetBooleanValueProperty() {
28
		super(Boolean.TYPE);
29
	}
30
31
	public Object getValue(Object source) {
32
		return doGetBooleanValue(source) ? Boolean.TRUE : Boolean.FALSE;
33
	}
34
35
	public void setValue(Object source, Object value) {
36
		if (value == null)
37
			value = Boolean.FALSE;
38
		doSetBooleanValue(source, ((Boolean) value).booleanValue());
39
	}
40
41
	abstract boolean doGetBooleanValue(Object source);
42
43
	abstract void doSetBooleanValue(Object source, boolean value);
44
}
(-)src/org/eclipse/jface/internal/databinding/swt/WidgetIntValueProperty.java (+42 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
/**
15
 * @since 3.3
16
 * 
17
 */
18
public abstract class WidgetIntValueProperty extends WidgetValueProperty {
19
	WidgetIntValueProperty() {
20
		super(Integer.TYPE);
21
	}
22
23
	WidgetIntValueProperty(int event) {
24
		super(event, Integer.TYPE);
25
	}
26
27
	WidgetIntValueProperty(int[] events) {
28
		super(events, Integer.TYPE);
29
	}
30
31
	public Object getValue(Object source) {
32
		return new Integer(doGetIntValue(source));
33
	}
34
35
	public void setValue(Object source, Object value) {
36
		doSetIntValue(source, ((Integer) value).intValue());
37
	}
38
39
	abstract int doGetIntValue(Object source);
40
41
	abstract void doSetIntValue(Object source, int intValue);
42
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlSizeProperty.java (+42 lines)
Added Link Here
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
10
 *     Tom Schindl - initial API and implementation
11
 ******************************************************************************/
12
13
package org.eclipse.jface.internal.databinding.swt;
14
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.graphics.Point;
17
import org.eclipse.swt.widgets.Control;
18
19
/**
20
 * @since 3.3
21
 * 
22
 */
23
public class ControlSizeProperty extends WidgetValueProperty {
24
	/**
25
	 * 
26
	 */
27
	public ControlSizeProperty() {
28
		super(SWT.Resize, Point.class);
29
	}
30
31
	public Object getValue(Object source) {
32
		return ((Control) source).getSize();
33
	}
34
35
	public void setValue(Object source, Object value) {
36
		((Control) source).setSize((Point) value);
37
	}
38
39
	public String toString() {
40
		return "Control.size <Point>"; //$NON-NLS-1$
41
	}
42
}
(-)src/org/eclipse/jface/internal/databinding/viewers/CheckboxTableViewerCheckedElementsProperty.java (+57 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.viewers;
13
14
import java.util.Arrays;
15
import java.util.Set;
16
17
import org.eclipse.core.databinding.observable.set.SetDiff;
18
import org.eclipse.jface.viewers.CheckboxTableViewer;
19
import org.eclipse.jface.viewers.ICheckable;
20
21
/**
22
 * @since 3.3
23
 * 
24
 */
25
public class CheckboxTableViewerCheckedElementsProperty extends
26
		CheckableCheckedElementsProperty {
27
	/**
28
	 * @param elementType
29
	 */
30
	public CheckboxTableViewerCheckedElementsProperty(Object elementType) {
31
		super(elementType);
32
	}
33
34
	protected Set createElementSet(ICheckable checkable) {
35
		return ViewerElementSet.withComparer(((CheckboxTableViewer) checkable)
36
				.getComparer());
37
	}
38
39
	protected Set doGetSet(ICheckable checkable) {
40
		CheckboxTableViewer viewer = (CheckboxTableViewer) checkable;
41
		Set set = createElementSet(viewer);
42
		set.addAll(Arrays.asList(viewer.getCheckedElements()));
43
		return set;
44
	}
45
46
	protected void doSetSet(Object source, Set set, SetDiff diff) {
47
		CheckboxTableViewer viewer = (CheckboxTableViewer) source;
48
		viewer.setCheckedElements(set.toArray());
49
	}
50
51
	public String toString() {
52
		String s = "CheckboxTableViewer.checkedElements{}"; //$NON-NLS-1$
53
		if (getElementType() != null)
54
			s += " <" + getElementType() + ">"; //$NON-NLS-1$//$NON-NLS-2$
55
		return s;
56
	}
57
}
(-)src/org/eclipse/jface/databinding/swt/TreeColumnProperties.java (+31 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.value.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.TreeItemTooltipTextProperty;
16
17
/**
18
 * A factory for creating properties of SWT TabItems
19
 * 
20
 * @since 1.3
21
 */
22
public class TreeColumnProperties {
23
	/**
24
	 * Returns a value property for the tooltip text of a SWT TreeColumn.
25
	 * 
26
	 * @return a value property for the tooltip text of a SWT TreeColumn.
27
	 */
28
	public static IValueProperty tooltipText() {
29
		return new TreeItemTooltipTextProperty();
30
	}
31
}
(-)src/org/eclipse/jface/databinding/viewers/ViewerProperties.java (+31 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.viewers;
13
14
import org.eclipse.core.databinding.property.value.IValueProperty;
15
import org.eclipse.jface.internal.databinding.viewers.ViewerInputProperty;
16
17
/**
18
 * A factory for creating properties of JFace Viewers
19
 * 
20
 * @since 1.3
21
 */
22
public class ViewerProperties {
23
	/**
24
	 * Returns a value property for the input of a JFace Viewer.
25
	 * 
26
	 * @return a value property for the input of a JFace Viewer.
27
	 */
28
	public static IValueProperty input() {
29
		return new ViewerInputProperty();
30
	}
31
}
(-)src/org/eclipse/jface/databinding/swt/CTabItemProperties.java (+31 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.value.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.CTabItemTooltipTextProperty;
16
17
/**
18
 * A factory for creating properties of SWT CTabItems
19
 * 
20
 * @since 1.3
21
 */
22
public class CTabItemProperties {
23
	/**
24
	 * Returns a value property for the tooltip text of a SWT CTabItem.
25
	 * 
26
	 * @return a value property for the tooltip text of a SWT CTabItem.
27
	 */
28
	public static IValueProperty tooltipText() {
29
		return new CTabItemTooltipTextProperty();
30
	}
31
}
(-)src/org/eclipse/jface/internal/databinding/swt/CComboSelectionProperty.java (+57 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.custom.CCombo;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class CComboSelectionProperty extends WidgetStringValueProperty {
22
	/**
23
	 * 
24
	 */
25
	public CComboSelectionProperty() {
26
		super(SWT.Modify);
27
	}
28
29
	String doGetStringValue(Object source) {
30
		return ((CCombo) source).getText();
31
	}
32
33
	void doSetStringValue(Object source, String value) {
34
		CCombo ccombo = (CCombo) source;
35
		String items[] = ccombo.getItems();
36
		int index = -1;
37
		if (value == null) {
38
			ccombo.select(-1);
39
		} else if (items != null) {
40
			for (int i = 0; i < items.length; i++) {
41
				if (value.equals(items[i])) {
42
					index = i;
43
					break;
44
				}
45
			}
46
			if (index == -1) {
47
				ccombo.setText(value);
48
			} else {
49
				ccombo.select(index); // -1 will not "unselect"
50
			}
51
		}
52
	}
53
54
	public String toString() {
55
		return "CCombo.selection <String>"; //$NON-NLS-1$
56
	}
57
}
(-)src/org/eclipse/jface/databinding/swt/TableProperties.java (+31 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.value.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.TableSingleSelectionIndexProperty;
16
17
/**
18
 * A factory for creating properties of SWT Tables.
19
 * 
20
 * @since 1.3
21
 */
22
public class TableProperties {
23
	/**
24
	 * Returns a value property for the single selection index of a SWT Table.
25
	 * 
26
	 * @return a value property for the single selection index of a SWT Table.
27
	 */
28
	public static IValueProperty singleSelectionIndex() {
29
		return new TableSingleSelectionIndexProperty();
30
	}
31
}
(-)src/org/eclipse/jface/databinding/swt/ComboProperties.java (+62 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.list.IListProperty;
15
import org.eclipse.core.databinding.property.value.IValueProperty;
16
import org.eclipse.jface.internal.databinding.swt.ComboItemsProperty;
17
import org.eclipse.jface.internal.databinding.swt.ComboSelectionProperty;
18
import org.eclipse.jface.internal.databinding.swt.ComboSingleSelectionIndexProperty;
19
import org.eclipse.jface.internal.databinding.swt.ComboTextProperty;
20
21
/**
22
 * A factory for creating properties of SWT Combos.
23
 * 
24
 * @since 1.3
25
 */
26
public class ComboProperties {
27
	/**
28
	 * Returns a value property for the selection text of a SWT Combo.
29
	 * 
30
	 * @return a value property for the selection text of a SWT Combo.
31
	 */
32
	public static IValueProperty selection() {
33
		return new ComboSelectionProperty();
34
	}
35
36
	/**
37
	 * Returns a value property for the text of a SWT Combo.
38
	 * 
39
	 * @return a value property for the text of a SWT Combo.
40
	 */
41
	public static IValueProperty text() {
42
		return new ComboTextProperty();
43
	}
44
45
	/**
46
	 * Returns a list property for the items of a SWT Combo.
47
	 * 
48
	 * @return a list property for the items of a SWT Combo.
49
	 */
50
	public static IListProperty items() {
51
		return new ComboItemsProperty();
52
	}
53
54
	/**
55
	 * Returns a value property for the single selection index of a SWT Combo.
56
	 * 
57
	 * @return a value property for the single selection index of a SWT Combo.
58
	 */
59
	public static IValueProperty singleSelectionIndex() {
60
		return new ComboSingleSelectionIndexProperty();
61
	}
62
}
(-)src/org/eclipse/jface/databinding/viewers/CheckableProperties.java (+34 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.viewers;
13
14
import org.eclipse.core.databinding.property.set.ISetProperty;
15
import org.eclipse.jface.internal.databinding.viewers.CheckableCheckedElementsProperty;
16
17
/**
18
 * A factory for creating properties of JFace ICheckables
19
 * 
20
 * @since 1.3
21
 */
22
public class CheckableProperties {
23
	/**
24
	 * Returns a set property for the checked elements of a JFace ICheckable.
25
	 * 
26
	 * @param elementType
27
	 *            the element type of the returned property
28
	 * 
29
	 * @return a set property for the checked elements of a JFace ICheckable.
30
	 */
31
	public static ISetProperty checkedElements(Object elementType) {
32
		return new CheckableCheckedElementsProperty(elementType);
33
	}
34
}
(-)src/org/eclipse/jface/internal/databinding/swt/ComboTextProperty.java (+40 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.Combo;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class ComboTextProperty extends WidgetStringValueProperty {
22
	/**
23
	 * 
24
	 */
25
	public ComboTextProperty() {
26
		super(SWT.Modify);
27
	}
28
29
	String doGetStringValue(Object source) {
30
		return ((Combo) source).getText();
31
	}
32
33
	void doSetStringValue(Object source, String value) {
34
		((Combo) source).setText(value != null ? value : ""); //$NON-NLS-1$
35
	}
36
37
	public String toString() {
38
		return "Combo.text <String>"; //$NON-NLS-1$
39
	}
40
}
(-)src/org/eclipse/jface/internal/databinding/viewers/ViewerObservableListDecorator.java (+41 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.viewers;
13
14
import org.eclipse.core.databinding.observable.list.DecoratingObservableList;
15
import org.eclipse.core.databinding.observable.list.IObservableList;
16
import org.eclipse.jface.databinding.viewers.IViewerObservableList;
17
import org.eclipse.jface.viewers.Viewer;
18
19
/**
20
 * @since 3.3
21
 * 
22
 */
23
public class ViewerObservableListDecorator extends DecoratingObservableList
24
		implements IViewerObservableList {
25
	private final Viewer viewer;
26
27
	/**
28
	 * @param decorated
29
	 * @param viewer
30
	 */
31
	public ViewerObservableListDecorator(IObservableList decorated,
32
			Viewer viewer) {
33
		super(decorated, true);
34
		this.viewer = viewer;
35
	}
36
37
	public Viewer getViewer() {
38
		return viewer;
39
	}
40
41
}
(-)src/org/eclipse/jface/internal/databinding/swt/ComboItemsProperty.java (+33 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Combo;
15
import org.eclipse.swt.widgets.Control;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class ComboItemsProperty extends ControlStringListProperty {
22
	protected void doSetStringList(Control control, String[] list) {
23
		((Combo) control).setItems(list);
24
	}
25
26
	public String[] doGetStringList(Control control) {
27
		return ((Combo) control).getItems();
28
	}
29
30
	public String toString() {
31
		return "Combo.items[] <String>"; //$NON-NLS-1$
32
	}
33
}
(-)src/org/eclipse/jface/databinding/swt/TableColumnProperties.java (+31 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.value.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.TableColumnTooltipTextProperty;
16
17
/**
18
 * A factory for creating properties of SWT TableColumns
19
 * 
20
 * @since 1.3
21
 */
22
public class TableColumnProperties {
23
	/**
24
	 * Returns a value property for the tooltip text of a SWT TableColumns.
25
	 * 
26
	 * @return a value property for the tooltip text of a SWT TableColumns.
27
	 */
28
	public static IValueProperty tooltipText() {
29
		return new TableColumnTooltipTextProperty();
30
	}
31
}
(-)src/org/eclipse/jface/internal/databinding/swt/CComboSingleSelectionIndexProperty.java (+40 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.custom.CCombo;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class CComboSingleSelectionIndexProperty extends WidgetIntValueProperty {
22
	/**
23
	 * 
24
	 */
25
	public CComboSingleSelectionIndexProperty() {
26
		super(new int[] { SWT.Selection, SWT.DefaultSelection });
27
	}
28
29
	int doGetIntValue(Object source) {
30
		return ((CCombo) source).getSelectionIndex();
31
	}
32
33
	void doSetIntValue(Object source, int value) {
34
		((CCombo) source).select(value);
35
	}
36
37
	public String toString() {
38
		return "CCombo.selectionIndex <int>"; //$NON-NLS-1$
39
	}
40
}
(-)src/org/eclipse/jface/internal/databinding/swt/WidgetStringValueProperty.java (+38 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
/**
15
 * @since 3.3
16
 * 
17
 */
18
public abstract class WidgetStringValueProperty extends WidgetValueProperty {
19
	WidgetStringValueProperty(int event) {
20
		super(event, String.class);
21
	}
22
23
	WidgetStringValueProperty() {
24
		super(String.class);
25
	}
26
27
	public Object getValue(Object source) {
28
		return doGetStringValue(source);
29
	}
30
31
	public void setValue(Object source, Object value) {
32
		doSetStringValue(source, (String) value);
33
	}
34
35
	abstract String doGetStringValue(Object source);
36
37
	abstract void doSetStringValue(Object source, String value);
38
}
(-)src/org/eclipse/jface/internal/databinding/swt/ScaleMinimumProperty.java (+32 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Scale;
15
16
/**
17
 * @since 3.3
18
 * 
19
 */
20
public class ScaleMinimumProperty extends WidgetIntValueProperty {
21
	int doGetIntValue(Object source) {
22
		return ((Scale) source).getMinimum();
23
	}
24
25
	void doSetIntValue(Object source, int value) {
26
		((Scale) source).setMinimum(value);
27
	}
28
29
	public String toString() {
30
		return "Scale.minimum <int>"; //$NON-NLS-1$
31
	}
32
}
(-)src/org/eclipse/jface/databinding/swt/ItemProperties.java (+31 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.value.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.ItemTextProperty;
16
17
/**
18
 * A factory for creating properties of SWT controls.
19
 * 
20
 * @since 1.3
21
 */
22
public class ItemProperties {
23
	/**
24
	 * Returns a value property for the text of a SWT Item.
25
	 * 
26
	 * @return a value property for the text of a SWT Item
27
	 */
28
	public static IValueProperty text() {
29
		return new ItemTextProperty();
30
	}
31
}
(-)src/org/eclipse/jface/internal/databinding/swt/SpinnerMinimumProperty.java (+32 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Spinner;
15
16
/**
17
 * @since 3.3
18
 * 
19
 */
20
public class SpinnerMinimumProperty extends WidgetIntValueProperty {
21
	int doGetIntValue(Object source) {
22
		return ((Spinner) source).getMinimum();
23
	}
24
25
	void doSetIntValue(Object source, int value) {
26
		((Spinner) source).setMinimum(value);
27
	}
28
29
	public String toString() {
30
		return "Spinner.minimum <int>"; //$NON-NLS-1$
31
	}
32
}
(-)src/org/eclipse/jface/databinding/swt/TrayItemProperties.java (+31 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.value.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.TrayItemTooltipTextProperty;
16
17
/**
18
 * A factory for creating properties of SWT TrayItems
19
 * 
20
 * @since 1.3
21
 */
22
public class TrayItemProperties {
23
	/**
24
	 * Returns a value property for the tooltip text of a SWT TrayItems.
25
	 * 
26
	 * @return a value property for the tooltip text of a SWT TrayItems.
27
	 */
28
	public static IValueProperty tooltipText() {
29
		return new TrayItemTooltipTextProperty();
30
	}
31
}
(-)src/org/eclipse/jface/internal/databinding/swt/WidgetValueProperty.java (+84 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.core.databinding.property.INativePropertyListener;
15
import org.eclipse.core.databinding.property.value.IValuePropertyChangeListener;
16
import org.eclipse.core.databinding.property.value.SimpleValueProperty;
17
import org.eclipse.core.databinding.property.value.ValuePropertyChangeEvent;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.widgets.Event;
20
import org.eclipse.swt.widgets.Listener;
21
import org.eclipse.swt.widgets.Widget;
22
23
abstract class WidgetValueProperty extends SimpleValueProperty {
24
	private int[] events;
25
	private Object valueType;
26
27
	WidgetValueProperty(Object valueType) {
28
		this(null, valueType);
29
	}
30
31
	WidgetValueProperty(int event, Object valueType) {
32
		this(new int[] { event }, valueType);
33
	}
34
35
	WidgetValueProperty(int[] events, Object valueType) {
36
		this.events = events;
37
		this.valueType = valueType;
38
	}
39
40
	public final Object getValueType() {
41
		return valueType;
42
	}
43
44
	public INativePropertyListener adaptListener(IValuePropertyChangeListener listener) {
45
		return new WidgetListener(listener);
46
	}
47
48
	public void addListener(Object source, INativePropertyListener listener) {
49
		if (events != null) {
50
			for (int i = 0; i < events.length; i++) {
51
				int event = events[i];
52
				if (event != SWT.None) {
53
					((Widget) source).addListener(event, (Listener) listener);
54
				}
55
			}
56
		}
57
	}
58
59
	public void removeListener(Object source, INativePropertyListener listener) {
60
		if (events != null) {
61
			Widget widget = (Widget) source;
62
			if (!widget.isDisposed()) {
63
				for (int i = 0; i < events.length; i++) {
64
					int event = events[i];
65
					if (event != SWT.None)
66
						widget.removeListener(event, (Listener) listener);
67
				}
68
			}
69
		}
70
	}
71
72
	private class WidgetListener implements INativePropertyListener, Listener {
73
		private final IValuePropertyChangeListener listener;
74
75
		protected WidgetListener(IValuePropertyChangeListener listener) {
76
			this.listener = listener;
77
		}
78
79
		public void handleEvent(Event event) {
80
			listener.handleValuePropertyChange(new ValuePropertyChangeEvent(
81
					event.widget, WidgetValueProperty.this, null));
82
		}
83
	}
84
}
(-)src/org/eclipse/jface/internal/databinding/swt/TextEditableProperty.java (+32 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Text;
15
16
/**
17
 * @since 3.3
18
 * 
19
 */
20
public class TextEditableProperty extends WidgetBooleanValueProperty {
21
	boolean doGetBooleanValue(Object source) {
22
		return ((Text) source).getEditable();
23
	}
24
25
	void doSetBooleanValue(Object source, boolean value) {
26
		((Text) source).setEditable(value);
27
	}
28
29
	public String toString() {
30
		return "Text.editable <boolean>"; //$NON-NLS-1$
31
	}
32
}
(-)src/org/eclipse/jface/internal/databinding/swt/ShellTextProperty.java (+32 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Shell;
15
16
/**
17
 * @since 3.3
18
 * 
19
 */
20
public class ShellTextProperty extends WidgetStringValueProperty {
21
	String doGetStringValue(Object source) {
22
		return ((Shell) source).getText();
23
	}
24
25
	void doSetStringValue(Object source, String value) {
26
		((Shell) source).setText(value == null ? "" : value); //$NON-NLS-1$
27
	}
28
29
	public String toString() {
30
		return "Shell.text <String>"; //$NON-NLS-1$
31
	}
32
}
(-)src/org/eclipse/jface/internal/databinding/swt/CComboTextProperty.java (+40 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.custom.CCombo;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class CComboTextProperty extends WidgetStringValueProperty {
22
	/**
23
	 * 
24
	 */
25
	public CComboTextProperty() {
26
		super(SWT.Modify);
27
	}
28
29
	String doGetStringValue(Object source) {
30
		return ((CCombo) source).getText();
31
	}
32
33
	void doSetStringValue(Object source, String value) {
34
		((CCombo) source).setText(value != null ? value : ""); //$NON-NLS-1$
35
	}
36
37
	public String toString() {
38
		return "CCombo.text <String>"; //$NON-NLS-1$
39
	}
40
}
(-)src/org/eclipse/core/databinding/property/set/SetPropertyChangeEvent.java (+58 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.set;
13
14
import org.eclipse.core.databinding.observable.set.SetDiff;
15
import org.eclipse.core.databinding.property.IPropertyChangeListener;
16
import org.eclipse.core.databinding.property.PropertyChangeEvent;
17
18
/**
19
 * Set change event describing an incremental change of a set property on a
20
 * particular property source.
21
 * 
22
 * @since 1.2
23
 */
24
public class SetPropertyChangeEvent extends PropertyChangeEvent {
25
	private static final long serialVersionUID = 1L;
26
27
	/**
28
	 * The set property that changed
29
	 */
30
	public final ISetProperty property;
31
32
	/**
33
	 * SetDiff enumerating the added and removed elements in the set, or null if
34
	 * the change is unknown.
35
	 */
36
	public final SetDiff diff;
37
38
	/**
39
	 * Constructs a SetPropertyChangeEvent with the given attributes
40
	 * 
41
	 * @param source
42
	 *            the property source
43
	 * @param property
44
	 *            the property that changed on the source
45
	 * @param diff
46
	 *            a SetDiff describing the changes to the set property
47
	 */
48
	public SetPropertyChangeEvent(Object source, ISetProperty property,
49
			SetDiff diff) {
50
		super(source);
51
		this.property = property;
52
		this.diff = diff;
53
	}
54
55
	protected void dispatch(IPropertyChangeListener listener) {
56
		((ISetPropertyChangeListener) listener).handleSetPropertyChange(this);
57
	}
58
}
(-)src/org/eclipse/core/databinding/property/value/IValuePropertyChangeListener.java (+29 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.value;
13
14
import org.eclipse.core.databinding.property.IPropertyChangeListener;
15
16
/**
17
 * Listener for changes to value properties on a property source
18
 * 
19
 * @since 1.2
20
 */
21
public interface IValuePropertyChangeListener extends IPropertyChangeListener {
22
	/**
23
	 * Handle a change to a value property on a specific property source.
24
	 * 
25
	 * @param event
26
	 *            an event describing the value change that occured.
27
	 */
28
	public void handleValuePropertyChange(ValuePropertyChangeEvent event);
29
}
(-)src/org/eclipse/core/databinding/property/list/ListPropertyObservableList.java (+639 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.list;
13
14
import java.util.ArrayList;
15
import java.util.Collection;
16
import java.util.Collections;
17
import java.util.ConcurrentModificationException;
18
import java.util.Iterator;
19
import java.util.List;
20
import java.util.ListIterator;
21
22
import org.eclipse.core.databinding.observable.Diffs;
23
import org.eclipse.core.databinding.observable.ObservableTracker;
24
import org.eclipse.core.databinding.observable.Realm;
25
import org.eclipse.core.databinding.observable.list.AbstractObservableList;
26
import org.eclipse.core.databinding.observable.list.ListDiff;
27
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
28
import org.eclipse.core.databinding.property.IProperty;
29
import org.eclipse.core.databinding.property.INativePropertyListener;
30
import org.eclipse.core.databinding.property.IPropertyObservable;
31
32
/**
33
 * @since 1.2
34
 * 
35
 */
36
public class ListPropertyObservableList extends AbstractObservableList
37
		implements IPropertyObservable {
38
	private Object source;
39
	private SimpleListProperty property;
40
41
	private volatile boolean updating = false;
42
43
	private volatile int modCount = 0;
44
45
	private INativePropertyListener listener;
46
47
	private List cachedList;
48
49
	/**
50
	 * @param realm
51
	 * @param source
52
	 * @param property
53
	 */
54
	public ListPropertyObservableList(Realm realm, Object source,
55
			SimpleListProperty property) {
56
		super(realm);
57
		this.source = source;
58
		this.property = property;
59
	}
60
61
	protected void firstListenerAdded() {
62
		if (!isDisposed()) {
63
			cachedList = property.getList(source);
64
65
			if (listener == null) {
66
				listener = property
67
						.adaptListener(new IListPropertyChangeListener() {
68
							public void handleListPropertyChange(
69
									final ListPropertyChangeEvent event) {
70
								modCount++;
71
								if (!isDisposed() && !updating) {
72
									getRealm().exec(new Runnable() {
73
										public void run() {
74
											List oldList = cachedList;
75
											List newList = cachedList = property
76
													.getList(source);
77
											ListDiff diff = event.diff;
78
											if (diff == null) {
79
												diff = Diffs.computeListDiff(
80
														oldList, newList);
81
											}
82
											fireListChange(diff);
83
										}
84
									});
85
								}
86
							}
87
						});
88
			}
89
			property.addListener(source, listener);
90
		}
91
	}
92
93
	protected void lastListenerRemoved() {
94
		if (listener != null) {
95
			property.removeListener(source, listener);
96
		}
97
98
		cachedList = null;
99
	}
100
101
	private void getterCalled() {
102
		ObservableTracker.getterCalled(this);
103
	}
104
105
	public Object getElementType() {
106
		return property.getElementType();
107
	}
108
109
	// Queries
110
111
	protected int doGetSize() {
112
		return property.size(source);
113
	}
114
115
	public boolean contains(Object o) {
116
		getterCalled();
117
		return property.contains(source, o);
118
	}
119
120
	public boolean containsAll(Collection c) {
121
		getterCalled();
122
		return property.containsAll(source, c);
123
	}
124
125
	public Object get(int index) {
126
		getterCalled();
127
		return property.get(source, index);
128
	}
129
130
	public int indexOf(Object o) {
131
		getterCalled();
132
		return property.indexOf(source, o);
133
	}
134
135
	public boolean isEmpty() {
136
		getterCalled();
137
		return property.isEmpty(source);
138
	}
139
140
	public int lastIndexOf(Object o) {
141
		getterCalled();
142
		return property.lastIndexOf(source, o);
143
	}
144
145
	public Object[] toArray() {
146
		getterCalled();
147
		return property.toArray(source);
148
	}
149
150
	public Object[] toArray(Object[] a) {
151
		getterCalled();
152
		return property.toArray(source, a);
153
	}
154
155
	// Single change operations
156
157
	public boolean add(Object o) {
158
		checkRealm();
159
		add(property.size(source), o);
160
		return true;
161
	}
162
163
	public void add(int index, Object o) {
164
		checkRealm();
165
		boolean wasUpdating = updating;
166
		updating = true;
167
		try {
168
			property.add(source, index, o);
169
			modCount++;
170
		} finally {
171
			updating = wasUpdating;
172
		}
173
174
		cachedList = property.getList(source);
175
		fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
176
				true, o)));
177
	}
178
179
	public Iterator iterator() {
180
		getterCalled();
181
		return new Iterator() {
182
			int expectedModCount = modCount;
183
			ListIterator delegate = new ArrayList(property.getList(source))
184
					.listIterator();
185
186
			Object lastElement = null;
187
			int lastIndex = -1;
188
189
			public boolean hasNext() {
190
				getterCalled();
191
				checkForComodification();
192
				return delegate.hasNext();
193
			}
194
195
			public Object next() {
196
				getterCalled();
197
				checkForComodification();
198
				Object next = lastElement = delegate.next();
199
				lastIndex = delegate.previousIndex();
200
				return next;
201
			}
202
203
			public void remove() {
204
				checkRealm();
205
				checkForComodification();
206
				if (lastIndex == -1)
207
					throw new IllegalStateException();
208
209
				delegate.remove(); // stay in sync
210
211
				boolean wasUpdating = updating;
212
				updating = true;
213
				try {
214
					property.remove(source, lastIndex);
215
					modCount++;
216
				} finally {
217
					updating = wasUpdating;
218
				}
219
220
				cachedList = property.getList(source);
221
				fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
222
						lastIndex, false, lastElement)));
223
224
				lastElement = null;
225
				lastIndex = -1;
226
227
				expectedModCount = modCount;
228
			}
229
230
			private void checkForComodification() {
231
				if (expectedModCount != modCount)
232
					throw new ConcurrentModificationException();
233
			}
234
		};
235
	}
236
237
	public Object move(int oldIndex, int newIndex) {
238
		checkRealm();
239
240
		int size = property.size(source);
241
		if (oldIndex < 0 || oldIndex >= size || newIndex < 0
242
				|| newIndex >= size)
243
			throw new IndexOutOfBoundsException();
244
		if (oldIndex == newIndex)
245
			return property.get(source, oldIndex);
246
247
		Object element;
248
249
		boolean wasUpdating = updating;
250
		updating = true;
251
		try {
252
			element = property.move(source, oldIndex, newIndex);
253
			modCount++;
254
		} finally {
255
			updating = wasUpdating;
256
		}
257
258
		cachedList = property.getList(source);
259
		fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(oldIndex,
260
				false, element), Diffs.createListDiffEntry(newIndex, true,
261
				element)));
262
263
		return element;
264
	}
265
266
	public boolean remove(Object o) {
267
		checkRealm();
268
269
		int index = property.indexOf(source, o);
270
		if (index == -1)
271
			return false;
272
273
		remove(index);
274
275
		return true;
276
	}
277
278
	public ListIterator listIterator() {
279
		return listIterator(0);
280
	}
281
282
	public ListIterator listIterator(final int index) {
283
		getterCalled();
284
		return new ListIterator() {
285
			int expectedModCount = modCount;
286
			ListIterator delegate = new ArrayList(property.getList(source))
287
					.listIterator(index);
288
289
			Object lastElement = null;
290
			int lastIndex = -1;
291
292
			public boolean hasNext() {
293
				getterCalled();
294
				checkForComodification();
295
				return delegate.hasNext();
296
			}
297
298
			public int nextIndex() {
299
				getterCalled();
300
				checkForComodification();
301
				return delegate.nextIndex();
302
			}
303
304
			public Object next() {
305
				getterCalled();
306
				checkForComodification();
307
				lastElement = delegate.next();
308
				lastIndex = delegate.previousIndex();
309
				return lastElement;
310
			}
311
312
			public boolean hasPrevious() {
313
				getterCalled();
314
				checkForComodification();
315
				return delegate.hasPrevious();
316
			}
317
318
			public int previousIndex() {
319
				getterCalled();
320
				checkForComodification();
321
				return delegate.previousIndex();
322
			}
323
324
			public Object previous() {
325
				getterCalled();
326
				checkForComodification();
327
				lastElement = delegate.previous();
328
				lastIndex = delegate.nextIndex();
329
				return lastElement;
330
			}
331
332
			public void add(Object o) {
333
				checkRealm();
334
				checkForComodification();
335
				int index = delegate.nextIndex();
336
337
				delegate.add(o); // keep in sync
338
339
				boolean wasUpdating = updating;
340
				updating = true;
341
				try {
342
					property.add(source, index, o);
343
					modCount++;
344
				} finally {
345
					updating = wasUpdating;
346
				}
347
348
				cachedList = property.getList(source);
349
				fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
350
						index, true, o)));
351
352
				lastElement = null;
353
				lastIndex = -1;
354
				expectedModCount = modCount;
355
			}
356
357
			public void set(Object o) {
358
				checkRealm();
359
				checkForComodification();
360
361
				delegate.set(o);
362
363
				boolean wasUpdating = updating;
364
				updating = true;
365
				try {
366
					property.set(source, lastIndex, o);
367
					modCount++;
368
				} finally {
369
					updating = wasUpdating;
370
				}
371
372
				cachedList = property.getList(source);
373
				fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
374
						lastIndex, false, lastElement), Diffs
375
						.createListDiffEntry(lastIndex, true, o)));
376
377
				lastElement = o;
378
379
				expectedModCount = modCount;
380
			}
381
382
			public void remove() {
383
				checkRealm();
384
				checkForComodification();
385
				if (lastIndex == -1)
386
					throw new IllegalStateException();
387
388
				delegate.remove(); // keep in sync
389
390
				boolean wasUpdating = updating;
391
				updating = true;
392
				try {
393
					property.remove(source, lastIndex);
394
					modCount++;
395
				} finally {
396
					updating = wasUpdating;
397
				}
398
399
				cachedList = property.getList(source);
400
				fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
401
						lastIndex, false, lastElement)));
402
403
				lastElement = null;
404
				lastIndex = -1;
405
				expectedModCount = modCount;
406
			}
407
408
			private void checkForComodification() {
409
				if (expectedModCount != modCount)
410
					throw new ConcurrentModificationException();
411
			}
412
		};
413
	}
414
415
	public Object remove(int index) {
416
		checkRealm();
417
418
		Object element;
419
420
		boolean wasUpdating = updating;
421
		updating = true;
422
		try {
423
			element = property.remove(source, index);
424
			modCount++;
425
		} finally {
426
			updating = wasUpdating;
427
		}
428
429
		cachedList = property.getList(source);
430
		fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
431
				false, element)));
432
433
		return element;
434
	}
435
436
	public Object set(int index, Object o) {
437
		checkRealm();
438
439
		Object oldElement;
440
441
		boolean wasUpdating = updating;
442
		updating = true;
443
		try {
444
			oldElement = property.set(source, index, o);
445
			modCount++;
446
		} finally {
447
			updating = wasUpdating;
448
		}
449
450
		cachedList = property.getList(source);
451
		fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
452
				false, oldElement), Diffs.createListDiffEntry(index, true, o)));
453
454
		return oldElement;
455
	}
456
457
	public List subList(int fromIndex, int toIndex) {
458
		getterCalled();
459
		return Collections.unmodifiableList(property.getList(source).subList(
460
				fromIndex, toIndex));
461
	}
462
463
	// Bulk change operations
464
465
	public boolean addAll(Collection c) {
466
		checkRealm();
467
468
		return addAll(property.size(source), c);
469
	}
470
471
	public boolean addAll(int index, Collection c) {
472
		checkRealm();
473
474
		if (c.isEmpty())
475
			return false;
476
477
		ListDiffEntry[] entries = new ListDiffEntry[c.size()];
478
		int offsetIndex = 0;
479
		for (Iterator it = c.iterator(); it.hasNext();) {
480
			Object element = it.next();
481
			entries[offsetIndex] = Diffs.createListDiffEntry(index
482
					+ offsetIndex, true, element);
483
			offsetIndex++;
484
		}
485
486
		boolean changed;
487
488
		boolean wasUpdating = updating;
489
		updating = true;
490
		try {
491
			changed = property.addAll(source, index, c);
492
			modCount++;
493
		} finally {
494
			updating = wasUpdating;
495
		}
496
497
		cachedList = property.getList(source);
498
		if (changed)
499
			fireListChange(Diffs.createListDiff(entries));
500
501
		return changed;
502
	}
503
504
	public boolean removeAll(Collection c) {
505
		checkRealm();
506
507
		if (property.isEmpty(source) || c.isEmpty())
508
			return false;
509
510
		boolean changed;
511
512
		List entries = new ArrayList();
513
514
		boolean wasUpdating = updating;
515
		updating = true;
516
		try {
517
			List list = new ArrayList(property.getList(source));
518
			for (ListIterator it = list.listIterator(); it.hasNext();) {
519
				Object element = it.next();
520
				int index = it.previousIndex();
521
				if (c.contains(element)) {
522
					it.remove();
523
					entries.add(Diffs
524
							.createListDiffEntry(index, false, element));
525
				}
526
			}
527
			changed = property.removeAll(source, c);
528
			modCount++;
529
		} finally {
530
			updating = wasUpdating;
531
		}
532
533
		cachedList = property.getList(source);
534
535
		if (changed)
536
			fireListChange(Diffs.createListDiff((ListDiffEntry[]) entries
537
					.toArray(new ListDiffEntry[entries.size()])));
538
539
		return changed;
540
	}
541
542
	public boolean retainAll(Collection c) {
543
		checkRealm();
544
545
		if (property.isEmpty(source))
546
			return false;
547
548
		if (c.isEmpty()) {
549
			clear();
550
			return true;
551
		}
552
553
		boolean changed;
554
555
		List entries = new ArrayList();
556
557
		boolean wasUpdating = updating;
558
		updating = true;
559
		try {
560
			List list = new ArrayList(property.getList(source));
561
			for (ListIterator it = list.listIterator(); it.hasNext();) {
562
				Object element = it.next();
563
				int index = it.previousIndex();
564
				if (!c.contains(element)) {
565
					it.remove();
566
					entries.add(Diffs
567
							.createListDiffEntry(index, false, element));
568
				}
569
			}
570
			changed = property.retainAll(source, c);
571
			modCount++;
572
		} finally {
573
			updating = wasUpdating;
574
		}
575
576
		cachedList = property.getList(source);
577
578
		if (changed)
579
			fireListChange(Diffs.createListDiff((ListDiffEntry[]) entries
580
					.toArray(new ListDiffEntry[entries.size()])));
581
582
		return changed;
583
	}
584
585
	public void clear() {
586
		checkRealm();
587
588
		if (property.isEmpty(source))
589
			return;
590
591
		List entries = new ArrayList();
592
		for (Iterator it = property.getList(source).iterator(); it.hasNext();) {
593
			// always report 0 as the remove index
594
			entries.add(Diffs.createListDiffEntry(0, false, it.next()));
595
		}
596
597
		boolean wasUpdating = updating;
598
		updating = true;
599
		try {
600
			property.clear(source);
601
			modCount++;
602
		} finally {
603
			updating = wasUpdating;
604
		}
605
606
		cachedList = property.getList(source);
607
		fireListChange(Diffs.createListDiff((ListDiffEntry[]) entries
608
				.toArray(new ListDiffEntry[entries.size()])));
609
	}
610
611
	public boolean equals(Object o) {
612
		getterCalled();
613
		return property.equals(source, o);
614
	}
615
616
	public int hashCode() {
617
		getterCalled();
618
		return property.hashCode(source);
619
	}
620
621
	public Object getObserved() {
622
		return source;
623
	}
624
625
	public IProperty getProperty() {
626
		return property;
627
	}
628
629
	public synchronized void dispose() {
630
		if (!isDisposed()) {
631
			if (listener != null)
632
				property.removeListener(source, listener);
633
			property = null;
634
			source = null;
635
			listener = null;
636
		}
637
		super.dispose();
638
	}
639
}
(-)src/org/eclipse/core/databinding/property/value/ListValuePropertyObservableList.java (+438 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.value;
13
14
import java.lang.reflect.Array;
15
import java.util.ArrayList;
16
import java.util.Collection;
17
import java.util.HashMap;
18
import java.util.HashSet;
19
import java.util.Iterator;
20
import java.util.List;
21
import java.util.ListIterator;
22
import java.util.Map;
23
import java.util.Set;
24
25
import org.eclipse.core.databinding.observable.Diffs;
26
import org.eclipse.core.databinding.observable.IObserving;
27
import org.eclipse.core.databinding.observable.IStaleListener;
28
import org.eclipse.core.databinding.observable.ObservableTracker;
29
import org.eclipse.core.databinding.observable.StaleEvent;
30
import org.eclipse.core.databinding.observable.list.AbstractObservableList;
31
import org.eclipse.core.databinding.observable.list.IListChangeListener;
32
import org.eclipse.core.databinding.observable.list.IObservableList;
33
import org.eclipse.core.databinding.observable.list.ListChangeEvent;
34
import org.eclipse.core.databinding.observable.list.ListDiff;
35
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
36
import org.eclipse.core.databinding.observable.set.IObservableSet;
37
import org.eclipse.core.databinding.observable.set.ISetChangeListener;
38
import org.eclipse.core.databinding.observable.set.SetChangeEvent;
39
import org.eclipse.core.databinding.observable.set.WritableSet;
40
import org.eclipse.core.databinding.property.INativePropertyListener;
41
import org.eclipse.core.internal.databinding.IdentityWrapper;
42
import org.eclipse.core.internal.databinding.Util;
43
44
/**
45
 * @since 1.2
46
 */
47
public class ListValuePropertyObservableList extends AbstractObservableList
48
		implements IObserving {
49
	private IObservableList masterList;
50
	private SimpleValueProperty detailProperty;
51
52
	private IObservableSet knownMasterElements;
53
	private Map cachedValues;
54
55
	private boolean updating;
56
57
	private IListChangeListener masterListener = new IListChangeListener() {
58
		public void handleListChange(ListChangeEvent event) {
59
			if (!isDisposed()) {
60
				updateKnownElements();
61
				fireListChange(convertDiff(event.diff));
62
			}
63
		}
64
65
		private void updateKnownElements() {
66
			Set identityKnownElements = new HashSet();
67
			for (Iterator it = masterList.iterator(); it.hasNext();) {
68
				identityKnownElements.add(new IdentityWrapper(it.next()));
69
			}
70
71
			knownMasterElements.retainAll(identityKnownElements);
72
			knownMasterElements.addAll(identityKnownElements);
73
		}
74
75
		private ListDiff convertDiff(ListDiff diff) {
76
			// Convert diff to detail value
77
			ListDiffEntry[] masterEntries = diff.getDifferences();
78
			ListDiffEntry[] detailEntries = new ListDiffEntry[masterEntries.length];
79
			for (int i = 0; i < masterEntries.length; i++) {
80
				ListDiffEntry masterDifference = masterEntries[i];
81
				int index = masterDifference.getPosition();
82
				boolean addition = masterDifference.isAddition();
83
				Object masterElement = masterDifference.getElement();
84
				Object elementDetailValue = detailProperty
85
						.getValue(masterElement);
86
				detailEntries[i] = Diffs.createListDiffEntry(index, addition,
87
						elementDetailValue);
88
			}
89
			return Diffs.createListDiff(detailEntries);
90
		}
91
	};
92
93
	private IStaleListener staleListener = new IStaleListener() {
94
		public void handleStale(StaleEvent staleEvent) {
95
			fireStale();
96
		}
97
	};
98
99
	private INativePropertyListener detailListener = detailProperty
100
			.adaptListener(new IValuePropertyChangeListener() {
101
				public void handleValuePropertyChange(
102
						final ValuePropertyChangeEvent event) {
103
					if (!isDisposed() && !updating) {
104
						Object masterElement = event.getSource();
105
						int[] indices = indicesOf(masterElement);
106
						Object oldValue = event.diff.getOldValue();
107
						Object newValue = event.diff.getNewValue();
108
						ListDiffEntry[] entries = new ListDiffEntry[indices.length * 2];
109
						for (int i = 0; i < indices.length; i++) {
110
							int index = indices[i];
111
							entries[i * 2] = Diffs.createListDiffEntry(index,
112
									false, oldValue);
113
							entries[i * 2 + 1] = Diffs.createListDiffEntry(
114
									index, true, newValue);
115
						}
116
117
						ListDiff diff = Diffs.createListDiff(entries);
118
						cachedValues.put(new IdentityWrapper(masterElement), newValue);
119
						fireListChange(diff);
120
					}
121
				}
122
123
				private int[] indicesOf(Object element) {
124
					List indices = new ArrayList();
125
126
					for (ListIterator it = masterList.listIterator(); it
127
							.hasNext();) {
128
						if (element == it.next())
129
							indices.add(new Integer(it.previousIndex()));
130
					}
131
132
					int[] result = new int[indices.size()];
133
					for (int i = 0; i < result.length; i++) {
134
						result[i] = ((Integer) indices.get(i)).intValue();
135
					}
136
					return result;
137
				}
138
			});
139
140
	/**
141
	 * @param masterList
142
	 * @param valueProperty
143
	 */
144
	public ListValuePropertyObservableList(IObservableList masterList,
145
			SimpleValueProperty valueProperty) {
146
		super(masterList.getRealm());
147
		this.masterList = masterList;
148
		this.detailProperty = valueProperty;
149
	}
150
151
	protected void firstListenerAdded() {
152
		knownMasterElements = new WritableSet(getRealm());
153
		cachedValues = new HashMap();
154
		knownMasterElements.addSetChangeListener(new ISetChangeListener() {
155
			public void handleSetChange(SetChangeEvent event) {
156
				for (Iterator it = event.diff.getRemovals().iterator(); it
157
						.hasNext();) {
158
					IdentityWrapper wrapper = (IdentityWrapper) it.next();
159
					Object key = wrapper.unwrap();
160
					detailProperty.removeListener(key, detailListener);
161
					cachedValues.remove(wrapper);
162
				}
163
				for (Iterator it = event.diff.getAdditions().iterator(); it
164
						.hasNext();) {
165
					IdentityWrapper wrapper = (IdentityWrapper) it.next();
166
					Object key = wrapper.unwrap();
167
					cachedValues.put(wrapper, detailProperty.getValue(key));
168
					detailProperty.addListener(key, detailListener);
169
				}
170
			}
171
		});
172
		for (Iterator it = masterList.iterator(); it.hasNext();) {
173
			knownMasterElements.add(new IdentityWrapper(it.next()));
174
		}
175
176
		masterList.addListChangeListener(masterListener);
177
		masterList.addStaleListener(staleListener);
178
	}
179
180
	protected void lastListenerRemoved() {
181
		masterList.removeListChangeListener(masterListener);
182
		masterList.removeStaleListener(staleListener);
183
		if (knownMasterElements != null) {
184
			knownMasterElements.clear(); // clears cachedValues
185
			knownMasterElements.dispose();
186
			knownMasterElements = null;
187
		}
188
		cachedValues = null;
189
	}
190
191
	protected int doGetSize() {
192
		getterCalled();
193
		return masterList.size();
194
	}
195
196
	private void getterCalled() {
197
		ObservableTracker.getterCalled(this);
198
	}
199
200
	public Object getElementType() {
201
		return detailProperty.getValueType();
202
	}
203
204
	public Object get(int index) {
205
		getterCalled();
206
		Object masterElement = masterList.get(index);
207
		return detailProperty.getValue(masterElement);
208
	}
209
210
	public boolean add(Object o) {
211
		throw new UnsupportedOperationException();
212
	}
213
214
	public boolean addAll(Collection c) {
215
		throw new UnsupportedOperationException();
216
	}
217
218
	public boolean addAll(int index, Collection c) {
219
		throw new UnsupportedOperationException();
220
	}
221
222
	public boolean contains(Object o) {
223
		getterCalled();
224
225
		for (Iterator it = masterList.iterator(); it.hasNext();) {
226
			if (Util.equals(detailProperty.getValue(it.next()), o))
227
				return true;
228
		}
229
		return false;
230
	}
231
232
	public boolean isEmpty() {
233
		getterCalled();
234
		return masterList.isEmpty();
235
	}
236
237
	public boolean isStale() {
238
		getterCalled();
239
		return masterList.isStale();
240
	}
241
242
	public Iterator iterator() {
243
		getterCalled();
244
		return new Iterator() {
245
			Iterator it = masterList.iterator();
246
247
			public boolean hasNext() {
248
				getterCalled();
249
				return it.hasNext();
250
			}
251
252
			public Object next() {
253
				getterCalled();
254
				Object masterElement = it.next();
255
				return detailProperty.getValue(masterElement);
256
			}
257
258
			public void remove() {
259
				throw new UnsupportedOperationException();
260
			}
261
		};
262
	}
263
264
	public Object move(int oldIndex, int newIndex) {
265
		throw new UnsupportedOperationException();
266
	}
267
268
	public boolean remove(Object o) {
269
		throw new UnsupportedOperationException();
270
	}
271
272
	public boolean removeAll(Collection c) {
273
		throw new UnsupportedOperationException();
274
	}
275
276
	public boolean retainAll(Collection c) {
277
		throw new UnsupportedOperationException();
278
	}
279
280
	public Object[] toArray() {
281
		getterCalled();
282
		Object[] masterElements = masterList.toArray();
283
		Object[] result = new Object[masterElements.length];
284
		for (int i = 0; i < result.length; i++) {
285
			result[i] = detailProperty.getValue(masterElements[i]);
286
		}
287
		return result;
288
	}
289
290
	public Object[] toArray(Object[] a) {
291
		getterCalled();
292
		Object[] masterElements = masterList.toArray();
293
		if (a.length < masterElements.length)
294
			a = (Object[]) Array.newInstance(a.getClass().getComponentType(),
295
					masterElements.length);
296
		for (int i = 0; i < masterElements.length; i++) {
297
			a[i] = detailProperty.getValue(masterElements[i]);
298
		}
299
		return a;
300
	}
301
302
	public void add(int index, Object o) {
303
		throw new UnsupportedOperationException();
304
	}
305
306
	public void clear() {
307
		throw new UnsupportedOperationException();
308
	}
309
310
	public ListIterator listIterator() {
311
		return listIterator(0);
312
	}
313
314
	public ListIterator listIterator(final int index) {
315
		getterCalled();
316
		return new ListIterator() {
317
			ListIterator it = masterList.listIterator(index);
318
			int lastIndex = -1;
319
			Object lastMasterElement;
320
			Object lastElement;
321
			boolean haveIterated = false;
322
323
			public void add(Object arg0) {
324
				throw new UnsupportedOperationException();
325
			}
326
327
			public boolean hasNext() {
328
				getterCalled();
329
				return it.hasNext();
330
			}
331
332
			public boolean hasPrevious() {
333
				getterCalled();
334
				return it.hasPrevious();
335
			}
336
337
			public Object next() {
338
				getterCalled();
339
				lastMasterElement = it.next();
340
				lastElement = detailProperty.getValue(lastMasterElement);
341
				lastIndex = it.previousIndex();
342
				haveIterated = true;
343
				return lastElement;
344
			}
345
346
			public int nextIndex() {
347
				getterCalled();
348
				return it.nextIndex();
349
			}
350
351
			public Object previous() {
352
				getterCalled();
353
				lastMasterElement = it.previous();
354
				lastElement = detailProperty.getValue(lastMasterElement);
355
				lastIndex = it.nextIndex();
356
				haveIterated = true;
357
				return lastElement;
358
			}
359
360
			public int previousIndex() {
361
				getterCalled();
362
				return it.previousIndex();
363
			}
364
365
			public void remove() {
366
				throw new UnsupportedOperationException();
367
			}
368
369
			public void set(Object o) {
370
				checkRealm();
371
				if (!haveIterated)
372
					throw new IllegalStateException();
373
374
				boolean wasUpdating = updating;
375
				updating = true;
376
				try {
377
					detailProperty.setValue(lastElement, o);
378
				} finally {
379
					updating = wasUpdating;
380
				}
381
382
				cachedValues.put(new IdentityWrapper(lastMasterElement), o);
383
				fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
384
						lastIndex, false, lastElement), Diffs
385
						.createListDiffEntry(lastIndex, true, o)));
386
				
387
				lastElement = o;
388
			}
389
		};
390
	}
391
392
	public Object remove(int index) {
393
		throw new UnsupportedOperationException();
394
	}
395
396
	public Object set(int index, Object o) {
397
		checkRealm();
398
		Object masterElement = masterList.get(index);
399
		Object oldValue = detailProperty.getValue(masterElement);
400
401
		boolean wasUpdating = updating;
402
		updating = true;
403
		try {
404
			detailProperty.setValue(masterElement, o);
405
		} finally {
406
			updating = wasUpdating;
407
		}
408
409
		cachedValues.put(new IdentityWrapper(masterElement), o);
410
		fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
411
				false, oldValue), Diffs.createListDiffEntry(index, true, o)));
412
413
		return oldValue;
414
	}
415
416
	public Object getObserved() {
417
		return masterList;
418
	}
419
420
	public synchronized void dispose() {
421
		if (masterList != null) {
422
			masterList.removeListChangeListener(masterListener);
423
			masterList = null;
424
		}
425
		if (knownMasterElements != null) {
426
			knownMasterElements.clear(); // detaches listeners
427
			knownMasterElements.dispose();
428
			knownMasterElements = null;
429
		}
430
431
		masterListener = null;
432
		detailListener = null;
433
		detailProperty = null;
434
		cachedValues = null;
435
436
		super.dispose();
437
	}
438
}
(-)src/org/eclipse/core/databinding/property/list/ListProperty.java (+21 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.list;
13
14
/**
15
 * Abstract implementation of IListProperty.
16
 * 
17
 * @since 1.2
18
 */
19
public abstract class ListProperty implements IListProperty {
20
	public abstract String toString();
21
}
(-)src/org/eclipse/core/databinding/property/set/SimpleSetProperty.java (+418 lines)
Added Link Here
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
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.set;
13
14
import java.util.Collection;
15
import java.util.Collections;
16
import java.util.HashSet;
17
import java.util.Iterator;
18
import java.util.Set;
19
20
import org.eclipse.core.databinding.observable.Diffs;
21
import org.eclipse.core.databinding.observable.IObservable;
22
import org.eclipse.core.databinding.observable.Realm;
23
import org.eclipse.core.databinding.observable.masterdetail.IObservableFactory;
24
import org.eclipse.core.databinding.observable.masterdetail.MasterDetailObservables;
25
import org.eclipse.core.databinding.observable.set.IObservableSet;
26
import org.eclipse.core.databinding.observable.set.SetDiff;
27
import org.eclipse.core.databinding.observable.value.IObservableValue;
28
import org.eclipse.core.databinding.property.INativePropertyListener;
29
30
/**
31
 * Simplified abstract implementation of ISetProperty. This class takes care of
32
 * most of the functional requirements for an ISetProperty implementation,
33
 * leaving only the property-specific details to subclasses.
34
 * <p>
35
 * Subclasses must implement these methods:
36
 * <ul>
37
 * <li>{@link #getElementType()}
38
 * <li>{@link #getSet(Object)}
39
 * <li>{@link #setSet(Object, Set, SetDiff)}
40
 * </ul>
41
 * 
42
 * @since 1.2
43
 */
44
public abstract class SimpleSetProperty implements ISetProperty {
45
	/**
46
	 * Returns whether the source's collection property contains the given
47
	 * element.
48
	 * 
49
	 * @param source
50
	 *            the property source
51
	 * @param o
52
	 *            the element
53
	 * @return whether the source's collection property contains the given
54
	 *         element.
55
	 */
56
	protected boolean contains(Object source, Object o) {
57
		return getSet(source).contains(o);
58
	}
59
60
	/**
61
	 * Returns whether the source's collection property contains all elements in
62
	 * the given collection
63
	 * 
64
	 * @param source
65
	 *            the property source
66
	 * @param c
67
	 *            the collection of elements to test for
68
	 * @return whether the source's collection property contains all elements in
69
	 *         the given collection
70
	 */
71
	protected boolean containsAll(Object source, Collection c) {
72
		return getSet(source).containsAll(c);
73
	}
74
75
	/**
76
	 * Returns a Set with the current contents of the source's set property
77
	 * 
78
	 * @param source
79
	 *            the property source
80
	 * @return a Set with the current contents of the source's set property
81
	 */
82
	protected final Set getSet(Object source) {
83
		return Collections.unmodifiableSet(doGetSet(source));
84
	}
85
86
	/**
87
	 * Returns an unmodifiable Set with the current contents of the source's set
88
	 * property
89
	 * 
90
	 * @param source
91
	 *            the property source
92
	 * @return an unmodifiable Set with the current contents of the source's set
93
	 *         property
94
	 */
95
	protected abstract Set doGetSet(Object source);
96
97
	/**
98
	 * Returns the type of the elements in the collection or <code>null</code>
99
	 * if untyped
100
	 * 
101
	 * @return the type of the elements in the collection or <code>null</code>
102
	 *         if untyped
103
	 */
104
	protected abstract Object getElementType();
105
106
	/**
107
	 * Returns whether the source's collection property is equal to the
108
	 * argument.
109
	 * 
110
	 * @param source
111
	 *            the property source
112
	 * @param o
113
	 *            the object to test for equality to the source's collection
114
	 *            property
115
	 * @return whether the source's collection property is equal to the argument
116
	 */
117
	protected boolean equals(Object source, Object o) {
118
		return getSet(source).equals(o);
119
	}
120
121
	/**
122
	 * Returns the hash code of the source's collection property.
123
	 * 
124
	 * @param source
125
	 *            the property source
126
	 * @return the hash code of the source's collection property
127
	 */
128
	protected int hashCode(Object source) {
129
		return getSet(source).hashCode();
130
	}
131
132
	/**
133
	 * Returns whether the source's collection property is empty
134
	 * 
135
	 * @param source
136
	 *            the property source
137
	 * @return whether the source's collection property is empty
138
	 */
139
	protected boolean isEmpty(Object source) {
140
		return getSet(source).isEmpty();
141
	}
142
143
	/**
144
	 * Returns the size of the source's collection property
145
	 * 
146
	 * @param source
147
	 *            the property source
148
	 * @return the size of the source's collection property
149
	 */
150
	protected int size(Object source) {
151
		return getSet(source).size();
152
	}
153
154
	/**
155
	 * Returns an array of all elements in the source's collection property
156
	 * 
157
	 * @param source
158
	 *            the property source
159
	 * @param array
160
	 *            the array into which the elements will be copied. If the array
161
	 *            is not large enough to hold all elements, the elements will be
162
	 *            returned in a new array of the same runtime type.
163
	 * @return an array of all elements in the source's collection property
164
	 */
165
	protected Object[] toArray(Object source, Object[] array) {
166
		return getSet(source).toArray(array);
167
	}
168
169
	/**
170
	 * Returns an array of all elements in the source's collection property
171
	 * 
172
	 * @param source
173
	 *            the property source
174
	 * @return an array of all elements in the source's collection property
175
	 */
176
	protected Object[] toArray(Object source) {
177
		return getSet(source).toArray();
178
	}
179
180
	/**
181
	 * Updates the property on the source with the specified change.
182
	 * 
183
	 * @param source
184
	 *            the property source
185
	 * @param set
186
	 *            the new set
187
	 * @param diff
188
	 *            a diff describing the change
189
	 */
190
	protected abstract void setSet(Object source, Set set, SetDiff diff);
191
192
	/**
193
	 * Adds the element to the source's collection property
194
	 * 
195
	 * @param source
196
	 *            the property source
197
	 * @param o
198
	 *            the element to add
199
	 * @return whether the element was added to the source's collection property
200
	 */
201
	protected boolean add(Object source, Object o) {
202
		Set set = getSet(source);
203
		if (!set.contains(o)) {
204
			set = new HashSet(set);
205
			boolean added = set.add(o);
206
			if (added) {
207
				setSet(source, set, Diffs.createSetDiff(Collections
208
						.singleton(o), Collections.EMPTY_SET));
209
			}
210
			return added;
211
		}
212
		return false;
213
	}
214
215
	/**
216
	 * Adds all elements in the specified collection to the source's collection
217
	 * property.
218
	 * 
219
	 * @param source
220
	 *            the property source
221
	 * @param c
222
	 *            the collection of elements to add.
223
	 * @return whether the source's collection property was changed
224
	 */
225
	protected boolean addAll(Object source, Collection c) {
226
		if (c.isEmpty())
227
			return false;
228
229
		Set set = getSet(source);
230
		Set additions = new HashSet();
231
		for (Iterator it = c.iterator(); it.hasNext();) {
232
			Object o = it.next();
233
			if (!set.contains(o)) {
234
				additions.add(o);
235
			}
236
		}
237
		boolean changed = !additions.isEmpty();
238
		if (changed) {
239
			set = new HashSet(set);
240
			set.addAll(additions);
241
242
			setSet(source, set, Diffs.createSetDiff(additions,
243
					Collections.EMPTY_SET));
244
		}
245
		return changed;
246
	}
247
248
	/**
249
	 * Removes all elements from the source's collection property.
250
	 * 
251
	 * @param source
252
	 *            the property source
253
	 */
254
	protected void clear(Object source) {
255
		if (!isEmpty(source)) {
256
			setSet(source, new HashSet(), Diffs.createSetDiff(
257
					Collections.EMPTY_SET, getSet(source)));
258
		}
259
	}
260
261
	/**
262
	 * Removes the element from the source's collection property
263
	 * 
264
	 * @param source
265
	 *            the property source
266
	 * @param o
267
	 *            the element to remove
268
	 * @return whether the element was removed from the source's collection
269
	 *         property
270
	 */
271
	protected boolean remove(Object source, Object o) {
272
		Set set = getSet(source);
273
		if (set.contains(o)) {
274
			set = new HashSet(set);
275
			boolean removed = set.remove(o);
276
			if (removed) {
277
				setSet(source, set, Diffs.createSetDiff(Collections.EMPTY_SET,
278
						Collections.singleton(o)));
279
			}
280
			return removed;
281
		}
282
		return false;
283
	}
284
285
	/**
286
	 * Removes all elements from the source's collection property which are
287
	 * contained in the specified collection.
288
	 * 
289
	 * @param source
290
	 *            the property source
291
	 * @param c
292
	 *            the collection of elements to be removed
293
	 * @return whether the source's collection property was changed
294
	 */
295
	protected boolean removeAll(Object source, Collection c) {
296
		if (c.isEmpty())
297
			return false;
298
299
		Set set = new HashSet(getSet(source));
300
		Set removals = new HashSet();
301
		for (Iterator it = set.iterator(); it.hasNext();) {
302
			Object o = it.next();
303
			if (c.contains(o)) {
304
				removals.add(o);
305
				it.remove();
306
			}
307
		}
308
		boolean changed = !removals.isEmpty();
309
		if (changed) {
310
			setSet(source, set, Diffs.createSetDiff(Collections.EMPTY_SET,
311
					removals));
312
		}
313
		return changed;
314
	}
315
316
	/**
317
	 * Removes all elements from the source's collection property which are not
318
	 * contained in the specified collection.
319
	 * 
320
	 * @param source
321
	 *            the property source
322
	 * @param c
323
	 *            the collection of elements to retain
324
	 * @return whether the source's collection property was changed
325
	 */
326
	protected boolean retainAll(Object source, Collection c) {
327
		if (isEmpty(source))
328
			return false;
329
		if (c.isEmpty()) {
330
			clear(source);
331
			return true;
332
		}
333
334
		Set set = new HashSet(getSet(source));
335
		Set removals = new HashSet();
336
		for (Iterator it = set.iterator(); it.hasNext();) {
337
			Object o = it.next();
338
			if (!c.contains(o)) {
339
				removals.add(o);
340
				it.remove();
341
			}
342
		}
343
		boolean changed = !removals.isEmpty();
344
		if (changed) {
345
			setSet(source, set, Diffs.createSetDiff(Collections.EMPTY_SET,
346
					removals));
347
		}
348
		return changed;
349
	}
350
351
	/**
352
	 * Returns a listener which implements the correct listener interface for
353
	 * the expected source object, and which parlays property change events from
354
	 * the source object to the given listener. If there is no listener API for
355
	 * this property, this method returns null.
356
	 * 
357
	 * @param listener
358
	 *            the property listener to receive events
359
	 * @return a native listener which parlays property change events to the
360
	 *         specified listener.
361
	 * @throws ClassCastException
362
	 *             if the provided listener does not implement the correct
363
	 *             listener interface (IValueProperty, IListProperty,
364
	 *             ISetProperty or IMapProperty) depending on the property.
365
	 * @noreference This method is not intended to be referenced by clients.
366
	 */
367
	protected abstract INativePropertyListener adaptListener(
368
			ISetPropertyChangeListener listener);
369
370
	/**
371
	 * Adds the specified listener as a listener for this property on the
372
	 * specified property source. If the source object has no listener API for
373
	 * this property (i.e. {@link #adaptListener(ISetPropertyChangeListener)}
374
	 * returns null), this method does nothing.
375
	 * 
376
	 * @param source
377
	 *            the property source
378
	 * @param listener
379
	 *            a listener obtained from calling
380
	 *            {@link #adaptListener(ISetPropertyChangeListener)}.
381
	 * @noreference This method is not intended to be referenced by clients.
382
	 */
383
	protected abstract void addListener(Object source,
384
			INativePropertyListener listener);
385
386
	/**
387
	 * Removes the specified listener as a listener for this property on the
388
	 * specified property source. If the source object has no listener API for
389
	 * this property (i.e. {@link #adaptListener(ISetPropertyChangeListener)}
390
	 * returns null), this method does nothing.
391
	 * 
392
	 * @param source
393
	 *            the property source
394
	 * @param listener
395
	 *            a listener obtained from calling
396
	 *            {@link #adaptListener(ISetPropertyChangeListener)} .
397
	 * @noreference This method is not intended to be referenced by clients.
398
	 */
399
	protected abstract void removeListener(Object source,
400
			INativePropertyListener listener);
401
402
	public IObservableSet observeSet(Realm realm, Object source) {
403
		return new SetPropertyObservableSet(realm, source, this);
404
	}
405
406
	public IObservableSet observeDetailSet(IObservableValue master) {
407
		final Realm realm = master.getRealm();
408
		IObservableFactory factory = new IObservableFactory() {
409
			public IObservable createObservable(Object target) {
410
				return SimpleSetProperty.this.observeSet(realm, target);
411
			}
412
		};
413
		return MasterDetailObservables.detailSet(master, factory,
414
				getElementType());
415
	}
416
417
	public abstract String toString();
418
}
(-)src/org/eclipse/core/databinding/property/PropertyChangeEvent.java (+44 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property;
13
14
import java.util.EventObject;
15
16
/**
17
 * Base class for change events in the properties API
18
 * 
19
 * @since 1.2
20
 */
21
public abstract class PropertyChangeEvent extends EventObject {
22
	private static final long serialVersionUID = 1L;
23
24
	protected PropertyChangeEvent(Object source) {
25
		super(source);
26
	}
27
28
	protected abstract void dispatch(IPropertyChangeListener listener);
29
30
	public boolean equals(Object obj) {
31
		if (obj == this)
32
			return true;
33
		if (obj == null)
34
			return false;
35
		if (getClass() != obj.getClass())
36
			return false;
37
38
		return getSource().equals(((PropertyChangeEvent) obj).getSource());
39
	}
40
41
	public int hashCode() {
42
		return getSource().hashCode();
43
	}
44
}
(-)src/org/eclipse/core/databinding/property/map/IMapPropertyChangeListener.java (+29 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.map;
13
14
import org.eclipse.core.databinding.property.IPropertyChangeListener;
15
16
/**
17
 * Listener for changes to map properties on a property source
18
 * 
19
 * @since 1.2
20
 */
21
public interface IMapPropertyChangeListener extends IPropertyChangeListener {
22
	/**
23
	 * Handle a change to a map property on a specific property source.
24
	 * 
25
	 * @param event
26
	 *            an event describing the map change that occured.
27
	 */
28
	public void handleMapPropertyChange(MapPropertyChangeEvent event);
29
}
(-)src/org/eclipse/core/databinding/property/set/SetProperty.java (+21 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.set;
13
14
/**
15
 * Abstract implementation of ISetProperty
16
 * 
17
 * @since 1.2
18
 */
19
public abstract class SetProperty implements ISetProperty {
20
	public abstract String toString();
21
}
(-)src/org/eclipse/core/internal/databinding/property/ValuePropertyDetailValue.java (+70 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.property;
13
14
import org.eclipse.core.databinding.observable.Realm;
15
import org.eclipse.core.databinding.observable.list.IObservableList;
16
import org.eclipse.core.databinding.observable.map.IObservableMap;
17
import org.eclipse.core.databinding.observable.set.IObservableSet;
18
import org.eclipse.core.databinding.observable.value.IObservableValue;
19
import org.eclipse.core.databinding.property.value.IValueProperty;
20
import org.eclipse.core.databinding.property.value.ValueProperty;
21
22
/**
23
 * @since 1.2
24
 * 
25
 */
26
public class ValuePropertyDetailValue extends ValueProperty implements
27
		IValueProperty {
28
	private IValueProperty masterProperty;
29
	private IValueProperty detailProperty;
30
31
	/**
32
	 * @param masterProperty
33
	 * @param detailProperty
34
	 */
35
	public ValuePropertyDetailValue(IValueProperty masterProperty,
36
			IValueProperty detailProperty) {
37
		this.masterProperty = masterProperty;
38
		this.detailProperty = detailProperty;
39
	}
40
41
	public IObservableValue observeValue(Realm realm, Object source) {
42
		IObservableValue master = masterProperty.observeValue(realm, source);
43
		return detailProperty.observeDetailValue(master);
44
	}
45
46
	public IObservableValue observeDetailValue(IObservableValue master) {
47
		IObservableValue masterValue = masterProperty
48
				.observeDetailValue(master);
49
		return detailProperty.observeDetailValue(masterValue);
50
	}
51
52
	public IObservableList observeDetailValues(IObservableList master) {
53
		master = masterProperty.observeDetailValues(master);
54
		return detailProperty.observeDetailValues(master);
55
	}
56
57
	public IObservableMap observeDetailValues(IObservableSet master) {
58
		IObservableMap masterMap = masterProperty.observeDetailValues(master);
59
		return detailProperty.observeDetailValues(masterMap);
60
	}
61
62
	public IObservableMap observeDetailValues(IObservableMap master) {
63
		master = masterProperty.observeDetailValues(master);
64
		return detailProperty.observeDetailValues(master);
65
	}
66
67
	public String toString() {
68
		return masterProperty + " => " + detailProperty; //$NON-NLS-1$
69
	}
70
}
(-)src/org/eclipse/core/internal/databinding/property/SetPropertyDetailValueMap.java (+53 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.property;
13
14
import org.eclipse.core.databinding.observable.Realm;
15
import org.eclipse.core.databinding.observable.map.IObservableMap;
16
import org.eclipse.core.databinding.observable.set.IObservableSet;
17
import org.eclipse.core.databinding.observable.value.IObservableValue;
18
import org.eclipse.core.databinding.property.map.MapProperty;
19
import org.eclipse.core.databinding.property.set.ISetProperty;
20
import org.eclipse.core.databinding.property.value.IValueProperty;
21
22
/**
23
 * @since 3.3
24
 * 
25
 */
26
public class SetPropertyDetailValueMap extends MapProperty {
27
	private final ISetProperty masterProperty;
28
	private final IValueProperty detailProperty;
29
30
	/**
31
	 * @param masterProperty
32
	 * @param detailProperty
33
	 */
34
	public SetPropertyDetailValueMap(ISetProperty masterProperty,
35
			IValueProperty detailProperty) {
36
		this.masterProperty = masterProperty;
37
		this.detailProperty = detailProperty;
38
	}
39
40
	public IObservableMap observeMap(Realm realm, Object source) {
41
		IObservableSet master = masterProperty.observeSet(realm, source);
42
		return detailProperty.observeDetailValues(master);
43
	}
44
45
	public IObservableMap observeDetailMap(IObservableValue master) {
46
		IObservableSet masterSet = masterProperty.observeDetailSet(master);
47
		return detailProperty.observeDetailValues(masterSet);
48
	}
49
50
	public String toString() {
51
		return masterProperty + " => " + detailProperty; //$NON-NLS-1$
52
	}
53
}
(-)src/org/eclipse/core/databinding/property/map/MapProperty.java (+21 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.map;
13
14
/**
15
 * Abstract implementation of IMapProperty
16
 * 
17
 * @since 1.2
18
 */
19
public abstract class MapProperty implements IMapProperty {
20
	public abstract String toString();
21
}
(-)src/org/eclipse/core/internal/databinding/property/ValuePropertyDetailSet.java (+53 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.property;
13
14
import org.eclipse.core.databinding.observable.Realm;
15
import org.eclipse.core.databinding.observable.set.IObservableSet;
16
import org.eclipse.core.databinding.observable.value.IObservableValue;
17
import org.eclipse.core.databinding.property.set.ISetProperty;
18
import org.eclipse.core.databinding.property.set.SetProperty;
19
import org.eclipse.core.databinding.property.value.IValueProperty;
20
21
/**
22
 * @since 3.3
23
 * 
24
 */
25
public class ValuePropertyDetailSet extends SetProperty {
26
	private IValueProperty masterProperty;
27
	private ISetProperty detailProperty;
28
29
	/**
30
	 * @param masterProperty
31
	 * @param detailProperty
32
	 */
33
	public ValuePropertyDetailSet(IValueProperty masterProperty,
34
			ISetProperty detailProperty) {
35
		this.masterProperty = masterProperty;
36
		this.detailProperty = detailProperty;
37
	}
38
39
	public IObservableSet observeSet(Realm realm, Object source) {
40
		IObservableValue master = masterProperty.observeValue(realm, source);
41
		return detailProperty.observeDetailSet(master);
42
	}
43
44
	public IObservableSet observeDetailSet(IObservableValue master) {
45
		IObservableValue masterValue = masterProperty
46
				.observeDetailValue(master);
47
		return detailProperty.observeDetailSet(masterValue);
48
	}
49
50
	public String toString() {
51
		return masterProperty + " => " + detailProperty; //$NON-NLS-1$
52
	}
53
}
(-)src/org/eclipse/core/internal/databinding/property/ListPropertyDetailValueList.java (+52 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.property;
13
14
import org.eclipse.core.databinding.observable.Realm;
15
import org.eclipse.core.databinding.observable.list.IObservableList;
16
import org.eclipse.core.databinding.observable.value.IObservableValue;
17
import org.eclipse.core.databinding.property.list.IListProperty;
18
import org.eclipse.core.databinding.property.list.ListProperty;
19
import org.eclipse.core.databinding.property.value.IValueProperty;
20
21
/**
22
 * @since 3.3
23
 * 
24
 */
25
public class ListPropertyDetailValueList extends ListProperty {
26
	private final IListProperty masterProperty;
27
	private final IValueProperty detailProperty;
28
29
	/**
30
	 * @param masterProperty
31
	 * @param detailProperty
32
	 */
33
	public ListPropertyDetailValueList(IListProperty masterProperty,
34
			IValueProperty detailProperty) {
35
		this.masterProperty = masterProperty;
36
		this.detailProperty = detailProperty;
37
	}
38
39
	public IObservableList observeList(Realm realm, Object source) {
40
		IObservableList master = masterProperty.observeList(realm, source);
41
		return detailProperty.observeDetailValues(master);
42
	}
43
44
	public IObservableList observeDetailList(IObservableValue master) {
45
		IObservableList masterList = masterProperty.observeDetailList(master);
46
		return detailProperty.observeDetailValues(masterList);
47
	}
48
49
	public String toString() {
50
		return masterProperty + " => " + detailProperty; //$NON-NLS-1$
51
	}
52
}
(-)src/org/eclipse/core/databinding/property/value/ValueProperty.java (+21 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 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
package org.eclipse.core.databinding.property.value;
13
14
/**
15
 * Abstract implementation of IValueProperty
16
 * 
17
 * @since 1.2
18
 */
19
public abstract class ValueProperty implements IValueProperty {
20
	public abstract String toString();
21
}
(-)src/org/eclipse/core/databinding/property/map/IMapProperty.java (+54 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.map;
13
14
import org.eclipse.core.databinding.observable.Realm;
15
import org.eclipse.core.databinding.observable.map.IObservableMap;
16
import org.eclipse.core.databinding.observable.value.IObservableValue;
17
import org.eclipse.core.databinding.property.IProperty;
18
19
/**
20
 * Interface for map-typed properties
21
 * 
22
 * @since 1.2
23
 * @noimplement This interface is not intended to be implemented by clients.
24
 *              Clients should instead subclass one of the classes that
25
 *              implement this interface. Note that direct implementers of this
26
 *              interface outside of the framework will be broken in future
27
 *              releases when methods are added to this interface.
28
 */
29
public interface IMapProperty extends IProperty {
30
	/**
31
	 * Returns an observable map observing this map property on the given
32
	 * property source
33
	 * 
34
	 * @param realm
35
	 *            the observable's realm
36
	 * @param source
37
	 *            the property source
38
	 * @return an observable map observing this map-typed property on the given
39
	 *         property source
40
	 */
41
	public IObservableMap observeMap(Realm realm, Object source);
42
43
	/**
44
	 * Returns an observable map on the master observable's realm which tracks
45
	 * this property of the values in the entry set of <code>master</code>.
46
	 * 
47
	 * @param master
48
	 *            the master observable
49
	 * @return an observable map on the master observable's realm which tracks
50
	 *         this property of the values in the entry set of
51
	 *         <code>master</code>.
52
	 */
53
	public IObservableMap observeDetailMap(IObservableValue master);
54
}
(-)src/org/eclipse/core/internal/databinding/property/MapPropertyDetailValueMap.java (+52 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.property;
13
14
import org.eclipse.core.databinding.observable.Realm;
15
import org.eclipse.core.databinding.observable.map.IObservableMap;
16
import org.eclipse.core.databinding.observable.value.IObservableValue;
17
import org.eclipse.core.databinding.property.map.IMapProperty;
18
import org.eclipse.core.databinding.property.map.MapProperty;
19
import org.eclipse.core.databinding.property.value.IValueProperty;
20
21
/**
22
 * @since 3.3
23
 * 
24
 */
25
public class MapPropertyDetailValueMap extends MapProperty {
26
	private final IMapProperty masterProperty;
27
	private final IValueProperty detailProperty;
28
29
	/**
30
	 * @param masterProperty
31
	 * @param detailProperty
32
	 */
33
	public MapPropertyDetailValueMap(IMapProperty masterProperty,
34
			IValueProperty detailProperty) {
35
		this.masterProperty = masterProperty;
36
		this.detailProperty = detailProperty;
37
	}
38
39
	public IObservableMap observeMap(Realm realm, Object source) {
40
		IObservableMap master = masterProperty.observeMap(realm, source);
41
		return detailProperty.observeDetailValues(master);
42
	}
43
44
	public IObservableMap observeDetailMap(IObservableValue master) {
45
		IObservableMap masterMap = masterProperty.observeDetailMap(master);
46
		return detailProperty.observeDetailValues(masterMap);
47
	}
48
49
	public String toString() {
50
		return masterProperty + " => " + detailProperty; //$NON-NLS-1$
51
	}
52
}
(-)src/org/eclipse/core/databinding/property/set/SetPropertyObservableSet.java (+343 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.set;
13
14
import java.util.Collection;
15
import java.util.Collections;
16
import java.util.ConcurrentModificationException;
17
import java.util.HashSet;
18
import java.util.Iterator;
19
import java.util.Set;
20
21
import org.eclipse.core.databinding.observable.Diffs;
22
import org.eclipse.core.databinding.observable.Realm;
23
import org.eclipse.core.databinding.observable.set.AbstractObservableSet;
24
import org.eclipse.core.databinding.observable.set.SetDiff;
25
import org.eclipse.core.databinding.property.IProperty;
26
import org.eclipse.core.databinding.property.INativePropertyListener;
27
import org.eclipse.core.databinding.property.IPropertyObservable;
28
29
/**
30
 * @since 1.2
31
 * 
32
 */
33
public class SetPropertyObservableSet extends AbstractObservableSet implements
34
		IPropertyObservable {
35
	private Object source;
36
	private SimpleSetProperty property;
37
38
	private volatile boolean updating = false;
39
40
	private volatile int modCount = 0;
41
42
	private INativePropertyListener listener;
43
44
	private Set cachedSet;
45
46
	/**
47
	 * @param realm
48
	 * @param source
49
	 * @param property
50
	 */
51
	public SetPropertyObservableSet(Realm realm, Object source,
52
			SimpleSetProperty property) {
53
		super(realm);
54
		this.source = source;
55
		this.property = property;
56
	}
57
58
	protected void firstListenerAdded() {
59
		if (!isDisposed()) {
60
			cachedSet = property.getSet(source);
61
62
			if (listener == null) {
63
				listener = property
64
						.adaptListener(new ISetPropertyChangeListener() {
65
							public void handleSetPropertyChange(
66
									final SetPropertyChangeEvent event) {
67
								modCount++;
68
								if (!isDisposed() && !updating) {
69
									getRealm().exec(new Runnable() {
70
										public void run() {
71
											Set oldSet = cachedSet;
72
											Set newSet = cachedSet = property
73
													.getSet(source);
74
											SetDiff diff = event.diff;
75
											if (diff == null) {
76
												diff = Diffs.computeSetDiff(
77
														oldSet, newSet);
78
											}
79
											fireSetChange(diff);
80
										}
81
									});
82
								}
83
							}
84
						});
85
			}
86
			property.addListener(source, listener);
87
		}
88
	}
89
90
	protected void lastListenerRemoved() {
91
		if (listener != null) {
92
			property.removeListener(source, listener);
93
		}
94
95
		cachedSet = null;
96
	}
97
98
	protected Set getWrappedSet() {
99
		return property.getSet(source);
100
	}
101
102
	public Object getElementType() {
103
		return property.getElementType();
104
	}
105
106
	// Queries
107
108
	protected int doGetSize() {
109
		return property.size(source);
110
	}
111
112
	// Single change operations
113
114
	public boolean add(Object o) {
115
		checkRealm();
116
117
		boolean changed;
118
119
		boolean wasUpdating = updating;
120
		updating = true;
121
		try {
122
			changed = property.add(source, o);
123
			modCount++;
124
		} finally {
125
			updating = wasUpdating;
126
		}
127
128
		cachedSet = property.getSet(source);
129
130
		if (changed)
131
			fireSetChange(Diffs.createSetDiff(Collections.singleton(o),
132
					Collections.EMPTY_SET));
133
134
		return changed;
135
	}
136
137
	public Iterator iterator() {
138
		getterCalled();
139
		return new Iterator() {
140
			int expectedModCount = modCount;
141
			Iterator delegate = new HashSet(property.getSet(source)).iterator();
142
			Object last = null;
143
144
			public boolean hasNext() {
145
				getterCalled();
146
				checkForComodification();
147
				return delegate.hasNext();
148
			}
149
150
			public Object next() {
151
				getterCalled();
152
				checkForComodification();
153
				last = delegate.next();
154
				return last;
155
			}
156
157
			public void remove() {
158
				checkRealm();
159
				checkForComodification();
160
161
				delegate.remove(); // stay in sync
162
163
				boolean wasUpdating = updating;
164
				updating = true;
165
				try {
166
					property.remove(source, last);
167
					modCount++;
168
				} finally {
169
					updating = wasUpdating;
170
				}
171
172
				cachedSet = property.getSet(source);
173
174
				fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET,
175
						Collections.singleton(last)));
176
177
				last = null;
178
				expectedModCount = modCount;
179
			}
180
181
			private void checkForComodification() {
182
				if (expectedModCount != modCount)
183
					throw new ConcurrentModificationException();
184
			}
185
		};
186
	}
187
188
	public boolean remove(Object o) {
189
		getterCalled();
190
191
		boolean changed;
192
193
		boolean wasUpdating = updating;
194
		updating = true;
195
		try {
196
			changed = property.remove(source, o);
197
			modCount++;
198
		} finally {
199
			updating = wasUpdating;
200
		}
201
202
		cachedSet = property.getSet(source);
203
		fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, Collections
204
				.singleton(o)));
205
206
		return changed;
207
	}
208
209
	// Bulk change operations
210
211
	public boolean addAll(Collection c) {
212
		getterCalled();
213
214
		if (c.isEmpty())
215
			return false;
216
217
		Set additions = new HashSet(c);
218
		additions.removeAll(property.getSet(source));
219
		if (additions.isEmpty())
220
			return false;
221
222
		boolean wasUpdating = updating;
223
		updating = true;
224
		try {
225
			property.addAll(source, c);
226
			modCount++;
227
		} finally {
228
			updating = wasUpdating;
229
		}
230
231
		cachedSet = property.getSet(source);
232
		fireSetChange(Diffs.createSetDiff(additions, Collections.EMPTY_SET));
233
234
		return true;
235
	}
236
237
	public boolean removeAll(Collection c) {
238
		getterCalled();
239
240
		if (property.isEmpty(source) || c.isEmpty())
241
			return false;
242
243
		Set removals = new HashSet(c);
244
		removals.retainAll(property.getSet(source));
245
		if (removals.isEmpty())
246
			return false;
247
248
		boolean wasUpdating = updating;
249
		updating = true;
250
		try {
251
			property.removeAll(source, c);
252
			modCount++;
253
		} finally {
254
			updating = wasUpdating;
255
		}
256
257
		cachedSet = property.getSet(source);
258
		fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removals));
259
260
		return true;
261
	}
262
263
	public boolean retainAll(Collection c) {
264
		getterCalled();
265
266
		if (property.isEmpty(source))
267
			return false;
268
269
		if (c.isEmpty()) {
270
			clear();
271
			return true;
272
		}
273
274
		Set removals = new HashSet(property.getSet(source));
275
		removals.removeAll(c);
276
		if (removals.isEmpty())
277
			return false;
278
279
		boolean wasUpdating = updating;
280
		updating = true;
281
		try {
282
			property.retainAll(source, c);
283
			modCount++;
284
		} finally {
285
			updating = wasUpdating;
286
		}
287
288
		cachedSet = property.getSet(source);
289
		fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removals));
290
291
		return true;
292
	}
293
294
	public void clear() {
295
		getterCalled();
296
297
		if (property.isEmpty(source))
298
			return;
299
300
		Set removals = new HashSet(property.getSet(source));
301
302
		boolean wasUpdating = updating;
303
		updating = true;
304
		try {
305
			property.clear(source);
306
			modCount++;
307
		} finally {
308
			updating = wasUpdating;
309
		}
310
		
311
		cachedSet = property.getSet(source);
312
		fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removals));
313
	}
314
315
	public boolean equals(Object o) {
316
		getterCalled();
317
		return property.equals(source, o);
318
	}
319
320
	public int hashCode() {
321
		getterCalled();
322
		return property.hashCode(source);
323
	}
324
325
	public Object getObserved() {
326
		return source;
327
	}
328
329
	public IProperty getProperty() {
330
		return property;
331
	}
332
333
	public synchronized void dispose() {
334
		if (!isDisposed()) {
335
			if (listener != null)
336
				property.removeListener(source, listener);
337
			property = null;
338
			source = null;
339
			listener = null;
340
		}
341
		super.dispose();
342
	}
343
}
(-)src/org/eclipse/core/databinding/property/value/MapValuePropertyObservableMap.java (+362 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.value;
13
14
import java.util.AbstractSet;
15
import java.util.Collections;
16
import java.util.HashMap;
17
import java.util.HashSet;
18
import java.util.Iterator;
19
import java.util.Map;
20
import java.util.Set;
21
22
import org.eclipse.core.databinding.observable.Diffs;
23
import org.eclipse.core.databinding.observable.IObserving;
24
import org.eclipse.core.databinding.observable.IStaleListener;
25
import org.eclipse.core.databinding.observable.ObservableTracker;
26
import org.eclipse.core.databinding.observable.StaleEvent;
27
import org.eclipse.core.databinding.observable.map.AbstractObservableMap;
28
import org.eclipse.core.databinding.observable.map.IMapChangeListener;
29
import org.eclipse.core.databinding.observable.map.IObservableMap;
30
import org.eclipse.core.databinding.observable.map.MapChangeEvent;
31
import org.eclipse.core.databinding.observable.map.MapDiff;
32
import org.eclipse.core.databinding.observable.set.IObservableSet;
33
import org.eclipse.core.databinding.observable.set.ISetChangeListener;
34
import org.eclipse.core.databinding.observable.set.SetChangeEvent;
35
import org.eclipse.core.databinding.observable.set.WritableSet;
36
import org.eclipse.core.databinding.property.INativePropertyListener;
37
import org.eclipse.core.internal.databinding.IdentityWrapper;
38
import org.eclipse.core.internal.databinding.Util;
39
40
/**
41
 * @since 1.2
42
 * 
43
 */
44
public class MapValuePropertyObservableMap extends AbstractObservableMap
45
		implements IObserving {
46
	private IObservableMap masterMap;
47
	private SimpleValueProperty detailProperty;
48
49
	private IObservableSet knownMasterValues;
50
	private Map cachedValues;
51
52
	private boolean updating = false;
53
54
	private IMapChangeListener masterListener = new IMapChangeListener() {
55
		public void handleMapChange(final MapChangeEvent event) {
56
			if (!isDisposed()) {
57
				updateKnownValues();
58
				if (!updating)
59
					fireMapChange(convertDiff(event.diff));
60
			}
61
		}
62
63
		private void updateKnownValues() {
64
			Set identityKnownValues = new HashSet();
65
			for (Iterator it = masterMap.values().iterator(); it.hasNext();) {
66
				identityKnownValues.add(new IdentityWrapper(it.next()));
67
			}
68
69
			knownMasterValues.retainAll(identityKnownValues);
70
			knownMasterValues.addAll(identityKnownValues);
71
		}
72
73
		private MapDiff convertDiff(MapDiff diff) {
74
			Map oldValues = new HashMap();
75
			Map newValues = new HashMap();
76
77
			Set addedKeys = diff.getAddedKeys();
78
			for (Iterator it = addedKeys.iterator(); it.hasNext();) {
79
				Object key = it.next();
80
				Object newSource = diff.getNewValue(key);
81
				Object newValue = detailProperty.getValue(newSource);
82
				newValues.put(key, newValue);
83
			}
84
85
			Set removedKeys = diff.getRemovedKeys();
86
			for (Iterator it = removedKeys.iterator(); it.hasNext();) {
87
				Object key = it.next();
88
				Object oldSource = diff.getOldValue(key);
89
				Object oldValue = detailProperty.getValue(oldSource);
90
				oldValues.put(key, oldValue);
91
			}
92
93
			Set changedKeys = new HashSet(diff.getChangedKeys());
94
			for (Iterator it = changedKeys.iterator(); it.hasNext();) {
95
				Object key = it.next();
96
97
				Object oldSource = diff.getOldValue(key);
98
				Object newSource = diff.getNewValue(key);
99
100
				Object oldValue = detailProperty.getValue(oldSource);
101
				Object newValue = detailProperty.getValue(newSource);
102
103
				if (Util.equals(oldValue, newValue)) {
104
					it.remove();
105
				} else {
106
					oldValues.put(key, oldValue);
107
					newValues.put(key, newValue);
108
				}
109
			}
110
111
			return Diffs.createMapDiff(addedKeys, removedKeys, changedKeys,
112
					oldValues, newValues);
113
		}
114
	};
115
116
	private IStaleListener staleListener = new IStaleListener() {
117
		public void handleStale(StaleEvent staleEvent) {
118
			fireStale();
119
		}
120
	};
121
122
	private INativePropertyListener detailListener = detailProperty
123
			.adaptListener(new IValuePropertyChangeListener() {
124
				public void handleValuePropertyChange(
125
						ValuePropertyChangeEvent event) {
126
					Object masterValue = event.getSource();
127
					final Set keys = keysFor(masterValue);
128
129
					final Object oldDetailValue = event.diff.getOldValue();
130
					final Object newDetailValue = event.diff.getNewValue();
131
132
					if (!Util.equals(oldDetailValue, newDetailValue)) {
133
						fireMapChange(new MapDiff() {
134
							public Set getAddedKeys() {
135
								return Collections.EMPTY_SET;
136
							}
137
138
							public Set getChangedKeys() {
139
								return keys;
140
							}
141
142
							public Set getRemovedKeys() {
143
								return Collections.EMPTY_SET;
144
							}
145
146
							public Object getNewValue(Object key) {
147
								return newDetailValue;
148
							}
149
150
							public Object getOldValue(Object key) {
151
								return oldDetailValue;
152
							}
153
						});
154
					}
155
				}
156
157
				private Set keysFor(Object value) {
158
					Set keys = new HashSet();
159
160
					for (Iterator it = masterMap.entrySet().iterator(); it
161
							.hasNext();) {
162
						Map.Entry entry = (Entry) it.next();
163
						if (entry.getValue() == value) {
164
							keys.add(entry.getKey());
165
						}
166
					}
167
168
					return keys;
169
				}
170
			});
171
172
	/**
173
	 * @param map
174
	 * @param valueProperty
175
	 */
176
	public MapValuePropertyObservableMap(IObservableMap map,
177
			SimpleValueProperty valueProperty) {
178
		super(map.getRealm());
179
		this.masterMap = map;
180
		this.detailProperty = valueProperty;
181
	}
182
183
	protected void firstListenerAdded() {
184
		knownMasterValues = new WritableSet(getRealm());
185
		cachedValues = new HashMap();
186
		knownMasterValues.addSetChangeListener(new ISetChangeListener() {
187
			public void handleSetChange(SetChangeEvent event) {
188
				for (Iterator it = event.diff.getRemovals().iterator(); it
189
						.hasNext();) {
190
					IdentityWrapper wrapper = (IdentityWrapper) it.next();
191
					Object key = wrapper.unwrap();
192
					detailProperty.removeListener(key, detailListener);
193
					cachedValues.remove(wrapper);
194
				}
195
				for (Iterator it = event.diff.getAdditions().iterator(); it
196
						.hasNext();) {
197
					IdentityWrapper wrapper = (IdentityWrapper) it.next();
198
					Object key = wrapper.unwrap();
199
					cachedValues.put(wrapper, detailProperty.getValue(key));
200
					detailProperty.addListener(key, detailListener);
201
				}
202
			}
203
		});
204
		for (Iterator it = masterMap.values().iterator(); it.hasNext();) {
205
			knownMasterValues.add(new IdentityWrapper(it.next()));
206
		}
207
208
		masterMap.addMapChangeListener(masterListener);
209
		masterMap.addStaleListener(staleListener);
210
	}
211
212
	protected void lastListenerRemoved() {
213
		masterMap.removeMapChangeListener(masterListener);
214
		masterMap.removeStaleListener(staleListener);
215
		if (knownMasterValues != null) {
216
			knownMasterValues.clear(); // removes attached listeners
217
			knownMasterValues.dispose();
218
			knownMasterValues = null;
219
		}
220
		cachedValues = null;
221
	}
222
223
	protected Object doGet(Object key) {
224
		if (!masterMap.containsKey(key))
225
			return null;
226
		return detailProperty.getValue(masterMap.get(key));
227
	}
228
229
	protected Object doPut(Object key, Object value) {
230
		if (!masterMap.containsKey(key))
231
			return null;
232
		Object source = masterMap.get(key);
233
234
		Object oldValue = detailProperty.getValue(source);
235
236
		updating = true;
237
		try {
238
			detailProperty.setValue(source, value);
239
		} finally {
240
			updating = false;
241
		}
242
243
		Object newValue = detailProperty.getValue(source);
244
245
		if (!Util.equals(oldValue, newValue)) {
246
			fireMapChange(Diffs.createMapDiffSingleChange(key, oldValue,
247
					newValue));
248
		}
249
250
		return oldValue;
251
	}
252
253
	private Set entrySet;
254
255
	public Set entrySet() {
256
		getterCalled();
257
		if (entrySet == null)
258
			entrySet = new EntrySet();
259
		return entrySet;
260
	}
261
262
	class EntrySet extends AbstractSet {
263
		public Iterator iterator() {
264
			return new Iterator() {
265
				Iterator it = masterMap.entrySet().iterator();
266
267
				public boolean hasNext() {
268
					getterCalled();
269
					return it.hasNext();
270
				}
271
272
				public Object next() {
273
					getterCalled();
274
					Map.Entry next = (Map.Entry) it.next();
275
					return new MapEntry(next.getKey());
276
				}
277
278
				public void remove() {
279
					it.remove();
280
				}
281
			};
282
		}
283
284
		public int size() {
285
			return masterMap.size();
286
		}
287
	}
288
289
	class MapEntry implements Map.Entry {
290
		private Object key;
291
292
		MapEntry(Object key) {
293
			this.key = key;
294
		}
295
296
		public Object getKey() {
297
			getterCalled();
298
			return key;
299
		}
300
301
		public Object getValue() {
302
			getterCalled();
303
			return get(key);
304
		}
305
306
		public Object setValue(Object value) {
307
			return put(key, value);
308
		}
309
310
		public boolean equals(Object o) {
311
			getterCalled();
312
			if (o == this)
313
				return true;
314
			if (o == null)
315
				return false;
316
			if (!(o instanceof Map.Entry))
317
				return false;
318
			Map.Entry that = (Map.Entry) o;
319
			return Util.equals(this.getKey(), that.getKey())
320
					&& Util.equals(this.getValue(), that.getValue());
321
		}
322
323
		public int hashCode() {
324
			getterCalled();
325
			Object value = getValue();
326
			return (key == null ? 0 : key.hashCode())
327
					^ (value == null ? 0 : value.hashCode());
328
		}
329
	}
330
331
	public boolean isStale() {
332
		getterCalled();
333
		return masterMap.isStale();
334
	}
335
336
	private void getterCalled() {
337
		ObservableTracker.getterCalled(this);
338
	}
339
340
	public Object getObserved() {
341
		return masterMap;
342
	}
343
344
	public synchronized void dispose() {
345
		if (masterMap != null) {
346
			masterMap.removeMapChangeListener(masterListener);
347
			masterMap = null;
348
		}
349
		if (knownMasterValues != null) {
350
			knownMasterValues.clear(); // detaches listeners
351
			knownMasterValues.dispose();
352
			knownMasterValues = null;
353
		}
354
355
		masterListener = null;
356
		detailListener = null;
357
		detailProperty = null;
358
		cachedValues = null;
359
360
		super.dispose();
361
	}
362
}
(-)src/org/eclipse/core/databinding/property/map/MapPropertyChangeEvent.java (+58 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.map;
13
14
import org.eclipse.core.databinding.observable.map.MapDiff;
15
import org.eclipse.core.databinding.property.IPropertyChangeListener;
16
import org.eclipse.core.databinding.property.PropertyChangeEvent;
17
18
/**
19
 * Map change event describing an incremental change of a map property on a
20
 * particular property source.
21
 * 
22
 * @since 1.2
23
 */
24
public class MapPropertyChangeEvent extends PropertyChangeEvent {
25
	private static final long serialVersionUID = 1L;
26
27
	/**
28
	 * The map property that changed
29
	 */
30
	public final IMapProperty property;
31
32
	/**
33
	 * MapDiff enumerating the added, changed, and removed entries in the map,
34
	 * or null if the change is unknown.
35
	 */
36
	public final MapDiff diff;
37
38
	/**
39
	 * Constructs a MapPropertyChangeEvent with the given attributes
40
	 * 
41
	 * @param source
42
	 *            the property source
43
	 * @param property
44
	 *            the property that changed on the source
45
	 * @param diff
46
	 *            a MapDiff describing the changes to the map property
47
	 */
48
	public MapPropertyChangeEvent(Object source, IMapProperty property,
49
			MapDiff diff) {
50
		super(source);
51
		this.property = property;
52
		this.diff = diff;
53
	}
54
55
	protected void dispatch(IPropertyChangeListener listener) {
56
		((IMapPropertyChangeListener) listener).handleMapPropertyChange(this);
57
	}
58
}
(-)src/org/eclipse/core/databinding/property/value/SetValuePropertyObservableMap.java (+128 lines)
Added Link Here
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 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.value;
13
14
import java.util.HashMap;
15
import java.util.Iterator;
16
import java.util.Map;
17
18
import org.eclipse.core.databinding.observable.Diffs;
19
import org.eclipse.core.databinding.observable.IObserving;
20
import org.eclipse.core.databinding.observable.map.ComputedObservableMap;
21
import org.eclipse.core.databinding.observable.set.IObservableSet;
22
import org.eclipse.core.databinding.observable.value.ValueDiff;
23
import org.eclipse.core.databinding.property.INativePropertyListener;
24
25
/**
26
 * @since 1.2
27
 */
28
public class SetValuePropertyObservableMap extends ComputedObservableMap
29
		implements IObserving {
30
	private SimpleValueProperty detailProperty;
31
32
	private INativePropertyListener listener;
33
34
	private Map cachedValues;
35
36
	/**
37
	 * @param keySet
38
	 * @param valueProperty
39
	 */
40
	public SetValuePropertyObservableMap(IObservableSet keySet,
41
			SimpleValueProperty valueProperty) {
42
		super(keySet);
43
		this.detailProperty = valueProperty;
44
	}
45
46
	protected void firstListenerAdded() {
47
		if (listener == null) {
48
			cachedValues = new HashMap(this);
49
50
			listener = detailProperty
51
					.adaptListener(new IValuePropertyChangeListener() {
52
						public void handleValuePropertyChange(
53
								final ValuePropertyChangeEvent event) {
54
							if (!isDisposed()) {
55
								getRealm().exec(new Runnable() {
56
									public void run() {
57
										Object key = event.getSource();
58
										Object oldValue;
59
										Object newValue;
60
61
										ValueDiff diff = event.diff;
62
										if (diff == null) {
63
											oldValue = cachedValues.get(key);
64
											newValue = detailProperty
65
													.getValue(key);
66
										} else {
67
											oldValue = event.diff.getOldValue();
68
											newValue = event.diff.getNewValue();
69
										}
70
71
										cachedValues.put(key, newValue);
72
										fireMapChange(Diffs
73
												.createMapDiffSingleChange(key,
74
														oldValue, newValue));
75
									}
76
								});
77
							}
78
						}
79
					});
80
		}
81
		super.firstListenerAdded();
82
	}
83
84
	protected void lastListenerRemoved() {
85
		super.lastListenerRemoved();
86
	}
87
88
	protected void hookListener(Object addedKey) {
89
		if (listener != null) {
90
			detailProperty.addListener(addedKey, listener);
91
		}
92
	}
93
94
	protected void unhookListener(Object removedKey) {
95
		if (listener != null) {
96
			detailProperty.removeListener(removedKey, listener);
97
		}
98
	}
99
100
	protected Object doGet(Object key) {
101
		return detailProperty.getValue(key);
102
	}
103
104
	protected Object doPut(Object key, Object value) {
105
		Object result = detailProperty.getValue(key);
106
		detailProperty.setValue(key, value);
107
		cachedValues.put(key, value);
108
		return result;
109
	}
110
111
	public Object getObserved() {
112
		return keySet();
113
	}
114
115
	public synchronized void dispose() {
116
		if (!isDisposed()) {
117
			if (listener != null) {
118
				for (Iterator it = values().iterator(); it.hasNext();) {
119
					unhookListener(it.next());
120
				}
121
				listener = null;
122
			}
123
			detailProperty = null;
124
		}
125
126
		super.dispose();
127
	}
128
}
(-)src/org/eclipse/jface/tests/databinding/swt/SWTObservablesTest.java (-62 / +124 lines)
Lines 7-45 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Chris Aniszczyk <zx@code9.com> - bug 131435
10
 *     Matthew Hall - bug 248621
11
 *     Matthew Hall - bug 248621
11
 ******************************************************************************/
12
 ******************************************************************************/
12
13
13
package org.eclipse.jface.tests.databinding.swt;
14
package org.eclipse.jface.tests.databinding.swt;
14
15
16
import org.eclipse.core.databinding.observable.IDecoratingObservable;
15
import org.eclipse.core.databinding.observable.list.IObservableList;
17
import org.eclipse.core.databinding.observable.list.IObservableList;
18
import org.eclipse.core.databinding.property.IPropertyObservable;
19
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
16
import org.eclipse.jface.databinding.conformance.util.RealmTester;
20
import org.eclipse.jface.databinding.conformance.util.RealmTester;
21
import org.eclipse.jface.databinding.swt.ISWTObservable;
17
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
22
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
18
import org.eclipse.jface.databinding.swt.SWTObservables;
23
import org.eclipse.jface.databinding.swt.SWTObservables;
19
import org.eclipse.jface.internal.databinding.swt.ButtonObservableValue;
24
import org.eclipse.jface.internal.databinding.swt.CComboSelectionProperty;
20
import org.eclipse.jface.internal.databinding.swt.CComboObservableList;
25
import org.eclipse.jface.internal.databinding.swt.CComboTextProperty;
21
import org.eclipse.jface.internal.databinding.swt.CComboObservableValue;
26
import org.eclipse.jface.internal.databinding.swt.CLabelTextProperty;
22
import org.eclipse.jface.internal.databinding.swt.CLabelObservableValue;
27
import org.eclipse.jface.internal.databinding.swt.CTabItemTooltipTextProperty;
23
import org.eclipse.jface.internal.databinding.swt.ComboObservableList;
28
import org.eclipse.jface.internal.databinding.swt.ComboSelectionProperty;
24
import org.eclipse.jface.internal.databinding.swt.ComboObservableValue;
29
import org.eclipse.jface.internal.databinding.swt.ComboTextProperty;
25
import org.eclipse.jface.internal.databinding.swt.ControlObservableValue;
30
import org.eclipse.jface.internal.databinding.swt.ControlTooltipTextProperty;
26
import org.eclipse.jface.internal.databinding.swt.ItemObservableValue;
31
import org.eclipse.jface.internal.databinding.swt.ItemTextProperty;
27
import org.eclipse.jface.internal.databinding.swt.ItemTooltipObservableValue;
32
import org.eclipse.jface.internal.databinding.swt.LabelTextProperty;
28
import org.eclipse.jface.internal.databinding.swt.LabelObservableValue;
33
import org.eclipse.jface.internal.databinding.swt.ScaleMaximumProperty;
29
import org.eclipse.jface.internal.databinding.swt.ListObservableList;
34
import org.eclipse.jface.internal.databinding.swt.ScaleMinimumProperty;
30
import org.eclipse.jface.internal.databinding.swt.ListObservableValue;
35
import org.eclipse.jface.internal.databinding.swt.ScaleSelectionProperty;
31
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
36
import org.eclipse.jface.internal.databinding.swt.SpinnerMaximumProperty;
32
import org.eclipse.jface.internal.databinding.swt.ScaleObservableValue;
37
import org.eclipse.jface.internal.databinding.swt.SpinnerMinimumProperty;
33
import org.eclipse.jface.internal.databinding.swt.SpinnerObservableValue;
38
import org.eclipse.jface.internal.databinding.swt.SpinnerSelectionProperty;
34
import org.eclipse.jface.internal.databinding.swt.TableSingleSelectionObservableValue;
39
import org.eclipse.jface.internal.databinding.swt.StyledTextTextProperty;
35
import org.eclipse.jface.internal.databinding.swt.TextEditableObservableValue;
40
import org.eclipse.jface.internal.databinding.swt.TableSingleSelectionIndexProperty;
36
import org.eclipse.jface.internal.databinding.swt.TextObservableValue;
41
import org.eclipse.jface.internal.databinding.swt.TextEditableProperty;
42
import org.eclipse.jface.internal.databinding.swt.TextTextProperty;
37
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
43
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
38
import org.eclipse.swt.SWT;
44
import org.eclipse.swt.SWT;
39
import org.eclipse.swt.custom.CCombo;
45
import org.eclipse.swt.custom.CCombo;
40
import org.eclipse.swt.custom.CLabel;
46
import org.eclipse.swt.custom.CLabel;
41
import org.eclipse.swt.custom.CTabFolder;
47
import org.eclipse.swt.custom.CTabFolder;
42
import org.eclipse.swt.custom.CTabItem;
48
import org.eclipse.swt.custom.CTabItem;
49
import org.eclipse.swt.custom.StyledText;
43
import org.eclipse.swt.graphics.Color;
50
import org.eclipse.swt.graphics.Color;
44
import org.eclipse.swt.graphics.Font;
51
import org.eclipse.swt.graphics.Font;
45
import org.eclipse.swt.widgets.Button;
52
import org.eclipse.swt.widgets.Button;
Lines 96-149 Link Here
96
		Spinner spinner = new Spinner(shell, SWT.NONE);
103
		Spinner spinner = new Spinner(shell, SWT.NONE);
97
		ISWTObservableValue value = SWTObservables.observeSelection(spinner);
104
		ISWTObservableValue value = SWTObservables.observeSelection(spinner);
98
		assertNotNull(value);
105
		assertNotNull(value);
99
		assertTrue(value instanceof SpinnerObservableValue);
106
		assertTrue(value.getWidget() == spinner);
100
107
101
		SpinnerObservableValue spinnerObservable = (SpinnerObservableValue) value;
108
		IPropertyObservable propertyObservable = getPropertyObservable(value);
102
		assertEquals(SWTProperties.SELECTION, spinnerObservable.getAttribute());
109
		assertTrue(propertyObservable.getProperty() instanceof SpinnerSelectionProperty);
103
	}
110
	}
104
111
105
	public void testObserveSelectionOfButton() throws Exception {
112
	public void testObserveSelectionOfButton() throws Exception {
106
		Button button = new Button(shell, SWT.PUSH);
113
		Button button = new Button(shell, SWT.PUSH);
107
		ISWTObservableValue value = SWTObservables.observeSelection(button);
114
		ISWTObservableValue value = SWTObservables.observeSelection(button);
108
		assertNotNull(value);
115
		assertNotNull(value);
109
		assertTrue(value instanceof ButtonObservableValue);
116
		assertTrue(value.getWidget() == button);
110
	}
117
	}
111
118
112
	public void testObserveSelectionOfCombo() throws Exception {
119
	public void testObserveSelectionOfCombo() throws Exception {
113
		Combo combo = new Combo(shell, SWT.NONE);
120
		Combo combo = new Combo(shell, SWT.NONE);
114
		ISWTObservableValue value = SWTObservables.observeSelection(combo);
121
		ISWTObservableValue value = SWTObservables.observeSelection(combo);
115
		assertNotNull(value);
122
		assertNotNull(value);
116
		assertTrue(value instanceof ComboObservableValue);
123
		assertTrue(value.getWidget() == combo);
117
124
118
		ComboObservableValue comboObservable = (ComboObservableValue) value;
125
		IPropertyObservable propertyObservable = getPropertyObservable(value);
119
		assertEquals(SWTProperties.SELECTION, comboObservable.getAttribute());
126
		assertTrue(propertyObservable.getProperty() instanceof ComboSelectionProperty);
120
	}
127
	}
121
128
122
	public void testObserveSelectionOfCCombo() throws Exception {
129
	public void testObserveSelectionOfCCombo() throws Exception {
123
		CCombo combo = new CCombo(shell, SWT.NONE);
130
		CCombo combo = new CCombo(shell, SWT.NONE);
124
		ISWTObservableValue value = SWTObservables.observeSelection(combo);
131
		ISWTObservableValue value = SWTObservables.observeSelection(combo);
125
		assertNotNull(value);
132
		assertNotNull(value);
126
		assertTrue(value instanceof CComboObservableValue);
133
		assertTrue(value.getWidget() == combo);
127
134
128
		CComboObservableValue ccomboObservable = (CComboObservableValue) value;
135
		IPropertyObservable property = getPropertyObservable(value);
129
		assertEquals(SWTProperties.SELECTION, ccomboObservable.getAttribute());
136
		assertTrue(property.getProperty() instanceof CComboSelectionProperty);
130
	}
137
	}
131
138
132
	public void testObserveSelectionOfList() throws Exception {
139
	public void testObserveSelectionOfList() throws Exception {
133
		List list = new List(shell, SWT.NONE);
140
		List list = new List(shell, SWT.NONE);
134
		ISWTObservableValue value = SWTObservables.observeSelection(list);
141
		ISWTObservableValue value = SWTObservables.observeSelection(list);
135
		assertNotNull(value);
142
		assertNotNull(value);
136
		assertTrue(value instanceof ListObservableValue);
143
		assertTrue(value.getWidget() == list);
137
	}
144
	}
138
	
145
	
139
	public void testObserveSelectionOfScale() throws Exception {
146
	public void testObserveSelectionOfScale() throws Exception {
140
		Scale scale = new Scale(shell, SWT.NONE);
147
		Scale scale = new Scale(shell, SWT.NONE);
141
		ISWTObservableValue value = SWTObservables.observeSelection(scale);
148
		ISWTObservableValue value = SWTObservables.observeSelection(scale);
142
		assertNotNull(value);
149
		assertNotNull(value);
143
		assertTrue(value instanceof ScaleObservableValue);
150
		assertTrue(value.getWidget() == scale);
144
		
151
		
145
		ScaleObservableValue scaleObservable = (ScaleObservableValue) value;
152
		IPropertyObservable property = getPropertyObservable(value);
146
		assertEquals(SWTProperties.SELECTION, scaleObservable.getAttribute());
153
		assertTrue(property.getProperty() instanceof ScaleSelectionProperty);
147
	}
154
	}
148
155
149
	public void testObserveSelectionOfUnsupportedControl() throws Exception {
156
	public void testObserveSelectionOfUnsupportedControl() throws Exception {
Lines 162-168 Link Here
162
		ISWTObservableValue value = SWTObservables.observeText(text,
169
		ISWTObservableValue value = SWTObservables.observeText(text,
163
				SWT.FocusOut);
170
				SWT.FocusOut);
164
		assertNotNull(value);
171
		assertNotNull(value);
165
		assertTrue(value instanceof TextObservableValue);
172
		assertTrue(value.getWidget() == text);
173
		IPropertyObservable propertyObservable = getPropertyObservable(value);
174
		assertTrue(propertyObservable.getProperty() instanceof TextTextProperty);
175
176
		assertFalse(text.isListening(SWT.FocusOut));
177
		ChangeEventTracker.observe(value);
178
		assertTrue(text.isListening(SWT.FocusOut));
179
	}
180
181
	public void testObserveTextOfStyledText() throws Exception {
182
		StyledText text = new StyledText(shell, SWT.NONE);
183
		assertFalse(text.isListening(SWT.FocusOut));
184
185
		ISWTObservableValue value = SWTObservables.observeText(text,
186
				SWT.FocusOut);
187
		assertNotNull(value);
188
		assertTrue(value.getWidget() == text);
189
		IPropertyObservable propertyObservable = getPropertyObservable(value);
190
		assertTrue(propertyObservable.getProperty() instanceof StyledTextTextProperty);
191
192
		assertFalse(text.isListening(SWT.FocusOut));
193
		ChangeEventTracker.observe(value);
166
		assertTrue(text.isListening(SWT.FocusOut));
194
		assertTrue(text.isListening(SWT.FocusOut));
167
	}
195
	}
168
196
Lines 179-219 Link Here
179
		Label label = new Label(shell, SWT.NONE);
207
		Label label = new Label(shell, SWT.NONE);
180
		ISWTObservableValue value = SWTObservables.observeText(label);
208
		ISWTObservableValue value = SWTObservables.observeText(label);
181
		assertNotNull(label);
209
		assertNotNull(label);
182
		assertTrue(value instanceof LabelObservableValue);
210
		assertTrue(value.getWidget() == label);
211
		IPropertyObservable propertyObservable = getPropertyObservable(value);
212
		assertTrue(propertyObservable.getProperty() instanceof LabelTextProperty);
183
	}
213
	}
184
214
185
	public void testObserveTextOfCLabel() throws Exception {
215
	public void testObserveTextOfCLabel() throws Exception {
186
		CLabel label = new CLabel(shell, SWT.NONE);
216
		CLabel label = new CLabel(shell, SWT.NONE);
187
		ISWTObservableValue value = SWTObservables.observeText(label);
217
		ISWTObservableValue value = SWTObservables.observeText(label);
188
		assertNotNull(label);
218
		assertNotNull(label);
189
		assertTrue(value instanceof CLabelObservableValue);
219
		assertTrue(value.getWidget() == label);
220
		IPropertyObservable propertyObservable = getPropertyObservable(value);
221
		assertTrue(propertyObservable.getProperty() instanceof CLabelTextProperty);
190
	}
222
	}
191
223
192
	public void testObserveTextOfCombo() throws Exception {
224
	public void testObserveTextOfCombo() throws Exception {
193
		Combo combo = new Combo(shell, SWT.NONE);
225
		Combo combo = new Combo(shell, SWT.NONE);
194
		ISWTObservableValue value = SWTObservables.observeText(combo);
226
		ISWTObservableValue value = SWTObservables.observeText(combo);
195
		assertNotNull(value);
227
		assertNotNull(value);
196
		assertTrue(value instanceof ComboObservableValue);
228
		assertTrue(value.getWidget() == combo);
197
229
198
		ComboObservableValue comboObservable = (ComboObservableValue) value;
230
		assertTrue(getPropertyObservable(value).getProperty() instanceof ComboTextProperty);
199
		assertEquals(SWTProperties.TEXT, comboObservable.getAttribute());
231
	}
232
233
	/**
234
	 * @param observable
235
	 * @return
236
	 */
237
	private IPropertyObservable getPropertyObservable(
238
			ISWTObservableValue observable) {
239
		IDecoratingObservable decoratingObservable = (IDecoratingObservable) observable;
240
		IPropertyObservable propertyObservable = (IPropertyObservable) decoratingObservable
241
				.getDecorated();
242
		return propertyObservable;
200
	}
243
	}
201
244
202
	public void testObserveTextOfCCombo() throws Exception {
245
	public void testObserveTextOfCCombo() throws Exception {
203
		CCombo combo = new CCombo(shell, SWT.NONE);
246
		CCombo combo = new CCombo(shell, SWT.NONE);
204
		ISWTObservableValue value = SWTObservables.observeText(combo);
247
		ISWTObservableValue value = SWTObservables.observeText(combo);
205
		assertNotNull(value);
248
		assertNotNull(value);
206
		assertTrue(value instanceof CComboObservableValue);
249
		assertTrue(value.getWidget() == combo);
207
250
208
		CComboObservableValue ccomboObservable = (CComboObservableValue) value;
251
		IDecoratingObservable decorating = (IDecoratingObservable) value;
209
		assertEquals(SWTProperties.TEXT, ccomboObservable.getAttribute());
252
		IPropertyObservable property = (IPropertyObservable) decorating
253
				.getDecorated();
254
		assertTrue(property.getProperty() instanceof CComboTextProperty);
210
	}
255
	}
211
256
212
	public void testObserveTextOfText() throws Exception {
257
	public void testObserveTextOfText() throws Exception {
213
		Text text = new Text(shell, SWT.NONE);
258
		Text text = new Text(shell, SWT.NONE);
214
		ISWTObservableValue value = SWTObservables.observeText(text);
259
		ISWTObservableValue value = SWTObservables.observeText(text);
215
		assertNotNull(value);
260
		assertNotNull(value);
216
		assertTrue(value instanceof TextObservableValue);
261
262
		assertTrue(value.getWidget() == text);
263
		IPropertyObservable propertyObservable = getPropertyObservable(value);
264
		assertTrue(propertyObservable.getProperty() instanceof TextTextProperty);
265
		
217
		assertFalse(text.isListening(SWT.Modify));
266
		assertFalse(text.isListening(SWT.Modify));
218
		assertFalse(text.isListening(SWT.FocusOut));
267
		assertFalse(text.isListening(SWT.FocusOut));
219
	}
268
	}
Lines 223-229 Link Here
223
		Item item = new CTabItem(ctf, SWT.NONE);
272
		Item item = new CTabItem(ctf, SWT.NONE);
224
		ISWTObservableValue value = SWTObservables.observeText(item);
273
		ISWTObservableValue value = SWTObservables.observeText(item);
225
		assertNotNull(value);
274
		assertNotNull(value);
226
		assertTrue(value instanceof ItemObservableValue);
275
		assertTrue(value.getWidget() == item);
276
		IPropertyObservable propertyObservable = getPropertyObservable(value);
277
		assertTrue(propertyObservable.getProperty() instanceof ItemTextProperty);
227
	}
278
	}
228
279
229
	public void testObserveTextOfUnsupportedControl() throws Exception {
280
	public void testObserveTextOfUnsupportedControl() throws Exception {
Lines 240-246 Link Here
240
		Item item = new CTabItem(ctf, SWT.NONE);
291
		Item item = new CTabItem(ctf, SWT.NONE);
241
		ISWTObservableValue value = SWTObservables.observeTooltipText(item);
292
		ISWTObservableValue value = SWTObservables.observeTooltipText(item);
242
		assertNotNull(value);
293
		assertNotNull(value);
243
		assertTrue(value instanceof ItemTooltipObservableValue);
294
		assertTrue(value.getWidget() == item);
295
		IPropertyObservable propertyObservable = getPropertyObservable(value);
296
		assertTrue(propertyObservable.getProperty() instanceof CTabItemTooltipTextProperty);
244
	}
297
	}
245
298
246
	public void testObserveTooltipOfUnsupportedControl() throws Exception {
299
	public void testObserveTooltipOfUnsupportedControl() throws Exception {
Lines 256-283 Link Here
256
		Label label = new Label(shell, SWT.NONE);
309
		Label label = new Label(shell, SWT.NONE);
257
		ISWTObservableValue value = SWTObservables.observeTooltipText(label);
310
		ISWTObservableValue value = SWTObservables.observeTooltipText(label);
258
		assertNotNull(value);
311
		assertNotNull(value);
259
		assertTrue(value instanceof ControlObservableValue);
312
		assertTrue(value.getWidget() == label);
313
		IPropertyObservable propertyObservable = getPropertyObservable(value);
314
		assertTrue(propertyObservable.getProperty() instanceof ControlTooltipTextProperty);
260
	}
315
	}
261
316
262
	public void testObserveItemsOfCombo() throws Exception {
317
	public void testObserveItemsOfCombo() throws Exception {
263
		Combo combo = new Combo(shell, SWT.NONE);
318
		Combo combo = new Combo(shell, SWT.NONE);
264
		IObservableList list = SWTObservables.observeItems(combo);
319
		IObservableList list = SWTObservables.observeItems(combo);
265
		assertNotNull(list);
320
		assertNotNull(list);
266
		assertTrue(list instanceof ComboObservableList);
321
		assertTrue(list instanceof ISWTObservable);
322
		assertTrue(((ISWTObservable) list).getWidget() == combo);
267
	}
323
	}
268
324
269
	public void testObserveItemsOfCCombo() throws Exception {
325
	public void testObserveItemsOfCCombo() throws Exception {
270
		CCombo ccombo = new CCombo(shell, SWT.NONE);
326
		CCombo ccombo = new CCombo(shell, SWT.NONE);
271
		IObservableList list = SWTObservables.observeItems(ccombo);
327
		IObservableList list = SWTObservables.observeItems(ccombo);
272
		assertNotNull(list);
328
		assertNotNull(list);
273
		assertTrue(list instanceof CComboObservableList);
329
		ISWTObservable swtObservable = (ISWTObservable) list;
330
		assertTrue(swtObservable.getWidget() == ccombo);
274
	}
331
	}
275
332
276
	public void testObserveItemsOfList() throws Exception {
333
	public void testObserveItemsOfList() throws Exception {
277
		List list = new List(shell, SWT.NONE);
334
		List list = new List(shell, SWT.NONE);
278
		IObservableList observableList = SWTObservables.observeItems(list);
335
		IObservableList observableList = SWTObservables.observeItems(list);
279
		assertNotNull(observableList);
336
		assertNotNull(observableList);
280
		assertTrue(observableList instanceof ListObservableList);
337
		ISWTObservable swtObservable = (ISWTObservable) observableList;
338
		assertTrue(swtObservable.getWidget() == list);
281
	}
339
	}
282
340
283
	public void testObserveItemsOfUnsupportedControl() throws Exception {
341
	public void testObserveItemsOfUnsupportedControl() throws Exception {
Lines 294-300 Link Here
294
		ISWTObservableValue value = SWTObservables
352
		ISWTObservableValue value = SWTObservables
295
				.observeSingleSelectionIndex(table);
353
				.observeSingleSelectionIndex(table);
296
		assertNotNull(value);
354
		assertNotNull(value);
297
		assertTrue(value instanceof TableSingleSelectionObservableValue);
355
		assertTrue(value.getWidget() == table);
356
		IPropertyObservable propertyObservable = getPropertyObservable(value);
357
		assertTrue(propertyObservable.getProperty() instanceof TableSingleSelectionIndexProperty);
298
	}
358
	}
299
359
300
	public void testObserveSingleSelectionIndexOfUnsupportedControl()
360
	public void testObserveSingleSelectionIndexOfUnsupportedControl()
Lines 312-331 Link Here
312
		Spinner spinner = new Spinner(shell, SWT.NONE);
372
		Spinner spinner = new Spinner(shell, SWT.NONE);
313
		ISWTObservableValue value = SWTObservables.observeMin(spinner);
373
		ISWTObservableValue value = SWTObservables.observeMin(spinner);
314
		assertNotNull(value);
374
		assertNotNull(value);
315
		assertTrue(value instanceof SpinnerObservableValue);
375
		assertTrue(value.getWidget() == spinner);
316
		
376
		
317
		SpinnerObservableValue spinnerObservable = (SpinnerObservableValue) value;
377
		IPropertyObservable propertyObservable = getPropertyObservable(value);
318
		assertEquals(SWTProperties.MIN, spinnerObservable.getAttribute());
378
		assertTrue(propertyObservable.getProperty() instanceof SpinnerMinimumProperty);
319
	}
379
	}
320
	
380
	
321
	public void testObserveMinOfScale() throws Exception {
381
	public void testObserveMinOfScale() throws Exception {
322
		Scale scale = new Scale(shell, SWT.NONE);
382
		Scale scale = new Scale(shell, SWT.NONE);
323
		ISWTObservableValue value = SWTObservables.observeMin(scale);
383
		ISWTObservableValue value = SWTObservables.observeMin(scale);
324
		assertNotNull(value);
384
		assertNotNull(value);
325
		assertTrue(value instanceof ScaleObservableValue);
385
		assertTrue(value.getWidget() == scale);
326
		
386
		
327
		ScaleObservableValue scaleObservable = (ScaleObservableValue) value;
387
		IPropertyObservable propertyObservable = getPropertyObservable(value);
328
		assertEquals(SWTProperties.MIN, scaleObservable.getAttribute());
388
		assertTrue(propertyObservable.getProperty() instanceof ScaleMinimumProperty);
329
	}
389
	}
330
390
331
	public void testObserveMinOfUnsupportedControl() throws Exception {
391
	public void testObserveMinOfUnsupportedControl() throws Exception {
Lines 341-360 Link Here
341
		Spinner spinner = new Spinner(shell, SWT.NONE);
401
		Spinner spinner = new Spinner(shell, SWT.NONE);
342
		ISWTObservableValue value = SWTObservables.observeMax(spinner);
402
		ISWTObservableValue value = SWTObservables.observeMax(spinner);
343
		assertNotNull(value);
403
		assertNotNull(value);
344
		assertTrue(value instanceof SpinnerObservableValue);
404
		assertTrue(value.getWidget() == spinner);
345
		
405
		
346
		SpinnerObservableValue spinnerObservable = (SpinnerObservableValue) value;
406
		IPropertyObservable propertyObservable = getPropertyObservable(value);
347
		assertEquals(SWTProperties.MAX, spinnerObservable.getAttribute());
407
		assertTrue(propertyObservable.getProperty() instanceof SpinnerMaximumProperty);
348
	}
408
	}
349
	
409
	
350
	public void testObserveMaxOfScale() throws Exception {
410
	public void testObserveMaxOfScale() throws Exception {
351
		Scale scale = new Scale(shell, SWT.NONE);
411
		Scale scale = new Scale(shell, SWT.NONE);
352
		ISWTObservableValue value = SWTObservables.observeMax(scale);
412
		ISWTObservableValue value = SWTObservables.observeMax(scale);
353
		assertNotNull(value);
413
		assertNotNull(value);
354
		assertTrue(value instanceof ScaleObservableValue);
414
		assertTrue(value.getWidget() == scale);
355
		
415
		
356
		ScaleObservableValue scaleObservable = (ScaleObservableValue) value;
416
		IPropertyObservable propertyObservable = getPropertyObservable(value);
357
		assertEquals(SWTProperties.MAX, scaleObservable.getAttribute());
417
		assertTrue(propertyObservable.getProperty() instanceof ScaleMaximumProperty);
358
	}
418
	}
359
	
419
	
360
	public void testObserveMaxOfUnsupportedControl() throws Exception {
420
	public void testObserveMaxOfUnsupportedControl() throws Exception {
Lines 370-376 Link Here
370
		Text text = new Text(shell, SWT.NONE);
430
		Text text = new Text(shell, SWT.NONE);
371
		ISWTObservableValue value = SWTObservables.observeEditable(text);
431
		ISWTObservableValue value = SWTObservables.observeEditable(text);
372
		assertNotNull(value);
432
		assertNotNull(value);
373
		assertTrue(value instanceof TextEditableObservableValue);
433
		assertTrue(value.getWidget() == text);
434
		IPropertyObservable propertyObservable = getPropertyObservable(value);
435
		assertTrue(propertyObservable.getProperty() instanceof TextEditableProperty);
374
	}
436
	}
375
	
437
	
376
	public void testObserveEditableOfUnsupportedControl() throws Exception {
438
	public void testObserveEditableOfUnsupportedControl() throws Exception {
(-)src/org/eclipse/jface/tests/internal/databinding/swt/ScaleObservableValueMinTest.java (-10 / +12 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Matthew Hall - bug 213145
10
 *     Matthew Hall - bug 213145, 194734
11
 *******************************************************************************/
11
 *******************************************************************************/
12
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
13
package org.eclipse.jface.tests.internal.databinding.swt;
Lines 22-29 Link Here
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.swt.SWTObservables;
24
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
25
import org.eclipse.jface.databinding.swt.ScaleProperties;
26
import org.eclipse.jface.internal.databinding.swt.ScaleObservableValue;
27
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.widgets.Display;
27
import org.eclipse.swt.widgets.Display;
29
import org.eclipse.swt.widgets.Scale;
28
import org.eclipse.swt.widgets.Scale;
Lines 42-52 Link Here
42
	public ScaleObservableValueMinTest() {
41
	public ScaleObservableValueMinTest() {
43
		this(null);
42
		this(null);
44
	}
43
	}
45
	
44
46
	public ScaleObservableValueMinTest(String testName) {
45
	public ScaleObservableValueMinTest(String testName) {
47
		super(testName, new Delegate());
46
		super(testName, new Delegate());
48
	}
47
	}
49
	
48
50
	protected void setUp() throws Exception {
49
	protected void setUp() throws Exception {
51
		super.setUp();
50
		super.setUp();
52
51
Lines 56-64 Link Here
56
	}
55
	}
57
56
58
	protected IObservable doCreateObservable() {
57
	protected IObservable doCreateObservable() {
59
		return getObservableContractDelegate().createObservable(SWTObservables.getRealm(Display.getDefault()));
58
		return getObservableContractDelegate().createObservable(
59
				SWTObservables.getRealm(Display.getDefault()));
60
	}
60
	}
61
	
61
62
	public void testGetValue() throws Exception {
62
	public void testGetValue() throws Exception {
63
		int min = 100;
63
		int min = 100;
64
		scale.setMinimum(min);
64
		scale.setMinimum(min);
Lines 72-80 Link Here
72
	}
72
	}
73
73
74
	public static Test suite() {
74
	public static Test suite() {
75
		TestSuite suite = new TestSuite(ScaleObservableValueMinTest.class.toString());
75
		TestSuite suite = new TestSuite(ScaleObservableValueMinTest.class
76
				.toString());
76
		suite.addTestSuite(ScaleObservableValueMinTest.class);
77
		suite.addTestSuite(ScaleObservableValueMinTest.class);
77
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
78
		suite.addTest(SWTMutableObservableValueContractTest
79
				.suite(new Delegate()));
78
		return suite;
80
		return suite;
79
	}
81
	}
80
82
Lines 95-101 Link Here
95
		}
97
		}
96
98
97
		public IObservableValue createObservableValue(Realm realm) {
99
		public IObservableValue createObservableValue(Realm realm) {
98
			return new ScaleObservableValue(realm, scale, SWTProperties.MIN);
100
			return ScaleProperties.minimum().observeValue(realm, scale);
99
		}
101
		}
100
102
101
		public void change(IObservable observable) {
103
		public void change(IObservable observable) {
(-)src/org/eclipse/jface/tests/internal/databinding/swt/CLabelObservableValueTest.java (-24 / +27 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Matthew Hall - bug 213145
10
 *     Matthew Hall - bug 213145, 194734
11
 ******************************************************************************/
11
 ******************************************************************************/
12
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
13
package org.eclipse.jface.tests.internal.databinding.swt;
Lines 21-28 Link Here
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.swt.CLabelProperties;
24
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.internal.databinding.swt.CLabelObservableValue;
26
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.custom.CLabel;
27
import org.eclipse.swt.custom.CLabel;
28
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.Display;
Lines 36-73 Link Here
36
	private Delegate delegate;
36
	private Delegate delegate;
37
	private IObservableValue observable;
37
	private IObservableValue observable;
38
	private CLabel label;
38
	private CLabel label;
39
	
39
40
	protected void setUp() throws Exception {
40
	protected void setUp() throws Exception {
41
		super.setUp();
41
		super.setUp();
42
		
42
43
		delegate = new Delegate();
43
		delegate = new Delegate();
44
		delegate.setUp();
44
		delegate.setUp();
45
		label = delegate.label;
45
		label = delegate.label;
46
		observable = delegate.createObservableValue(SWTObservables.getRealm(Display.getDefault()));
46
		observable = delegate.createObservableValue(SWTObservables
47
				.getRealm(Display.getDefault()));
47
	}
48
	}
48
	
49
49
	protected void tearDown() throws Exception {
50
	protected void tearDown() throws Exception {
50
		super.tearDown();
51
		super.tearDown();
51
		
52
52
		delegate.tearDown();
53
		delegate.tearDown();
53
		observable.dispose();
54
		observable.dispose();
54
	}
55
	}
55
	
56
56
    public void testSetValue() throws Exception {
57
	public void testSetValue() throws Exception {
57
    	//preconditions
58
		// preconditions
58
        assertEquals(null, label.getText());
59
		assertEquals(null, label.getText());
59
        assertEquals(null, observable.getValue());
60
		assertEquals(null, observable.getValue());
60
        
61
61
        String value = "value";
62
		String value = "value";
62
        observable.setValue(value);
63
		observable.setValue(value);
63
        assertEquals("label text", value, label.getText());
64
		assertEquals("label text", value, label.getText());
64
        assertEquals("observable value", value, observable.getValue());
65
		assertEquals("observable value", value, observable.getValue());
65
    }
66
	}
66
	
67
67
	public static Test suite() {
68
	public static Test suite() {
68
		TestSuite suite = new TestSuite(CLabelObservableValueTest.class.getName());
69
		TestSuite suite = new TestSuite(CLabelObservableValueTest.class
70
				.getName());
69
		suite.addTestSuite(CLabelObservableValueTest.class);
71
		suite.addTestSuite(CLabelObservableValueTest.class);
70
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
72
		suite.addTest(SWTMutableObservableValueContractTest
73
				.suite(new Delegate()));
71
		return suite;
74
		return suite;
72
	}
75
	}
73
76
Lines 87-104 Link Here
87
		}
90
		}
88
91
89
		public IObservableValue createObservableValue(Realm realm) {
92
		public IObservableValue createObservableValue(Realm realm) {
90
			return new CLabelObservableValue(realm, label);
93
			return CLabelProperties.text().observeValue(realm, label);
91
		}
94
		}
92
95
93
		public void change(IObservable observable) {
96
		public void change(IObservable observable) {
94
			IObservableValue value = (IObservableValue) observable;
97
			IObservableValue value = (IObservableValue) observable;
95
			value.setValue(value.getValue() + "a");
98
			value.setValue(value.getValue() + "a");
96
		}
99
		}
97
		
100
98
		public Object getValueType(IObservableValue observable) {
101
		public Object getValueType(IObservableValue observable) {
99
			return String.class;
102
			return String.class;
100
		}
103
		}
101
		
104
102
		public Object createValue(IObservableValue observable) {
105
		public Object createValue(IObservableValue observable) {
103
			return observable.getValue() + "a";
106
			return observable.getValue() + "a";
104
		}
107
		}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/TextObservableValueFocusOutTest.java (-8 / +10 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Matthew Hall - bug 213145
10
 *     Matthew Hall - bug 213145, 194734
11
 *******************************************************************************/
11
 *******************************************************************************/
12
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
13
package org.eclipse.jface.tests.internal.databinding.swt;
Lines 21-27 Link Here
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.internal.databinding.swt.TextObservableValue;
24
import org.eclipse.jface.databinding.swt.TextProperties;
25
import org.eclipse.swt.SWT;
25
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.widgets.Shell;
26
import org.eclipse.swt.widgets.Shell;
27
import org.eclipse.swt.widgets.Text;
27
import org.eclipse.swt.widgets.Text;
Lines 31-38 Link Here
31
 */
31
 */
32
public class TextObservableValueFocusOutTest extends TestCase {
32
public class TextObservableValueFocusOutTest extends TestCase {
33
	public static Test suite() {
33
	public static Test suite() {
34
		TestSuite suite = new TestSuite(TextObservableValueFocusOutTest.class.toString());
34
		TestSuite suite = new TestSuite(TextObservableValueFocusOutTest.class
35
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
35
				.toString());
36
		suite.addTest(SWTMutableObservableValueContractTest
37
				.suite(new Delegate()));
36
		return suite;
38
		return suite;
37
	}
39
	}
38
40
Lines 41-47 Link Here
41
		private Shell shell;
43
		private Shell shell;
42
44
43
		private Text text;
45
		private Text text;
44
		
46
45
		public void setUp() {
47
		public void setUp() {
46
			shell = new Shell();
48
			shell = new Shell();
47
			text = new Text(shell, SWT.NONE);
49
			text = new Text(shell, SWT.NONE);
Lines 52-58 Link Here
52
		}
54
		}
53
55
54
		public IObservableValue createObservableValue(Realm realm) {
56
		public IObservableValue createObservableValue(Realm realm) {
55
			return new TextObservableValue(realm, text, SWT.FocusOut);
57
			return TextProperties.text(SWT.FocusOut).observeValue(realm, text);
56
		}
58
		}
57
59
58
		public Object getValueType(IObservableValue observable) {
60
		public Object getValueType(IObservableValue observable) {
Lines 61-70 Link Here
61
63
62
		public void change(IObservable observable) {
64
		public void change(IObservable observable) {
63
			text.setFocus();
65
			text.setFocus();
64
			
66
65
			IObservableValue observableValue = (IObservableValue) observable;
67
			IObservableValue observableValue = (IObservableValue) observable;
66
			text.setText((String) createValue(observableValue));
68
			text.setText((String) createValue(observableValue));
67
			
69
68
			text.notifyListeners(SWT.FocusOut, null);
70
			text.notifyListeners(SWT.FocusOut, null);
69
		}
71
		}
70
72
(-)src/org/eclipse/jface/tests/internal/databinding/swt/TextEditableObservableValueTest.java (-28 / +34 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Matthew Hall - bug 213145
10
 *     Matthew Hall - bug 213145, 194734
11
 *******************************************************************************/
11
 *******************************************************************************/
12
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
13
package org.eclipse.jface.tests.internal.databinding.swt;
Lines 21-27 Link Here
21
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
21
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.internal.databinding.swt.TextEditableObservableValue;
24
import org.eclipse.jface.databinding.swt.TextProperties;
25
import org.eclipse.swt.SWT;
25
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.widgets.Shell;
26
import org.eclipse.swt.widgets.Shell;
27
import org.eclipse.swt.widgets.Text;
27
import org.eclipse.swt.widgets.Text;
Lines 29-37 Link Here
29
/**
29
/**
30
 * @since 1.1
30
 * @since 1.1
31
 */
31
 */
32
public class TextEditableObservableValueTest extends
32
public class TextEditableObservableValueTest extends ObservableDelegateTest {
33
		ObservableDelegateTest {
33
34
	
35
	private Delegate delegate;
34
	private Delegate delegate;
36
	private Text text;
35
	private Text text;
37
	private IObservableValue observable;
36
	private IObservableValue observable;
Lines 39-116 Link Here
39
	public TextEditableObservableValueTest() {
38
	public TextEditableObservableValueTest() {
40
		this(null);
39
		this(null);
41
	}
40
	}
42
	
41
43
	public TextEditableObservableValueTest(String testName) {
42
	public TextEditableObservableValueTest(String testName) {
44
		super(testName, new Delegate());
43
		super(testName, new Delegate());
45
	}
44
	}
46
45
47
	/* (non-Javadoc)
46
	/*
48
	 * @see org.eclipse.jface.conformance.databinding.ObservableDelegateTest#setUp()
47
	 * (non-Javadoc)
48
	 * 
49
	 * @see
50
	 * org.eclipse.jface.conformance.databinding.ObservableDelegateTest#setUp()
49
	 */
51
	 */
50
	protected void setUp() throws Exception {
52
	protected void setUp() throws Exception {
51
		super.setUp();
53
		super.setUp();
52
		
54
53
		delegate = (Delegate) getObservableContractDelegate();
55
		delegate = (Delegate) getObservableContractDelegate();
54
		observable = (IObservableValue) getObservable();
56
		observable = (IObservableValue) getObservable();
55
		text = delegate.text;
57
		text = delegate.text;
56
	}
58
	}
57
	
59
58
	protected IObservable doCreateObservable() {
60
	protected IObservable doCreateObservable() {
59
		return super.doCreateObservable();
61
		return super.doCreateObservable();
60
	}
62
	}
61
	
63
62
	public void testGetValue() throws Exception {
64
	public void testGetValue() throws Exception {
63
		text.setEditable(false);
65
		text.setEditable(false);
64
		assertEquals(Boolean.valueOf(text.getEditable()), observable.getValue());
66
		assertEquals(Boolean.valueOf(text.getEditable()), observable.getValue());
65
		
67
66
		text.setEditable(true);
68
		text.setEditable(true);
67
		assertEquals(Boolean.valueOf(text.getEditable()), observable.getValue());
69
		assertEquals(Boolean.valueOf(text.getEditable()), observable.getValue());
68
	}
70
	}
69
	
71
70
	public void testSetValue() throws Exception {
72
	public void testSetValue() throws Exception {
71
		text.setEditable(false);
73
		text.setEditable(false);
72
		observable.setValue(Boolean.TRUE);
74
		observable.setValue(Boolean.TRUE);
73
		assertEquals(Boolean.TRUE, Boolean.valueOf(text.getEditable()));
75
		assertEquals(Boolean.TRUE, Boolean.valueOf(text.getEditable()));
74
		
76
75
		observable.setValue(Boolean.FALSE);
77
		observable.setValue(Boolean.FALSE);
76
		assertEquals(Boolean.FALSE, Boolean.valueOf(text.getEditable()));
78
		assertEquals(Boolean.FALSE, Boolean.valueOf(text.getEditable()));
77
	}
79
	}
78
	
80
79
	public static Test suite() {
81
	public static Test suite() {
80
		TestSuite suite = new TestSuite(TextEditableObservableValueTest.class.toString());
82
		TestSuite suite = new TestSuite(TextEditableObservableValueTest.class
83
				.toString());
81
		suite.addTestSuite(TextEditableObservableValueTest.class);
84
		suite.addTestSuite(TextEditableObservableValueTest.class);
82
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
85
		suite.addTest(SWTMutableObservableValueContractTest
86
				.suite(new Delegate()));
83
		return suite;
87
		return suite;
84
	}
88
	}
85
	
89
86
	/*package*/ static class Delegate extends AbstractObservableValueContractDelegate {
90
	/* package */static class Delegate extends
91
			AbstractObservableValueContractDelegate {
87
		private Shell shell;
92
		private Shell shell;
88
		Text text;
93
		Text text;
89
		
94
90
		public void setUp() {			
95
		public void setUp() {
91
			shell = new Shell();
96
			shell = new Shell();
92
			text = new Text(shell, SWT.NONE);
97
			text = new Text(shell, SWT.NONE);
93
		}
98
		}
94
99
95
		public void tearDown() {			
100
		public void tearDown() {
96
			shell.dispose();
101
			shell.dispose();
97
		}
102
		}
98
		
103
99
		public IObservableValue createObservableValue(Realm realm) {
104
		public IObservableValue createObservableValue(Realm realm) {
100
			return new TextEditableObservableValue(realm, text);
105
			return TextProperties.editable().observeValue(realm, text);
101
		}
106
		}
102
		
107
103
		public Object getValueType(IObservableValue observable) {
108
		public Object getValueType(IObservableValue observable) {
104
			return Boolean.TYPE;
109
			return Boolean.TYPE;
105
		}
110
		}
106
		
111
107
		public void change(IObservable observable) {
112
		public void change(IObservable observable) {
108
			IObservableValue observableValue = (IObservableValue) observable;
113
			IObservableValue observableValue = (IObservableValue) observable;
109
			observableValue.setValue(createValue(observableValue));
114
			observableValue.setValue(createValue(observableValue));
110
		}
115
		}
111
		
116
112
		public Object createValue(IObservableValue observable) {
117
		public Object createValue(IObservableValue observable) {
113
			return (Boolean.TRUE.equals(observable.getValue()) ? Boolean.FALSE: Boolean.TRUE);
118
			return (Boolean.TRUE.equals(observable.getValue()) ? Boolean.FALSE
119
					: Boolean.TRUE);
114
		}
120
		}
115
	}
121
	}
116
}
122
}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/LabelObservableValueTest.java (-28 / +31 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Matthew Hall - bug 213145
10
 *     Matthew Hall - bug 213145, 194734
11
 ******************************************************************************/
11
 ******************************************************************************/
12
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
13
package org.eclipse.jface.tests.internal.databinding.swt;
Lines 21-28 Link Here
21
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
21
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.swt.LabelProperties;
24
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.internal.databinding.swt.LabelObservableValue;
26
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.widgets.Display;
27
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.Label;
28
import org.eclipse.swt.widgets.Label;
Lines 35-77 Link Here
35
	private Delegate delegate;
35
	private Delegate delegate;
36
	private IObservableValue observable;
36
	private IObservableValue observable;
37
	private Label label;
37
	private Label label;
38
	
38
39
	public LabelObservableValueTest() {
39
	public LabelObservableValueTest() {
40
		this(null);
40
		this(null);
41
	}
41
	}
42
	
42
43
	public LabelObservableValueTest(String testName) {
43
	public LabelObservableValueTest(String testName) {
44
		super(testName, new Delegate());
44
		super(testName, new Delegate());
45
	}
45
	}
46
	
46
47
	protected void setUp() throws Exception {
47
	protected void setUp() throws Exception {
48
		super.setUp();
48
		super.setUp();
49
		
49
50
		delegate = (Delegate) getObservableContractDelegate();
50
		delegate = (Delegate) getObservableContractDelegate();
51
		observable = (IObservableValue) getObservable();
51
		observable = (IObservableValue) getObservable();
52
		label = delegate.label;
52
		label = delegate.label;
53
	}
53
	}
54
	
54
55
	protected IObservable doCreateObservable() {
55
	protected IObservable doCreateObservable() {
56
		return getObservableContractDelegate().createObservable(SWTObservables.getRealm(Display.getDefault()));
56
		return getObservableContractDelegate().createObservable(
57
				SWTObservables.getRealm(Display.getDefault()));
57
	}
58
	}
58
	
59
59
    public void testSetValue() throws Exception {
60
	public void testSetValue() throws Exception {
60
    	//preconditions
61
		// preconditions
61
        assertEquals("", label.getText());
62
		assertEquals("", label.getText());
62
        assertEquals("", observable.getValue());
63
		assertEquals("", observable.getValue());
63
        
64
64
        String value = "value";
65
		String value = "value";
65
        observable.setValue(value);
66
		observable.setValue(value);
66
        assertEquals("label text", value, label.getText());
67
		assertEquals("label text", value, label.getText());
67
        assertEquals("observable value", value, observable.getValue());
68
		assertEquals("observable value", value, observable.getValue());
68
    }
69
	}
69
    
70
70
    public static Test suite() {
71
	public static Test suite() {
71
    	TestSuite suite = new TestSuite(LabelObservableValueTest.class.toString());
72
		TestSuite suite = new TestSuite(LabelObservableValueTest.class
72
    	suite.addTestSuite(LabelObservableValueTest.class);
73
				.toString());
73
    	suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
74
		suite.addTestSuite(LabelObservableValueTest.class);
74
    	return suite;
75
		suite.addTest(SWTMutableObservableValueContractTest
76
				.suite(new Delegate()));
77
		return suite;
75
	}
78
	}
76
79
77
	/* package */static class Delegate extends
80
	/* package */static class Delegate extends
Lines 90-107 Link Here
90
		}
93
		}
91
94
92
		public IObservableValue createObservableValue(Realm realm) {
95
		public IObservableValue createObservableValue(Realm realm) {
93
			return new LabelObservableValue(realm, label);
96
			return LabelProperties.text().observeValue(realm, label);
94
		}
97
		}
95
98
96
		public void change(IObservable observable) {
99
		public void change(IObservable observable) {
97
			IObservableValue value = (IObservableValue) observable;
100
			IObservableValue value = (IObservableValue) observable;
98
			value.setValue(value.getValue() + "a");
101
			value.setValue(value.getValue() + "a");
99
		}
102
		}
100
		
103
101
		public Object getValueType(IObservableValue observable) {
104
		public Object getValueType(IObservableValue observable) {
102
			return String.class;
105
			return String.class;
103
		}
106
		}
104
		
107
105
		public Object createValue(IObservableValue observable) {
108
		public Object createValue(IObservableValue observable) {
106
			return observable.getValue() + "a";
109
			return observable.getValue() + "a";
107
		}
110
		}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/CComboObservableValueSelectionTest.java (-11 / +12 lines)
Lines 22-31 Link Here
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
24
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
25
import org.eclipse.jface.databinding.swt.CComboProperties;
25
import org.eclipse.jface.databinding.swt.ISWTObservable;
26
import org.eclipse.jface.databinding.swt.ISWTObservable;
26
import org.eclipse.jface.databinding.swt.SWTObservables;
27
import org.eclipse.jface.databinding.swt.SWTObservables;
27
import org.eclipse.jface.internal.databinding.swt.CComboObservableValue;
28
import org.eclipse.jface.internal.databinding.swt.SWTObservableValueDecorator;
28
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
29
import org.eclipse.swt.SWT;
29
import org.eclipse.swt.SWT;
30
import org.eclipse.swt.custom.CCombo;
30
import org.eclipse.swt.custom.CCombo;
31
import org.eclipse.swt.widgets.Display;
31
import org.eclipse.swt.widgets.Display;
Lines 57-72 Link Here
57
		IObservableValue observable = (IObservableValue) delegate
57
		IObservableValue observable = (IObservableValue) delegate
58
				.createObservable(SWTObservables.getRealm(Display.getDefault()));
58
				.createObservable(SWTObservables.getRealm(Display.getDefault()));
59
59
60
		ValueChangeEventTracker listener = ValueChangeEventTracker.observe(observable);
60
		ValueChangeEventTracker listener = ValueChangeEventTracker
61
				.observe(observable);
61
		combo.select(0);
62
		combo.select(0);
62
63
63
		assertEquals("Observable was not notified.", 1, listener.count);
64
		assertEquals("Observable was not notified.", 1, listener.count);
64
	}
65
	}
65
66
66
	public static Test suite() {
67
	public static Test suite() {
67
		TestSuite suite = new TestSuite(CComboObservableValueSelectionTest.class.getName());
68
		TestSuite suite = new TestSuite(
69
				CComboObservableValueSelectionTest.class.getName());
68
		suite.addTestSuite(CComboObservableValueSelectionTest.class);
70
		suite.addTestSuite(CComboObservableValueSelectionTest.class);
69
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
71
		suite.addTest(SWTMutableObservableValueContractTest
72
				.suite(new Delegate()));
70
		return suite;
73
		return suite;
71
	}
74
	}
72
75
Lines 88-102 Link Here
88
		}
91
		}
89
92
90
		public IObservableValue createObservableValue(Realm realm) {
93
		public IObservableValue createObservableValue(Realm realm) {
91
			return new CComboObservableValue(realm, combo,
94
			return new SWTObservableValueDecorator(CComboProperties.selection()
92
					SWTProperties.SELECTION);
95
					.observeValue(realm, combo), combo);
93
		}
96
		}
94
97
95
		public void change(IObservable observable) {
98
		public void change(IObservable observable) {
96
			int index = combo
99
			IObservableValue ov = (IObservableValue) observable;
97
					.indexOf((String) createValue((IObservableValue) observable));
100
			ov.setValue(createValue(ov));
98
99
			combo.select(index);
100
		}
101
		}
101
102
102
		public Object getValueType(IObservableValue observable) {
103
		public Object getValueType(IObservableValue observable) {
(-)src/org/eclipse/jface/tests/internal/databinding/swt/CComboSingleSelectionObservableValueTest.java (-16 / +18 lines)
Lines 8-14 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Ashley Cambrell - bug 198903
10
 *     Ashley Cambrell - bug 198903
11
 *     Matthew Hall - bug 213145
11
 *     Matthew Hall - bug 213145, 194734
12
 ******************************************************************************/
12
 ******************************************************************************/
13
13
14
package org.eclipse.jface.tests.internal.databinding.swt;
14
package org.eclipse.jface.tests.internal.databinding.swt;
Lines 21-28 Link Here
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.swt.ISWTObservable;
24
import org.eclipse.jface.databinding.swt.CComboProperties;
25
import org.eclipse.jface.internal.databinding.swt.CComboSingleSelectionObservableValue;
25
import org.eclipse.jface.databinding.swt.SWTObservables;
26
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
26
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
27
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.custom.CCombo;
28
import org.eclipse.swt.custom.CCombo;
Lines 31-41 Link Here
31
/**
31
/**
32
 * @since 3.2
32
 * @since 3.2
33
 */
33
 */
34
public class CComboSingleSelectionObservableValueTest extends AbstractSWTTestCase {
34
public class CComboSingleSelectionObservableValueTest extends
35
		AbstractSWTTestCase {
35
	public void testSetValue() throws Exception {
36
	public void testSetValue() throws Exception {
36
		CCombo combo = new CCombo(getShell(), SWT.NONE);
37
		CCombo combo = new CCombo(getShell(), SWT.NONE);
37
		CComboSingleSelectionObservableValue observableValue = new CComboSingleSelectionObservableValue(
38
		IObservableValue observableValue = SWTObservables
38
				combo);
39
				.observeSingleSelectionIndex(combo);
39
		combo.add("Item1");
40
		combo.add("Item1");
40
		combo.add("Item2");
41
		combo.add("Item2");
41
42
Lines 52-60 Link Here
52
	}
53
	}
53
54
54
	public static Test suite() {
55
	public static Test suite() {
55
		TestSuite suite = new TestSuite(CComboSingleSelectionObservableValueTest.class.getName());
56
		TestSuite suite = new TestSuite(
57
				CComboSingleSelectionObservableValueTest.class.getName());
56
		suite.addTestSuite(CComboSingleSelectionObservableValueTest.class);
58
		suite.addTestSuite(CComboSingleSelectionObservableValueTest.class);
57
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
59
		suite.addTest(SWTMutableObservableValueContractTest
60
				.suite(new Delegate()));
58
		return suite;
61
		return suite;
59
	}
62
	}
60
63
Lines 75-87 Link Here
75
		}
78
		}
76
79
77
		public IObservableValue createObservableValue(Realm realm) {
80
		public IObservableValue createObservableValue(Realm realm) {
78
			return new CComboSingleSelectionObservableValue(realm, combo);
81
			return CComboProperties.singleSelectionIndex().observeValue(realm,
82
					combo);
79
		}
83
		}
80
84
81
		public void change(IObservable observable) {
85
		public void change(IObservable observable) {
82
			int index = _createValue((IObservableValue) observable);
86
			IObservableValue value = (IObservableValue) observable;
83
			combo.select(index);
87
			value.setValue(createValue(value));
84
			combo.notifyListeners(SWT.Selection, null);
85
		}
88
		}
86
89
87
		public Object getValueType(IObservableValue observable) {
90
		public Object getValueType(IObservableValue observable) {
Lines 91-102 Link Here
91
		public Object createValue(IObservableValue observable) {
94
		public Object createValue(IObservableValue observable) {
92
			return new Integer(_createValue(observable));
95
			return new Integer(_createValue(observable));
93
		}
96
		}
94
		
97
95
		private int _createValue(IObservableValue observable) {
98
		private int _createValue(IObservableValue observable) {
96
			CCombo combo = ((CCombo) ((ISWTObservable) observable).getWidget());
97
			int value = Math.max(0, combo.getSelectionIndex());
99
			int value = Math.max(0, combo.getSelectionIndex());
98
			
100
99
			//returns either 0 or 1 depending upon current value
101
			// returns either 0 or 1 depending upon current value
100
			return Math.abs(value - 1);
102
			return Math.abs(value - 1);
101
		}
103
		}
102
	}
104
	}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/SWTDelayedObservableValueDecoratorTest.java (-45 / +8 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation (bug 212223)
9
 *     Matthew Hall - initial API and implementation (bug 212223)
10
 *     Matthew Hall - bug 213145, 245647
10
 *     Matthew Hall - bug 213145, 245647, 194734
11
 ******************************************************************************/
11
 ******************************************************************************/
12
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
13
package org.eclipse.jface.tests.internal.databinding.swt;
Lines 15-36 Link Here
15
import junit.framework.Test;
15
import junit.framework.Test;
16
import junit.framework.TestSuite;
16
import junit.framework.TestSuite;
17
17
18
import org.eclipse.core.databinding.observable.Diffs;
19
import org.eclipse.core.databinding.observable.IObservable;
18
import org.eclipse.core.databinding.observable.IObservable;
20
import org.eclipse.core.databinding.observable.Realm;
19
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.observable.value.WritableValue;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
24
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
25
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
25
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
26
import org.eclipse.jface.databinding.swt.SWTObservables;
26
import org.eclipse.jface.databinding.swt.SWTObservables;
27
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
27
import org.eclipse.jface.internal.databinding.swt.SWTObservableValueDecorator;
28
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
28
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
29
import org.eclipse.swt.SWT;
29
import org.eclipse.swt.SWT;
30
import org.eclipse.swt.widgets.Display;
30
import org.eclipse.swt.widgets.Display;
31
import org.eclipse.swt.widgets.Event;
31
import org.eclipse.swt.widgets.Event;
32
import org.eclipse.swt.widgets.Shell;
32
import org.eclipse.swt.widgets.Shell;
33
import org.eclipse.swt.widgets.Widget;
34
33
35
/**
34
/**
36
 * Tests for DelayedObservableValue
35
 * Tests for DelayedObservableValue
Lines 43-57 Link Here
43
	private Shell shell;
42
	private Shell shell;
44
	private Object oldValue;
43
	private Object oldValue;
45
	private Object newValue;
44
	private Object newValue;
46
	private SWTObservableValueStub target;
45
	private ISWTObservableValue target;
47
	private ISWTObservableValue delayed;
46
	private ISWTObservableValue delayed;
48
47
49
	protected void setUp() throws Exception {
48
	protected void setUp() throws Exception {
50
		super.setUp();
49
		super.setUp();
51
		display = Display.getCurrent();
50
		display = Display.getCurrent();
52
		shell = new Shell(display);
51
		shell = new Shell(display);
53
		target = new SWTObservableValueStub(SWTObservables.getRealm(display),
52
		target = new SWTObservableValueDecorator(new WritableValue(
54
				shell);
53
				SWTObservables.getRealm(display)), shell);
55
		oldValue = new Object();
54
		oldValue = new Object();
56
		newValue = new Object();
55
		newValue = new Object();
57
		target.setValue(oldValue);
56
		target.setValue(oldValue);
Lines 93-135 Link Here
93
		assertEquals(newValue, tracker.event.diff.getNewValue());
92
		assertEquals(newValue, tracker.event.diff.getNewValue());
94
	}
93
	}
95
94
96
	static class SWTObservableValueStub extends AbstractSWTObservableValue {
97
		private Object value;
98
		private boolean stale;
99
100
		Object overrideValue;
101
102
		public SWTObservableValueStub(Realm realm, Widget widget) {
103
			super(realm, widget);
104
		}
105
106
		protected Object doGetValue() {
107
			return value;
108
		}
109
110
		protected void doSetValue(Object value) {
111
			Object oldValue = this.value;
112
			if (overrideValue != null)
113
				value = overrideValue;
114
			this.value = value;
115
			stale = false;
116
			fireValueChange(Diffs.createValueDiff(oldValue, value));
117
		}
118
119
		public Object getValueType() {
120
			return Object.class;
121
		}
122
123
		protected void fireStale() {
124
			stale = true;
125
			super.fireStale();
126
		}
127
128
		public boolean isStale() {
129
			return stale;
130
		}
131
	}
132
133
	public static Test suite() {
95
	public static Test suite() {
134
		TestSuite suite = new TestSuite(
96
		TestSuite suite = new TestSuite(
135
				SWTDelayedObservableValueDecoratorTest.class.getName());
97
				SWTDelayedObservableValueDecoratorTest.class.getName());
Lines 155-161 Link Here
155
117
156
		public IObservableValue createObservableValue(Realm realm) {
118
		public IObservableValue createObservableValue(Realm realm) {
157
			return SWTObservables.observeDelayedValue(0,
119
			return SWTObservables.observeDelayedValue(0,
158
					new SWTObservableValueStub(realm, shell));
120
					new SWTObservableValueDecorator(new WritableValue(realm,
121
							null, Object.class), shell));
159
		}
122
		}
160
123
161
		public Object getValueType(IObservableValue observable) {
124
		public Object getValueType(IObservableValue observable) {
(-)src/org/eclipse/jface/tests/internal/databinding/swt/CComboObservableValueTest.java (-12 / +16 lines)
Lines 9-21 Link Here
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Ashley Cambrell - bug 198904
10
 *     Ashley Cambrell - bug 198904
11
 *     Eric Rizzo - bug 134884
11
 *     Eric Rizzo - bug 134884
12
 *     Matthew Hall - bug 194734
12
 ******************************************************************************/
13
 ******************************************************************************/
13
14
14
package org.eclipse.jface.tests.internal.databinding.swt;
15
package org.eclipse.jface.tests.internal.databinding.swt;
15
16
17
import org.eclipse.core.databinding.observable.Realm;
18
import org.eclipse.core.databinding.observable.value.IObservableValue;
19
import org.eclipse.core.databinding.property.value.IValueProperty;
16
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
20
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
17
import org.eclipse.jface.internal.databinding.swt.CComboObservableValue;
21
import org.eclipse.jface.databinding.swt.CComboProperties;
18
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
22
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
23
import org.eclipse.jface.databinding.swt.SWTObservables;
19
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
24
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
20
import org.eclipse.swt.SWT;
25
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.custom.CCombo;
26
import org.eclipse.swt.custom.CCombo;
Lines 26-33 Link Here
26
public class CComboObservableValueTest extends AbstractSWTTestCase {
31
public class CComboObservableValueTest extends AbstractSWTTestCase {
27
	public void testDispose() throws Exception {
32
	public void testDispose() throws Exception {
28
		CCombo combo = new CCombo(getShell(), SWT.NONE);
33
		CCombo combo = new CCombo(getShell(), SWT.NONE);
29
		CComboObservableValue observableValue = new CComboObservableValue(
34
		ISWTObservableValue observableValue = SWTObservables.observeText(combo);
30
				combo, SWTProperties.TEXT);
31
35
32
		ValueChangeEventTracker testCounterValueChangeListener = new ValueChangeEventTracker();
36
		ValueChangeEventTracker testCounterValueChangeListener = new ValueChangeEventTracker();
33
		observableValue.addValueChangeListener(testCounterValueChangeListener);
37
		observableValue.addValueChangeListener(testCounterValueChangeListener);
Lines 52-72 Link Here
52
	}
56
	}
53
57
54
	public void testSetValueWithNull() {
58
	public void testSetValueWithNull() {
55
		testSetValueWithNull(SWTProperties.TEXT);
59
		testSetValueWithNull(CComboProperties.text());
56
		testSetValueWithNull(SWTProperties.SELECTION);
60
		testSetValueWithNull(CComboProperties.selection());
57
	}
61
	}
58
62
59
	protected void testSetValueWithNull(String observableMode) {
63
	protected void testSetValueWithNull(IValueProperty property) {
60
		CCombo combo = new CCombo(getShell(), SWT.NONE);
64
		CCombo combo = new CCombo(getShell(), SWT.NONE);
61
		combo.setItems(new String[] {"one", "two", "three"});
65
		combo.setItems(new String[] { "one", "two", "three" });
62
		CComboObservableValue observable = new CComboObservableValue(
66
		IObservableValue observable = property.observeValue(Realm.getDefault(),
63
				combo, observableMode);
67
				combo);
64
68
65
		observable.doSetValue("two");
69
		observable.setValue("two");
66
		assertEquals("two", combo.getText());
70
		assertEquals("two", combo.getText());
67
		assertEquals(1, combo.getSelectionIndex());
71
		assertEquals(1, combo.getSelectionIndex());
68
72
69
		observable.doSetValue(null);
73
		observable.setValue(null);
70
		assertEquals("", combo.getText());
74
		assertEquals("", combo.getText());
71
		assertEquals(-1, combo.getSelectionIndex());
75
		assertEquals(-1, combo.getSelectionIndex());
72
	}
76
	}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/ComboObservableValueSelectionTest.java (-10 / +11 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Matthew Hall - bug 213145
10
 *     Matthew Hall - bug 213145, 194734
11
 *******************************************************************************/
11
 *******************************************************************************/
12
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
13
package org.eclipse.jface.tests.internal.databinding.swt;
Lines 22-31 Link Here
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
24
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
25
import org.eclipse.jface.databinding.swt.ComboProperties;
25
import org.eclipse.jface.databinding.swt.ISWTObservable;
26
import org.eclipse.jface.databinding.swt.ISWTObservable;
26
import org.eclipse.jface.databinding.swt.SWTObservables;
27
import org.eclipse.jface.databinding.swt.SWTObservables;
27
import org.eclipse.jface.internal.databinding.swt.ComboObservableValue;
28
import org.eclipse.jface.internal.databinding.swt.SWTObservableValueDecorator;
28
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
29
import org.eclipse.swt.SWT;
29
import org.eclipse.swt.SWT;
30
import org.eclipse.swt.widgets.Combo;
30
import org.eclipse.swt.widgets.Combo;
31
import org.eclipse.swt.widgets.Display;
31
import org.eclipse.swt.widgets.Display;
Lines 33-39 Link Here
33
33
34
/**
34
/**
35
 * @since 3.2
35
 * @since 3.2
36
 *
36
 * 
37
 */
37
 */
38
public class ComboObservableValueSelectionTest extends TestCase {
38
public class ComboObservableValueSelectionTest extends TestCase {
39
	private Delegate delegate;
39
	private Delegate delegate;
Lines 67-75 Link Here
67
	}
67
	}
68
68
69
	public static Test suite() {
69
	public static Test suite() {
70
		TestSuite suite = new TestSuite(ComboObservableValueSelectionTest.class.toString());
70
		TestSuite suite = new TestSuite(ComboObservableValueSelectionTest.class
71
				.toString());
71
		suite.addTestSuite(ComboObservableValueSelectionTest.class);
72
		suite.addTestSuite(ComboObservableValueSelectionTest.class);
72
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
73
		suite.addTest(SWTMutableObservableValueContractTest
74
				.suite(new Delegate()));
73
		return suite;
75
		return suite;
74
	}
76
	}
75
77
Lines 91-106 Link Here
91
		}
93
		}
92
94
93
		public IObservableValue createObservableValue(Realm realm) {
95
		public IObservableValue createObservableValue(Realm realm) {
94
			return new ComboObservableValue(realm, combo,
96
			return new SWTObservableValueDecorator(ComboProperties.selection()
95
					SWTProperties.SELECTION);
97
					.observeValue(realm, combo), combo);
96
		}
98
		}
97
99
98
		public void change(IObservable observable) {
100
		public void change(IObservable observable) {
99
			int index = combo
101
			int index = combo
100
					.indexOf((String) createValue((IObservableValue) observable));
102
					.indexOf((String) createValue((IObservableValue) observable));
101
103
102
			combo.select(index);
104
			((IObservableValue) observable).setValue(combo.getItem(index));
103
			combo.notifyListeners(SWT.Selection, null);
104
		}
105
		}
105
106
106
		public Object getValueType(IObservableValue observable) {
107
		public Object getValueType(IObservableValue observable) {
(-)src/org/eclipse/core/tests/internal/databinding/beans/JavaBeanObservableArrayBasedListTest.java (-64 / +76 lines)
Lines 7-18 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Matthew Hall - bugs 221351, 213145, 244098, 246103
10
 *     Matthew Hall - bugs 221351, 213145, 244098, 246103, 194734
11
 ******************************************************************************/
11
 ******************************************************************************/
12
12
13
package org.eclipse.core.tests.internal.databinding.beans;
13
package org.eclipse.core.tests.internal.databinding.beans;
14
14
15
import java.beans.IntrospectionException;
16
import java.beans.PropertyChangeEvent;
15
import java.beans.PropertyChangeEvent;
17
import java.beans.PropertyChangeListener;
16
import java.beans.PropertyChangeListener;
18
import java.beans.PropertyDescriptor;
17
import java.beans.PropertyDescriptor;
Lines 25-38 Link Here
25
import junit.framework.Test;
24
import junit.framework.Test;
26
import junit.framework.TestSuite;
25
import junit.framework.TestSuite;
27
26
27
import org.eclipse.core.databinding.beans.BeanProperties;
28
import org.eclipse.core.databinding.beans.BeansObservables;
28
import org.eclipse.core.databinding.beans.BeansObservables;
29
import org.eclipse.core.databinding.beans.IBeanObservable;
30
import org.eclipse.core.databinding.beans.IBeanProperty;
29
import org.eclipse.core.databinding.observable.IObservable;
31
import org.eclipse.core.databinding.observable.IObservable;
30
import org.eclipse.core.databinding.observable.IObservableCollection;
32
import org.eclipse.core.databinding.observable.IObservableCollection;
31
import org.eclipse.core.databinding.observable.Realm;
33
import org.eclipse.core.databinding.observable.Realm;
32
import org.eclipse.core.databinding.observable.list.IObservableList;
34
import org.eclipse.core.databinding.observable.list.IObservableList;
33
import org.eclipse.core.databinding.observable.list.ListChangeEvent;
35
import org.eclipse.core.databinding.observable.list.ListChangeEvent;
34
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
36
import org.eclipse.core.databinding.observable.list.ListDiff;
35
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableList;
36
import org.eclipse.jface.databinding.conformance.MutableObservableListContractTest;
37
import org.eclipse.jface.databinding.conformance.MutableObservableListContractTest;
37
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
38
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
38
import org.eclipse.jface.databinding.conformance.util.CurrentRealm;
39
import org.eclipse.jface.databinding.conformance.util.CurrentRealm;
Lines 46-52 Link Here
46
 */
47
 */
47
public class JavaBeanObservableArrayBasedListTest extends
48
public class JavaBeanObservableArrayBasedListTest extends
48
		AbstractDefaultRealmTestCase {
49
		AbstractDefaultRealmTestCase {
49
	private JavaBeanObservableList list;
50
	private IObservableList list;
51
	private IBeanObservable beanObservable;
50
52
51
	private PropertyDescriptor propertyDescriptor;
53
	private PropertyDescriptor propertyDescriptor;
52
54
Lines 63-91 Link Here
63
		super.setUp();
65
		super.setUp();
64
66
65
		propertyName = "array";
67
		propertyName = "array";
66
		propertyDescriptor = new PropertyDescriptor(propertyName, Bean.class);
68
		propertyDescriptor = ((IBeanProperty) BeanProperties.listProperty(
69
				Bean.class, propertyName)).getPropertyDescriptor();
67
		bean = new Bean(new Object[0]);
70
		bean = new Bean(new Object[0]);
68
71
69
		list = new JavaBeanObservableList(SWTObservables.getRealm(Display
72
		list = BeansObservables.observeList(SWTObservables.getRealm(Display
70
				.getDefault()), bean, propertyDescriptor, Bean.class);
73
				.getDefault()), bean, propertyName);
74
		beanObservable = (IBeanObservable) list;
71
	}
75
	}
72
76
73
	public void testGetObserved() throws Exception {
77
	public void testGetObserved() throws Exception {
74
		assertSame(bean, list.getObserved());
78
		assertSame(bean, beanObservable.getObserved());
75
	}
79
	}
76
80
77
	public void testGetPropertyDescriptor() throws Exception {
81
	public void testGetPropertyDescriptor() throws Exception {
78
		assertSame(propertyDescriptor, list.getPropertyDescriptor());
82
		assertEquals(propertyDescriptor, beanObservable.getPropertyDescriptor());
79
	}
83
	}
80
84
81
	public void testRegistersListenerOnCreation()
85
	public void testRegistersListenerAfterFirstListenerIsAdded()
82
			throws Exception {
86
			throws Exception {
87
		assertFalse(bean.changeSupport.hasListeners(propertyName));
88
		list.addListChangeListener(new ListChangeEventTracker());
83
		assertTrue(bean.changeSupport.hasListeners(propertyName));
89
		assertTrue(bean.changeSupport.hasListeners(propertyName));
84
	}
90
	}
85
91
86
	public void testRemovesListenerOnDisposal()
92
	public void testRemovesListenerAfterLastListenerIsRemoved()
87
			throws Exception {
93
			throws Exception {
88
		list.dispose();
94
		ListChangeEventTracker listener = new ListChangeEventTracker();
95
		list.addListChangeListener(listener);
96
97
		assertTrue(bean.changeSupport.hasListeners(propertyName));
98
		list.removeListChangeListener(listener);
89
		assertFalse(bean.changeSupport.hasListeners(propertyName));
99
		assertFalse(bean.changeSupport.hasListeners(propertyName));
90
	}
100
	}
91
101
Lines 120-127 Link Here
120
		assertEquals(1, listener.count);
130
		assertEquals(1, listener.count);
121
		ListChangeEvent event = listener.event;
131
		ListChangeEvent event = listener.event;
122
132
123
		assertEquals(list, event.getObservableList());
133
		assertSame(list, event.getObservableList());
124
		assertEntry(event.diff.getDifferences()[0], true, 0, element);
134
		assertDiff(event.diff, Collections.EMPTY_LIST, Collections
135
				.singletonList("1"));
125
	}
136
	}
126
137
127
	public void testAdd_FiresPropertyChangeEvent() throws Exception {
138
	public void testAdd_FiresPropertyChangeEvent() throws Exception {
Lines 150-156 Link Here
150
		list.add(0, element);
161
		list.add(0, element);
151
162
152
		ListChangeEvent event = listener.event;
163
		ListChangeEvent event = listener.event;
153
		assertEntry(event.diff.getDifferences()[0], true, 0, element);
164
		assertDiff(event.diff, Collections.EMPTY_LIST, Collections
165
				.singletonList("1"));
154
	}
166
	}
155
167
156
	public void testAddAtIndexPropertyChangeEvent() throws Exception {
168
	public void testAddAtIndexPropertyChangeEvent() throws Exception {
Lines 182-189 Link Here
182
194
183
		assertEquals(1, listener.count);
195
		assertEquals(1, listener.count);
184
		ListChangeEvent event = listener.event;
196
		ListChangeEvent event = listener.event;
185
		assertEquals(list, event.getObservableList());
197
		assertSame(list, event.getObservableList());
186
		assertEntry(event.diff.getDifferences()[0], false, 0, element);
198
199
		assertDiff(event.diff, Collections.singletonList("1"),
200
				Collections.EMPTY_LIST);
187
	}
201
	}
188
202
189
	public void testRemovePropertyChangeEvent() throws Exception {
203
	public void testRemovePropertyChangeEvent() throws Exception {
Lines 218-225 Link Here
218
232
219
		assertEquals(1, listener.count);
233
		assertEquals(1, listener.count);
220
		ListChangeEvent event = listener.event;
234
		ListChangeEvent event = listener.event;
221
		assertEquals(list, event.getObservableList());
235
		assertSame(list, event.getObservableList());
222
		assertEntry(event.diff.getDifferences()[0], false, 0, element);
236
237
		assertDiff(event.diff, Collections.singletonList(element),
238
				Collections.EMPTY_LIST);
223
	}
239
	}
224
240
225
	public void testRemoveAtIndexPropertyChangeEvent() throws Exception {
241
	public void testRemoveAtIndexPropertyChangeEvent() throws Exception {
Lines 252-261 Link Here
252
268
253
		assertEquals(1, listener.count);
269
		assertEquals(1, listener.count);
254
		ListChangeEvent event = listener.event;
270
		ListChangeEvent event = listener.event;
255
		assertEquals(list, event.getObservableList());
271
		assertSame(list, event.getObservableList());
256
272
257
		assertEntry(event.diff.getDifferences()[0], true, 0, elements.get(0));
273
		assertDiff(event.diff, Collections.EMPTY_LIST, Arrays
258
		assertEntry(event.diff.getDifferences()[1], true, 1, elements.get(1));
274
				.asList(new String[] { "1", "2" }));
259
	}
275
	}
260
276
261
	public void testAddAllPropertyChangeEvent() throws Exception {
277
	public void testAddAllPropertyChangeEvent() throws Exception {
Lines 292-300 Link Here
292
308
293
		assertEquals(1, listener.count);
309
		assertEquals(1, listener.count);
294
		ListChangeEvent event = listener.event;
310
		ListChangeEvent event = listener.event;
295
		assertEquals(list, event.getObservableList());
311
		assertSame(list, event.getObservableList());
296
		assertEntry(event.diff.getDifferences()[0], true, 2, elements.get(0));
312
297
		assertEntry(event.diff.getDifferences()[1], true, 3, elements.get(1));
313
		assertDiff(event.diff, Arrays.asList(new Object[] { "1", "2" }), Arrays
314
				.asList(new Object[] { "1", "2", "1", "2" }));
298
	}
315
	}
299
316
300
	public void testAddAllAtIndexPropertyChangeEvent() throws Exception {
317
	public void testAddAllAtIndexPropertyChangeEvent() throws Exception {
Lines 306-321 Link Here
306
	}
323
	}
307
324
308
	public void testRemoveAll() throws Exception {
325
	public void testRemoveAll() throws Exception {
309
		List elements = Arrays.asList(new String[] { "1", "2" });
326
		list.addAll(Arrays.asList(new String[] { "1", "2", "3", "4" }));
310
		list.addAll(elements);
311
		list.addAll(elements);
312
313
		assertEquals(4, bean.getArray().length);
327
		assertEquals(4, bean.getArray().length);
314
		list.removeAll(elements);
328
329
		list.removeAll(Arrays.asList(new String[] { "2", "4" }));
315
330
316
		assertEquals(2, bean.getArray().length);
331
		assertEquals(2, bean.getArray().length);
317
		assertEquals(elements.get(0), bean.getArray()[0]);
332
		assertEquals("1", bean.getArray()[0]);
318
		assertEquals(elements.get(1), bean.getArray()[1]);
333
		assertEquals("3", bean.getArray()[1]);
319
	}
334
	}
320
335
321
	public void testRemoveAllListChangeEvent() throws Exception {
336
	public void testRemoveAllListChangeEvent() throws Exception {
Lines 330-338 Link Here
330
		list.removeAll(elements);
345
		list.removeAll(elements);
331
346
332
		ListChangeEvent event = listener.event;
347
		ListChangeEvent event = listener.event;
333
		assertEquals(list, event.getObservableList());
348
		assertSame(list, event.getObservableList());
334
		assertEntry(event.diff.getDifferences()[0], false, 0, elements.get(0));
349
335
		assertEntry(event.diff.getDifferences()[1], false, 0, elements.get(1));
350
		assertDiff(event.diff, Arrays
351
				.asList(new Object[] { "1", "2", "1", "2" }),
352
				Collections.EMPTY_LIST);
336
	}
353
	}
337
354
338
	public void testRemoveAllPropertyChangeEvent() throws Exception {
355
	public void testRemoveAllPropertyChangeEvent() throws Exception {
Lines 369-377 Link Here
369
386
370
		assertEquals(1, listener.count);
387
		assertEquals(1, listener.count);
371
		ListChangeEvent event = listener.event;
388
		ListChangeEvent event = listener.event;
372
		assertEquals(list, event.getObservableList());
389
		assertSame(list, event.getObservableList());
373
		assertEntry(event.diff.getDifferences()[0], false, 2, elements.get(2));
390
374
		assertEntry(event.diff.getDifferences()[1], false, 2, elements.get(3));
391
		assertDiff(event.diff, Arrays
392
				.asList(new Object[] { "0", "1", "2", "3" }), Arrays
393
				.asList(new Object[] { "0", "1" }));
375
	}
394
	}
376
395
377
	public void testRetainAllPropertyChangeEvent() throws Exception {
396
	public void testRetainAllPropertyChangeEvent() throws Exception {
Lines 425-433 Link Here
425
444
426
		assertEquals(1, listener.count);
445
		assertEquals(1, listener.count);
427
		ListChangeEvent event = listener.event;
446
		ListChangeEvent event = listener.event;
428
		assertEquals(list, event.getObservableList());
447
		assertSame(list, event.getObservableList());
429
		assertEntry(event.diff.getDifferences()[0], false, 0, oldElement);
448
430
		assertEntry(event.diff.getDifferences()[1], true, 0, newElement);
449
		assertDiff(event.diff, Collections.singletonList(oldElement),
450
				Collections.singletonList(newElement));
431
	}
451
	}
432
452
433
	public void testSetPropertyChangeEvent() throws Exception {
453
	public void testSetPropertyChangeEvent() throws Exception {
Lines 473-483 Link Here
473
		assertEquals(Collections.singletonList("new"), list);
493
		assertEquals(Collections.singletonList("new"), list);
474
	}
494
	}
475
495
476
	private static void assertEntry(ListDiffEntry entry, boolean addition,
496
	private static void assertDiff(ListDiff diff, List oldList, List newList) {
477
			int position, Object element) {
497
		oldList = new ArrayList(oldList); // defensive copy in case arg is
478
		assertEquals("addition", addition, entry.isAddition());
498
		// unmodifiable
479
		assertEquals("position", position, entry.getPosition());
499
		diff.applyTo(oldList);
480
		assertEquals("element", element, entry.getElement());
500
		assertEquals("applying diff to list did not produce expected result",
501
				newList, oldList);
481
	}
502
	}
482
503
483
	private static void assertPropertyChangeEvent(Bean bean, Runnable runnable) {
504
	private static void assertPropertyChangeEvent(Bean bean, Runnable runnable) {
Lines 492-499 Link Here
492
		PropertyChangeEvent event = listener.evt;
513
		PropertyChangeEvent event = listener.evt;
493
		assertEquals("event did not fire", 1, listener.count);
514
		assertEquals("event did not fire", 1, listener.count);
494
		assertEquals("array", event.getPropertyName());
515
		assertEquals("array", event.getPropertyName());
495
		assertTrue("old value", Arrays.equals(old, (Object[]) event.getOldValue()));
516
		assertTrue("old value", Arrays.equals(old, (Object[]) event
496
		assertTrue("new value", Arrays.equals(bean.getArray(), (Object[]) event.getNewValue()));
517
				.getOldValue()));
518
		assertTrue("new value", Arrays.equals(bean.getArray(), (Object[]) event
519
				.getNewValue()));
497
		assertFalse("lists are equal", Arrays.equals(bean.getArray(), old));
520
		assertFalse("lists are equal", Arrays.equals(bean.getArray(), old));
498
	}
521
	}
499
522
Lines 503-513 Link Here
503
526
504
		PropertyChangeEvent evt;
527
		PropertyChangeEvent evt;
505
528
506
		/*
507
		 * (non-Javadoc)
508
		 * 
509
		 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
510
		 */
511
		public void propertyChange(PropertyChangeEvent evt) {
529
		public void propertyChange(PropertyChangeEvent evt) {
512
			count++;
530
			count++;
513
			this.evt = evt;
531
			this.evt = evt;
Lines 515-521 Link Here
515
	}
533
	}
516
534
517
	public static Test suite() {
535
	public static Test suite() {
518
		TestSuite suite = new TestSuite(JavaBeanObservableArrayBasedListTest.class.getName());
536
		TestSuite suite = new TestSuite(
537
				JavaBeanObservableArrayBasedListTest.class.getName());
519
		suite.addTestSuite(JavaBeanObservableArrayBasedListTest.class);
538
		suite.addTestSuite(JavaBeanObservableArrayBasedListTest.class);
520
		suite.addTest(MutableObservableListContractTest.suite(new Delegate()));
539
		suite.addTest(MutableObservableListContractTest.suite(new Delegate()));
521
		return suite;
540
		return suite;
Lines 525-541 Link Here
525
		public IObservableCollection createObservableCollection(Realm realm,
544
		public IObservableCollection createObservableCollection(Realm realm,
526
				int elementCount) {
545
				int elementCount) {
527
			String propertyName = "array";
546
			String propertyName = "array";
528
			PropertyDescriptor propertyDescriptor;
529
			try {
530
				propertyDescriptor = new PropertyDescriptor(propertyName,
531
						Bean.class);
532
			} catch (IntrospectionException e) {
533
				throw new RuntimeException(e);
534
			}
535
			Object bean = new Bean(new Object[0]);
547
			Object bean = new Bean(new Object[0]);
536
548
537
			IObservableList list = new JavaBeanObservableList(realm, bean,
549
			IObservableList list = BeansObservables.observeList(realm, bean,
538
					propertyDescriptor, String.class);
550
					propertyName, String.class);
539
			for (int i = 0; i < elementCount; i++)
551
			for (int i = 0; i < elementCount; i++)
540
				list.add(createElement(list));
552
				list.add(createElement(list));
541
			return list;
553
			return list;
(-)src/org/eclipse/core/tests/internal/databinding/beans/JavaBeanObservableSetTest.java (-31 / +31 lines)
Lines 7-18 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Matthew Hall - bugs 221351, 213145, 244098, 246103
10
 *     Matthew Hall - bugs 221351, 213145, 244098, 246103, 194734
11
 ******************************************************************************/
11
 ******************************************************************************/
12
12
13
package org.eclipse.core.tests.internal.databinding.beans;
13
package org.eclipse.core.tests.internal.databinding.beans;
14
14
15
import java.beans.IntrospectionException;
16
import java.beans.PropertyDescriptor;
15
import java.beans.PropertyDescriptor;
17
import java.util.Arrays;
16
import java.util.Arrays;
18
import java.util.Collections;
17
import java.util.Collections;
Lines 22-35 Link Here
22
import junit.framework.TestCase;
21
import junit.framework.TestCase;
23
import junit.framework.TestSuite;
22
import junit.framework.TestSuite;
24
23
24
import org.eclipse.core.databinding.beans.BeanProperties;
25
import org.eclipse.core.databinding.beans.BeansObservables;
25
import org.eclipse.core.databinding.beans.BeansObservables;
26
import org.eclipse.core.databinding.beans.IBeanObservable;
27
import org.eclipse.core.databinding.beans.IBeanProperty;
28
import org.eclipse.core.databinding.beans.PojoObservables;
26
import org.eclipse.core.databinding.observable.IObservable;
29
import org.eclipse.core.databinding.observable.IObservable;
27
import org.eclipse.core.databinding.observable.IObservableCollection;
30
import org.eclipse.core.databinding.observable.IObservableCollection;
28
import org.eclipse.core.databinding.observable.Realm;
31
import org.eclipse.core.databinding.observable.Realm;
29
import org.eclipse.core.databinding.observable.set.IObservableSet;
32
import org.eclipse.core.databinding.observable.set.IObservableSet;
30
import org.eclipse.core.databinding.observable.set.ISetChangeListener;
33
import org.eclipse.core.databinding.observable.set.ISetChangeListener;
31
import org.eclipse.core.databinding.observable.set.SetChangeEvent;
34
import org.eclipse.core.databinding.observable.set.SetChangeEvent;
32
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableSet;
33
import org.eclipse.jface.databinding.conformance.MutableObservableSetContractTest;
35
import org.eclipse.jface.databinding.conformance.MutableObservableSetContractTest;
34
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
36
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
35
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
37
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
Lines 42-87 Link Here
42
 * @since 3.3
44
 * @since 3.3
43
 */
45
 */
44
public class JavaBeanObservableSetTest extends TestCase {
46
public class JavaBeanObservableSetTest extends TestCase {
45
	private JavaBeanObservableSet observableSet;
47
	private IObservableSet observableSet;
48
	private IBeanObservable beanObservable;
46
	private Bean bean;
49
	private Bean bean;
47
	private PropertyDescriptor propertyDescriptor;
50
	private PropertyDescriptor propertyDescriptor;
48
	private String propertyName;
51
	private String propertyName;
49
	private SetChangeListener listener;
52
	private SetChangeListener listener;
50
53
51
	/*
52
	 * (non-Javadoc)
53
	 * 
54
	 * @see junit.framework.TestCase#setUp()
55
	 */
56
	protected void setUp() throws Exception {
54
	protected void setUp() throws Exception {
57
		bean = new Bean();
55
		bean = new Bean();
58
		propertyName = "set";
56
		propertyName = "set";
59
		propertyDescriptor = new PropertyDescriptor(propertyName, Bean.class);
57
		propertyDescriptor = ((IBeanProperty) BeanProperties.setProperty(
58
				Bean.class, propertyName)).getPropertyDescriptor();
60
59
61
		observableSet = new JavaBeanObservableSet(SWTObservables
60
		observableSet = BeansObservables
62
				.getRealm(Display.getDefault()), bean, propertyDescriptor,
61
				.observeSet(SWTObservables.getRealm(Display.getDefault()),
63
				Bean.class);
62
						bean, propertyName, Bean.class);
63
		beanObservable = (IBeanObservable) observableSet;
64
		listener = new SetChangeListener();
64
		listener = new SetChangeListener();
65
	}
65
	}
66
66
67
	public void testGetObserved() throws Exception {
67
	public void testGetObserved() throws Exception {
68
		assertEquals(bean, observableSet.getObserved());
68
		assertEquals(bean, beanObservable.getObserved());
69
	}
69
	}
70
70
71
	public void testGetPropertyDescriptor() throws Exception {
71
	public void testGetPropertyDescriptor() throws Exception {
72
		assertEquals(propertyDescriptor, observableSet.getPropertyDescriptor());
72
		assertEquals(propertyDescriptor, beanObservable.getPropertyDescriptor());
73
	}
73
	}
74
	
74
	
75
	public void testGetElementType() throws Exception {
75
	public void testGetElementType() throws Exception {
76
		assertEquals(Bean.class, observableSet.getElementType());
76
		assertEquals(Bean.class, observableSet.getElementType());
77
	}
77
	}
78
	
78
	
79
	public void testRegistersListenerOnCreation() throws Exception {
79
	public void testRegistersListenerAfterFirstListenerIsAdded() throws Exception {
80
		assertFalse(bean.changeSupport.hasListeners(propertyName));
81
		observableSet.addSetChangeListener(new SetChangeListener());
80
		assertTrue(bean.changeSupport.hasListeners(propertyName));
82
		assertTrue(bean.changeSupport.hasListeners(propertyName));
81
	}
83
	}
82
		
84
		
83
	public void testRemovesListenerOnDisposal() throws Exception {
85
    public void testRemovesListenerAfterLastListenerIsRemoved() throws Exception {
84
		observableSet.dispose();
86
		observableSet.addSetChangeListener(listener);
87
		
88
		assertTrue(bean.changeSupport.hasListeners(propertyName));
89
		observableSet.removeSetChangeListener(listener);
85
		assertFalse(bean.changeSupport.hasListeners(propertyName));
90
		assertFalse(bean.changeSupport.hasListeners(propertyName));
86
	}
91
	}
87
	
92
	
Lines 94-109 Link Here
94
99
95
	public void testConstructor_RegisterListeners() throws Exception {
100
	public void testConstructor_RegisterListeners() throws Exception {
96
		bean = new Bean();
101
		bean = new Bean();
97
		new JavaBeanObservableSet(new CurrentRealm(true), bean,
102
		observableSet = BeansObservables.observeSet(new CurrentRealm(true), bean,
98
				propertyDescriptor, Bean.class);
103
				propertyName);
104
		assertFalse(bean.hasListeners(propertyName));
105
		ChangeEventTracker.observe(observableSet);
99
		assertTrue(bean.hasListeners(propertyName));
106
		assertTrue(bean.hasListeners(propertyName));
100
	}
107
	}
101
108
102
	public void testConstructor_SkipsRegisterListeners() throws Exception {
109
	public void testConstructor_SkipsRegisterListeners() throws Exception {
103
		bean = new Bean();
110
		bean = new Bean();
104
111
105
		observableSet = new JavaBeanObservableSet(new CurrentRealm(true), bean,
112
		observableSet = PojoObservables.observeSet(new CurrentRealm(true),
106
				propertyDescriptor, Bean.class, false);
113
				bean, propertyName);
107
		assertFalse(bean.hasListeners(propertyName));
114
		assertFalse(bean.hasListeners(propertyName));
108
		ChangeEventTracker.observe(observableSet);
115
		ChangeEventTracker.observe(observableSet);
109
		assertFalse(bean.hasListeners(propertyName));
116
		assertFalse(bean.hasListeners(propertyName));
Lines 151-166 Link Here
151
				int elementCount) {
158
				int elementCount) {
152
			Bean bean = new Bean();
159
			Bean bean = new Bean();
153
			String propertyName = "set";
160
			String propertyName = "set";
154
			PropertyDescriptor propertyDescriptor;
155
			try {
156
				propertyDescriptor = new PropertyDescriptor(propertyName,
157
						Bean.class);
158
			} catch (IntrospectionException e) {
159
				throw new RuntimeException(e);
160
			}
161
161
162
			IObservableSet set = new JavaBeanObservableSet(realm,
162
			IObservableSet set = BeansObservables.observeSet(realm, bean,
163
					bean, propertyDescriptor, String.class);
163
					propertyName, String.class);
164
			for (int i = 0; i < elementCount; i++)
164
			for (int i = 0; i < elementCount; i++)
165
				set.add(createElement(set));
165
				set.add(createElement(set));
166
			return set;
166
			return set;
(-)src/org/eclipse/core/tests/internal/databinding/beans/BeanObservableSetDecoratorTest.java (-19 / +13 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Matthew Hall - bug 246625
10
 *     Matthew Hall - bug 246625, 194734
11
 ******************************************************************************/
11
 ******************************************************************************/
12
12
13
package org.eclipse.core.tests.internal.databinding.beans;
13
package org.eclipse.core.tests.internal.databinding.beans;
Lines 16-23 Link Here
16
16
17
import junit.framework.TestCase;
17
import junit.framework.TestCase;
18
18
19
import org.eclipse.core.databinding.beans.BeansObservables;
20
import org.eclipse.core.databinding.observable.set.IObservableSet;
19
import org.eclipse.core.internal.databinding.beans.BeanObservableSetDecorator;
21
import org.eclipse.core.internal.databinding.beans.BeanObservableSetDecorator;
20
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableSet;
21
import org.eclipse.jface.databinding.swt.SWTObservables;
22
import org.eclipse.jface.databinding.swt.SWTObservables;
22
import org.eclipse.swt.widgets.Display;
23
import org.eclipse.swt.widgets.Display;
23
24
Lines 26-62 Link Here
26
 */
27
 */
27
public class BeanObservableSetDecoratorTest extends TestCase {
28
public class BeanObservableSetDecoratorTest extends TestCase {
28
	private PropertyDescriptor propertyDescriptor;
29
	private PropertyDescriptor propertyDescriptor;
29
	private JavaBeanObservableSet observableSet;
30
	private IObservableSet observableSet;
30
	private BeanObservableSetDecorator decorator;
31
	private BeanObservableSetDecorator decorator;
31
	private Bean bean;
32
	private Bean bean;
32
33
33
	/*
34
	 * (non-Javadoc)
35
	 * 
36
	 * @see junit.framework.TestCase#setUp()
37
	 */
38
	protected void setUp() throws Exception {
34
	protected void setUp() throws Exception {
39
		super.setUp();
35
		super.setUp();
40
36
41
		bean = new Bean();
37
		bean = new Bean();
42
		propertyDescriptor = new PropertyDescriptor("set",
38
		propertyDescriptor = new PropertyDescriptor("set", Bean.class);
43
				Bean.class);
39
		observableSet = BeansObservables.observeSet(SWTObservables
44
		observableSet = new JavaBeanObservableSet(
40
				.getRealm(Display.getDefault()), bean, "set");
45
				SWTObservables.getRealm(Display.getDefault()), bean,
41
		decorator = new BeanObservableSetDecorator(observableSet,
46
				propertyDescriptor, String.class);
42
				propertyDescriptor);
47
		decorator = new BeanObservableSetDecorator(
48
				observableSet, propertyDescriptor);
49
	}
43
	}
50
44
51
	public void testGetDelegate() throws Exception {
45
	public void testGetDecorated() throws Exception {
52
		assertEquals(observableSet, decorator.getDecorated());
46
		assertSame(observableSet, decorator.getDecorated());
53
	}
47
	}
54
48
55
	public void testGetObserved() throws Exception {
49
	public void testGetObserved() throws Exception {
56
		assertEquals(bean, decorator.getObserved());
50
		assertSame(bean, decorator.getObserved());
57
	}
51
	}
58
52
59
	public void testGetPropertyDescriptor() throws Exception {
53
	public void testGetPropertyDescriptor() throws Exception {
60
		assertEquals(propertyDescriptor, decorator.getPropertyDescriptor());
54
		assertSame(propertyDescriptor, decorator.getPropertyDescriptor());
61
	}
55
	}
62
}
56
}
(-)src/org/eclipse/core/tests/internal/databinding/beans/BeanObservableListDecoratorTest.java (-14 / +8 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Matthew Hall - bugs 208858, 213145, 246625
10
 *     Matthew Hall - bugs 208858, 213145, 246625, 194734
11
 ******************************************************************************/
11
 ******************************************************************************/
12
12
13
package org.eclipse.core.tests.internal.databinding.beans;
13
package org.eclipse.core.tests.internal.databinding.beans;
Lines 18-30 Link Here
18
import junit.framework.TestCase;
18
import junit.framework.TestCase;
19
import junit.framework.TestSuite;
19
import junit.framework.TestSuite;
20
20
21
import org.eclipse.core.databinding.beans.BeansObservables;
21
import org.eclipse.core.databinding.observable.IObservable;
22
import org.eclipse.core.databinding.observable.IObservable;
22
import org.eclipse.core.databinding.observable.IObservableCollection;
23
import org.eclipse.core.databinding.observable.IObservableCollection;
23
import org.eclipse.core.databinding.observable.Realm;
24
import org.eclipse.core.databinding.observable.Realm;
24
import org.eclipse.core.databinding.observable.list.IObservableList;
25
import org.eclipse.core.databinding.observable.list.IObservableList;
25
import org.eclipse.core.databinding.observable.list.WritableList;
26
import org.eclipse.core.databinding.observable.list.WritableList;
26
import org.eclipse.core.internal.databinding.beans.BeanObservableListDecorator;
27
import org.eclipse.core.internal.databinding.beans.BeanObservableListDecorator;
27
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableList;
28
import org.eclipse.jface.databinding.conformance.MutableObservableListContractTest;
28
import org.eclipse.jface.databinding.conformance.MutableObservableListContractTest;
29
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
29
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
30
import org.eclipse.jface.databinding.swt.SWTObservables;
30
import org.eclipse.jface.databinding.swt.SWTObservables;
Lines 36-71 Link Here
36
public class BeanObservableListDecoratorTest extends TestCase {
36
public class BeanObservableListDecoratorTest extends TestCase {
37
	private Bean bean;
37
	private Bean bean;
38
	private PropertyDescriptor propertyDescriptor;
38
	private PropertyDescriptor propertyDescriptor;
39
	private JavaBeanObservableList observableList;
39
	private IObservableList observableList;
40
	private BeanObservableListDecorator decorator;
40
	private BeanObservableListDecorator decorator;
41
41
42
	/*
43
	 * (non-Javadoc)
44
	 * 
45
	 * @see junit.framework.TestCase#setUp()
46
	 */
47
	protected void setUp() throws Exception {
42
	protected void setUp() throws Exception {
48
		super.setUp();
43
		super.setUp();
49
		
44
		
50
		bean = new Bean();
45
		bean = new Bean();
51
		propertyDescriptor = new PropertyDescriptor(
46
		propertyDescriptor = new PropertyDescriptor(
52
				"list", Bean.class,"getList","setList");
47
				"list", Bean.class,"getList","setList");
53
		observableList = new JavaBeanObservableList(
48
		observableList = BeansObservables.observeList(
54
				SWTObservables.getRealm(Display.getDefault()), bean,
49
				SWTObservables.getRealm(Display.getDefault()), bean, "list");
55
				propertyDescriptor, Bean.class);
56
		decorator = new BeanObservableListDecorator(observableList, propertyDescriptor);
50
		decorator = new BeanObservableListDecorator(observableList, propertyDescriptor);
57
	}
51
	}
58
52
59
	public void testGetDelegate() throws Exception {
53
	public void testGetDelegate() throws Exception {
60
		assertEquals(observableList, decorator.getDecorated());
54
		assertSame(observableList, decorator.getDecorated());
61
	}
55
	}
62
56
63
	public void testGetObserved() throws Exception {
57
	public void testGetObserved() throws Exception {
64
		assertEquals(bean, decorator.getObserved());
58
		assertSame(bean, decorator.getObserved());
65
	}
59
	}
66
60
67
	public void testGetPropertyDescriptor() throws Exception {
61
	public void testGetPropertyDescriptor() throws Exception {
68
		assertEquals(propertyDescriptor, decorator.getPropertyDescriptor());
62
		assertSame(propertyDescriptor, decorator.getPropertyDescriptor());
69
	}
63
	}
70
64
71
	public static Test suite() {
65
	public static Test suite() {
(-)src/org/eclipse/jface/tests/internal/databinding/viewers/SelectionProviderMultiSelectionObservableListTest.java (-43 / +46 lines)
Lines 8-22 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *    Brad Reynolds - initial API and implementation
9
 *    Brad Reynolds - initial API and implementation
10
 *     Brad Reynolds - bug 116920
10
 *     Brad Reynolds - bug 116920
11
 *     Matthew Hall - bug 194734
11
 *******************************************************************************/
12
 *******************************************************************************/
12
package org.eclipse.jface.tests.internal.databinding.viewers;
13
package org.eclipse.jface.tests.internal.databinding.viewers;
13
14
15
import java.util.ArrayList;
16
import java.util.Arrays;
17
import java.util.Collections;
18
import java.util.List;
19
14
import junit.framework.TestCase;
20
import junit.framework.TestCase;
15
21
16
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
22
import org.eclipse.core.databinding.observable.list.IObservableList;
23
import org.eclipse.core.databinding.observable.list.ListDiff;
17
import org.eclipse.jface.databinding.conformance.util.ListChangeEventTracker;
24
import org.eclipse.jface.databinding.conformance.util.ListChangeEventTracker;
18
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.databinding.viewers.ViewersObservables;
19
import org.eclipse.jface.internal.databinding.viewers.SelectionProviderMultipleSelectionObservableList;
20
import org.eclipse.jface.viewers.ISelectionProvider;
26
import org.eclipse.jface.viewers.ISelectionProvider;
21
import org.eclipse.jface.viewers.IStructuredContentProvider;
27
import org.eclipse.jface.viewers.IStructuredContentProvider;
22
import org.eclipse.jface.viewers.IStructuredSelection;
28
import org.eclipse.jface.viewers.IStructuredSelection;
Lines 24-30 Link Here
24
import org.eclipse.jface.viewers.TableViewer;
30
import org.eclipse.jface.viewers.TableViewer;
25
import org.eclipse.jface.viewers.Viewer;
31
import org.eclipse.jface.viewers.Viewer;
26
import org.eclipse.swt.SWT;
32
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.Shell;
33
import org.eclipse.swt.widgets.Shell;
29
34
30
/**
35
/**
Lines 37-43 Link Here
37
42
38
	private TableViewer viewer;
43
	private TableViewer viewer;
39
44
40
	private static String[] model = new String[] { "0", "1", "2", "3" };
45
	private static String[] model = new String[] { "element0", "element1",
46
			"element2", "element3" };
41
47
42
	protected void setUp() throws Exception {
48
	protected void setUp() throws Exception {
43
		Shell shell = new Shell();
49
		Shell shell = new Shell();
Lines 55-62 Link Here
55
61
56
	public void testConstructorIllegalArgumentException() {
62
	public void testConstructorIllegalArgumentException() {
57
		try {
63
		try {
58
			new SelectionProviderMultipleSelectionObservableList(SWTObservables
64
			ViewersObservables.observeMultiSelection(null);
59
					.getRealm(Display.getDefault()), null, Object.class);
60
			fail();
65
			fail();
61
		} catch (IllegalArgumentException e) {
66
		} catch (IllegalArgumentException e) {
62
		}
67
		}
Lines 70-86 Link Here
70
	 * </ul>
75
	 * </ul>
71
	 */
76
	 */
72
	public void testAddRemove() {
77
	public void testAddRemove() {
73
		SelectionProviderMultipleSelectionObservableList observable = new SelectionProviderMultipleSelectionObservableList(
78
		IObservableList observable = ViewersObservables
74
				SWTObservables.getRealm(Display.getDefault()),
79
				.observeMultiSelection(selectionProvider);
75
				selectionProvider, Object.class);
76
		ListChangeEventTracker listener = new ListChangeEventTracker();
80
		ListChangeEventTracker listener = new ListChangeEventTracker();
77
		observable.addListChangeListener(listener);
81
		observable.addListChangeListener(listener);
78
		assertEquals(0, observable.size());
82
		assertEquals(0, observable.size());
79
83
80
		selectionProvider.setSelection(new StructuredSelection(model[0]));
84
		selectionProvider.setSelection(new StructuredSelection(model[0]));
81
		assertEquals(1, listener.count);
85
		assertEquals(1, listener.count);
82
		assertEquals(1, listener.event.diff.getDifferences().length);
86
		assertDiff(listener.event.diff, Collections.EMPTY_LIST, Collections
83
		assertDiffEntry(listener.event.diff.getDifferences()[0], 0, model[0], true);
87
				.singletonList(model[0]));
84
		assertEquals(observable, listener.event.getObservableList());
88
		assertEquals(observable, listener.event.getObservableList());
85
		assertEquals(1, observable.size());
89
		assertEquals(1, observable.size());
86
		assertEquals(model[0], observable.get(0));
90
		assertEquals(model[0], observable.get(0));
Lines 88-155 Link Here
88
		selectionProvider.setSelection(new StructuredSelection(model[1]));
92
		selectionProvider.setSelection(new StructuredSelection(model[1]));
89
		assertEquals(2, listener.count);
93
		assertEquals(2, listener.count);
90
		assertEquals(2, listener.event.diff.getDifferences().length);
94
		assertEquals(2, listener.event.diff.getDifferences().length);
91
		assertDiffEntry(listener.event.diff.getDifferences()[0], 0, model[1], true);
95
		assertDiff(listener.event.diff, Collections.singletonList(model[0]),
92
		assertDiffEntry(listener.event.diff.getDifferences()[1], 1, model[0], false);
96
				Collections.singletonList(model[1]));
93
		assertEquals(observable, listener.event.getObservableList());
97
		assertEquals(observable, listener.event.getObservableList());
94
		assertEquals(1, observable.size());
98
		assertEquals(1, observable.size());
95
		assertEquals(model[1], observable.get(0));
99
		assertEquals(model[1], observable.get(0));
96
100
97
		selectionProvider.setSelection(new StructuredSelection(new Object[]{model[2],model[3]}));
101
		selectionProvider.setSelection(new StructuredSelection(new Object[] {
102
				model[2], model[3] }));
98
		assertEquals(3, listener.count);
103
		assertEquals(3, listener.count);
99
		assertEquals(3, listener.event.diff.getDifferences().length);
104
		assertEquals(3, listener.event.diff.getDifferences().length);
100
		assertDiffEntry(listener.event.diff.getDifferences()[0], 0, model[2], true);
105
		assertDiff(listener.event.diff, Collections.singletonList(model[1]),
101
		assertDiffEntry(listener.event.diff.getDifferences()[1], 1, model[3], true);
106
				Arrays.asList(new Object[] { model[2], model[3] }));
102
		assertDiffEntry(listener.event.diff.getDifferences()[2], 2, model[1], false);
103
		assertEquals(observable, listener.event.getObservableList());
107
		assertEquals(observable, listener.event.getObservableList());
104
		assertEquals(2, observable.size());
108
		assertEquals(2, observable.size());
105
		assertEquals(model[2], observable.get(0));
109
		assertEquals(model[2], observable.get(0));
106
		assertEquals(model[3], observable.get(1));
110
		assertEquals(model[3], observable.get(1));
107
		
111
108
		selectionProvider.setSelection(StructuredSelection.EMPTY);
112
		selectionProvider.setSelection(StructuredSelection.EMPTY);
109
		assertEquals(4, listener.count);
113
		assertEquals(4, listener.count);
110
		assertEquals(2, listener.event.diff.getDifferences().length);
114
		assertEquals(2, listener.event.diff.getDifferences().length);
111
		assertDiffEntry(listener.event.diff.getDifferences()[0], 1, model[3], false);
115
		assertDiff(listener.event.diff, Arrays.asList(new Object[] { model[2],
112
		assertDiffEntry(listener.event.diff.getDifferences()[1], 0, model[2], false);
116
				model[3] }), Collections.EMPTY_LIST);
113
		assertEquals(observable, listener.event.getObservableList());
117
		assertEquals(observable, listener.event.getObservableList());
114
		assertEquals(0, observable.size());
118
		assertEquals(0, observable.size());
115
		
119
116
		observable.add(model[1]);
120
		observable.add(model[1]);
117
		assertEquals(5, listener.count);
121
		assertEquals(5, listener.count);
118
		assertEquals(1, listener.event.diff.getDifferences().length);
122
		assertEquals(1, listener.event.diff.getDifferences().length);
119
		assertDiffEntry(listener.event.diff.getDifferences()[0], 0, model[1], true);
123
		assertDiff(listener.event.diff, Collections.EMPTY_LIST, Collections
124
				.singletonList(model[1]));
120
		assertEquals(observable, listener.event.getObservableList());
125
		assertEquals(observable, listener.event.getObservableList());
121
		assertEquals(1, ((IStructuredSelection)viewer.getSelection()).size());
126
		assertEquals(1, ((IStructuredSelection) viewer.getSelection()).size());
122
127
123
		observable.add(0, model[2]);
128
		observable.add(0, model[2]);
124
		assertEquals(6, listener.count);
129
		assertEquals(6, listener.count);
125
		assertEquals(1, listener.event.diff.getDifferences().length);
130
		assertEquals(1, listener.event.diff.getDifferences().length);
126
		// This is a bit surprising (we added at index 0 but the event says index 1).
131
		// This is a bit surprising (we added at index 0 but the event says
127
		// It is to the fact that the observable list tracks the underlying selection
132
		// index 1).
133
		// It is to the fact that the observable list tracks the underlying
134
		// selection
128
		// provider's notion of which element is at which index.
135
		// provider's notion of which element is at which index.
129
		assertDiffEntry(listener.event.diff.getDifferences()[0], 1, model[2], true);
136
		assertDiff(listener.event.diff, Collections.singletonList(model[1]),
137
				Arrays.asList(new Object[] { model[2], model[1] }));
130
		assertEquals(observable, listener.event.getObservableList());
138
		assertEquals(observable, listener.event.getObservableList());
131
		assertEquals(2, ((IStructuredSelection)viewer.getSelection()).size());
139
		assertEquals(2, ((IStructuredSelection) viewer.getSelection()).size());
132
140
133
		observable.clear();
141
		observable.clear();
134
		assertEquals(7, listener.count);
142
		assertEquals(7, listener.count);
135
		assertEquals(2, listener.event.diff.getDifferences().length);
143
		assertEquals(2, listener.event.diff.getDifferences().length);
136
		assertDiffEntry(listener.event.diff.getDifferences()[0], 1, model[2], false);
144
		assertDiff(listener.event.diff, Arrays.asList(new Object[] { model[1],
137
		assertDiffEntry(listener.event.diff.getDifferences()[1], 0, model[1], false);
145
				model[2] }), Collections.EMPTY_LIST);
138
		assertEquals(observable, listener.event.getObservableList());
146
		assertEquals(observable, listener.event.getObservableList());
139
		assertEquals(0, ((IStructuredSelection)viewer.getSelection()).size());
147
		assertEquals(0, ((IStructuredSelection) viewer.getSelection()).size());
140
}
148
	}
141
149
142
	/**
150
	private void assertDiff(ListDiff diff, List oldList, List newList) {
143
	 * @param diffEntry
151
		// defensive copy in case arg is unmodifiable
144
	 * @param position
152
		oldList = new ArrayList(oldList);
145
	 * @param element
153
		diff.applyTo(oldList);
146
	 * @param isAddition
154
		assertEquals("applying diff to list did not produce expected result",
147
	 */
155
				newList, oldList);
148
	private void assertDiffEntry(ListDiffEntry diffEntry, int position,
149
			String element, boolean isAddition) {
150
		assertEquals(isAddition, diffEntry.isAddition());
151
		assertEquals(position, diffEntry.getPosition());
152
		assertEquals(element, diffEntry.getElement());
153
	}
156
	}
154
157
155
	private class ContentProvider implements IStructuredContentProvider {
158
	private class ContentProvider implements IStructuredContentProvider {
(-)src/org/eclipse/jface/tests/internal/databinding/viewers/SelectionProviderSingleSelectionObservableValueTest.java (-11 / +8 lines)
Lines 9-29 Link Here
9
 *    Brad Reynolds - initial API and implementation
9
 *    Brad Reynolds - initial API and implementation
10
 *    Brad Reynolds - bug 116920
10
 *    Brad Reynolds - bug 116920
11
 *    Ashley Cambrell - bug 198906
11
 *    Ashley Cambrell - bug 198906
12
 *    Matthew Hall - bug 194734
12
 *******************************************************************************/
13
 *******************************************************************************/
13
package org.eclipse.jface.tests.internal.databinding.viewers;
14
package org.eclipse.jface.tests.internal.databinding.viewers;
14
15
15
import junit.framework.TestCase;
16
import junit.framework.TestCase;
16
17
18
import org.eclipse.core.databinding.observable.value.IObservableValue;
17
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
19
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
18
import org.eclipse.jface.databinding.swt.SWTObservables;
20
import org.eclipse.jface.databinding.viewers.ViewersObservables;
19
import org.eclipse.jface.internal.databinding.viewers.SelectionProviderSingleSelectionObservableValue;
20
import org.eclipse.jface.viewers.ISelectionProvider;
21
import org.eclipse.jface.viewers.ISelectionProvider;
21
import org.eclipse.jface.viewers.IStructuredContentProvider;
22
import org.eclipse.jface.viewers.IStructuredContentProvider;
22
import org.eclipse.jface.viewers.StructuredSelection;
23
import org.eclipse.jface.viewers.StructuredSelection;
23
import org.eclipse.jface.viewers.TableViewer;
24
import org.eclipse.jface.viewers.TableViewer;
24
import org.eclipse.jface.viewers.Viewer;
25
import org.eclipse.jface.viewers.Viewer;
25
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.widgets.Display;
27
import org.eclipse.swt.widgets.Shell;
27
import org.eclipse.swt.widgets.Shell;
28
28
29
/**
29
/**
Lines 65-72 Link Here
65
65
66
	public void testConstructorIllegalArgumentException() {
66
	public void testConstructorIllegalArgumentException() {
67
		try {
67
		try {
68
			new SelectionProviderSingleSelectionObservableValue(SWTObservables
68
			ViewersObservables.observeSingleSelection(null);
69
					.getRealm(Display.getDefault()), null);
70
			fail();
69
			fail();
71
		} catch (IllegalArgumentException e) {
70
		} catch (IllegalArgumentException e) {
72
		}
71
		}
Lines 80-88 Link Here
80
	 * </ul>
79
	 * </ul>
81
	 */
80
	 */
82
	public void testGetSetValue() {
81
	public void testGetSetValue() {
83
		SelectionProviderSingleSelectionObservableValue observable = new SelectionProviderSingleSelectionObservableValue(
82
		IObservableValue observable = ViewersObservables
84
				SWTObservables.getRealm(Display.getDefault()),
83
				.observeSingleSelection(selectionProvider);
85
				selectionProvider);
86
		ValueChangeEventTracker listener = new ValueChangeEventTracker();
84
		ValueChangeEventTracker listener = new ValueChangeEventTracker();
87
		observable.addValueChangeListener(listener);
85
		observable.addValueChangeListener(listener);
88
		assertNull(observable.getValue());
86
		assertNull(observable.getValue());
Lines 110-118 Link Here
110
	}
108
	}
111
109
112
	public void testDispose() throws Exception {
110
	public void testDispose() throws Exception {
113
		SelectionProviderSingleSelectionObservableValue observable = new SelectionProviderSingleSelectionObservableValue(
111
		IObservableValue observable = ViewersObservables
114
				SWTObservables.getRealm(Display.getDefault()),
112
				.observeSingleSelection(selectionProvider);
115
				selectionProvider);
116
		ValueChangeEventTracker listener = new ValueChangeEventTracker();
113
		ValueChangeEventTracker listener = new ValueChangeEventTracker();
117
		observable.addValueChangeListener(listener);
114
		observable.addValueChangeListener(listener);
118
115
(-)src/org/eclipse/jface/tests/databinding/BindingTestSuite.java (-28 / +33 lines)
Lines 77-82 Link Here
77
import org.eclipse.core.tests.internal.databinding.beans.BeanObservableListDecoratorTest;
77
import org.eclipse.core.tests.internal.databinding.beans.BeanObservableListDecoratorTest;
78
import org.eclipse.core.tests.internal.databinding.beans.BeanObservableSetDecoratorTest;
78
import org.eclipse.core.tests.internal.databinding.beans.BeanObservableSetDecoratorTest;
79
import org.eclipse.core.tests.internal.databinding.beans.BeanObservableValueDecoratorTest;
79
import org.eclipse.core.tests.internal.databinding.beans.BeanObservableValueDecoratorTest;
80
import org.eclipse.core.tests.internal.databinding.beans.BeanPropertyListenerSupportTest;
81
import org.eclipse.core.tests.internal.databinding.beans.BeanValuePropertyTest;
80
import org.eclipse.core.tests.internal.databinding.beans.JavaBeanObservableArrayBasedListTest;
82
import org.eclipse.core.tests.internal.databinding.beans.JavaBeanObservableArrayBasedListTest;
81
import org.eclipse.core.tests.internal.databinding.beans.JavaBeanObservableArrayBasedSetTest;
83
import org.eclipse.core.tests.internal.databinding.beans.JavaBeanObservableArrayBasedSetTest;
82
import org.eclipse.core.tests.internal.databinding.beans.JavaBeanObservableListTest;
84
import org.eclipse.core.tests.internal.databinding.beans.JavaBeanObservableListTest;
Lines 84-90 Link Here
84
import org.eclipse.core.tests.internal.databinding.beans.JavaBeanObservableSetTest;
86
import org.eclipse.core.tests.internal.databinding.beans.JavaBeanObservableSetTest;
85
import org.eclipse.core.tests.internal.databinding.beans.JavaBeanObservableValueTest;
87
import org.eclipse.core.tests.internal.databinding.beans.JavaBeanObservableValueTest;
86
import org.eclipse.core.tests.internal.databinding.beans.JavaBeanPropertyObservableMapTest;
88
import org.eclipse.core.tests.internal.databinding.beans.JavaBeanPropertyObservableMapTest;
87
import org.eclipse.core.tests.internal.databinding.beans.ListenerSupportTest;
88
import org.eclipse.core.tests.internal.databinding.conversion.DateConversionSupportTest;
89
import org.eclipse.core.tests.internal.databinding.conversion.DateConversionSupportTest;
89
import org.eclipse.core.tests.internal.databinding.conversion.IdentityConverterTest;
90
import org.eclipse.core.tests.internal.databinding.conversion.IdentityConverterTest;
90
import org.eclipse.core.tests.internal.databinding.conversion.IntegerToStringConverterTest;
91
import org.eclipse.core.tests.internal.databinding.conversion.IntegerToStringConverterTest;
Lines 163-169 Link Here
163
import org.eclipse.jface.tests.internal.databinding.swt.LabelObservableValueTest;
164
import org.eclipse.jface.tests.internal.databinding.swt.LabelObservableValueTest;
164
import org.eclipse.jface.tests.internal.databinding.swt.ListSingleSelectionObservableValueTest;
165
import org.eclipse.jface.tests.internal.databinding.swt.ListSingleSelectionObservableValueTest;
165
import org.eclipse.jface.tests.internal.databinding.swt.SWTDelayedObservableValueDecoratorTest;
166
import org.eclipse.jface.tests.internal.databinding.swt.SWTDelayedObservableValueDecoratorTest;
166
import org.eclipse.jface.tests.internal.databinding.swt.SWTObservableListTest;
167
import org.eclipse.jface.tests.internal.databinding.swt.ScaleObservableValueMaxTest;
167
import org.eclipse.jface.tests.internal.databinding.swt.ScaleObservableValueMaxTest;
168
import org.eclipse.jface.tests.internal.databinding.swt.ScaleObservableValueMinTest;
168
import org.eclipse.jface.tests.internal.databinding.swt.ScaleObservableValueMinTest;
169
import org.eclipse.jface.tests.internal.databinding.swt.ScaleObservableValueSelectionTest;
169
import org.eclipse.jface.tests.internal.databinding.swt.ScaleObservableValueSelectionTest;
Lines 172-177 Link Here
172
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueMinTest;
172
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueMinTest;
173
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueSelectionTest;
173
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueSelectionTest;
174
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueTest;
174
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueTest;
175
import org.eclipse.jface.tests.internal.databinding.swt.StyledTextObservableValueFocusOutTest;
176
import org.eclipse.jface.tests.internal.databinding.swt.StyledTextObservableValueModifyTest;
177
import org.eclipse.jface.tests.internal.databinding.swt.StyledTextObservableValueTest;
175
import org.eclipse.jface.tests.internal.databinding.swt.TableObservableValueTest;
178
import org.eclipse.jface.tests.internal.databinding.swt.TableObservableValueTest;
176
import org.eclipse.jface.tests.internal.databinding.swt.TableSingleSelectionObservableValueTest;
179
import org.eclipse.jface.tests.internal.databinding.swt.TableSingleSelectionObservableValueTest;
177
import org.eclipse.jface.tests.internal.databinding.swt.TextEditableObservableValueTest;
180
import org.eclipse.jface.tests.internal.databinding.swt.TextEditableObservableValueTest;
Lines 198-204 Link Here
198
	}
201
	}
199
202
200
	public BindingTestSuite() {
203
	public BindingTestSuite() {
201
		
204
202
		// org.eclipse.core.tests.databinding
205
		// org.eclipse.core.tests.databinding
203
		addTestSuite(AggregateValidationStatusTest.class);
206
		addTestSuite(AggregateValidationStatusTest.class);
204
		addTestSuite(BindingTest.class);
207
		addTestSuite(BindingTest.class);
Lines 253-271 Link Here
253
		addTest(ObservableSetTest.suite());
256
		addTest(ObservableSetTest.suite());
254
		addTest(UnionSetTest.suite());
257
		addTest(UnionSetTest.suite());
255
		addTest(WritableSetTest.suite());
258
		addTest(WritableSetTest.suite());
256
		
259
257
		//org.eclipse.core.tests.databinding.observable.value
260
		// org.eclipse.core.tests.databinding.observable.value
258
		addTestSuite(AbstractObservableValueTest.class);
261
		addTestSuite(AbstractObservableValueTest.class);
259
		addTestSuite(AbstractVetoableValueTest.class);
262
		addTestSuite(AbstractVetoableValueTest.class);
260
		addTestSuite(ComputedValueTest.class);
263
		addTestSuite(ComputedValueTest.class);
261
		addTest(DecoratingObservableValueTest.suite());
264
		addTest(DecoratingObservableValueTest.suite());
262
		addTest(SelectObservableValueTest.suite());
265
		addTest(SelectObservableValueTest.suite());
263
		addTest(WritableValueTest.suite());
266
		addTest(WritableValueTest.suite());
264
		
267
265
		//org.eclipse.core.tests.databinding.validation
268
		// org.eclipse.core.tests.databinding.validation
266
		addTestSuite(MultiValidatorTest.class);
269
		addTestSuite(MultiValidatorTest.class);
267
		addTestSuite(ValidationStatusTest.class);
270
		addTestSuite(ValidationStatusTest.class);
268
		
271
269
		// org.eclipse.core.tests.internal.databinding
272
		// org.eclipse.core.tests.internal.databinding
270
		addTestSuite(BindingMessagesTest.class);
273
		addTestSuite(BindingMessagesTest.class);
271
		addTestSuite(BindingStatusTest.class);
274
		addTestSuite(BindingStatusTest.class);
Lines 298-308 Link Here
298
		addTestSuite(StringToNumberParserTest.class);
301
		addTestSuite(StringToNumberParserTest.class);
299
		addTestSuite(StringToShortConverterTest.class);
302
		addTestSuite(StringToShortConverterTest.class);
300
303
301
		//org.eclipse.core.tests.internal.databinding.internal.beans
304
		// org.eclipse.core.tests.internal.databinding.internal.beans
302
		addTest(BeanObservableListDecoratorTest.suite());
305
		addTest(BeanObservableListDecoratorTest.suite());
303
		addTestSuite(BeanObservableSetDecoratorTest.class);
306
		addTestSuite(BeanObservableSetDecoratorTest.class);
304
		addTestSuite(BeanObservableValueDecoratorTest.class);
307
		addTestSuite(BeanObservableValueDecoratorTest.class);
305
		addTestSuite(BeanObservableListDecoratorTest.class);
308
		addTestSuite(BeanObservableListDecoratorTest.class);
309
		addTestSuite(BeanValuePropertyTest.class);
306
		addTest(JavaBeanObservableArrayBasedListTest.suite());
310
		addTest(JavaBeanObservableArrayBasedListTest.suite());
307
		addTest(JavaBeanObservableArrayBasedSetTest.suite());
311
		addTest(JavaBeanObservableArrayBasedSetTest.suite());
308
		addTest(JavaBeanObservableListTest.suite());
312
		addTest(JavaBeanObservableListTest.suite());
Lines 310-318 Link Here
310
		addTest(JavaBeanObservableSetTest.suite());
314
		addTest(JavaBeanObservableSetTest.suite());
311
		addTest(JavaBeanObservableValueTest.suite());
315
		addTest(JavaBeanObservableValueTest.suite());
312
		addTestSuite(JavaBeanPropertyObservableMapTest.class);
316
		addTestSuite(JavaBeanPropertyObservableMapTest.class);
313
		addTestSuite(ListenerSupportTest.class);
317
		addTestSuite(BeanPropertyListenerSupportTest.class);
314
		
318
315
		//org.eclipse.core.tests.internal.databinding.observable
319
		// org.eclipse.core.tests.internal.databinding.observable
316
		addTest(ConstantObservableValueTest.suite());
320
		addTest(ConstantObservableValueTest.suite());
317
		addTest(DelayedObservableValueTest.suite());
321
		addTest(DelayedObservableValueTest.suite());
318
		addTest(EmptyObservableListTest.suite());
322
		addTest(EmptyObservableListTest.suite());
Lines 325-332 Link Here
325
		addTest(ValidatedObservableValueTest.suite());
329
		addTest(ValidatedObservableValueTest.suite());
326
		addTest(ValidatedObservableListTest.suite());
330
		addTest(ValidatedObservableListTest.suite());
327
		addTest(ValidatedObservableSetTest.suite());
331
		addTest(ValidatedObservableSetTest.suite());
328
//		addTest(ValidatedObservableMapTest.suite());
332
		// addTest(ValidatedObservableMapTest.suite());
329
		
333
330
		// org.eclipse.core.tests.internal.databinding.observable.masterdetail
334
		// org.eclipse.core.tests.internal.databinding.observable.masterdetail
331
		addTest(DetailObservableListTest.suite());
335
		addTest(DetailObservableListTest.suite());
332
		addTest(DetailObservableSetTest.suite());
336
		addTest(DetailObservableSetTest.suite());
Lines 353-376 Link Here
353
		addTest(BindingScenariosTestSuite.suite());
357
		addTest(BindingScenariosTestSuite.suite());
354
		// The files in this package are in the above test suite
358
		// The files in this package are in the above test suite
355
359
356
		//org.eclipse.jface.tests.databinding.swt
360
		// org.eclipse.jface.tests.databinding.swt
357
		addTestSuite(SWTObservablesTest.class);
361
		addTestSuite(SWTObservablesTest.class);
358
		
362
359
		// org.eclipse.jface.tests.databinding.viewers
363
		// org.eclipse.jface.tests.databinding.viewers
360
		addTestSuite(ObservableListTreeContentProviderTest.class);
364
		addTestSuite(ObservableListTreeContentProviderTest.class);
361
		addTestSuite(ObservableMapLabelProviderTest.class);
365
		addTestSuite(ObservableMapLabelProviderTest.class);
362
		addTestSuite(ObservableSetContentProviderTest.class);
366
		addTestSuite(ObservableSetContentProviderTest.class);
363
		addTestSuite(ObservableSetTreeContentProviderTest.class);
367
		addTestSuite(ObservableSetTreeContentProviderTest.class);
364
		addTestSuite(ViewersObservablesTest.class);
368
		addTestSuite(ViewersObservablesTest.class);
365
		
369
366
		// org.eclipse.jface.tests.databinding.wizard
370
		// org.eclipse.jface.tests.databinding.wizard
367
		addTestSuite(WizardPageSupportTest.class);
371
		addTestSuite(WizardPageSupportTest.class);
368
		
372
369
		//org.eclipse.jface.tests.example.databinding.mask.internal
373
		// org.eclipse.jface.tests.example.databinding.mask.internal
370
		addTestSuite(EditMaskLexerAndTokenTest.class);
374
		addTestSuite(EditMaskLexerAndTokenTest.class);
371
		addTestSuite(EditMaskParserTest.class);
375
		addTestSuite(EditMaskParserTest.class);
372
376
373
		//org.eclipse.jface.tests.internal.databinding.internal.swt
377
		// org.eclipse.jface.tests.internal.databinding.internal.swt
374
		addTest(ButtonObservableValueTest.suite());
378
		addTest(ButtonObservableValueTest.suite());
375
		addTestSuite(CComboObservableValueTest.class);
379
		addTestSuite(CComboObservableValueTest.class);
376
		addTest(CComboObservableValueSelectionTest.suite());
380
		addTest(CComboObservableValueSelectionTest.suite());
Lines 383-413 Link Here
383
		addTest(ComboObservableValueTextTest.suite());
387
		addTest(ComboObservableValueTextTest.suite());
384
		addTestSuite(ComboSingleSelectionObservableValueTest.class);
388
		addTestSuite(ComboSingleSelectionObservableValueTest.class);
385
		addTest(SWTDelayedObservableValueDecoratorTest.suite());
389
		addTest(SWTDelayedObservableValueDecoratorTest.suite());
386
		
390
387
		addTest(SWTObservableListTest.suite());
388
		
389
		addTestSuite(ControlObservableValueTest.class);
391
		addTestSuite(ControlObservableValueTest.class);
390
		addTest(LabelObservableValueTest.suite());
392
		addTest(LabelObservableValueTest.suite());
391
		addTestSuite(ListSingleSelectionObservableValueTest.class);
393
		addTestSuite(ListSingleSelectionObservableValueTest.class);
392
		addTest(ScaleObservableValueMinTest.suite());
394
		addTest(ScaleObservableValueMinTest.suite());
393
		addTest(ScaleObservableValueMaxTest.suite());
395
		addTest(ScaleObservableValueMaxTest.suite());
394
		addTest(ScaleObservableValueSelectionTest.suite());
396
		addTest(ScaleObservableValueSelectionTest.suite());
395
		
397
396
		addTest(ShellObservableValueTest.suite());
398
		addTest(ShellObservableValueTest.suite());
397
		
399
398
		addTestSuite(SpinnerObservableValueTest.class);
400
		addTestSuite(SpinnerObservableValueTest.class);
399
		addTest(SpinnerObservableValueMinTest.suite());
401
		addTest(SpinnerObservableValueMinTest.suite());
400
		addTest(SpinnerObservableValueMaxTest.suite());
402
		addTest(SpinnerObservableValueMaxTest.suite());
401
		addTest(SpinnerObservableValueSelectionTest.suite());
403
		addTest(SpinnerObservableValueSelectionTest.suite());
402
		
404
403
		addTestSuite(TableObservableValueTest.class);
405
		addTestSuite(TableObservableValueTest.class);
404
		addTest(TableSingleSelectionObservableValueTest.suite());
406
		addTest(TableSingleSelectionObservableValueTest.suite());
405
		addTest(TextEditableObservableValueTest.suite());
407
		addTest(TextEditableObservableValueTest.suite());
406
		addTest(TextObservableValueFocusOutTest.suite());
408
		addTest(TextObservableValueFocusOutTest.suite());
407
		addTest(TextObservableValueModifyTest.suite());
409
		addTest(TextObservableValueModifyTest.suite());
408
		addTestSuite(TextObservableValueTest.class);
410
		addTestSuite(TextObservableValueTest.class);
409
		
411
		addTest(StyledTextObservableValueFocusOutTest.suite());
410
		//org.eclipse.jface.tests.internal.databinding.internal.viewers
412
		addTest(StyledTextObservableValueModifyTest.suite());
413
		addTestSuite(StyledTextObservableValueTest.class);
414
415
		// org.eclipse.jface.tests.internal.databinding.internal.viewers
411
		addTest(ObservableViewerElementSetTest.suite());
416
		addTest(ObservableViewerElementSetTest.suite());
412
		addTestSuite(ObservableCollectionTreeContentProviderTest.class);
417
		addTestSuite(ObservableCollectionTreeContentProviderTest.class);
413
		addTestSuite(SelectionProviderMultiSelectionObservableListTest.class);
418
		addTestSuite(SelectionProviderMultiSelectionObservableListTest.class);
(-)src/org/eclipse/core/tests/databinding/conversion/StringToNumberConverterTest.java (-1 / +1 lines)
Lines 112-118 Link Here
112
		assertEquals("Non-integer BigDecimal", input, result);
112
		assertEquals("Non-integer BigDecimal", input, result);
113
113
114
		// Test 2: Long
114
		// Test 2: Long
115
		input = new BigDecimal((long) (Integer.MAX_VALUE + 100));
115
		input = new BigDecimal(Integer.MAX_VALUE + 100L);
116
		result = (BigDecimal) converter.convert(formatBigDecimal(input));
116
		result = (BigDecimal) converter.convert(formatBigDecimal(input));
117
		assertEquals("Integral BigDecimal in long range", input, result);
117
		assertEquals("Integral BigDecimal in long range", input, result);
118
118
(-)src/org/eclipse/jface/tests/databinding/viewers/ViewersObservablesTest.java (-4 / +11 lines)
Lines 7-21 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation (bug 206839)
9
 *     Matthew Hall - initial API and implementation (bug 206839)
10
 *     Matthew Hall - bug 194734
10
 ******************************************************************************/
11
 ******************************************************************************/
11
12
12
package org.eclipse.jface.tests.databinding.viewers;
13
package org.eclipse.jface.tests.databinding.viewers;
13
14
15
import org.eclipse.core.databinding.observable.IDecoratingObservable;
14
import org.eclipse.core.databinding.observable.Realm;
16
import org.eclipse.core.databinding.observable.Realm;
15
import org.eclipse.core.databinding.observable.value.IObservableValue;
17
import org.eclipse.core.databinding.property.IPropertyObservable;
16
import org.eclipse.jface.databinding.swt.SWTObservables;
18
import org.eclipse.jface.databinding.swt.SWTObservables;
19
import org.eclipse.jface.databinding.viewers.IViewerObservableValue;
17
import org.eclipse.jface.databinding.viewers.ViewersObservables;
20
import org.eclipse.jface.databinding.viewers.ViewersObservables;
18
import org.eclipse.jface.internal.databinding.viewers.ViewerInputObservableValue;
21
import org.eclipse.jface.internal.databinding.viewers.ViewerInputProperty;
19
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
22
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
20
import org.eclipse.jface.viewers.TableViewer;
23
import org.eclipse.jface.viewers.TableViewer;
21
import org.eclipse.swt.SWT;
24
import org.eclipse.swt.SWT;
Lines 48-54 Link Here
48
	}
51
	}
49
52
50
	public void testObserveInput_InstanceOfViewerInputObservableValue() {
53
	public void testObserveInput_InstanceOfViewerInputObservableValue() {
51
		IObservableValue observable = ViewersObservables.observeInput(viewer);
54
		IViewerObservableValue observable = (IViewerObservableValue) ViewersObservables
52
		assertTrue(observable instanceof ViewerInputObservableValue);
55
				.observeInput(viewer);
56
		assertTrue(observable.getViewer() == viewer);
57
		IPropertyObservable propertyObservable = (IPropertyObservable) ((IDecoratingObservable) observable)
58
				.getDecorated();
59
		assertTrue(propertyObservable.getProperty() instanceof ViewerInputProperty);
53
	}
60
	}
54
}
61
}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/StyledTextObservableValueFocusOutTest.java (+80 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Code 9 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
 *     Code 9 Corporation - initial API and implementation
10
 *     Chris Aniszczyk <zx@code9.com> - bug 131435
11
 *     Matthew Hall - bug 194734
12
 *******************************************************************************/
13
14
package org.eclipse.jface.tests.internal.databinding.swt;
15
16
import junit.framework.Test;
17
import junit.framework.TestCase;
18
import junit.framework.TestSuite;
19
20
import org.eclipse.core.databinding.observable.IObservable;
21
import org.eclipse.core.databinding.observable.Realm;
22
import org.eclipse.core.databinding.observable.value.IObservableValue;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
25
import org.eclipse.jface.databinding.swt.StyledTextProperties;
26
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.custom.StyledText;
28
import org.eclipse.swt.widgets.Shell;
29
30
/**
31
 * Tests for the FocusOut version of StyledTextObservableValue.
32
 */
33
public class StyledTextObservableValueFocusOutTest extends TestCase {
34
	public static Test suite() {
35
		TestSuite suite = new TestSuite(
36
				StyledTextObservableValueFocusOutTest.class.toString());
37
		suite.addTest(SWTMutableObservableValueContractTest
38
				.suite(new Delegate()));
39
		return suite;
40
	}
41
42
	/* package */static class Delegate extends
43
			AbstractObservableValueContractDelegate {
44
		private Shell shell;
45
46
		private StyledText text;
47
48
		public void setUp() {
49
			shell = new Shell();
50
			text = new StyledText(shell, SWT.NONE);
51
		}
52
53
		public void tearDown() {
54
			shell.dispose();
55
		}
56
57
		public IObservableValue createObservableValue(Realm realm) {
58
			return StyledTextProperties.text(SWT.FocusOut).observeValue(realm,
59
					text);
60
		}
61
62
		public Object getValueType(IObservableValue observable) {
63
			return String.class;
64
		}
65
66
		public void change(IObservable observable) {
67
			text.setFocus();
68
69
			IObservableValue observableValue = (IObservableValue) observable;
70
			text.setText((String) createValue(observableValue));
71
72
			text.notifyListeners(SWT.FocusOut, null);
73
		}
74
75
		public Object createValue(IObservableValue observable) {
76
			String value = (String) observable.getValue();
77
			return value + "a";
78
		}
79
	}
80
}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/StyledTextObservableValueTest.java (+112 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Code 9 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
 *     Code 9 Corporation - initial API and implementation
10
 *     Chris Aniszczyk <zx@code9.com> - bug 131435
11
 *     Matthew Hall - bug 194734
12
 *******************************************************************************/
13
14
package org.eclipse.jface.tests.internal.databinding.swt;
15
16
import org.eclipse.core.databinding.observable.value.IObservableValue;
17
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
18
import org.eclipse.jface.databinding.swt.SWTObservables;
19
import org.eclipse.jface.internal.databinding.swt.StyledTextTextProperty;
20
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
21
import org.eclipse.swt.SWT;
22
import org.eclipse.swt.custom.StyledText;
23
import org.eclipse.swt.widgets.Shell;
24
25
/**
26
 * Tests to assert the inputs of the StyledTextObservableValue constructor.
27
 */
28
public class StyledTextObservableValueTest extends AbstractDefaultRealmTestCase {
29
	private StyledText text;
30
	private ValueChangeEventTracker listener;
31
32
	protected void setUp() throws Exception {
33
		super.setUp();
34
35
		Shell shell = new Shell();
36
		text = new StyledText(shell, SWT.NONE);
37
38
		listener = new ValueChangeEventTracker();
39
	}
40
41
	/**
42
	 * Asserts that only valid SWT event types are accepted on construction of
43
	 * StyledTextObservableValue.
44
	 */
45
	public void testConstructorUpdateEventTypes() {
46
		try {
47
			new StyledTextTextProperty(SWT.NONE);
48
			new StyledTextTextProperty(SWT.FocusOut);
49
			new StyledTextTextProperty(SWT.Modify);
50
			assertTrue(true);
51
		} catch (IllegalArgumentException e) {
52
			fail();
53
		}
54
55
		try {
56
			new StyledTextTextProperty(SWT.Verify);
57
			fail();
58
		} catch (IllegalArgumentException e) {
59
			assertTrue(true);
60
		}
61
	}
62
63
	/**
64
	 * s
65
	 * 
66
	 * @throws Exception
67
	 */
68
	public void testGetValueBeforeFocusOutChangeEventsFire() throws Exception {
69
		IObservableValue observableValue = SWTObservables.observeText(text,
70
				SWT.FocusOut);
71
		observableValue.addValueChangeListener(listener);
72
73
		String a = "a";
74
		String b = "b";
75
76
		text.setText(a);
77
		
78
		// fetching the value updates the buffered value
79
		assertEquals(a, observableValue.getValue()); 
80
		assertEquals(1, listener.count);
81
82
		text.setText(b);
83
84
		text.notifyListeners(SWT.FocusOut, null);
85
86
		assertEquals(2, listener.count);
87
		assertEquals(a, listener.event.diff.getOldValue());
88
		assertEquals(b, listener.event.diff.getNewValue());
89
	}
90
91
	public void testDispose() throws Exception {
92
		IObservableValue observableValue = SWTObservables.observeText(text,
93
				SWT.Modify);
94
		ValueChangeEventTracker testCounterValueChangeListener = new ValueChangeEventTracker();
95
		observableValue.addValueChangeListener(testCounterValueChangeListener);
96
97
		String expected1 = "Test123";
98
		text.setText(expected1);
99
100
		assertEquals(1, testCounterValueChangeListener.count);
101
		assertEquals(expected1, text.getText());
102
		assertEquals(expected1, observableValue.getValue());
103
104
		observableValue.dispose();
105
106
		String expected2 = "NewValue123";
107
		text.setText(expected2);
108
109
		assertEquals(1, testCounterValueChangeListener.count);
110
		assertEquals(expected2, text.getText());
111
	}
112
}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/StyledTextObservableValueModifyTest.java (+78 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Code 9 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
 *     Code 9 Corporation - initial API and implementation
10
 *     Chris Aniszczyk <zx@code9.com> - bug 131435
11
 *     Matthew Hall - bug 194734
12
 *******************************************************************************/
13
14
package org.eclipse.jface.tests.internal.databinding.swt;
15
16
import junit.framework.Test;
17
import junit.framework.TestCase;
18
import junit.framework.TestSuite;
19
20
import org.eclipse.core.databinding.observable.IObservable;
21
import org.eclipse.core.databinding.observable.Realm;
22
import org.eclipse.core.databinding.observable.value.IObservableValue;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
25
import org.eclipse.jface.databinding.swt.StyledTextProperties;
26
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.custom.StyledText;
28
import org.eclipse.swt.widgets.Shell;
29
30
/**
31
 * Tests for the Modify version of StyledTextObservableValue.
32
 */
33
public class StyledTextObservableValueModifyTest extends TestCase {
34
	public static Test suite() {
35
		TestSuite suite = new TestSuite(
36
				StyledTextObservableValueModifyTest.class.toString());
37
		suite.addTest(SWTMutableObservableValueContractTest
38
				.suite(new Delegate()));
39
		return suite;
40
	}
41
42
	/* package */static class Delegate extends
43
			AbstractObservableValueContractDelegate {
44
		private Shell shell;
45
46
		private StyledText text;
47
48
		public void setUp() {
49
			shell = new Shell();
50
			text = new StyledText(shell, SWT.NONE);
51
		}
52
53
		public void tearDown() {
54
			shell.dispose();
55
		}
56
57
		public IObservableValue createObservableValue(Realm realm) {
58
			return StyledTextProperties.text(SWT.Modify).observeValue(realm,
59
					text);
60
		}
61
62
		public Object getValueType(IObservableValue observable) {
63
			return String.class;
64
		}
65
66
		public void change(IObservable observable) {
67
			text.setFocus();
68
69
			IObservableValue observableValue = (IObservableValue) observable;
70
			text.setText((String) createValue(observableValue));
71
		}
72
73
		public Object createValue(IObservableValue observable) {
74
			String value = (String) observable.getValue();
75
			return value + "a";
76
		}
77
	}
78
}
(-)src/org/eclipse/jface/databinding/conformance/MutableObservableListContractTest.java (-227 / +138 lines)
Lines 21-33 Link Here
21
import junit.framework.Test;
21
import junit.framework.Test;
22
22
23
import org.eclipse.core.databinding.observable.list.IObservableList;
23
import org.eclipse.core.databinding.observable.list.IObservableList;
24
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
24
import org.eclipse.core.databinding.observable.list.ListDiff;
25
import org.eclipse.jface.databinding.conformance.delegate.IObservableCollectionContractDelegate;
25
import org.eclipse.jface.databinding.conformance.delegate.IObservableCollectionContractDelegate;
26
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
26
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
27
import org.eclipse.jface.databinding.conformance.util.ListChangeEventTracker;
27
import org.eclipse.jface.databinding.conformance.util.ListChangeEventTracker;
28
import org.eclipse.jface.databinding.conformance.util.SuiteBuilder;
28
import org.eclipse.jface.databinding.conformance.util.SuiteBuilder;
29
29
30
31
/**
30
/**
32
 * Mutability tests for IObservableList.
31
 * Mutability tests for IObservableList.
33
 * 
32
 * 
Lines 68-89 Link Here
68
	}
67
	}
69
68
70
	public void testAdd_ListChangeEvent() throws Exception {
69
	public void testAdd_ListChangeEvent() throws Exception {
70
		final Object element = delegate.createElement(list);
71
		assertListChangeEventFired(new Runnable() {
71
		assertListChangeEventFired(new Runnable() {
72
			public void run() {
72
			public void run() {
73
				list.add(delegate.createElement(list));
73
				list.add(element);
74
			}
74
			}
75
		}, "List.add(Object)", list);
75
		}, "List.add(Object)", list, Collections.singletonList(element));
76
	}
76
	}
77
77
78
	public void testAdd_ListDiffEntry() throws Exception {
78
	public void testAdd_ListDiffEntry() throws Exception {
79
		list.add(delegate.createElement(list));
79
		Object element0 = delegate.createElement(list);
80
		final Object element = delegate.createElement(list);
80
		list.add(element0);
81
		final Object element1 = delegate.createElement(list);
81
82
82
		assertAddDiffEntry(new Runnable() {
83
		assertListChangeEventFired(new Runnable() {
83
			public void run() {
84
			public void run() {
84
				list.add(element);
85
				list.add(element1);
85
			}
86
			}
86
		}, "List.add(Object)", list, element, 1);
87
		}, "List.add(Object)", list, Arrays.asList(new Object[] { element0,
88
				element1 }));
87
	}
89
	}
88
90
89
	public void testAddAtIndex_ChangeEvent() throws Exception {
91
	public void testAddAtIndex_ChangeEvent() throws Exception {
Lines 95-105 Link Here
95
	}
97
	}
96
98
97
	public void testAddAtIndex_ListChangeEvent() throws Exception {
99
	public void testAddAtIndex_ListChangeEvent() throws Exception {
100
		final Object element = delegate.createElement(list);
98
		assertListChangeEventFired(new Runnable() {
101
		assertListChangeEventFired(new Runnable() {
99
			public void run() {
102
			public void run() {
100
				list.add(0, delegate.createElement(list));
103
				list.add(0, element);
101
			}
104
			}
102
		}, "List.add(int, Object)", list);
105
		}, "List.add(int, Object)", list, Collections.singletonList(element));
103
	}
106
	}
104
107
105
	public void testAddAtIndex_ChangeEventFiredAfterElementIsAdded()
108
	public void testAddAtIndex_ChangeEventFiredAfterElementIsAdded()
Lines 114-157 Link Here
114
	}
117
	}
115
118
116
	public void testAddAtIndex_ListDiffEntry() throws Exception {
119
	public void testAddAtIndex_ListDiffEntry() throws Exception {
117
		list.add(delegate.createElement(list));
120
		Object element0 = delegate.createElement(list);
118
		final Object element = delegate.createElement(list);
121
		list.add(element0);
122
		final Object element1 = delegate.createElement(list);
119
123
120
		assertAddDiffEntry(new Runnable() {
124
		assertListChangeEventFired(new Runnable() {
121
			public void run() {
125
			public void run() {
122
				list.add(1, element);
126
				list.add(1, element1);
123
			}
127
			}
124
		}, "List.add(int, Object)", list, element, 1);
128
		}, "List.add(int, Object)", list, Arrays.asList(new Object[] {
129
				element0, element1 }));
125
	}
130
	}
126
131
127
	public void testAddAll_ListChangeEvent() throws Exception {
132
	public void testAddAll_ListChangeEvent() throws Exception {
133
		final Object element = delegate.createElement(list);
128
		assertListChangeEventFired(new Runnable() {
134
		assertListChangeEventFired(new Runnable() {
129
			public void run() {
135
			public void run() {
130
				list.addAll(Arrays.asList(new Object[] { delegate
136
				list.addAll(Collections.singletonList(element));
131
						.createElement(list) }));
132
			}
137
			}
133
		}, "List.addAll(Collection", list);
138
		}, "List.addAll(Collection", list, Collections.singletonList(element));
134
	}
139
	}
135
140
136
	public void testAddAll_ListDiffEntry() throws Exception {
141
	public void testAddAll_ListDiffEntry() throws Exception {
137
		final Object element = delegate.createElement(list);
142
		final Object element = delegate.createElement(list);
138
143
139
		assertAddDiffEntry(new Runnable() {
144
		assertListChangeEventFired(new Runnable() {
140
			public void run() {
145
			public void run() {
141
				list.addAll(Arrays.asList(new Object[] { element }));
146
				list.addAll(Collections.singletonList(element));
142
			}
147
			}
143
		}, "List.addAll(Collection)", list, element, 0);
148
		}, "List.addAll(Collection)", list, Collections.singletonList(element));
144
	}
149
	}
145
150
146
	public void testAddAll_ListDiffEntry2() throws Exception {
151
	public void testAddAll_ListDiffEntry2() throws Exception {
147
		list.add(delegate.createElement(list));
152
		final Object element0 = delegate.createElement(list);
148
		final Object element = delegate.createElement(list);
153
		list.add(element0);
154
		final Object element1 = delegate.createElement(list);
149
155
150
		assertAddDiffEntry(new Runnable() {
156
		assertListChangeEventFired(new Runnable() {
151
			public void run() {
157
			public void run() {
152
				list.addAll(Collections.singletonList(element));
158
				list.addAll(Collections.singletonList(element1));
153
			}
159
			}
154
		}, "List.addAll(Collection)", list, element, 1);
160
		}, "List.addAll(Collection)", list, Arrays.asList(new Object[] {
161
				element0, element1 }));
155
	}
162
	}
156
163
157
	public void testAddAllAtIndex_ChangeEvent() throws Exception {
164
	public void testAddAllAtIndex_ChangeEvent() throws Exception {
Lines 164-175 Link Here
164
	}
171
	}
165
172
166
	public void testAddAllAtIndex_ListChangeEvent() throws Exception {
173
	public void testAddAllAtIndex_ListChangeEvent() throws Exception {
174
		final Object element = delegate.createElement(list);
167
		assertListChangeEventFired(new Runnable() {
175
		assertListChangeEventFired(new Runnable() {
168
			public void run() {
176
			public void run() {
169
				list.addAll(0, Arrays.asList(new Object[] { delegate
177
				list.addAll(0, Collections.singletonList(element));
170
						.createElement(list) }));
171
			}
178
			}
172
		}, "List.addAll(int, Collection)", list);
179
		}, "List.addAll(int, Collection)", list, Collections
180
				.singletonList(element));
173
	}
181
	}
174
182
175
	public void testAddAllAtIndex_ChangeEventFiredAfterElementIsAdded()
183
	public void testAddAllAtIndex_ChangeEventFiredAfterElementIsAdded()
Lines 184-197 Link Here
184
	}
192
	}
185
193
186
	public void testAddAllAtIndex_ListDiffEntry() throws Exception {
194
	public void testAddAllAtIndex_ListDiffEntry() throws Exception {
187
		list.add(delegate.createElement(list));
195
		Object element0 = delegate.createElement(list);
188
		final Object element = delegate.createElement(list);
196
		list.add(element0);
197
		final Object element1 = delegate.createElement(list);
189
198
190
		assertAddDiffEntry(new Runnable() {
199
		assertListChangeEventFired(new Runnable() {
191
			public void run() {
200
			public void run() {
192
				list.addAll(1, Arrays.asList(new Object[] { element }));
201
				list.addAll(1, Collections.singletonList(element1));
193
			}
202
			}
194
		}, "List.addAll(int, Collection)", list, element, 1);
203
		}, "List.addAll(int, Collection)", list, Arrays.asList(new Object[] {
204
				element0, element1 }));
195
	}
205
	}
196
206
197
	public void testSet_ChangeEvent() throws Exception {
207
	public void testSet_ChangeEvent() throws Exception {
Lines 205-263 Link Here
205
	}
215
	}
206
216
207
	public void testSet_ListChangeEvent() throws Exception {
217
	public void testSet_ListChangeEvent() throws Exception {
208
		list.add(delegate.createElement(list));
218
		final Object element0 = delegate.createElement(list);
219
		list.add(element0);
220
		final Object element1 = delegate.createElement(list);
209
221
210
		assertListChangeEventFired(new Runnable() {
222
		assertListChangeEventFired(new Runnable() {
211
			public void run() {
223
			public void run() {
212
				list.set(0, delegate.createElement(list));
224
				assertSame(element0, list.set(0, element1));
213
			}
225
			}
214
		}, "List.set(int, Object)", list);
226
		}, "List.set(int, Object)", list, Arrays
227
				.asList(new Object[] { element1 }));
215
	}
228
	}
216
229
217
	public void testSet_ChangeEventFiredAfterElementIsSet() throws Exception {
230
	public void testSet_ChangeEventFiredAfterElementIsSet() throws Exception {
218
		Object element1 = delegate.createElement(list);
231
		final Object element1 = delegate.createElement(list);
219
		list.add(element1);
232
		list.add(element1);
220
		final Object element2 = delegate.createElement(list);
233
		final Object element2 = delegate.createElement(list);
221
234
222
		assertContainsDuringChangeEvent(new Runnable() {
235
		assertContainsDuringChangeEvent(new Runnable() {
223
			public void run() {
236
			public void run() {
224
				list.set(0, element2);
237
				assertSame(element1, list.set(0, element2));
225
			}
238
			}
226
		}, "List.set(int, Object)", list, element2);
239
		}, "List.set(int, Object)", list, element2);
227
	}
240
	}
228
241
229
	public void testSet_ListDiffEntry() throws Exception {
242
	public void testSet_ListChangeEvent2() throws Exception {
230
		list.add(delegate.createElement(list));
243
		Object element0 = delegate.createElement(list);
231
		Object oldElement = delegate.createElement(list);
244
		list.add(element0);
232
		list.add(oldElement);
245
		Object oldElement1 = delegate.createElement(list);
233
246
		list.add(oldElement1);
234
		ListChangeEventTracker listener = ListChangeEventTracker.observe(list);
247
		final Object newElement1 = delegate.createElement(list);
235
236
		Object newElement = delegate.createElement(list);
237
		list.set(1, newElement);
238
239
		ListDiffEntry[] entries = listener.event.diff.getDifferences();
240
		assertEquals(
241
				"List.set(int, Object) should result in 2 list diff entries.",
242
				2, entries.length);
243
244
		ListDiffEntry remove = entries[0];
245
		assertFalse(remove.isAddition());
246
		assertEquals(
247
				"List.set(int, Object) removed element should be the old element.",
248
				oldElement, remove.getElement());
249
		assertEquals(
250
				"List.set(int, Object) removed index should be the index the new element was set at.",
251
				1, remove.getPosition());
252
248
253
		ListDiffEntry add = entries[1];
249
		assertListChangeEventFired(new Runnable() {
254
		assertTrue(add.isAddition());
250
			public void run() {
255
		assertEquals(
251
				list.set(1, newElement1);
256
				"List.set(int, Object) added element should be the set element.",
252
			}
257
				newElement, add.getElement());
253
		}, "List.set(int, Object)", list, Arrays.asList(new Object[] {
258
		assertEquals(
254
				element0, newElement1 }));
259
				"List.set(int, Object) add index should be the index the new element was set at.",
260
				1, add.getPosition());
261
	}
255
	}
262
256
263
	public void testMove_ChangeEvent() throws Exception {
257
	public void testMove_ChangeEvent() throws Exception {
Lines 289-304 Link Here
289
	}
283
	}
290
284
291
	public void testMove_ListChangeEvent() throws Exception {
285
	public void testMove_ListChangeEvent() throws Exception {
292
		final Object element = delegate.createElement(list);
286
		final Object element0 = delegate.createElement(list);
293
		list.add(element);
287
		list.add(element0);
294
		list.add(delegate.createElement(list));
288
		final Object element1 = delegate.createElement(list);
289
		list.add(element1);
295
290
296
		assertListChangeEventFired(new Runnable() {
291
		assertListChangeEventFired(new Runnable() {
297
			public void run() {
292
			public void run() {
298
				Object movedElement = list.move(0, 1);
293
				assertSame(element0, list.move(0, 1));
299
				assertEquals(element, movedElement);
300
			}
294
			}
301
		}, "IObservableList.move(int, int)", list);
295
		}, "IObservableList.move(int, int)", list, Arrays.asList(new Object[] {
296
				element1, element0 }));
302
	}
297
	}
303
298
304
	public void testMove_ChangeEventFiredAfterElementIsMoved() throws Exception {
299
	public void testMove_ChangeEventFiredAfterElementIsMoved() throws Exception {
Lines 316-357 Link Here
316
		assertSame(element0, list.get(1));
311
		assertSame(element0, list.get(1));
317
	}
312
	}
318
313
319
	public void testMove_ListDiffEntry() {
314
	public void testMove_ListChangeEvent2() {
320
		Object element = delegate.createElement(list);
315
		Object element0 = delegate.createElement(list);
321
		list.add(element);
316
		list.add(element0);
322
		list.add(delegate.createElement(list));
317
		Object element1 = delegate.createElement(list);
323
318
		list.add(element1);
324
		ListChangeEventTracker listener = ListChangeEventTracker.observe(list);
325
326
		list.move(0, 1);
327
328
		ListDiffEntry[] entries = listener.event.diff.getDifferences();
329
		assertEquals(
330
				"List.set(int, Object) should result in 2 list diff entries.",
331
				2, entries.length);
332
333
		ListDiffEntry remove = entries[0];
334
		ListDiffEntry add = entries[1];
335
		assertFalse(
336
				"IObservableList.move(int, int) removed element should be first in list diff",
337
				remove.isAddition());
338
		assertTrue(
339
				"IObservableList.move(int, int) added element should be second in list diff",
340
				add.isAddition());
341
342
		assertEquals(
343
				"IObservableList.move(int, int) remove entry contains incorrect element",
344
				element, remove.getElement());
345
		assertEquals(
346
				"IObservableList.move(int, int) add entry contains incorrect element",
347
				element, add.getElement());
348
319
349
		assertEquals(
320
		assertListChangeEventFired(new Runnable() {
350
				"IObservableList.move(int, int) remove entry should be the old element index",
321
			public void run() {
351
				0, remove.getPosition());
322
				list.move(0, 1);
352
		assertEquals(
323
			}
353
				"IObservableList.move(int, int) add entry should be the new element index",
324
		}, "IObservableList.move(int, int)", list, Arrays.asList(new Object[] {
354
				1, add.getPosition());
325
				element1, element0 }));
355
	}
326
	}
356
327
357
	public void testRemove_ListChangeEvent() throws Exception {
328
	public void testRemove_ListChangeEvent() throws Exception {
Lines 362-380 Link Here
362
			public void run() {
333
			public void run() {
363
				list.remove(element);
334
				list.remove(element);
364
			}
335
			}
365
		}, "List.remove(Object)", list);
336
		}, "List.remove(Object)", list, Collections.EMPTY_LIST);
366
	}
337
	}
367
338
368
	public void testRemove_ListDiffEntry() throws Exception {
339
	public void testRemove_ListDiffEntry() throws Exception {
369
		list.add(delegate.createElement(list));
340
		final Object element0 = delegate.createElement(list);
370
		final Object element = delegate.createElement(list);
341
		list.add(element0);
371
		list.add(element);
342
		final Object element1 = delegate.createElement(list);
343
		list.add(element1);
372
344
373
		assertRemoveDiffEntry(new Runnable() {
345
		assertListChangeEventFired(new Runnable() {
374
			public void run() {
346
			public void run() {
375
				list.remove(element);
347
				list.remove(element1);
376
			}
348
			}
377
		}, "List.remove(Object)", list, element, 1);
349
		}, "List.remove(Object)", list, Collections.singletonList(element0));
378
	}
350
	}
379
351
380
	public void testRemoveAtIndex_ChangeEvent() throws Exception {
352
	public void testRemoveAtIndex_ChangeEvent() throws Exception {
Lines 394-400 Link Here
394
			public void run() {
366
			public void run() {
395
				list.remove(0);
367
				list.remove(0);
396
			}
368
			}
397
		}, "List.remove(int)", list);
369
		}, "List.remove(int)", list, Collections.EMPTY_LIST);
398
	}
370
	}
399
371
400
	public void testRemoveAtIndex_ChangeEventFiredAfterElementIsRemoved()
372
	public void testRemoveAtIndex_ChangeEventFiredAfterElementIsRemoved()
Lines 410-424 Link Here
410
	}
382
	}
411
383
412
	public void testRemoveAtIndex_ListDiffEntry() throws Exception {
384
	public void testRemoveAtIndex_ListDiffEntry() throws Exception {
413
		list.add(delegate.createElement(list));
385
		Object element0 = delegate.createElement(list);
414
		Object element = delegate.createElement(list);
386
		list.add(element0);
415
		list.add(element);
387
		Object element1 = delegate.createElement(list);
388
		list.add(element1);
416
389
417
		assertRemoveDiffEntry(new Runnable() {
390
		assertListChangeEventFired(new Runnable() {
418
			public void run() {
391
			public void run() {
419
				list.remove(1);
392
				list.remove(1);
420
			}
393
			}
421
		}, "List.remove(int)", list, element, 1);
394
		}, "List.remove(int)", list, Collections.singletonList(element0));
422
	}
395
	}
423
396
424
	public void testRemoveAll_ListChangeEvent() throws Exception {
397
	public void testRemoveAll_ListChangeEvent() throws Exception {
Lines 427-483 Link Here
427
400
428
		assertListChangeEventFired(new Runnable() {
401
		assertListChangeEventFired(new Runnable() {
429
			public void run() {
402
			public void run() {
430
				list.removeAll(Arrays.asList(new Object[] { element }));
403
				list.removeAll(Collections.singletonList(element));
431
			}
404
			}
432
		}, "List.removeAll(Collection)", list);
405
		}, "List.removeAll(Collection)", list, Collections.EMPTY_LIST);
433
	}
406
	}
434
407
435
	public void testRemoveAll_ListDiffEntry() throws Exception {
408
	public void testRemoveAll_ListDiffEntry() throws Exception {
436
		final Object element = delegate.createElement(list);
409
		final Object element = delegate.createElement(list);
437
		list.add(element);
410
		list.add(element);
438
411
439
		assertRemoveDiffEntry(new Runnable() {
412
		assertListChangeEventFired(new Runnable() {
440
			public void run() {
413
			public void run() {
441
				list.removeAll(Arrays.asList(new Object[] { element }));
414
				list.removeAll(Collections.singletonList(element));
442
			}
415
			}
443
		}, "List.removeAll(Collection)", list, element, 0);
416
		}, "List.removeAll(Collection)", list, Collections.EMPTY_LIST);
444
	}
417
	}
445
418
446
	public void testRemoveAll_ListDiffEntry2() throws Exception {
419
	public void testRemoveAll_ListDiffEntry2() throws Exception {
447
		list.add(delegate.createElement(list));
420
		Object element0 = delegate.createElement(list);
448
		final Object element = delegate.createElement(list);
421
		list.add(element0);
449
		list.add(element);
422
		final Object element1 = delegate.createElement(list);
423
		list.add(element1);
450
424
451
		assertRemoveDiffEntry(new Runnable() {
425
		assertListChangeEventFired(new Runnable() {
452
			public void run() {
426
			public void run() {
453
				list.removeAll(Arrays.asList(new Object[] { element }));
427
				list.removeAll(Arrays.asList(new Object[] { element1 }));
454
			}
428
			}
455
		}, "List.removeAll(Collection)", list, element, 1);
429
		}, "List.removeAll(Collection)", list, Collections
430
				.singletonList(element0));
456
	}
431
	}
457
432
458
	public void testRetainAll_ListChangeEvent() throws Exception {
433
	public void testRetainAll_ListChangeEvent() throws Exception {
459
		final Object element1 = delegate.createElement(list);
434
		final Object element0 = delegate.createElement(list);
460
		list.add(element1);
435
		list.add(element0);
461
		list.add(delegate.createElement(list));
436
		list.add(delegate.createElement(list));
462
437
463
		assertListChangeEventFired(new Runnable() {
438
		assertListChangeEventFired(new Runnable() {
464
			public void run() {
439
			public void run() {
465
				list.retainAll(Arrays.asList(new Object[] { element1 }));
440
				list.retainAll(Arrays.asList(new Object[] { element0 }));
466
			}
441
			}
467
		}, "List.retainAll(Collection", list);
442
		}, "List.retainAll(Collection", list, Collections
443
				.singletonList(element0));
468
	}
444
	}
469
445
470
	public void testRetainAll_ListDiffEntry() throws Exception {
446
	public void testRetainAll_ListDiffEntry() throws Exception {
471
		final Object element1 = delegate.createElement(list);
447
		final Object element = delegate.createElement(list);
472
		list.add(element1);
448
		list.add(element);
473
		Object element2 = delegate.createElement(list);
449
		list.add(delegate.createElement(list));
474
		list.add(element2);
475
450
476
		assertRemoveDiffEntry(new Runnable() {
451
		assertListChangeEventFired(new Runnable() {
477
			public void run() {
452
			public void run() {
478
				list.retainAll(Arrays.asList(new Object[] { element1 }));
453
				list.retainAll(Arrays.asList(new Object[] { element }));
479
			}
454
			}
480
		}, "List.retainAll(Collection)", list, element2, 1);
455
		}, "List.retainAll(Collection)", list, Collections
456
				.singletonList(element));
481
	}
457
	}
482
458
483
	public void testClear_ListChangeEvent() throws Exception {
459
	public void testClear_ListChangeEvent() throws Exception {
Lines 487-504 Link Here
487
			public void run() {
463
			public void run() {
488
				list.clear();
464
				list.clear();
489
			}
465
			}
490
		}, "List.clear()", list);
466
		}, "List.clear()", list, Collections.EMPTY_LIST);
491
	}
467
	}
492
468
493
	public void testClear_ListDiffEntry() throws Exception {
469
	public void testClear_ListDiffEntry() throws Exception {
494
		Object element = delegate.createElement(list);
470
		list.add(delegate.createElement(list));
495
		list.add(element);
496
471
497
		assertRemoveDiffEntry(new Runnable() {
472
		assertListChangeEventFired(new Runnable() {
498
			public void run() {
473
			public void run() {
499
				list.clear();
474
				list.clear();
500
			}
475
			}
501
		}, "List.clear()", list, element, 0);
476
		}, "List.clear()", list, Collections.EMPTY_LIST);
502
	}
477
	}
503
478
504
	public void testClear_ClearsList() {
479
	public void testClear_ClearsList() {
Lines 509-528 Link Here
509
		Assert.assertEquals(Collections.EMPTY_LIST, list);
484
		Assert.assertEquals(Collections.EMPTY_LIST, list);
510
	}
485
	}
511
486
512
	/**
513
	 * Asserts standard behaviors of firing list change events.
514
	 * <ul>
515
	 * <li>Event fires once.</li>
516
	 * <li>Source of the event is the provided <code>list</code>.
517
	 * <li>The list change event is fired after the change event.</li>
518
	 * </ul>
519
	 * 
520
	 * @param runnable
521
	 * @param methodName
522
	 * @param list
523
	 */
524
	private void assertListChangeEventFired(Runnable runnable,
487
	private void assertListChangeEventFired(Runnable runnable,
525
			String methodName, IObservableList list) {
488
			String methodName, IObservableList list, List newList) {
489
		List oldList = new ArrayList(list);
490
526
		List queue = new ArrayList();
491
		List queue = new ArrayList();
527
		ListChangeEventTracker listListener = new ListChangeEventTracker(queue);
492
		ListChangeEventTracker listListener = new ListChangeEventTracker(queue);
528
		ChangeEventTracker changeListener = new ChangeEventTracker(queue);
493
		ChangeEventTracker changeListener = new ChangeEventTracker(queue);
Lines 546-617 Link Here
546
		assertEquals("ListChangeEvent of " + methodName
511
		assertEquals("ListChangeEvent of " + methodName
547
				+ " should have fired after the ChangeEvent.", listListener,
512
				+ " should have fired after the ChangeEvent.", listListener,
548
				queue.get(1));
513
				queue.get(1));
549
	}
550
551
	/**
552
	 * Asserts the list diff entry for a remove operation.
553
	 * 
554
	 * @param runnable
555
	 * @param methodName
556
	 * @param list
557
	 * @param element
558
	 * @param index
559
	 */
560
	private void assertRemoveDiffEntry(Runnable runnable, String methodName,
561
			IObservableList list, Object element, int index) {
562
		ListChangeEventTracker listener = new ListChangeEventTracker();
563
		list.addListChangeListener(listener);
564
514
565
		runnable.run();
515
		assertEquals(formatFail(methodName
516
				+ " did not leave observable list with the expected contents"),
517
				newList, list);
566
518
567
		ListDiffEntry[] entries = listener.event.diff.getDifferences();
519
		ListDiff diff = listListener.event.diff;
568
		assertEquals(methodName + " should result in one diff entry.", 1,
520
		diff.applyTo(oldList);
569
				entries.length);
570
571
		ListDiffEntry entry = entries[0];
572
		assertFalse(methodName
573
				+ " should result in a diff entry that is an removal.", entry
574
				.isAddition());
575
		assertEquals(methodName
576
				+ " remove diff entry should have removed the element.",
577
				element, entry.getElement());
578
		assertEquals(
521
		assertEquals(
579
				methodName
522
				formatFail(methodName
580
						+ " remove diff entry should have removed the element from the provided index.",
523
						+ " fired a diff which does not represent the expected list change"),
581
				index, entry.getPosition());
524
				newList, oldList);
582
	}
583
525
584
	/**
585
	 * Asserts the list diff entry for an add operation.
586
	 * 
587
	 * @param runnable
588
	 * @param methodName
589
	 * @param list
590
	 * @param element
591
	 * @param index
592
	 */
593
	private void assertAddDiffEntry(Runnable runnable, String methodName,
594
			IObservableList list, Object element, int index) {
595
		ListChangeEventTracker listener = new ListChangeEventTracker();
596
		list.addListChangeListener(listener);
597
598
		runnable.run();
599
600
		ListDiffEntry[] entries = listener.event.diff.getDifferences();
601
		assertEquals(methodName + " should result in one diff entry.", 1,
602
				entries.length);
603
604
		ListDiffEntry entry = entries[0];
605
		assertTrue(methodName
606
				+ " should result in a diff entry that is an addition.", entry
607
				.isAddition());
608
		assertEquals(methodName
609
				+ " add diff entry should have added the element.", element,
610
				entry.getElement());
611
		assertEquals(
612
				methodName
613
						+ "add diff entry should have added the element at the provided index.",
614
				index, entry.getPosition());
615
	}
526
	}
616
527
617
	public static Test suite(IObservableCollectionContractDelegate delegate) {
528
	public static Test suite(IObservableCollectionContractDelegate delegate) {

Return to bug 194734