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/databinding/observable/map/ComputedObservableMap.java (-5 / +37 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 241585
10
 *     Matthew Hall - bugs 241585, 194734
11
 *******************************************************************************/
11
 *******************************************************************************/
12
12
13
package org.eclipse.core.databinding.observable.map;
13
package org.eclipse.core.databinding.observable.map;
Lines 31-37 Link Here
31
 */
31
 */
32
public abstract class ComputedObservableMap extends AbstractObservableMap {
32
public abstract class ComputedObservableMap extends AbstractObservableMap {
33
33
34
	private final IObservableSet keySet;
34
	private IObservableSet keySet;
35
35
36
	private ISetChangeListener setChangeListener = new ISetChangeListener() {
36
	private ISetChangeListener setChangeListener = new ISetChangeListener() {
37
		public void handleSetChange(SetChangeEvent event) {
37
		public void handleSetChange(SetChangeEvent event) {
Lines 109-118 Link Here
109
		this.keySet.addSetChangeListener(setChangeListener);
109
		this.keySet.addSetChangeListener(setChangeListener);
110
	}
110
	}
111
111
112
	/**
113
     * @deprecated Subclasses are no longer required to call this method.
114
     */
112
	protected void init() {
115
	protected void init() {
113
		for (Iterator it = this.keySet.iterator(); it.hasNext();) {
116
	}
114
			Object key = it.next();
117
115
			hookListener(key);
118
	protected void firstListenerAdded() {
119
		hookListeners();
120
	}
121
122
	protected void lastListenerRemoved() {
123
		unhookListeners();
124
	}
125
126
	private void hookListeners() {
127
		if (keySet != null) {
128
			for (Iterator it = this.keySet.iterator(); it.hasNext();) {
129
				Object key = it.next();
130
				hookListener(key);
131
			}
132
		}
133
	}
134
135
	private void unhookListeners() {
136
		if (keySet != null) {
137
			Object[] keys = keySet.toArray();
138
			for (int i = 0; i < keys.length; i++) {
139
				unhookListener(keys[i]);
140
			}
141
			keySet = null;
116
		}
142
		}
117
	}
143
	}
118
144
Lines 163-166 Link Here
163
	 * @return the old value for the given key
189
	 * @return the old value for the given key
164
	 */
190
	 */
165
	protected abstract Object doPut(Object key, Object value);
191
	protected abstract Object doPut(Object key, Object value);
192
193
	public void dispose() {
194
		unhookListeners();
195
		entrySet = null;
196
		super.dispose();
197
	}
166
}
198
}
(-)src/org/eclipse/core/databinding/observable/list/AbstractObservableList.java (-11 / +20 lines)
Lines 7-17 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Brad Reynolds - bug 164653
10
 *     Brad Reynolds - bugs 164653, 167204
11
 *     Brad Reynolds - bug 167204
11
 *     Matthew Hall - bugs 118516, 208858, 208332, 194734
12
 *     Matthew Hall - bug 118516
13
 *     Matthew Hall - bug 208858
14
 *     Matthew Hall - bug 208332
15
 *******************************************************************************/
12
 *******************************************************************************/
16
13
17
package org.eclipse.core.databinding.observable.list;
14
package org.eclipse.core.databinding.observable.list;
Lines 76-86 Link Here
76
	}
73
	}
77
74
78
	public synchronized void addListChangeListener(IListChangeListener listener) {
75
	public synchronized void addListChangeListener(IListChangeListener listener) {
79
		changeSupport.addListener(ListChangeEvent.TYPE, listener);
76
		if (changeSupport != null) {
77
			changeSupport.addListener(ListChangeEvent.TYPE, listener);
78
		}
80
	}
79
	}
81
80
82
	public synchronized void removeListChangeListener(IListChangeListener listener) {
81
	public synchronized void removeListChangeListener(IListChangeListener listener) {
83
		changeSupport.removeListener(ListChangeEvent.TYPE, listener);
82
		if (changeSupport != null) {
83
			changeSupport.removeListener(ListChangeEvent.TYPE, listener);
84
		}
84
	}
85
	}
85
86
86
	protected void fireListChange(ListDiff diff) {
87
	protected void fireListChange(ListDiff diff) {
Lines 90-108 Link Here
90
	}
91
	}
91
92
92
	public synchronized void addChangeListener(IChangeListener listener) {
93
	public synchronized void addChangeListener(IChangeListener listener) {
93
		changeSupport.addChangeListener(listener);
94
		if (changeSupport != null) {
95
			changeSupport.addChangeListener(listener);
96
		}
94
	}
97
	}
95
98
96
	public synchronized void removeChangeListener(IChangeListener listener) {
99
	public synchronized void removeChangeListener(IChangeListener listener) {
97
		changeSupport.removeChangeListener(listener);
100
		if (changeSupport != null) {
101
			changeSupport.removeChangeListener(listener);
102
		}
98
	}
103
	}
99
104
100
	public synchronized void addStaleListener(IStaleListener listener) {
105
	public synchronized void addStaleListener(IStaleListener listener) {
101
		changeSupport.addStaleListener(listener);
106
		if (changeSupport != null) {
107
			changeSupport.addStaleListener(listener);
108
		}
102
	}
109
	}
103
110
104
	public synchronized void removeStaleListener(IStaleListener listener) {
111
	public synchronized void removeStaleListener(IStaleListener listener) {
105
		changeSupport.removeStaleListener(listener);
112
		if (changeSupport != null) {
113
			changeSupport.removeStaleListener(listener);
114
		}
106
	}
115
	}
107
116
108
	/**
117
	/**
(-)META-INF/MANIFEST.MF (+4 lines)
Lines 14-19 Link Here
14
 org.eclipse.core.databinding.observable.masterdetail,
14
 org.eclipse.core.databinding.observable.masterdetail,
15
 org.eclipse.core.databinding.observable.set;x-internal:=false,
15
 org.eclipse.core.databinding.observable.set;x-internal:=false,
16
 org.eclipse.core.databinding.observable.value;x-internal:=false,
16
 org.eclipse.core.databinding.observable.value;x-internal:=false,
17
 org.eclipse.core.databinding.property,
18
 org.eclipse.core.databinding.property.masterdetail,
17
 org.eclipse.core.databinding.util,
19
 org.eclipse.core.databinding.util,
18
 org.eclipse.core.databinding.validation;x-internal:=false,
20
 org.eclipse.core.databinding.validation;x-internal:=false,
19
 org.eclipse.core.internal.databinding;x-friends:="org.eclipse.core.databinding.beans",
21
 org.eclipse.core.internal.databinding;x-friends:="org.eclipse.core.databinding.beans",
Lines 21-26 Link Here
21
 org.eclipse.core.internal.databinding.observable;x-internal:=true,
23
 org.eclipse.core.internal.databinding.observable;x-internal:=true,
22
 org.eclipse.core.internal.databinding.observable.masterdetail;x-friends:="org.eclipse.jface.tests.databinding",
24
 org.eclipse.core.internal.databinding.observable.masterdetail;x-friends:="org.eclipse.jface.tests.databinding",
23
 org.eclipse.core.internal.databinding.observable.tree;x-friends:="org.eclipse.jface.databinding,org.eclipse.jface.tests.databinding",
25
 org.eclipse.core.internal.databinding.observable.tree;x-friends:="org.eclipse.jface.databinding,org.eclipse.jface.tests.databinding",
26
 org.eclipse.core.internal.databinding.property;x-friends:="org.eclipse.jface.databinding,org.eclipse.jface.tests.databinding",
27
 org.eclipse.core.internal.databinding.property.masterdetail;x-friends:="org.eclipse.jface.databinding,org.eclipse.jface.tests.databinding",
24
 org.eclipse.core.internal.databinding.validation;x-friends:="org.eclipse.jface.tests.databinding"
28
 org.eclipse.core.internal.databinding.validation;x-friends:="org.eclipse.jface.tests.databinding"
25
Require-Bundle: org.eclipse.equinox.common;bundle-version="[3.2.0,4.0.0)"
29
Require-Bundle: org.eclipse.equinox.common;bundle-version="[3.2.0,4.0.0)"
26
Import-Package-Comment: see http://wiki.eclipse.org/
30
Import-Package-Comment: see http://wiki.eclipse.org/
(-)src/org/eclipse/core/databinding/property/BasicMapProperty.java (+146 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;
13
14
import java.util.Collections;
15
import java.util.HashMap;
16
import java.util.HashSet;
17
import java.util.Iterator;
18
import java.util.Map;
19
import java.util.Set;
20
21
import org.eclipse.core.databinding.observable.Diffs;
22
import org.eclipse.core.databinding.observable.map.MapDiff;
23
import org.eclipse.core.internal.databinding.property.PropertyUpdateHelper;
24
25
/**
26
 * Abstract map property implementation for properties where the map can be
27
 * completely replaced in a single atomic operation.
28
 * <p>
29
 * For example, a map-typed bean property Customer.phoneNumbers can be modified
30
 * by calling Customer.setPhoneNumbers(Map phoneNumbers).
31
 * 
32
 * @since 1.2
33
 */
34
public abstract class BasicMapProperty extends MapProperty {
35
	private PropertyUpdateHelper updateHelper = new PropertyUpdateHelper();
36
37
	/**
38
	 * Returns whether this property is currently being updated on the source.
39
	 * Implementors should query this value to avoid unnecessary calculations,
40
	 * such as computing a diff.
41
	 * 
42
	 * @param source
43
	 *            the property source
44
	 * @return whether this property is currently being updated on the source.
45
	 */
46
	protected boolean isUpdating(Object source) {
47
		return updateHelper.isUpdating(source);
48
	}
49
50
	/**
51
	 * Sets the map property on the source to the specified map, then fires a
52
	 * map change using the specified diff.
53
	 * 
54
	 * @param source
55
	 *            the property source
56
	 * @param map
57
	 *            the new map
58
	 * @param diff
59
	 *            the diff to be fired after the map property has been set. If
60
	 *            null, the diff will be computed.
61
	 */
62
	protected void setMap(Object source, Map map, MapDiff diff) {
63
		if (diff == null) {
64
			diff = Diffs.computeMapDiff(getMap(source), map);
65
		}
66
67
		updateHelper.setUpdating(source, true);
68
		try {
69
			doSetMap(source, map);
70
		} finally {
71
			updateHelper.setUpdating(source, false);
72
		}
73
74
		if (hasListeners(source)) {
75
			fireMapChange(source, diff);
76
		}
77
	}
78
79
	public void clear(Object source) {
80
		setMap(source, new HashMap(), Diffs.createMapDiffRemoveAll(new HashMap(
81
				getMap(source))));
82
	}
83
84
	public Object put(Object source, Object key, Object value) {
85
		Map map = new HashMap(getMap(source));
86
		boolean addition = !map.containsKey(key);
87
		Object result = map.put(key, value);
88
		MapDiff diff;
89
		if (addition) {
90
			diff = Diffs.createMapDiffSingleAdd(key, value);
91
		} else {
92
			diff = Diffs.createMapDiffSingleChange(key, result, value);
93
		}
94
		setMap(source, map, diff);
95
		return result;
96
	}
97
98
	public void putAll(Object source, Map t) {
99
		Map map = new HashMap(getMap(source));
100
		Set addedKeys = new HashSet();
101
		Set changedKeys = new HashSet();
102
		Map oldValues = new HashMap();
103
		Map newValues = new HashMap();
104
		for (Iterator it = t.entrySet().iterator(); it.hasNext();) {
105
			Map.Entry entry = (Map.Entry) it.next();
106
			Object key = entry.getKey();
107
			Object newValue = entry.getValue();
108
			boolean addition = !map.containsKey(key);
109
			Object oldValue = map.put(key, newValue);
110
			if (addition) {
111
				addedKeys.add(key);
112
			} else {
113
				changedKeys.add(key);
114
				oldValues.put(key, oldValue);
115
			}
116
			newValues.put(key, newValue);
117
		}
118
		setMap(source, map, Diffs.createMapDiff(addedKeys,
119
				Collections.EMPTY_SET, changedKeys, oldValues, newValues));
120
	}
121
122
	public Object remove(Object source, Object key) {
123
		Map map = getMap(source);
124
		if (map.containsKey(key)) {
125
			map = new HashMap(map);
126
			Object result = map.remove(key);
127
			setMap(source, map, Diffs.createMapDiffSingleRemove(key, result));
128
			return result;
129
		}
130
		return null;
131
	}
132
133
	public int size(Object source) {
134
		return getMap(source).size();
135
	}
136
137
	protected abstract void doSetMap(Object source, Map map);
138
139
	public synchronized void dispose() {
140
		if (updateHelper != null) {
141
			updateHelper.dispose();
142
			updateHelper = null;
143
		}
144
		super.dispose();
145
	}
146
}
(-)src/org/eclipse/core/databinding/property/IListProperty.java (+162 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.Collection;
15
import java.util.List;
16
17
/**
18
 * Interface for list-typed properties.
19
 * 
20
 * @since 1.2
21
 */
22
public interface IListProperty extends ICollectionProperty {
23
	/**
24
	 * Returns a List with the current contents of the source's list property
25
	 * 
26
	 * @param source
27
	 *            the property source
28
	 * @return a List with the current contents of the source's list property
29
	 */
30
	List getList(Object source);
31
32
	/**
33
	 * Inserts all elements in the specified collection into the source's list
34
	 * property at the specified index.
35
	 * 
36
	 * @param source
37
	 *            the property source
38
	 * @param index
39
	 *            the insertion index
40
	 * @param c
41
	 *            the collection of elements to add
42
	 * @return whether the source's list property was changed
43
	 */
44
	boolean addAll(Object source, int index, Collection c);
45
46
	/**
47
	 * Returns the element at the specified position in the source's list
48
	 * property
49
	 * 
50
	 * @param source
51
	 *            the property source
52
	 * @param index
53
	 *            the element position
54
	 * @return the element at the given position in the source's list property
55
	 */
56
	Object get(Object source, int index);
57
58
	/**
59
	 * Replaces the element at the specified position in the source's list
60
	 * property with the given element.
61
	 * 
62
	 * @param source
63
	 *            the property source
64
	 * @param index
65
	 *            the element position
66
	 * @param element
67
	 *            the replacement element
68
	 * @return the element previously at the specified position in the source's
69
	 *         list property
70
	 */
71
	Object set(Object source, int index, Object element);
72
73
	/**
74
	 * Moves the element at the specified old position in the source's list
75
	 * property to the specified new position
76
	 * 
77
	 * @param source
78
	 *            the property source
79
	 * @param oldIndex
80
	 *            the old element position
81
	 * @param newIndex
82
	 *            the new element position
83
	 * @return the element that was moved
84
	 */
85
	Object move(Object source, int oldIndex, int newIndex);
86
87
	/**
88
	 * Inserts the element into the source's list property at the specified
89
	 * position
90
	 * 
91
	 * @param source
92
	 *            the property source
93
	 * @param index
94
	 *            the insertion index
95
	 * @param element
96
	 *            the element to insert
97
	 */
98
	void add(Object source, int index, Object element);
99
100
	/**
101
	 * Removes the element from the source's list property which is located at
102
	 * the specified position
103
	 * 
104
	 * @param source
105
	 *            the property source
106
	 * @param index
107
	 *            the index of the element to remove
108
	 * @return the element that was removed from the source's list property
109
	 */
110
	Object remove(Object source, int index);
111
112
	/**
113
	 * Returns the index of the first location of the given element in the
114
	 * source's list property, or -1 if the list does not contain the element.
115
	 * 
116
	 * @param source
117
	 *            the property source
118
	 * @param o
119
	 *            the element
120
	 * @return the index of the first location of the given element in the
121
	 *         source's list property, or -1 if the list does not contain the
122
	 *         element
123
	 */
124
	int indexOf(Object source, Object o);
125
126
	/**
127
	 * Returns the index of the last location of the given element in the
128
	 * source's list property, or -1 if the list does not contain the given
129
	 * element.
130
	 * 
131
	 * @param source
132
	 * @param o
133
	 * @return the index of the last location of the given element in the
134
	 *         source's list property, or -1 if the list does not contain the
135
	 *         element
136
	 */
137
	int lastIndexOf(Object source, Object o);
138
139
	/**
140
	 * Adds the given list property change listener to the list of listeners for
141
	 * the given source.
142
	 * 
143
	 * @param source
144
	 *            the property source
145
	 * @param listener
146
	 *            the listener
147
	 */
148
	public void addListChangeListener(Object source,
149
			IListPropertyChangeListener listener);
150
151
	/**
152
	 * Removes the given list property change listener from the list of
153
	 * listeners for the given source.
154
	 * 
155
	 * @param source
156
	 *            the property source
157
	 * @param listener
158
	 *            the listener
159
	 */
160
	public void removeListChangeListener(Object source,
161
			IListPropertyChangeListener listener);
162
}
(-)src/org/eclipse/core/databinding/property/ISetProperty.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;
13
14
import java.util.Set;
15
16
/**
17
 * Interface for set-typed properties
18
 * 
19
 * @since 1.2
20
 */
21
public interface ISetProperty extends ICollectionProperty {
22
	/**
23
	 * Returns a Set with the current contents of the source's set property
24
	 * 
25
	 * @param source
26
	 *            the property source
27
	 * @return a Set with the current contents of the source's set property
28
	 */
29
	public Set getSet(Object source);
30
31
	/**
32
	 * Adds the given set property change listener to the list of listeners for
33
	 * the given source.
34
	 * 
35
	 * @param source
36
	 *            the property source
37
	 * @param listener
38
	 *            the listener
39
	 */
40
	public void addSetChangeListener(Object source,
41
			ISetPropertyChangeListener listener);
42
43
	/**
44
	 * Removes the given set property change listener from the list of listeners
45
	 * for the given source.
46
	 * 
47
	 * @param source
48
	 *            the property source
49
	 * @param listener
50
	 *            the listener
51
	 */
52
	public void removeSetChangeListener(Object source,
53
			ISetPropertyChangeListener listener);
54
}
(-)src/org/eclipse/core/databinding/property/ValuePropertyChangeEvent.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;
13
14
import org.eclipse.core.databinding.observable.value.ValueDiff;
15
import org.eclipse.core.runtime.Assert;
16
17
/**
18
 * Value change event describing a change of a value property on a particular
19
 * property source.
20
 * 
21
 * @since 1.2
22
 */
23
public class ValuePropertyChangeEvent extends PropertyChangeEvent {
24
	private static final long serialVersionUID = 1L;
25
26
	/**
27
	 * The value property that changed
28
	 */
29
	public final IValueProperty property;
30
31
	/**
32
	 * ValueDiff with the old and new values of the property.
33
	 */
34
	public final ValueDiff diff;
35
36
	/**
37
	 * Constructs a ValuePropertyChangeEvent with the given attributes
38
	 * 
39
	 * @param source
40
	 *            the property source
41
	 * @param property
42
	 *            the property that changed on the source
43
	 * @param diff
44
	 *            a ValueDiff describing the changes to the value property
45
	 */
46
	public ValuePropertyChangeEvent(Object source, IValueProperty property,
47
			ValueDiff diff) {
48
		super(source);
49
		this.property = property;
50
		Assert.isNotNull(diff, "diff cannot be null"); //$NON-NLS-1$
51
		this.diff = diff;
52
	}
53
54
	void dispatch(IPropertyChangeListener listener) {
55
		((IValuePropertyChangeListener) listener)
56
				.handleValuePropertyChange(this);
57
	}
58
}
(-)src/org/eclipse/core/databinding/property/MapProperty.java (+72 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 org.eclipse.core.databinding.observable.map.MapDiff;
15
16
/**
17
 * Abstract implementation of IMapProperty
18
 * 
19
 * @since 1.2
20
 */
21
public abstract class MapProperty extends Property implements IMapProperty {
22
	public boolean containsKey(Object source, Object key) {
23
		return getMap(source).containsKey(key);
24
	}
25
26
	public boolean containsValue(Object source, Object value) {
27
		return getMap(source).containsValue(value);
28
	}
29
30
	public boolean equals(Object source, Object o) {
31
		return getMap(source).equals(o);
32
	}
33
34
	public Object get(Object source, Object key) {
35
		return getMap(source).get(key);
36
	}
37
38
	public int hashCode(Object source) {
39
		return getMap(source).hashCode();
40
	}
41
42
	public boolean isEmpty(Object source) {
43
		return getMap(source).isEmpty();
44
	}
45
46
	public int size(Object source) {
47
		return getMap(source).size();
48
	}
49
50
	public final void addMapChangeListener(Object source,
51
			IMapPropertyChangeListener listener) {
52
		getChangeSupport().addListener(source, listener);
53
	}
54
55
	public final void removeMapChangeListener(Object source,
56
			IMapPropertyChangeListener listener) {
57
		getChangeSupport().removeListener(source, listener);
58
	}
59
60
	/**
61
	 * Fires a MapPropertyChangeEvent with the given source and diff
62
	 * 
63
	 * @param source
64
	 *            the property source
65
	 * @param diff
66
	 *            the map diff
67
	 */
68
	protected final void fireMapChange(Object source, MapDiff diff) {
69
		getChangeSupport().firePropertyChange(
70
				new MapPropertyChangeEvent(source, this, diff));
71
	}
72
}
(-)src/org/eclipse/core/databinding/property/ListProperty.java (+63 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.Collection;
15
16
import org.eclipse.core.databinding.observable.list.ListDiff;
17
18
/**
19
 * Abstract implementation of IListProperty.
20
 * 
21
 * @since 1.2
22
 */
23
public abstract class ListProperty extends CollectionProperty implements
24
		IListProperty {
25
	Collection getCollection(Object source) {
26
		return getList(source);
27
	}
28
29
	public Object get(Object source, int index) {
30
		return getList(source).get(index);
31
	}
32
33
	public int indexOf(Object source, Object o) {
34
		return getList(source).indexOf(o);
35
	}
36
37
	public int lastIndexOf(Object source, Object o) {
38
		return getList(source).lastIndexOf(o);
39
	}
40
41
	public final void addListChangeListener(Object source,
42
			IListPropertyChangeListener listener) {
43
		getChangeSupport().addListener(source, listener);
44
	}
45
46
	public final void removeListChangeListener(Object source,
47
			IListPropertyChangeListener listener) {
48
		getChangeSupport().removeListener(source, listener);
49
	}
50
51
	/**
52
	 * Fires a ListPropertyChangeEvent with the given source and diff
53
	 * 
54
	 * @param source
55
	 *            the property source
56
	 * @param diff
57
	 *            the list diff
58
	 */
59
	protected void fireListChange(Object source, ListDiff diff) {
60
		getChangeSupport().firePropertyChange(
61
				new ListPropertyChangeEvent(source, this, diff));
62
	}
63
}
(-)src/org/eclipse/core/internal/databinding/property/PropertyObservableMap.java (+177 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 java.util.Collection;
15
import java.util.Collections;
16
import java.util.Map;
17
import java.util.Set;
18
19
import org.eclipse.core.databinding.observable.ObservableTracker;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.map.AbstractObservableMap;
22
import org.eclipse.core.databinding.property.IMapProperty;
23
import org.eclipse.core.databinding.property.IMapPropertyChangeListener;
24
import org.eclipse.core.databinding.property.IProperty;
25
import org.eclipse.core.databinding.property.IPropertyObservable;
26
import org.eclipse.core.databinding.property.MapPropertyChangeEvent;
27
28
/**
29
 * @since 3.3
30
 * 
31
 */
32
public class PropertyObservableMap extends AbstractObservableMap implements
33
		IPropertyObservable {
34
	private Object source;
35
	private IMapProperty property;
36
37
	private volatile boolean updating = false;
38
39
	private boolean disposed = false;
40
41
	private transient volatile int modCount = 0;
42
43
	private IMapPropertyChangeListener listener = new IMapPropertyChangeListener() {
44
		public void handleMapPropertyChange(final MapPropertyChangeEvent event) {
45
			if (!disposed && !updating) {
46
				getRealm().exec(new Runnable() {
47
					public void run() {
48
						getRealm().exec(new Runnable() {
49
							public void run() {
50
								modCount++;
51
								fireMapChange(event.diff);
52
							}
53
						});
54
					}
55
				});
56
			}
57
		}
58
	};
59
60
	/**
61
	 * @param realm
62
	 * @param source
63
	 * @param property
64
	 */
65
	public PropertyObservableMap(Realm realm, Object source,
66
			IMapProperty property) {
67
		super(realm);
68
		this.source = source;
69
		this.property = property;
70
	}
71
72
	private void getterCalled() {
73
		ObservableTracker.getterCalled(this);
74
	}
75
76
	protected void firstListenerAdded() {
77
		if (!disposed) {
78
			property.addMapChangeListener(source, listener);
79
		}
80
	}
81
82
	protected void lastListenerRemoved() {
83
		if (!disposed) {
84
			property.removeMapChangeListener(source, listener);
85
		}
86
	}
87
88
	public boolean containsKey(Object key) {
89
		getterCalled();
90
		return property.containsKey(source, key);
91
	}
92
93
	public boolean containsValue(Object value) {
94
		getterCalled();
95
		return property.containsValue(source, value);
96
	}
97
98
	public Set entrySet() {
99
		getterCalled();
100
		// unmodifiable for now
101
		return Collections.unmodifiableSet(property.getMap(source).entrySet());
102
	}
103
104
	public Object get(Object key) {
105
		getterCalled();
106
		return property.get(source, key);
107
	}
108
109
	public boolean isEmpty() {
110
		getterCalled();
111
		return property.isEmpty(source);
112
	}
113
114
	public Set keySet() {
115
		getterCalled();
116
		return Collections.unmodifiableSet(property.getMap(source).keySet());
117
	}
118
119
	public Object put(Object key, Object value) {
120
		checkRealm();
121
		return property.put(source, key, value);
122
	}
123
124
	public void putAll(Map m) {
125
		checkRealm();
126
		property.putAll(source, m);
127
	}
128
129
	public Object remove(Object key) {
130
		checkRealm();
131
		return property.remove(source, key);
132
	}
133
134
	public int size() {
135
		getterCalled();
136
		return property.size(source);
137
	}
138
139
	public Collection values() {
140
		getterCalled();
141
		return Collections.unmodifiableCollection(property.getMap(source)
142
				.values());
143
	}
144
145
	public void clear() {
146
		getterCalled();
147
		property.clear(source);
148
	}
149
150
	public boolean equals(Object o) {
151
		getterCalled();
152
		return property.equals(source, o);
153
	}
154
155
	public int hashCode() {
156
		getterCalled();
157
		return property.hashCode(source);
158
	}
159
160
	public Object getObserved() {
161
		return source;
162
	}
163
164
	public IProperty getProperty() {
165
		return property;
166
	}
167
168
	public synchronized void dispose() {
169
		if (!disposed) {
170
			disposed = true;
171
			property.removeMapChangeListener(source, listener);
172
			property = null;
173
			source = null;
174
		}
175
		super.dispose();
176
	}
177
}
(-)src/org/eclipse/core/databinding/property/SetPropertyChangeEvent.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.core.databinding.property;
13
14
import org.eclipse.core.databinding.observable.set.SetDiff;
15
import org.eclipse.core.runtime.Assert;
16
17
/**
18
 * Set change event describing an incremental change of a set property on a
19
 * particular property source.
20
 * 
21
 * @since 1.2
22
 */
23
public class SetPropertyChangeEvent extends PropertyChangeEvent {
24
	private static final long serialVersionUID = 1L;
25
26
	/**
27
	 * The set property that changed
28
	 */
29
	public final ISetProperty property;
30
31
	/**
32
	 * SetDiff enumerating the added and removed elements in the set.
33
	 */
34
	public final SetDiff diff;
35
36
	/**
37
	 * Constructs a SetPropertyChangeEvent with the given attributes
38
	 * 
39
	 * @param source
40
	 *            the property source
41
	 * @param property
42
	 *            the property that changed on the source
43
	 * @param diff
44
	 *            a SetDiff describing the changes to the set property
45
	 */
46
	public SetPropertyChangeEvent(Object source, ISetProperty property,
47
			SetDiff diff) {
48
		super(source);
49
		this.property = property;
50
		Assert.isNotNull(diff, "diff cannot be null"); //$NON-NLS-1$
51
		this.diff = diff;
52
	}
53
54
	void dispatch(IPropertyChangeListener listener) {
55
		((ISetPropertyChangeListener) listener).handleSetPropertyChange(this);
56
	}
57
}
(-)src/org/eclipse/core/databinding/property/IMapProperty.java (+176 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.Map;
15
16
/**
17
 * Interface for map-typed properties
18
 * 
19
 * @since 1.2
20
 */
21
public interface IMapProperty extends IProperty {
22
	/**
23
	 * Returns a Map with the current contents of the source's map property
24
	 * 
25
	 * @param source
26
	 *            the property source
27
	 * @return a Map with the current contents of the source's map property
28
	 */
29
	Map getMap(Object source);
30
31
	/**
32
	 * Returns the size of the source's map property
33
	 * 
34
	 * @param source
35
	 *            the property source
36
	 * @return the size of the source's map property
37
	 */
38
	int size(Object source);
39
40
	/**
41
	 * Returns whether the source's map property is empty
42
	 * 
43
	 * @param source
44
	 *            the property source
45
	 * @return whether the source's map property is empty
46
	 */
47
	boolean isEmpty(Object source);
48
49
	/**
50
	 * Returns whether the specified key is contained in the key set of the
51
	 * source's map property
52
	 * 
53
	 * @param source
54
	 *            the property source
55
	 * @param key
56
	 *            the key
57
	 * @return whether the specified key is contained in the key set of the
58
	 *         source's map property
59
	 */
60
	boolean containsKey(Object source, Object key);
61
62
	/**
63
	 * Returns whether the specified value is contains in the values collection
64
	 * of the source's map property
65
	 * 
66
	 * @param source
67
	 *            the property source
68
	 * @param value
69
	 *            the value
70
	 * @return whether the specified value is contains in the values collection
71
	 *         of the source's map property
72
	 */
73
	boolean containsValue(Object source, Object value);
74
75
	/**
76
	 * Returns the value associated with the specified key in the source's map
77
	 * property
78
	 * 
79
	 * @param source
80
	 *            the property source
81
	 * @param key
82
	 *            the key
83
	 * @return the value associated with the specified key in the source's map
84
	 *         property
85
	 */
86
	Object get(Object source, Object key);
87
88
	/**
89
	 * Associates the specified value with the specified key in the source's map
90
	 * property
91
	 * 
92
	 * @param source
93
	 *            the property source
94
	 * @param key
95
	 *            the key
96
	 * @param value
97
	 *            the value
98
	 * @return the value that was previously associated with the given key in
99
	 *         the source's map property
100
	 */
101
	Object put(Object source, Object key, Object value);
102
103
	/**
104
	 * Removes the mapping for the specified key from the source's map property
105
	 * 
106
	 * @param source
107
	 *            the property source
108
	 * @param key
109
	 *            the key
110
	 * @return the value that was previously associated with the specified key
111
	 *         in the source's map property, or null if no such mapping exists
112
	 */
113
	Object remove(Object source, Object key);
114
115
	/**
116
	 * Adds all mappings in the specified map to the source's map property.
117
	 * 
118
	 * @param source
119
	 *            the property source
120
	 * @param t
121
	 *            the map
122
	 */
123
	void putAll(Object source, Map t);
124
125
	/**
126
	 * Removes all mapping from the source's map property
127
	 * 
128
	 * @param source
129
	 *            the property source
130
	 */
131
	void clear(Object source);
132
133
	/**
134
	 * Returns whether the source's map property is equal to the argument
135
	 * 
136
	 * @param source
137
	 *            the property source
138
	 * @param o
139
	 *            the object to test for equality
140
	 * @return whether the source's map property is equal to the argument
141
	 */
142
	boolean equals(Object source, Object o);
143
144
	/**
145
	 * Returns the hash code of the source's map property
146
	 * 
147
	 * @param source
148
	 *            the property source
149
	 * @return the hash code of the source's map property
150
	 */
151
	int hashCode(Object source);
152
153
	/**
154
	 * Adds the given map property change listener to the list of listeners for
155
	 * the given source.
156
	 * 
157
	 * @param source
158
	 *            the property source
159
	 * @param listener
160
	 *            the listener
161
	 */
162
	public void addMapChangeListener(Object source,
163
			IMapPropertyChangeListener listener);
164
165
	/**
166
	 * Removes the given map property change listener from the list of listeners
167
	 * for the given source.
168
	 * 
169
	 * @param source
170
	 *            the property source
171
	 * @param listener
172
	 *            the listener
173
	 */
174
	public void removeMapChangeListener(Object source,
175
			IMapPropertyChangeListener listener);
176
}
(-)src/org/eclipse/core/databinding/property/IValueProperty.java (+69 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
/**
15
 * Interface for value-typed properties
16
 * 
17
 * @since 1.2
18
 */
19
public interface IValueProperty extends IProperty {
20
	/**
21
	 * Returns the source's value property
22
	 * 
23
	 * @param source
24
	 *            the property source
25
	 * @return the current value of the source's value property
26
	 */
27
	public Object getValue(Object source);
28
29
	/**
30
	 * Sets the source's value property to the specified value
31
	 * 
32
	 * @param source
33
	 *            the property source
34
	 * @param value
35
	 *            the new value
36
	 */
37
	public void setValue(Object source, Object value);
38
39
	/**
40
	 * Returns the value type of the property, or <code>null</code> if untyped.
41
	 * 
42
	 * @return the value type of the property, or <code>null</code> if untyped.
43
	 */
44
	public Object getValueType();
45
46
	/**
47
	 * Adds the given value property change listener to the list of listeners
48
	 * for the given source.
49
	 * 
50
	 * @param source
51
	 *            the property source
52
	 * @param listener
53
	 *            the listener
54
	 */
55
	public void addValueChangeListener(Object source,
56
			IValuePropertyChangeListener listener);
57
58
	/**
59
	 * Removes the given list property change listener from the list of
60
	 * listeners for the given source.
61
	 * 
62
	 * @param source
63
	 *            the property source
64
	 * @param listener
65
	 *            the listener
66
	 */
67
	public void removeValueChangeListener(Object source,
68
			IValuePropertyChangeListener listener);
69
}
(-)src/org/eclipse/core/databinding/property/IMapPropertyChangeListener.java (+27 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
/**
15
 * Listener for changes to map properties on a property source
16
 * 
17
 * @since 1.2
18
 */
19
public interface IMapPropertyChangeListener extends IPropertyChangeListener {
20
	/**
21
	 * Handle a change to a map property on a specific property source.
22
	 * 
23
	 * @param event
24
	 *            an event describing the map change that occured.
25
	 */
26
	public void handleMapPropertyChange(MapPropertyChangeEvent event);
27
}
(-)src/org/eclipse/core/databinding/property/ValueProperty.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 org.eclipse.core.databinding.observable.value.ValueDiff;
15
16
/**
17
 * Abstract implementation of IValueProperty.
18
 * 
19
 * @since 1.2
20
 */
21
public abstract class ValueProperty extends Property implements IValueProperty {
22
	public final void addValueChangeListener(Object source,
23
			IValuePropertyChangeListener listener) {
24
		getChangeSupport().addListener(source, listener);
25
	}
26
27
	public final void removeValueChangeListener(Object source,
28
			IValuePropertyChangeListener listener) {
29
		getChangeSupport().removeListener(source, listener);
30
	}
31
32
	/**
33
	 * Fires a ValuePropertyChangeEvent with the given source and diff
34
	 * 
35
	 * @param source
36
	 *            the property source
37
	 * @param diff
38
	 *            the value diff
39
	 */
40
	protected void fireValueChange(Object source, ValueDiff diff) {
41
		getChangeSupport().firePropertyChange(
42
				new ValuePropertyChangeEvent(source, this, diff));
43
	}
44
}
(-)src/org/eclipse/core/databinding/property/BasicListProperty.java (+244 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;
13
14
import java.util.ArrayList;
15
import java.util.Collection;
16
import java.util.Iterator;
17
import java.util.List;
18
import java.util.ListIterator;
19
20
import org.eclipse.core.databinding.observable.Diffs;
21
import org.eclipse.core.databinding.observable.list.ListDiff;
22
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
23
import org.eclipse.core.internal.databinding.property.PropertyUpdateHelper;
24
25
/**
26
 * Abstract list property implementation for properties where the list can be
27
 * completely replaced in a single atomic operation.
28
 * <p>
29
 * For example, a list-typed bean property Customer.invoices can be modified by
30
 * calling Customer.setInvoices(List invoices).
31
 * 
32
 * @since 1.2
33
 */
34
public abstract class BasicListProperty extends ListProperty {
35
	private PropertyUpdateHelper updateHelper = new PropertyUpdateHelper();
36
37
	/**
38
	 * Returns whether this property is currently being updated on the source.
39
	 * Implementors should query this value to avoid unnecessary calculations,
40
	 * such as computing a diff.
41
	 * 
42
	 * @param source
43
	 *            the property source
44
	 * @return whether this property is currently being updated on the source.
45
	 */
46
	protected boolean isUpdating(Object source) {
47
		return updateHelper.isUpdating(source);
48
	}
49
50
	/**
51
	 * Sets the list property on the source to the specified list, then fires a
52
	 * list change using the specified diff.
53
	 * 
54
	 * @param source
55
	 *            the property source
56
	 * @param list
57
	 *            the new list
58
	 * @param diff
59
	 *            the diff to be fired after the list property has been set. If
60
	 *            null, the diff will be computed.
61
	 */
62
	protected void setList(Object source, List list, ListDiff diff) {
63
		if (diff == null) {
64
			diff = Diffs.computeListDiff(getList(source), list);
65
		}
66
67
		updateHelper.setUpdating(source, true);
68
		try {
69
			doSetList(source, list);
70
		} finally {
71
			updateHelper.setUpdating(source, false);
72
		}
73
74
		if (hasListeners(source)) {
75
			fireListChange(source, diff);
76
		}
77
	}
78
79
	protected void fireListChange(Object source, ListDiff diff) {
80
		if (!isUpdating(source))
81
			super.fireListChange(source, diff);
82
	}
83
84
	/**
85
	 * Sets the list property on the source to the specified list.
86
	 * 
87
	 * @param source
88
	 *            the property source
89
	 * @param list
90
	 *            the new list
91
	 */
92
	protected abstract void doSetList(Object source, List list);
93
94
	public boolean add(Object source, Object o) {
95
		add(source, size(source), o);
96
		return true;
97
	}
98
99
	public void add(Object source, int index, Object element) {
100
		List list = new ArrayList(getList(source));
101
		list.add(index, element);
102
		setList(source, list, Diffs.createListDiff(Diffs.createListDiffEntry(
103
				index, true, element)));
104
	}
105
106
	public boolean addAll(Object source, Collection c) {
107
		if (c.isEmpty())
108
			return false;
109
		addAll(source, size(source), c);
110
		return true;
111
	}
112
113
	public boolean addAll(Object source, int index, Collection c) {
114
		if (c.isEmpty()) {
115
			return false;
116
		}
117
118
		List list = new ArrayList(getList(source));
119
		List entries = new ArrayList();
120
		int i = index;
121
		for (Iterator it = c.iterator(); it.hasNext(); i++) {
122
			Object o = it.next();
123
			list.add(i, o);
124
			entries.add(Diffs.createListDiffEntry(i, true, o));
125
		}
126
		boolean changed = !entries.isEmpty();
127
		if (changed) {
128
			ListDiffEntry[] ea = (ListDiffEntry[]) entries
129
					.toArray(new ListDiffEntry[entries.size()]);
130
			setList(source, list, Diffs.createListDiff(ea));
131
		}
132
		return changed;
133
	}
134
135
	public void clear(Object source) {
136
		if (isEmpty(source))
137
			return;
138
		List list = getList(source);
139
		ListDiffEntry[] entries = new ListDiffEntry[list.size()];
140
		int i = 0;
141
		for (Iterator it = getList(source).iterator(); it.hasNext(); i++) {
142
			entries[i] = Diffs.createListDiffEntry(0, false, it.next());
143
		}
144
		setList(source, new ArrayList(), Diffs.createListDiff(entries));
145
	}
146
147
	public Object move(Object source, int oldIndex, int newIndex) {
148
		if (oldIndex == newIndex)
149
			return get(source, oldIndex);
150
		List list = new ArrayList(getList(source));
151
		Object result = list.remove(oldIndex);
152
		list.add(newIndex, result);
153
		setList(source, list, Diffs.createListDiff(Diffs.createListDiffEntry(
154
				oldIndex, false, result), Diffs.createListDiffEntry(newIndex,
155
				true, result)));
156
		return result;
157
	}
158
159
	public boolean remove(Object source, Object o) {
160
		int i = indexOf(source, o);
161
		if (i == -1)
162
			return false;
163
		remove(source, i);
164
		return true;
165
	}
166
167
	public Object remove(Object source, int index) {
168
		List list = new ArrayList(getList(source));
169
		Object result = list.remove(index);
170
		setList(source, list, Diffs.createListDiff(Diffs.createListDiffEntry(
171
				index, false, result)));
172
		return result;
173
	}
174
175
	public boolean removeAll(Object source, Collection c) {
176
		if (isEmpty(source)) {
177
			return false;
178
		}
179
		if (c.isEmpty()) {
180
			return false;
181
		}
182
		List list = new ArrayList(getList(source));
183
		List entries = new ArrayList();
184
		for (ListIterator it = list.listIterator(); it.hasNext();) {
185
			Object o = it.next();
186
			if (c.contains(o)) {
187
				entries.add(Diffs.createListDiffEntry(it.previousIndex(),
188
						false, o));
189
				it.remove();
190
			}
191
		}
192
		boolean changed = !entries.isEmpty();
193
		if (changed) {
194
			ListDiffEntry[] ea = (ListDiffEntry[]) entries
195
					.toArray(new ListDiffEntry[entries.size()]);
196
			setList(source, list, Diffs.createListDiff(ea));
197
		}
198
		return changed;
199
	}
200
201
	public boolean retainAll(Object source, Collection c) {
202
		if (isEmpty(source)) {
203
			return false;
204
		}
205
		if (c.isEmpty()) {
206
			clear(source);
207
			return true;
208
		}
209
		List list = new ArrayList(getList(source));
210
		List entries = new ArrayList();
211
		for (ListIterator it = list.listIterator(); it.hasNext();) {
212
			Object o = it.next();
213
			if (!c.contains(o)) {
214
				entries.add(Diffs.createListDiffEntry(it.previousIndex(),
215
						false, o));
216
				it.remove();
217
			}
218
		}
219
		boolean changed = !entries.isEmpty();
220
		if (changed) {
221
			ListDiffEntry[] ea = (ListDiffEntry[]) entries
222
					.toArray(new ListDiffEntry[entries.size()]);
223
			setList(source, list, Diffs.createListDiff(ea));
224
		}
225
		return changed;
226
	}
227
228
	public Object set(Object source, int index, Object element) {
229
		List list = new ArrayList(getList(source));
230
		Object result = list.set(index, element);
231
		setList(source, list, Diffs.createListDiff(Diffs.createListDiffEntry(
232
				index, false, result), Diffs.createListDiffEntry(index, true,
233
				element)));
234
		return result;
235
	}
236
237
	public synchronized void dispose() {
238
		if (updateHelper != null) {
239
			updateHelper.dispose();
240
			updateHelper = null;
241
		}
242
		super.dispose();
243
	}
244
}
(-)src/org/eclipse/core/databinding/property/IValuePropertyChangeListener.java (+27 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
/**
15
 * Listener for changes to value properties on a property source
16
 * 
17
 * @since 1.2
18
 */
19
public interface IValuePropertyChangeListener extends IPropertyChangeListener {
20
	/**
21
	 * Handle a change to a value property on a specific property source.
22
	 * 
23
	 * @param event
24
	 *            an event describing the value change that occured.
25
	 */
26
	public void handleValuePropertyChange(ValuePropertyChangeEvent event);
27
}
(-)src/org/eclipse/core/databinding/property/masterdetail/MasterDetailProperties.java (+200 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.masterdetail;
13
14
import org.eclipse.core.databinding.property.IListProperty;
15
import org.eclipse.core.databinding.property.IMapProperty;
16
import org.eclipse.core.databinding.property.ISetProperty;
17
import org.eclipse.core.databinding.property.IValueProperty;
18
import org.eclipse.core.internal.databinding.property.masterdetail.ListPropertyDetailValueList;
19
import org.eclipse.core.internal.databinding.property.masterdetail.MapPropertyDetailValueMap;
20
import org.eclipse.core.internal.databinding.property.masterdetail.SetPropertyDetailValueMap;
21
import org.eclipse.core.internal.databinding.property.masterdetail.ValuePropertyDetailList;
22
import org.eclipse.core.internal.databinding.property.masterdetail.ValuePropertyDetailMap;
23
import org.eclipse.core.internal.databinding.property.masterdetail.ValuePropertyDetailSet;
24
import org.eclipse.core.internal.databinding.property.masterdetail.ValuePropertyDetailValue;
25
26
/**
27
 * A factory for combining properties to create nested properties.
28
 * <p>
29
 * Example: Suppose class <code>A</code> has a property <code>b</code> of type
30
 * <code>B</code>, and that class <code>B</code> has a property <code>c</code>
31
 * of type <code>C</code>:
32
 * 
33
 * <pre>
34
 * A a = new A();
35
 * B b = a.getB();
36
 * IValueProperty ab = BeanProperties.valueProperty(A.class, &quot;b&quot;);
37
 * assertTrue(ab.getValue(a) == b);
38
 * 
39
 * IValueProperty bc = BeanProperties.valueProperty(B.class, &quot;c&quot;);
40
 * C c = b.getC();
41
 * assertTrue(bc.getValue(b) == c);
42
 * </pre>
43
 * 
44
 * Using MasterDetailProperties, the <code>ab</code> and <code>bc</code>
45
 * properties may be combined to form a nested <code>abc</code> property:
46
 * 
47
 * <pre>
48
 * IValueProperty abc = MasterDetailProperties.detailValue(ab, bc)
49
 * assertTrue(abc.getValue(a) == c);
50
 * </pre>
51
 * 
52
 * @since 1.2
53
 */
54
public class MasterDetailProperties {
55
	// Properties of IValueProperty master properties
56
57
	/**
58
	 * Returns the nested combination of the master value and detail value
59
	 * properties. Value modifications made through the returned property are
60
	 * delegated to the detail property, using the value of the master property
61
	 * as the source.
62
	 * 
63
	 * @param masterValue
64
	 *            the master property
65
	 * @param detailValue
66
	 *            the detail property
67
	 * @return the nested combination of the master and detail properties
68
	 */
69
	public static IValueProperty detailValue(IValueProperty masterValue,
70
			IValueProperty detailValue) {
71
		return new ValuePropertyDetailValue(masterValue, detailValue);
72
	}
73
74
	/**
75
	 * Returns the nested combination of the master value and detail list
76
	 * properties. List modifications made through the returned property are
77
	 * delegated to the detail property, using the value of the master property
78
	 * as the source.
79
	 * 
80
	 * @param masterValue
81
	 *            the master property
82
	 * @param detailList
83
	 *            the detail property
84
	 * @return the nested combination of the master value and detail list
85
	 *         properties
86
	 */
87
	public static IListProperty detailList(IValueProperty masterValue,
88
			IListProperty detailList) {
89
		return new ValuePropertyDetailList(masterValue, detailList);
90
	}
91
92
	/**
93
	 * Returns the nested combination of the master value and detail set
94
	 * properties. Set modifications made through the returned property are
95
	 * delegated to the detail property, using the value of the master property
96
	 * as the source.
97
	 * 
98
	 * @param masterValue
99
	 *            the master property
100
	 * @param detailSet
101
	 *            the detail property
102
	 * @return the nested combination of the master value and detail set
103
	 *         properties
104
	 */
105
	public static ISetProperty detailSet(IValueProperty masterValue,
106
			ISetProperty detailSet) {
107
		return new ValuePropertyDetailSet(masterValue, detailSet);
108
	}
109
110
	/**
111
	 * Returns the nested combination of the master value and detail map
112
	 * properties. Map modifications made through the returned property are
113
	 * delegated to the detail property, using the value of the master property
114
	 * as the source.
115
	 * 
116
	 * @param masterValue
117
	 *            the master property
118
	 * @param detailMap
119
	 *            the detail property
120
	 * @return the nested combination of the master value and detial map
121
	 *         properties
122
	 */
123
	public static IMapProperty detailMap(IValueProperty masterValue,
124
			IMapProperty detailMap) {
125
		return new ValuePropertyDetailMap(masterValue, detailMap);
126
	}
127
128
	// Properties of IListProperty master properties
129
130
	/**
131
	 * Returns the nested combination of the master list and detail value
132
	 * properties. Note that because this property is a projection of value
133
	 * properties over a list, the only modification supported is through the
134
	 * {@link IValueProperty#setValue(Object, Object)} method. Modifications
135
	 * made through the returned property are delegated to the detail property,
136
	 * using the corresponding list element from the master property as the
137
	 * source.
138
	 * 
139
	 * @param masterList
140
	 *            the master property
141
	 * @param detailValue
142
	 *            the detail property
143
	 * @return the nested combination of the master list and detail value
144
	 *         properties
145
	 */
146
	public static IListProperty detailValues(IListProperty masterList,
147
			IValueProperty detailValue) {
148
		return new ListPropertyDetailValueList(masterList, detailValue);
149
	}
150
151
	// Properties of ISetProperty master properties
152
153
	/**
154
	 * Returns the nested combination of the master set and detail value
155
	 * properties. Note that because this property is a projection of value
156
	 * properties over a set, the only modifications supported are through the
157
	 * {@link IMapProperty#put(Object, Object, Object)} and
158
	 * {@link IMapProperty#putAll(Object, java.util.Map)} methods. In the latter
159
	 * case, this property does not put entries for keys not already in the
160
	 * master key set. Modifications made through the returned property are
161
	 * delegated to the detail property, using the corresponding set element
162
	 * from the master property as the source.
163
	 * 
164
	 * @param masterKeySet
165
	 *            the master property
166
	 * @param detailValues
167
	 *            the detail property
168
	 * @return the nested combination of the master set and detail value
169
	 *         properties
170
	 */
171
	public static IMapProperty detailValues(ISetProperty masterKeySet,
172
			IValueProperty detailValues) {
173
		return new SetPropertyDetailValueMap(masterKeySet, detailValues);
174
	}
175
176
	// Properties of IMapProperty master properties
177
178
	/**
179
	 * Returns the nested combination of the master map and detail value
180
	 * properties. Note that because this property is a projection of value
181
	 * properties over a values collection, the only modifications supported are
182
	 * through the {@link IMapProperty#put(Object, Object, Object)} and
183
	 * {@link IMapProperty#putAll(Object, java.util.Map)} methods. In the latter
184
	 * case, this property does not entries for keys not already contained in
185
	 * the master map's key set. Modifications made through the returned
186
	 * property are delegated to the detail property, using the corresponding
187
	 * entry value from the master property as the source.
188
	 * 
189
	 * @param masterMap
190
	 *            the master property
191
	 * @param detailValues
192
	 *            the detail property
193
	 * @return the nested combination of the master map and detail value
194
	 *         properties.
195
	 */
196
	public static IMapProperty detailValues(IMapProperty masterMap,
197
			IValueProperty detailValues) {
198
		return new MapPropertyDetailValueMap(masterMap, detailValues);
199
	}
200
}
(-)src/org/eclipse/core/internal/databinding/property/masterdetail/ValuePropertyDetailValue.java (+123 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.masterdetail;
13
14
import java.util.HashMap;
15
import java.util.Map;
16
17
import org.eclipse.core.databinding.observable.Diffs;
18
import org.eclipse.core.databinding.observable.value.ValueDiff;
19
import org.eclipse.core.databinding.property.IValueProperty;
20
import org.eclipse.core.databinding.property.IValuePropertyChangeListener;
21
import org.eclipse.core.databinding.property.ValueProperty;
22
import org.eclipse.core.databinding.property.ValuePropertyChangeEvent;
23
24
/**
25
 * @since 1.2
26
 * 
27
 */
28
public class ValuePropertyDetailValue extends ValueProperty implements
29
		IValueProperty {
30
	private IValueProperty masterProperty;
31
	private IValueProperty detailProperty;
32
33
	private Map sourceToDetailListener = new HashMap();
34
35
	private IValuePropertyChangeListener masterListener = new MasterPropertyListener();
36
37
	private class MasterPropertyListener implements
38
			IValuePropertyChangeListener {
39
		public void handleValuePropertyChange(ValuePropertyChangeEvent event) {
40
			Object oldSource = event.diff.getOldValue();
41
			Object newSource = event.diff.getNewValue();
42
43
			Object oldValue = detailProperty.getValue(oldSource);
44
			Object newValue = detailProperty.getValue(newSource);
45
46
			ValueDiff diff = Diffs.createValueDiff(oldValue, newValue);
47
48
			Object source = event.getSource();
49
50
			removeDetailPropertyListener(source);
51
			addDetailPropertyListener(source);
52
53
			fireValueChange(source, diff);
54
		}
55
	}
56
57
	private class DetailPropertyListener implements
58
			IValuePropertyChangeListener {
59
		private Object source;
60
		private Object masterValue;
61
62
		DetailPropertyListener(Object source, Object masterValue) {
63
			this.source = source;
64
			this.masterValue = masterValue;
65
		}
66
67
		public void handleValuePropertyChange(ValuePropertyChangeEvent event) {
68
			fireValueChange(source, event.diff);
69
		}
70
	}
71
72
	/**
73
	 * @param masterProperty
74
	 * @param detailProperty
75
	 */
76
	public ValuePropertyDetailValue(IValueProperty masterProperty,
77
			IValueProperty detailProperty) {
78
		this.masterProperty = masterProperty;
79
		this.detailProperty = detailProperty;
80
	}
81
82
	protected void addListenerTo(Object source) {
83
		masterProperty.addValueChangeListener(source, masterListener);
84
		addDetailPropertyListener(source);
85
	}
86
87
	protected void removeListenerFrom(Object source) {
88
		masterProperty.removeValueChangeListener(source, masterListener);
89
		removeDetailPropertyListener(source);
90
	}
91
92
	private void addDetailPropertyListener(Object source) {
93
		Object masterValue = masterProperty.getValue(source);
94
		DetailPropertyListener detailListener = new DetailPropertyListener(
95
				source, masterValue);
96
		detailProperty.addValueChangeListener(masterValue, detailListener);
97
		sourceToDetailListener.put(source, detailListener);
98
	}
99
100
	private void removeDetailPropertyListener(Object source) {
101
		DetailPropertyListener detailListener = (DetailPropertyListener) sourceToDetailListener
102
				.remove(source);
103
		if (detailListener != null) {
104
			detailProperty.removeValueChangeListener(
105
					detailListener.masterValue, detailListener);
106
		}
107
		sourceToDetailListener.remove(source);
108
	}
109
110
	public Object getValue(Object source) {
111
		Object masterValue = masterProperty.getValue(source);
112
		return detailProperty.getValue(masterValue);
113
	}
114
115
	public Object getValueType() {
116
		return detailProperty.getValueType();
117
	}
118
119
	public void setValue(Object source, Object value) {
120
		Object masterValue = masterProperty.getValue(source);
121
		detailProperty.setValue(masterValue, value);
122
	}
123
}
(-)src/org/eclipse/core/internal/databinding/property/MapValuePropertyObservableMap.java (+323 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 java.util.AbstractSet;
15
import java.util.HashMap;
16
import java.util.HashSet;
17
import java.util.IdentityHashMap;
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.property.IValueProperty;
32
import org.eclipse.core.databinding.property.IValuePropertyChangeListener;
33
import org.eclipse.core.databinding.property.ValuePropertyChangeEvent;
34
import org.eclipse.core.internal.databinding.Util;
35
36
/**
37
 * @since 3.3
38
 * 
39
 */
40
public class MapValuePropertyObservableMap extends AbstractObservableMap
41
		implements IObserving {
42
	private IObservableMap map;
43
	private IValueProperty property;
44
45
	private Map keyToMasterValueListener;
46
47
	private boolean updating = false;
48
	private boolean disposed = false;
49
50
	private IMapChangeListener mapListener = new IMapChangeListener() {
51
		public void handleMapChange(final MapChangeEvent event) {
52
			if (!updating && !disposed) {
53
				getRealm().exec(new Runnable() {
54
					public void run() {
55
						Map oldValues = new HashMap();
56
						Map newValues = new HashMap();
57
58
						Set addedKeys = event.diff.getAddedKeys();
59
						for (Iterator it = addedKeys.iterator(); it.hasNext();) {
60
							Object key = it.next();
61
							Object newSource = event.diff.getNewValue(key);
62
							Object newValue = property.getValue(newSource);
63
							newValues.put(key, newValue);
64
							addPropertySourceListener(key, newSource);
65
						}
66
67
						Set removedKeys = event.diff.getRemovedKeys();
68
						for (Iterator it = removedKeys.iterator(); it.hasNext();) {
69
							Object key = it.next();
70
							Object oldSource = event.diff.getOldValue(key);
71
							Object oldValue = property.getValue(oldSource);
72
							oldValues.put(key, oldValue);
73
							removePropertySourceListener(key, oldSource);
74
						}
75
76
						Set changedKeys = new HashSet(event.diff
77
								.getChangedKeys());
78
						for (Iterator it = changedKeys.iterator(); it.hasNext();) {
79
							Object key = it.next();
80
81
							Object oldSource = event.diff.getOldValue(key);
82
							Object newSource = event.diff.getNewValue(key);
83
84
							Object oldValue = property.getValue(oldSource);
85
							Object newValue = property.getValue(newSource);
86
87
							if (Util.equals(oldValue, newValue)) {
88
								it.remove();
89
							} else {
90
								oldValues.put(key, oldValue);
91
								newValues.put(key, newValue);
92
							}
93
94
							removePropertySourceListener(key, oldSource);
95
							addPropertySourceListener(key, newSource);
96
						}
97
98
						fireMapChange(Diffs.createMapDiff(addedKeys,
99
								removedKeys, changedKeys, oldValues, newValues));
100
					}
101
				});
102
			}
103
		}
104
	};
105
106
	private IStaleListener staleListener = new IStaleListener() {
107
		public void handleStale(StaleEvent staleEvent) {
108
			fireStale();
109
		}
110
	};
111
112
	/**
113
	 * @param map
114
	 * @param valueProperty
115
	 */
116
	public MapValuePropertyObservableMap(IObservableMap map,
117
			IValueProperty valueProperty) {
118
		super(map.getRealm());
119
		this.map = map;
120
		this.property = valueProperty;
121
122
		this.keyToMasterValueListener = new IdentityHashMap();
123
	}
124
125
	private class ValuePropertySourceChangeListener implements
126
			IValuePropertyChangeListener {
127
		private final Object key;
128
129
		public ValuePropertySourceChangeListener(Object key) {
130
			this.key = key;
131
		}
132
133
		public void handleValuePropertyChange(ValuePropertyChangeEvent event) {
134
			Object oldSource = event.diff.getOldValue();
135
			Object oldValue = property.getValue(oldSource);
136
			property.removeValueChangeListener(oldSource, this);
137
138
			Object newSource = event.diff.getNewValue();
139
			Object newValue = property.getValue(newSource);
140
			property.addValueChangeListener(newSource, this);
141
142
			if (!Util.equals(oldValue, newValue))
143
				fireMapChange(Diffs.createMapDiffSingleChange(key, oldValue,
144
						newValue));
145
		}
146
	}
147
148
	protected void firstListenerAdded() {
149
		if (!disposed) {
150
			map.addMapChangeListener(mapListener);
151
			map.addStaleListener(staleListener);
152
			for (Iterator iterator = map.entrySet().iterator(); iterator
153
					.hasNext();) {
154
				Map.Entry entry = (Map.Entry) iterator.next();
155
				Object key = entry.getKey();
156
				Object masterValue = entry.getValue();
157
158
				addPropertySourceListener(key, masterValue);
159
			}
160
		}
161
	}
162
163
	protected void lastListenerRemoved() {
164
		if (!disposed) {
165
			map.removeMapChangeListener(mapListener);
166
			map.removeStaleListener(staleListener);
167
			for (Iterator iterator = map.entrySet().iterator(); iterator
168
					.hasNext();) {
169
				Map.Entry entry = (Map.Entry) iterator.next();
170
				Object key = entry.getKey();
171
				Object propertySource = entry.getValue();
172
173
				removePropertySourceListener(key, propertySource);
174
			}
175
		}
176
	}
177
178
	private void addPropertySourceListener(Object key, Object propertySource) {
179
		IValuePropertyChangeListener propertyListener = new ValuePropertySourceChangeListener(
180
				key);
181
		property.addValueChangeListener(propertySource, propertyListener);
182
		keyToMasterValueListener.put(key, propertyListener);
183
	}
184
185
	private void removePropertySourceListener(Object key, Object propertySource) {
186
		IValuePropertyChangeListener propertyListener = (IValuePropertyChangeListener) keyToMasterValueListener
187
				.remove(key);
188
		if (propertyListener != null) {
189
			property
190
					.removeValueChangeListener(propertySource, propertyListener);
191
		}
192
	}
193
194
	protected Object doGet(Object key) {
195
		if (!map.containsKey(key))
196
			return null;
197
		return property.getValue(map.get(key));
198
	}
199
200
	protected Object doPut(Object key, Object value) {
201
		if (!map.containsKey(key))
202
			return null;
203
		Object source = map.get(key);
204
205
		Object oldValue = property.getValue(source);
206
207
		updating = true;
208
		try {
209
			property.setValue(source, value);
210
		} finally {
211
			updating = false;
212
		}
213
214
		Object newValue = property.getValue(source);
215
216
		if (!Util.equals(oldValue, newValue)) {
217
			fireMapChange(Diffs.createMapDiffSingleChange(key, oldValue,
218
					newValue));
219
		}
220
221
		return oldValue;
222
	}
223
224
	private Set entrySet;
225
226
	public Set entrySet() {
227
		getterCalled();
228
		if (entrySet == null)
229
			entrySet = new EntrySet();
230
		return entrySet;
231
	}
232
233
	class EntrySet extends AbstractSet {
234
		public Iterator iterator() {
235
			return new Iterator() {
236
				Iterator it = map.entrySet().iterator();
237
238
				public boolean hasNext() {
239
					getterCalled();
240
					return it.hasNext();
241
				}
242
243
				public Object next() {
244
					getterCalled();
245
					Map.Entry next = (Map.Entry) it.next();
246
					return new MapEntry(next.getKey());
247
				}
248
249
				public void remove() {
250
					it.remove();
251
				}
252
			};
253
		}
254
255
		public int size() {
256
			return map.size();
257
		}
258
	}
259
260
	class MapEntry implements Map.Entry {
261
		private Object key;
262
263
		MapEntry(Object key) {
264
			this.key = key;
265
		}
266
267
		public Object getKey() {
268
			getterCalled();
269
			return key;
270
		}
271
272
		public Object getValue() {
273
			getterCalled();
274
			return get(key);
275
		}
276
277
		public Object setValue(Object value) {
278
			return put(key, value);
279
		}
280
281
		public boolean equals(Object o) {
282
			getterCalled();
283
			if (o == this)
284
				return true;
285
			if (o == null)
286
				return false;
287
			if (!(o instanceof Map.Entry))
288
				return false;
289
			Map.Entry that = (Map.Entry) o;
290
			return Util.equals(this.getKey(), that.getKey())
291
					&& Util.equals(this.getValue(), that.getValue());
292
		}
293
294
		public int hashCode() {
295
			getterCalled();
296
			Object value = getValue();
297
			return (key == null ? 0 : key.hashCode())
298
					^ (value == null ? 0 : value.hashCode());
299
		}
300
	}
301
302
	public boolean isStale() {
303
		getterCalled();
304
		return map.isStale();
305
	}
306
307
	private void getterCalled() {
308
		ObservableTracker.getterCalled(this);
309
	}
310
311
	public Object getObserved() {
312
		return map;
313
	}
314
315
	public synchronized void dispose() {
316
		if (!disposed) {
317
			disposed = true;
318
			property = null;
319
		}
320
321
		super.dispose();
322
	}
323
}
(-)src/org/eclipse/core/internal/databinding/property/masterdetail/SetPropertyDetailValueMap.java (+217 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.masterdetail;
13
14
import java.util.Collections;
15
import java.util.HashMap;
16
import java.util.Iterator;
17
import java.util.Map;
18
import java.util.Set;
19
20
import org.eclipse.core.databinding.observable.Diffs;
21
import org.eclipse.core.databinding.observable.map.MapDiff;
22
import org.eclipse.core.databinding.property.ISetProperty;
23
import org.eclipse.core.databinding.property.ISetPropertyChangeListener;
24
import org.eclipse.core.databinding.property.IValueProperty;
25
import org.eclipse.core.databinding.property.IValuePropertyChangeListener;
26
import org.eclipse.core.databinding.property.MapProperty;
27
import org.eclipse.core.databinding.property.SetPropertyChangeEvent;
28
import org.eclipse.core.databinding.property.ValuePropertyChangeEvent;
29
30
/**
31
 * @since 3.3
32
 * 
33
 */
34
public class SetPropertyDetailValueMap extends MapProperty {
35
	private final ISetProperty masterProperty;
36
	private final IValueProperty detailProperty;
37
38
	private Map sourceToMasterElementToDetailListener = new HashMap();
39
40
	private ISetPropertyChangeListener masterListener = new MasterPropertyListener();
41
42
	private class MasterPropertyListener implements ISetPropertyChangeListener {
43
		public void handleSetPropertyChange(final SetPropertyChangeEvent event) {
44
			Object source = event.getSource();
45
46
			MapDiff diff;
47
			if (event.diff == null) {
48
				diff = null;
49
			} else {
50
				final Set removals = event.diff.getRemovals();
51
				final Set additions = event.diff.getAdditions();
52
53
				diff = new MapDiff() {
54
					public Set getAddedKeys() {
55
						return additions;
56
					}
57
58
					public Set getChangedKeys() {
59
						return Collections.EMPTY_SET;
60
					}
61
62
					public Object getNewValue(Object key) {
63
						return getValue(key);
64
					}
65
66
					public Object getOldValue(Object key) {
67
						return getValue(key);
68
					}
69
70
					private Object getValue(Object key) {
71
						return detailProperty.getValue(key);
72
					}
73
74
					public Set getRemovedKeys() {
75
						return removals;
76
					}
77
				};
78
79
				for (Iterator it = removals.iterator(); it.hasNext();) {
80
					removeElementPropertyListener(source, it.next());
81
				}
82
				for (Iterator it = additions.iterator(); it.hasNext();) {
83
					addElementPropertyListener(source, it.next());
84
				}
85
			}
86
87
			fireMapChange(source, diff);
88
		}
89
90
		private void addElementPropertyListener(Object source, Object element) {
91
			Map elementToDetailListener = (Map) sourceToMasterElementToDetailListener
92
					.get(source);
93
			if (elementToDetailListener == null) {
94
				sourceToMasterElementToDetailListener.put(source,
95
						elementToDetailListener = new HashMap());
96
			}
97
			if (!elementToDetailListener.containsKey(element)) {
98
				DetailPropertyListener detailListener = new DetailPropertyListener(
99
						source, element);
100
				detailProperty.addValueChangeListener(element, detailListener);
101
				elementToDetailListener.put(element, detailListener);
102
			}
103
		}
104
105
		private void removeElementPropertyListener(Object source, Object element) {
106
			Map elementToDetailListener = (Map) sourceToMasterElementToDetailListener
107
					.get(source);
108
			if (elementToDetailListener != null
109
					&& elementToDetailListener.containsKey(element)) {
110
				DetailPropertyListener detailListener = (DetailPropertyListener) elementToDetailListener
111
						.remove(element);
112
				detailProperty.removeValueChangeListener(element,
113
						detailListener);
114
			}
115
		}
116
	}
117
118
	private class DetailPropertyListener implements
119
			IValuePropertyChangeListener {
120
		private Object source;
121
		private Object masterValue;
122
123
		DetailPropertyListener(Object source, Object masterValue) {
124
			this.source = source;
125
			this.masterValue = masterValue;
126
		}
127
128
		public void handleValuePropertyChange(ValuePropertyChangeEvent event) {
129
			MapDiff diff;
130
			if (event.diff == null) {
131
				diff = null;
132
			} else {
133
				diff = Diffs.createMapDiffSingleChange(masterValue, event.diff
134
						.getOldValue(), event.diff.getNewValue());
135
			}
136
			fireMapChange(source, diff);
137
		}
138
	}
139
140
	/**
141
	 * @param masterProperty
142
	 * @param detailProperty
143
	 */
144
	public SetPropertyDetailValueMap(ISetProperty masterProperty,
145
			IValueProperty detailProperty) {
146
		this.masterProperty = masterProperty;
147
		this.detailProperty = detailProperty;
148
	}
149
150
	protected void addListenerTo(Object source) {
151
		masterProperty.addSetChangeListener(source, masterListener);
152
153
		Map masterElementToDetailListener = new HashMap();
154
		for (Iterator it = masterProperty.getSet(source).iterator(); it
155
				.hasNext();) {
156
			Object masterElement = it.next();
157
			DetailPropertyListener detailListener = new DetailPropertyListener(
158
					source, masterElement);
159
			detailProperty
160
					.addValueChangeListener(masterElement, detailListener);
161
			masterElementToDetailListener.put(masterElement, detailListener);
162
		}
163
164
		sourceToMasterElementToDetailListener.put(source,
165
				masterElementToDetailListener);
166
	}
167
168
	protected void removeListenerFrom(Object source) {
169
		masterProperty.removeSetChangeListener(source, masterListener);
170
		Map masterElementToDetailListener = (Map) sourceToMasterElementToDetailListener
171
				.remove(source);
172
		if (masterElementToDetailListener != null) {
173
			for (Iterator it = masterElementToDetailListener.entrySet()
174
					.iterator(); it.hasNext();) {
175
				Map.Entry entry = (Map.Entry) it.next();
176
				detailProperty.removeValueChangeListener(entry.getKey(),
177
						(DetailPropertyListener) entry.getValue());
178
			}
179
		}
180
	}
181
182
	public Map getMap(Object source) {
183
		Map result = new HashMap();
184
		for (Iterator it = masterProperty.getSet(source).iterator(); it
185
				.hasNext();) {
186
			Object element = it.next();
187
			result.put(element, detailProperty.getValue(element));
188
		}
189
		return result;
190
	}
191
192
	public void clear(Object source) {
193
		throw new UnsupportedOperationException();
194
	}
195
196
	public Object put(Object source, Object key, Object value) {
197
		if (!masterProperty.contains(source, key))
198
			return null;
199
		Object result = detailProperty.getValue(key);
200
		detailProperty.setValue(key, value);
201
		return result;
202
	}
203
204
	public void putAll(Object source, Map t) {
205
		Set masterSet = masterProperty.getSet(source);
206
		for (Iterator it = t.entrySet().iterator(); it.hasNext();) {
207
			Map.Entry entry = (Map.Entry) it.next();
208
			if (masterSet.contains(entry.getKey())) {
209
				detailProperty.setValue(entry.getKey(), entry.getValue());
210
			}
211
		}
212
	}
213
214
	public Object remove(Object source, Object key) {
215
		throw new UnsupportedOperationException();
216
	}
217
}
(-)src/org/eclipse/core/internal/databinding/property/masterdetail/ValuePropertyDetailList.java (+173 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.masterdetail;
13
14
import java.util.Collection;
15
import java.util.HashMap;
16
import java.util.List;
17
import java.util.Map;
18
19
import org.eclipse.core.databinding.observable.Diffs;
20
import org.eclipse.core.databinding.property.IListProperty;
21
import org.eclipse.core.databinding.property.IListPropertyChangeListener;
22
import org.eclipse.core.databinding.property.IValueProperty;
23
import org.eclipse.core.databinding.property.IValuePropertyChangeListener;
24
import org.eclipse.core.databinding.property.ListProperty;
25
import org.eclipse.core.databinding.property.ListPropertyChangeEvent;
26
import org.eclipse.core.databinding.property.ValuePropertyChangeEvent;
27
28
/**
29
 * @since 3.3
30
 * 
31
 */
32
public class ValuePropertyDetailList extends ListProperty {
33
	private final IValueProperty masterProperty;
34
	private final IListProperty detailProperty;
35
36
	private Map sourceToDetailListener = new HashMap();
37
38
	private IValuePropertyChangeListener masterListener = new MasterPropertyListener();
39
40
	private class MasterPropertyListener implements
41
			IValuePropertyChangeListener {
42
		public void handleValuePropertyChange(ValuePropertyChangeEvent event) {
43
			Object oldSource = event.diff.getOldValue();
44
			Object newSource = event.diff.getNewValue();
45
46
			List oldList = detailProperty.getList(oldSource);
47
			List newList = detailProperty.getList(newSource);
48
49
			Object source = event.getSource();
50
51
			removeDetailPropertyListener(source);
52
			addDetailPropertyListener(source);
53
54
			fireListChange(source, Diffs.computeListDiff(oldList, newList));
55
		}
56
	}
57
58
	private class DetailPropertyListener implements IListPropertyChangeListener {
59
		private Object source;
60
		private Object masterValue;
61
62
		DetailPropertyListener(Object source, Object masterValue) {
63
			this.source = source;
64
			this.masterValue = masterValue;
65
		}
66
67
		public void handleListPropertyChange(ListPropertyChangeEvent event) {
68
			fireListChange(source, event.diff);
69
		}
70
	}
71
72
	/**
73
	 * @param masterProperty
74
	 * @param detailProperty
75
	 */
76
	public ValuePropertyDetailList(IValueProperty masterProperty,
77
			IListProperty detailProperty) {
78
		this.masterProperty = masterProperty;
79
		this.detailProperty = detailProperty;
80
	}
81
82
	protected void addListenerTo(Object source) {
83
		masterProperty.addValueChangeListener(source, masterListener);
84
		addDetailPropertyListener(source);
85
	}
86
87
	protected void removeListenerFrom(Object source) {
88
		masterProperty.removeValueChangeListener(source, masterListener);
89
		removeDetailPropertyListener(source);
90
	}
91
92
	private void addDetailPropertyListener(Object source) {
93
		Object masterValue = masterProperty.getValue(source);
94
		DetailPropertyListener detailListener = new DetailPropertyListener(
95
				source, masterValue);
96
		detailProperty.addListChangeListener(masterValue, detailListener);
97
		sourceToDetailListener.put(source, detailListener);
98
	}
99
100
	private void removeDetailPropertyListener(Object source) {
101
		DetailPropertyListener detailListener = (DetailPropertyListener) sourceToDetailListener
102
				.remove(source);
103
		if (detailListener != null) {
104
			detailProperty.removeListChangeListener(detailListener.masterValue,
105
					detailListener);
106
		}
107
		sourceToDetailListener.remove(source);
108
	}
109
110
	public List getList(Object source) {
111
		Object masterValue = masterProperty.getValue(source);
112
		return detailProperty.getList(masterValue);
113
	}
114
115
	public boolean add(Object source, Object o) {
116
		Object masterValue = masterProperty.getValue(source);
117
		return detailProperty.add(masterValue, o);
118
	}
119
120
	public boolean addAll(Object source, Collection c) {
121
		Object masterValue = masterProperty.getValue(source);
122
		return detailProperty.addAll(masterValue, c);
123
	}
124
125
	public void clear(Object source) {
126
		Object masterValue = masterProperty.getValue(source);
127
		detailProperty.clear(masterValue);
128
	}
129
130
	public Object getElementType() {
131
		return detailProperty.getElementType();
132
	}
133
134
	public boolean remove(Object source, Object o) {
135
		Object masterValue = masterProperty.getValue(source);
136
		return detailProperty.remove(masterValue, o);
137
	}
138
139
	public boolean removeAll(Object source, Collection c) {
140
		Object masterValue = masterProperty.getValue(source);
141
		return detailProperty.removeAll(masterValue, c);
142
	}
143
144
	public boolean retainAll(Object source, Collection c) {
145
		Object masterValue = masterProperty.getValue(source);
146
		return detailProperty.retainAll(masterValue, c);
147
	}
148
149
	public void add(Object source, int index, Object element) {
150
		Object masterValue = masterProperty.getValue(source);
151
		detailProperty.add(masterValue, index, element);
152
	}
153
154
	public boolean addAll(Object source, int index, Collection c) {
155
		Object masterValue = masterProperty.getValue(source);
156
		return detailProperty.addAll(masterValue, index, c);
157
	}
158
159
	public Object move(Object source, int oldIndex, int newIndex) {
160
		Object masterValue = masterProperty.getValue(source);
161
		return detailProperty.move(masterValue, oldIndex, newIndex);
162
	}
163
164
	public Object remove(Object source, int index) {
165
		Object masterValue = masterProperty.getValue(source);
166
		return detailProperty.remove(masterValue, index);
167
	}
168
169
	public Object set(Object source, int index, Object element) {
170
		Object masterValue = masterProperty.getValue(source);
171
		return detailProperty.set(masterValue, index, element);
172
	}
173
}
(-)src/org/eclipse/core/databinding/property/SetProperty.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.core.databinding.property;
13
14
import java.util.Collection;
15
16
import org.eclipse.core.databinding.observable.set.SetDiff;
17
18
/**
19
 * Abstract implementation of ISetProperty
20
 * 
21
 * @since 1.2
22
 */
23
public abstract class SetProperty extends CollectionProperty implements
24
		ISetProperty {
25
	Collection getCollection(Object source) {
26
		return getSet(source);
27
	}
28
29
	public final void addSetChangeListener(Object source,
30
			ISetPropertyChangeListener listener) {
31
		getChangeSupport().addListener(source, listener);
32
	}
33
34
	public final void removeSetChangeListener(Object source,
35
			ISetPropertyChangeListener listener) {
36
		getChangeSupport().removeListener(source, listener);
37
	}
38
39
	/**
40
	 * Fires a SetPropertyChangeEvent with the given source and diff
41
	 * 
42
	 * @param source
43
	 *            the property source
44
	 * @param diff
45
	 *            the set diff
46
	 */
47
	protected void fireSetChange(Object source, SetDiff diff) {
48
		getChangeSupport().firePropertyChange(
49
				new SetPropertyChangeEvent(source, this, diff));
50
	}
51
}
(-)src/org/eclipse/core/databinding/property/IProperty.java (+25 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
/**
15
 * Interface for observing a property of a source object.
16
 * 
17
 * @noimplement
18
 * @since 1.2
19
 */
20
public interface IProperty {
21
	/**
22
	 * Disposes the property, removing all property listeners on source objects.
23
	 */
24
	public void dispose();
25
}
(-)src/org/eclipse/core/internal/databinding/property/masterdetail/ListPropertyDetailValueList.java (+259 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.masterdetail;
13
14
import java.util.ArrayList;
15
import java.util.Collection;
16
import java.util.HashMap;
17
import java.util.HashSet;
18
import java.util.Iterator;
19
import java.util.List;
20
import java.util.ListIterator;
21
import java.util.Map;
22
import java.util.Set;
23
24
import org.eclipse.core.databinding.observable.Diffs;
25
import org.eclipse.core.databinding.observable.list.ListDiff;
26
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
27
import org.eclipse.core.databinding.property.IListProperty;
28
import org.eclipse.core.databinding.property.IListPropertyChangeListener;
29
import org.eclipse.core.databinding.property.IValueProperty;
30
import org.eclipse.core.databinding.property.IValuePropertyChangeListener;
31
import org.eclipse.core.databinding.property.ListProperty;
32
import org.eclipse.core.databinding.property.ListPropertyChangeEvent;
33
import org.eclipse.core.databinding.property.ValuePropertyChangeEvent;
34
import org.eclipse.core.internal.databinding.Util;
35
36
/**
37
 * @since 3.3
38
 * 
39
 */
40
public class ListPropertyDetailValueList extends ListProperty {
41
	private final IListProperty masterProperty;
42
	private final IValueProperty detailProperty;
43
44
	private Map sourceToMasterElementToDetailListener = new HashMap();
45
46
	private IListPropertyChangeListener masterListener = new MasterPropertyListener();
47
48
	private class MasterPropertyListener implements IListPropertyChangeListener {
49
		public void handleListPropertyChange(ListPropertyChangeEvent event) {
50
			Object source = event.getSource();
51
			ListDiffEntry[] masterEntries = event.diff.getDifferences();
52
			ListDiffEntry[] detailEntries = new ListDiffEntry[masterEntries.length];
53
			Set masterElementsAdded = new HashSet();
54
			Set masterElementsRemoved = new HashSet();
55
			for (int i = 0; i < masterEntries.length; i++) {
56
				ListDiffEntry masterDifference = masterEntries[i];
57
				int index = masterDifference.getPosition();
58
				boolean addition = masterDifference.isAddition();
59
				Object masterElement = masterDifference.getElement();
60
				Object elementDetailValue = detailProperty
61
						.getValue(masterElement);
62
				detailEntries[i] = Diffs.createListDiffEntry(index, addition,
63
						elementDetailValue);
64
				if (addition)
65
					masterElementsAdded.add(masterElement);
66
				else
67
					masterElementsRemoved.add(masterElement);
68
			}
69
70
			ListDiff diff = Diffs.createListDiff(detailEntries);
71
72
			for (Iterator it = masterElementsRemoved.iterator(); it.hasNext();) {
73
				Object element = it.next();
74
				if (!masterProperty.contains(source, element))
75
					removeElementPropertyListener(source, element);
76
			}
77
			for (Iterator it = masterElementsAdded.iterator(); it.hasNext();) {
78
				Object element = it.next();
79
				if (masterProperty.contains(source, element))
80
					addElementPropertyListener(source, element);
81
			}
82
83
			fireListChange(source, diff);
84
		}
85
86
		private void addElementPropertyListener(Object source, Object element) {
87
			Map elementToDetailListener = (Map) sourceToMasterElementToDetailListener
88
					.get(source);
89
			if (elementToDetailListener == null) {
90
				sourceToMasterElementToDetailListener.put(source,
91
						elementToDetailListener = new HashMap());
92
			}
93
			if (!elementToDetailListener.containsKey(element)) {
94
				DetailPropertyListener detailListener = new DetailPropertyListener(
95
						source, element);
96
				detailProperty.addValueChangeListener(element, detailListener);
97
				elementToDetailListener.put(element, detailListener);
98
			}
99
		}
100
101
		private void removeElementPropertyListener(Object source, Object element) {
102
			Map elementToDetailListener = (Map) sourceToMasterElementToDetailListener
103
					.get(source);
104
			if (elementToDetailListener != null
105
					&& elementToDetailListener.containsKey(element)) {
106
				DetailPropertyListener detailListener = (DetailPropertyListener) elementToDetailListener
107
						.remove(element);
108
				detailProperty.removeValueChangeListener(element,
109
						detailListener);
110
			}
111
		}
112
	}
113
114
	private class DetailPropertyListener implements
115
			IValuePropertyChangeListener {
116
		private Object source;
117
		private Object masterElement;
118
119
		DetailPropertyListener(Object source, Object masterElement) {
120
			this.source = source;
121
			this.masterElement = masterElement;
122
		}
123
124
		private int[] findIndices() {
125
			List indices = new ArrayList();
126
127
			List list = masterProperty.getList(source);
128
			for (ListIterator it = list.listIterator(); it.hasNext();) {
129
				if (Util.equals(masterElement, it.next()))
130
					indices.add(Integer.valueOf(it.previousIndex()));
131
			}
132
133
			int[] result = new int[indices.size()];
134
			for (int i = 0; i < result.length; i++) {
135
				result[i] = ((Integer) indices.get(i)).intValue();
136
			}
137
			return result;
138
		}
139
140
		public void handleValuePropertyChange(ValuePropertyChangeEvent event) {
141
			int[] indices = findIndices();
142
			Object oldValue = event.diff.getOldValue();
143
			Object newValue = event.diff.getNewValue();
144
			ListDiffEntry[] entries = new ListDiffEntry[indices.length * 2];
145
			for (int i = 0; i < indices.length; i++) {
146
				int index = indices[i];
147
				entries[i * 2] = Diffs.createListDiffEntry(index, false,
148
						oldValue);
149
				entries[i * 2 + 1] = Diffs.createListDiffEntry(index, true,
150
						newValue);
151
			}
152
153
			ListDiff diff = Diffs.createListDiff(entries);
154
			fireListChange(source, diff);
155
		}
156
	}
157
158
	/**
159
	 * @param masterProperty
160
	 * @param detailProperty
161
	 */
162
	public ListPropertyDetailValueList(IListProperty masterProperty,
163
			IValueProperty detailProperty) {
164
		this.masterProperty = masterProperty;
165
		this.detailProperty = detailProperty;
166
	}
167
168
	protected void addListenerTo(Object source) {
169
		masterProperty.addListChangeListener(source, masterListener);
170
171
		Map masterElementToDetailListener = new HashMap();
172
		for (Iterator it = masterProperty.getList(source).iterator(); it
173
				.hasNext();) {
174
			Object masterElement = it.next();
175
			DetailPropertyListener detailListener = new DetailPropertyListener(
176
					source, masterElement);
177
			detailProperty
178
					.addValueChangeListener(masterElement, detailListener);
179
			masterElementToDetailListener.put(masterElement, detailListener);
180
		}
181
182
		sourceToMasterElementToDetailListener.put(source,
183
				masterElementToDetailListener);
184
	}
185
186
	protected void removeListenerFrom(Object source) {
187
		masterProperty.removeListChangeListener(source, masterListener);
188
		Map masterElementToDetailListener = (Map) sourceToMasterElementToDetailListener
189
				.remove(source);
190
		if (masterElementToDetailListener != null) {
191
			for (Iterator it = masterElementToDetailListener.entrySet()
192
					.iterator(); it.hasNext();) {
193
				Map.Entry entry = (Map.Entry) it.next();
194
				detailProperty.removeValueChangeListener(entry.getKey(),
195
						(DetailPropertyListener) entry.getValue());
196
			}
197
		}
198
	}
199
200
	public List getList(Object source) {
201
		List result = new ArrayList();
202
		for (Iterator it = masterProperty.getList(source).iterator(); it
203
				.hasNext();) {
204
			result.add(detailProperty.getValue(it.next()));
205
		}
206
		return result;
207
	}
208
209
	public boolean add(Object source, Object o) {
210
		throw new UnsupportedOperationException();
211
	}
212
213
	public boolean addAll(Object source, Collection c) {
214
		throw new UnsupportedOperationException();
215
	}
216
217
	public void clear(Object source) {
218
		throw new UnsupportedOperationException();
219
	}
220
221
	public Object getElementType() {
222
		return detailProperty.getValueType();
223
	}
224
225
	public boolean remove(Object source, Object o) {
226
		throw new UnsupportedOperationException();
227
	}
228
229
	public boolean removeAll(Object source, Collection c) {
230
		throw new UnsupportedOperationException();
231
	}
232
233
	public boolean retainAll(Object source, Collection c) {
234
		throw new UnsupportedOperationException();
235
	}
236
237
	public void add(Object source, int index, Object element) {
238
		throw new UnsupportedOperationException();
239
	}
240
241
	public boolean addAll(Object source, int index, Collection c) {
242
		throw new UnsupportedOperationException();
243
	}
244
245
	public Object move(Object source, int oldIndex, int newIndex) {
246
		throw new UnsupportedOperationException();
247
	}
248
249
	public Object remove(Object source, int index) {
250
		throw new UnsupportedOperationException();
251
	}
252
253
	public Object set(Object source, int index, Object element) {
254
		Object masterElement = masterProperty.get(source, index);
255
		Object result = detailProperty.getValue(masterElement);
256
		detailProperty.setValue(masterElement, element);
257
		return result;
258
	}
259
}
(-)src/org/eclipse/core/databinding/property/IPropertyObservable.java (+28 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;
13
14
import org.eclipse.core.databinding.observable.IObserving;
15
16
/**
17
 * Provides access to the details of property observables
18
 * 
19
 * @since 1.2
20
 */
21
public interface IPropertyObservable extends IObserving {
22
	/**
23
	 * Returns the property being observed
24
	 * 
25
	 * @return the property being observed
26
	 */
27
	IProperty getProperty();
28
}
(-)src/org/eclipse/core/internal/databinding/property/masterdetail/ValuePropertyDetailMap.java (+133 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.masterdetail;
13
14
import java.util.HashMap;
15
import java.util.Map;
16
17
import org.eclipse.core.databinding.observable.Diffs;
18
import org.eclipse.core.databinding.observable.map.MapDiff;
19
import org.eclipse.core.databinding.property.IMapProperty;
20
import org.eclipse.core.databinding.property.IMapPropertyChangeListener;
21
import org.eclipse.core.databinding.property.IValueProperty;
22
import org.eclipse.core.databinding.property.IValuePropertyChangeListener;
23
import org.eclipse.core.databinding.property.MapProperty;
24
import org.eclipse.core.databinding.property.MapPropertyChangeEvent;
25
import org.eclipse.core.databinding.property.ValuePropertyChangeEvent;
26
27
/**
28
 * @since 3.3
29
 * 
30
 */
31
public class ValuePropertyDetailMap extends MapProperty {
32
	private final IValueProperty masterProperty;
33
	private final IMapProperty detailProperty;
34
35
	private Map sourceToDetailListener = new HashMap();
36
37
	private IValuePropertyChangeListener masterListener = new MasterValueListener();
38
39
	private class MasterValueListener implements IValuePropertyChangeListener {
40
		public void handleValuePropertyChange(ValuePropertyChangeEvent event) {
41
			Object oldSource = event.diff.getOldValue();
42
			Object newSource = event.diff.getNewValue();
43
44
			Map oldMap = detailProperty.getMap(oldSource);
45
			Map newMap = detailProperty.getMap(newSource);
46
			MapDiff diff = Diffs.computeMapDiff(oldMap, newMap);
47
48
			Object source = event.getSource();
49
50
			removeDetailPropertyListener(source);
51
			addDetailPropertyListener(source);
52
53
			fireMapChange(source, diff);
54
		}
55
	}
56
57
	private class DetailPropertyListener implements IMapPropertyChangeListener {
58
		private Object source;
59
		private Object masterValue;
60
61
		DetailPropertyListener(Object source, Object masterValue) {
62
			this.source = source;
63
			this.masterValue = masterValue;
64
		}
65
66
		public void handleMapPropertyChange(MapPropertyChangeEvent event) {
67
			fireMapChange(source, event.diff);
68
		}
69
	}
70
71
	/**
72
	 * @param masterProperty
73
	 * @param detailProperty
74
	 */
75
	public ValuePropertyDetailMap(IValueProperty masterProperty,
76
			IMapProperty detailProperty) {
77
		this.masterProperty = masterProperty;
78
		this.detailProperty = detailProperty;
79
	}
80
81
	protected void addListenerTo(Object source) {
82
		masterProperty.addValueChangeListener(source, masterListener);
83
		addDetailPropertyListener(source);
84
	}
85
86
	protected void removeListenerFrom(Object source) {
87
		masterProperty.removeValueChangeListener(source, masterListener);
88
		removeDetailPropertyListener(source);
89
	}
90
91
	private void addDetailPropertyListener(Object source) {
92
		Object masterValue = masterProperty.getValue(source);
93
		DetailPropertyListener detailListener = new DetailPropertyListener(
94
				source, masterValue);
95
		detailProperty.addMapChangeListener(masterValue, detailListener);
96
		sourceToDetailListener.put(source, detailListener);
97
	}
98
99
	private void removeDetailPropertyListener(Object source) {
100
		DetailPropertyListener detailListener = (DetailPropertyListener) sourceToDetailListener
101
				.remove(source);
102
		if (detailListener != null) {
103
			detailProperty.removeMapChangeListener(detailListener.masterValue,
104
					detailListener);
105
		}
106
		sourceToDetailListener.remove(source);
107
	}
108
109
	public Map getMap(Object source) {
110
		Object masterValue = masterProperty.getValue(source);
111
		return detailProperty.getMap(masterValue);
112
	}
113
114
	public void clear(Object source) {
115
		Object masterValue = masterProperty.getValue(source);
116
		detailProperty.clear(masterValue);
117
	}
118
119
	public Object put(Object source, Object key, Object value) {
120
		Object masterValue = masterProperty.getValue(source);
121
		return detailProperty.put(masterValue, key, value);
122
	}
123
124
	public void putAll(Object source, Map t) {
125
		Object masterValue = masterProperty.getValue(source);
126
		detailProperty.putAll(masterValue, t);
127
	}
128
129
	public Object remove(Object source, Object key) {
130
		Object masterValue = masterProperty.getValue(source);
131
		return detailProperty.remove(masterValue, key);
132
	}
133
}
(-)src/org/eclipse/core/databinding/property/BasicSetProperty.java (+186 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;
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.set.SetDiff;
22
import org.eclipse.core.internal.databinding.property.PropertyUpdateHelper;
23
24
/**
25
 * Abstract set property implementation for properties where the set can be
26
 * completely replaced in a single atomic operation.
27
 * <p>
28
 * For example, a set-typed bean property Customer.addresses can be modified by
29
 * calling Customer.setAddresses(List addresses).
30
 * 
31
 * @since 1.2
32
 */
33
public abstract class BasicSetProperty extends SetProperty {
34
	private PropertyUpdateHelper updateHelper = new PropertyUpdateHelper();
35
36
	/**
37
	 * Returns whether this property is currently being updated on the source.
38
	 * Implementors should query this value to avoid unnecessary calculations,
39
	 * such as computing a diff.
40
	 * 
41
	 * @param source
42
	 *            the property source
43
	 * @return whether this property is currently being updated on the source.
44
	 */
45
	protected boolean isUpdating(Object source) {
46
		return updateHelper.isUpdating(source);
47
	}
48
49
	/**
50
	 * Sets the set property on the source to the specified set, then fires a
51
	 * set change using the specified diff.
52
	 * 
53
	 * @param source
54
	 *            the property source
55
	 * @param set
56
	 *            the new set
57
	 * @param diff
58
	 *            the diff to be fired after the set property has been set. If
59
	 *            null, the diff will be computed.
60
	 */
61
	protected void setSet(Object source, Set set, SetDiff diff) {
62
		if (diff == null) {
63
			diff = Diffs.computeSetDiff(getSet(source), set);
64
		}
65
66
		updateHelper.setUpdating(source, true);
67
		try {
68
			doSetSet(source, set);
69
		} finally {
70
			updateHelper.setUpdating(source, false);
71
		}
72
73
		if (hasListeners(source)) {
74
			fireSetChange(source, diff);
75
		}
76
	}
77
78
	protected void fireSetChange(Object source, SetDiff diff) {
79
		if (!isUpdating(source))
80
			super.fireSetChange(source, diff);
81
	}
82
83
	/**
84
	 * Sets the set property on the source to the specified set.
85
	 * 
86
	 * @param source
87
	 *            the property source
88
	 * @param set
89
	 *            the new set
90
	 */
91
	protected abstract void doSetSet(Object source, Set set);
92
93
	public boolean add(Object source, Object o) {
94
		Set set = getSet(source);
95
		if (!set.contains(o)) {
96
			set = new HashSet(set);
97
			boolean added = set.add(o);
98
			if (added)
99
				setSet(source, set, Diffs.createSetDiff(Collections
100
						.singleton(o), Collections.EMPTY_SET));
101
			return added;
102
		}
103
		return false;
104
	}
105
106
	public boolean addAll(Object source, Collection c) {
107
		Set set = getSet(source);
108
		Set additions = new HashSet();
109
		for (Iterator it = c.iterator(); it.hasNext();) {
110
			Object o = it.next();
111
			if (!set.contains(o)) {
112
				additions.add(o);
113
			}
114
		}
115
		boolean changed = !additions.isEmpty();
116
		if (changed) {
117
			set = new HashSet(set);
118
			set.addAll(additions);
119
			setSet(source, set, Diffs.createSetDiff(additions,
120
					Collections.EMPTY_SET));
121
		}
122
		return changed;
123
	}
124
125
	public void clear(Object source) {
126
		setSet(source, new HashSet(), Diffs.createSetDiff(
127
				Collections.EMPTY_SET, getSet(source)));
128
	}
129
130
	public boolean remove(Object source, Object o) {
131
		Set set = getSet(source);
132
		if (set.contains(o)) {
133
			set = new HashSet(set);
134
			boolean removed = set.remove(o);
135
			if (removed)
136
				setSet(source, set, Diffs.createSetDiff(Collections.EMPTY_SET,
137
						Collections.singleton(o)));
138
			return removed;
139
		}
140
		return false;
141
	}
142
143
	public boolean removeAll(Object source, Collection c) {
144
		Set set = new HashSet(getSet(source));
145
		Set removals = new HashSet();
146
		for (Iterator it = set.iterator(); it.hasNext();) {
147
			Object o = it.next();
148
			if (c.contains(o)) {
149
				removals.add(o);
150
				it.remove();
151
			}
152
		}
153
		boolean changed = !removals.isEmpty();
154
		if (changed) {
155
			setSet(source, set, Diffs.createSetDiff(Collections.EMPTY_SET,
156
					removals));
157
		}
158
		return changed;
159
	}
160
161
	public boolean retainAll(Object source, Collection c) {
162
		Set set = new HashSet(getSet(source));
163
		Set removals = new HashSet();
164
		for (Iterator it = set.iterator(); it.hasNext();) {
165
			Object o = it.next();
166
			if (!c.contains(o)) {
167
				removals.add(o);
168
				it.remove();
169
			}
170
		}
171
		boolean changed = !removals.isEmpty();
172
		if (changed) {
173
			setSet(source, set, Diffs.createSetDiff(Collections.EMPTY_SET,
174
					removals));
175
		}
176
		return changed;
177
	}
178
179
	public synchronized void dispose() {
180
		if (updateHelper != null) {
181
			updateHelper.dispose();
182
			updateHelper = null;
183
		}
184
		super.dispose();
185
	}
186
}
(-)src/org/eclipse/core/databinding/property/ISetPropertyChangeListener.java (+27 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
/**
15
 * Listener for changes to set properties on a property source
16
 * 
17
 * @since 1.2
18
 */
19
public interface ISetPropertyChangeListener extends IPropertyChangeListener {
20
	/**
21
	 * Handle a change to a set property on a specific property source.
22
	 * 
23
	 * @param event
24
	 *            an event describing the set change that occured.
25
	 */
26
	public void handleSetPropertyChange(SetPropertyChangeEvent event);
27
}
(-)src/org/eclipse/core/databinding/property/MapPropertyChangeEvent.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.core.databinding.property;
13
14
import org.eclipse.core.databinding.observable.map.MapDiff;
15
import org.eclipse.core.runtime.Assert;
16
17
/**
18
 * Map change event describing an incremental change of a map property on a
19
 * particular property source.
20
 * 
21
 * @since 1.2
22
 */
23
public class MapPropertyChangeEvent extends PropertyChangeEvent {
24
	private static final long serialVersionUID = 1L;
25
26
	/**
27
	 * The map property that changed
28
	 */
29
	public final IMapProperty property;
30
31
	/**
32
	 * MapDiff enumerating the added, changed, and removed entries in the map.
33
	 */
34
	public final MapDiff diff;
35
36
	/**
37
	 * Constructs a MapPropertyChangeEvent with the given attributes
38
	 * 
39
	 * @param source
40
	 *            the property source
41
	 * @param property
42
	 *            the property that changed on the source
43
	 * @param diff
44
	 *            a MapDiff describing the changes to the map property
45
	 */
46
	public MapPropertyChangeEvent(Object source, IMapProperty property,
47
			MapDiff diff) {
48
		super(source);
49
		this.property = property;
50
		Assert.isNotNull(diff, "diff cannot be null"); //$NON-NLS-1$
51
		this.diff = diff;
52
	}
53
54
	void dispatch(IPropertyChangeListener listener) {
55
		((IMapPropertyChangeListener) listener).handleMapPropertyChange(this);
56
	}
57
}
(-)src/org/eclipse/core/databinding/property/PropertyChangeEvent.java (+24 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
abstract class PropertyChangeEvent extends EventObject {
17
	private static final long serialVersionUID = 1L;
18
19
	PropertyChangeEvent(Object source) {
20
		super(source);
21
	}
22
23
	abstract void dispatch(IPropertyChangeListener listener);
24
}
(-)src/org/eclipse/core/internal/databinding/property/masterdetail/MapPropertyDetailValueMap.java (+223 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.masterdetail;
13
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.Iterator;
17
import java.util.Map;
18
import java.util.Set;
19
20
import org.eclipse.core.databinding.observable.Diffs;
21
import org.eclipse.core.databinding.observable.map.MapDiff;
22
import org.eclipse.core.databinding.property.IMapProperty;
23
import org.eclipse.core.databinding.property.IMapPropertyChangeListener;
24
import org.eclipse.core.databinding.property.IValueProperty;
25
import org.eclipse.core.databinding.property.IValuePropertyChangeListener;
26
import org.eclipse.core.databinding.property.MapProperty;
27
import org.eclipse.core.databinding.property.MapPropertyChangeEvent;
28
import org.eclipse.core.databinding.property.ValuePropertyChangeEvent;
29
import org.eclipse.core.internal.databinding.Util;
30
31
/**
32
 * @since 3.3
33
 * 
34
 */
35
public class MapPropertyDetailValueMap extends MapProperty {
36
	private final IMapProperty masterProperty;
37
	private final IValueProperty detailProperty;
38
39
	private Map sourceToKeyToDetailListener = new HashMap();
40
41
	private IMapPropertyChangeListener masterListener = new MasterPropertyListener();
42
43
	private class MasterPropertyListener implements IMapPropertyChangeListener {
44
		public void handleMapPropertyChange(final MapPropertyChangeEvent event) {
45
			Object source = event.getSource();
46
47
			Map oldValues = new HashMap();
48
			Map newValues = new HashMap();
49
50
			Set addedKeys = event.diff.getAddedKeys();
51
			for (Iterator it = addedKeys.iterator(); it.hasNext();) {
52
				Object key = it.next();
53
				Object newMasterValue = event.diff.getNewValue(key);
54
				Object newDetailValue = detailProperty.getValue(newMasterValue);
55
				newValues.put(key, newDetailValue);
56
				addPropertySourceListener(source, key, newMasterValue);
57
			}
58
59
			Set removedKeys = event.diff.getRemovedKeys();
60
			for (Iterator it = removedKeys.iterator(); it.hasNext();) {
61
				Object key = it.next();
62
				Object oldMasterValue = event.diff.getOldValue(key);
63
				Object oldDetailValue = detailProperty.getValue(oldMasterValue);
64
				oldValues.put(key, oldDetailValue);
65
				removePropertySourceListener(source, key, oldMasterValue);
66
			}
67
68
			Set changedKeys = new HashSet(event.diff.getChangedKeys());
69
			for (Iterator it = changedKeys.iterator(); it.hasNext();) {
70
				Object key = it.next();
71
72
				Object oldMasterValue = event.diff.getOldValue(key);
73
				Object newMasterValue = event.diff.getNewValue(key);
74
75
				Object oldDetailValue = detailProperty.getValue(oldMasterValue);
76
				Object newDetailValue = detailProperty.getValue(newMasterValue);
77
78
				if (Util.equals(oldDetailValue, newDetailValue)) {
79
					it.remove();
80
				} else {
81
					oldValues.put(key, oldDetailValue);
82
					newValues.put(key, newDetailValue);
83
				}
84
85
				removePropertySourceListener(source, key, oldMasterValue);
86
				addPropertySourceListener(source, key, newMasterValue);
87
			}
88
89
			MapDiff diff = Diffs.createMapDiff(addedKeys, removedKeys,
90
					changedKeys, oldValues, newValues);
91
92
			fireMapChange(source, diff);
93
		}
94
95
		private void addPropertySourceListener(Object source, Object key,
96
				Object masterValue) {
97
			Map keyToDetailListener = (Map) sourceToKeyToDetailListener
98
					.get(source);
99
			if (keyToDetailListener == null) {
100
				sourceToKeyToDetailListener.put(source,
101
						keyToDetailListener = new HashMap());
102
			}
103
			if (!keyToDetailListener.containsKey(key)) {
104
				DetailPropertyListener detailListener = new DetailPropertyListener(
105
						source, key);
106
				detailProperty.addValueChangeListener(masterValue,
107
						detailListener);
108
				keyToDetailListener.put(key, detailListener);
109
			}
110
		}
111
112
		private void removePropertySourceListener(Object source, Object key,
113
				Object masterValue) {
114
			Map keyToDetailListener = (Map) sourceToKeyToDetailListener
115
					.get(source);
116
			if (keyToDetailListener != null
117
					&& keyToDetailListener.containsKey(key)) {
118
				DetailPropertyListener detailListener = (DetailPropertyListener) keyToDetailListener
119
						.remove(key);
120
				detailProperty.removeValueChangeListener(masterValue,
121
						detailListener);
122
			}
123
		}
124
	}
125
126
	private class DetailPropertyListener implements
127
			IValuePropertyChangeListener {
128
		private Object source;
129
		private Object key;
130
131
		DetailPropertyListener(Object source, Object key) {
132
			this.source = source;
133
			this.key = key;
134
		}
135
136
		public void handleValuePropertyChange(ValuePropertyChangeEvent event) {
137
			fireMapChange(source, Diffs.createMapDiffSingleChange(key,
138
					event.diff.getOldValue(), event.diff.getNewValue()));
139
		}
140
	}
141
142
	/**
143
	 * @param masterProperty
144
	 * @param detailProperty
145
	 */
146
	public MapPropertyDetailValueMap(IMapProperty masterProperty,
147
			IValueProperty detailProperty) {
148
		this.masterProperty = masterProperty;
149
		this.detailProperty = detailProperty;
150
	}
151
152
	protected void addListenerTo(Object source) {
153
		masterProperty.addMapChangeListener(source, masterListener);
154
155
		Map keyToDetailListener = new HashMap();
156
		for (Iterator it = masterProperty.getMap(source).entrySet().iterator(); it
157
				.hasNext();) {
158
			Map.Entry entry = (Map.Entry) it.next();
159
			Object key = entry.getKey();
160
			Object masterValue = entry.getValue();
161
			DetailPropertyListener detailListener = new DetailPropertyListener(
162
					source, key);
163
			detailProperty.addValueChangeListener(masterValue, detailListener);
164
			keyToDetailListener.put(masterValue, detailListener);
165
		}
166
167
		sourceToKeyToDetailListener.put(source, keyToDetailListener);
168
	}
169
170
	protected void removeListenerFrom(Object source) {
171
		masterProperty.removeMapChangeListener(source, masterListener);
172
		Map masterElementToDetailListener = (Map) sourceToKeyToDetailListener
173
				.remove(source);
174
		if (masterElementToDetailListener != null) {
175
			for (Iterator it = masterElementToDetailListener.entrySet()
176
					.iterator(); it.hasNext();) {
177
				Map.Entry entry = (Map.Entry) it.next();
178
				detailProperty.removeValueChangeListener(entry.getKey(),
179
						(DetailPropertyListener) entry.getValue());
180
			}
181
		}
182
	}
183
184
	public Map getMap(Object source) {
185
		Map result = new HashMap();
186
		for (Iterator it = masterProperty.getMap(source).entrySet().iterator(); it
187
				.hasNext();) {
188
			Map.Entry entry = (Map.Entry) it.next();
189
			result.put(entry.getKey(), detailProperty
190
					.getValue(entry.getValue()));
191
		}
192
		return result;
193
	}
194
195
	public void clear(Object source) {
196
		throw new UnsupportedOperationException();
197
	}
198
199
	public Object put(Object source, Object key, Object value) {
200
		if (!masterProperty.containsKey(source, key))
201
			return null;
202
		Object masterValue = masterProperty.get(source, key);
203
204
		Object result = detailProperty.getValue(masterValue);
205
		detailProperty.setValue(masterValue, value);
206
		return result;
207
	}
208
209
	public void putAll(Object source, Map t) {
210
		for (Iterator it = t.entrySet().iterator(); it.hasNext();) {
211
			Map.Entry entry = (Map.Entry) it.next();
212
			Object masterKey = entry.getKey();
213
			Object detailValue = entry.getValue();
214
			if (masterProperty.getMap(source).containsKey(masterKey)) {
215
				detailProperty.setValue(masterKey, detailValue);
216
			}
217
		}
218
	}
219
220
	public Object remove(Object source, Object key) {
221
		throw new UnsupportedOperationException();
222
	}
223
}
(-)src/org/eclipse/core/databinding/property/ICollectionProperty.java (+185 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.Collection;
15
16
/**
17
 * Interface for collection-typed properties.
18
 * 
19
 * @since 1.2
20
 */
21
public interface ICollectionProperty extends IProperty {
22
	/**
23
	 * Returns the size of the source's collection property
24
	 * 
25
	 * @param source
26
	 *            the property source
27
	 * @return the size of the source's collection property
28
	 */
29
	int size(Object source);
30
31
	/**
32
	 * Returns whether the source's collection property is empty
33
	 * 
34
	 * @param source
35
	 *            the property source
36
	 * @return whether the source's collection property is empty
37
	 */
38
	boolean isEmpty(Object source);
39
40
	/**
41
	 * Returns whether the source's collection property contains the given
42
	 * element.
43
	 * 
44
	 * @param source
45
	 *            the property source
46
	 * @param o
47
	 *            the element
48
	 * @return whether the source's collection property contains the given
49
	 *         element.
50
	 */
51
	boolean contains(Object source, Object o);
52
53
	/**
54
	 * Returns an array of all elements in the source's collection property
55
	 * 
56
	 * @param source
57
	 *            the property source
58
	 * @return an array of all elements in the source's collection property
59
	 */
60
	Object[] toArray(Object source);
61
62
	/**
63
	 * Returns an array of all elements in the source's collection property
64
	 * 
65
	 * @param source
66
	 *            the property source
67
	 * @param array
68
	 *            the array into which the elements will be copied. If the array
69
	 *            is not large enough to hold all elements, the elements will be
70
	 *            returned in a new array of the same runtime type.
71
	 * @return an array of all elements in the source's collection property
72
	 */
73
	Object[] toArray(Object source, Object[] array);
74
75
	/**
76
	 * Adds the element to the source's collection property
77
	 * 
78
	 * @param source
79
	 *            the property source
80
	 * @param o
81
	 *            the element to add
82
	 * @return whether the element was added to the source's collection property
83
	 */
84
	boolean add(Object source, Object o);
85
86
	/**
87
	 * Removes the element from the source's collection property
88
	 * 
89
	 * @param source
90
	 *            the property source
91
	 * @param o
92
	 *            the element to remove
93
	 * @return whether the element was removed from the source's collection
94
	 *         property
95
	 */
96
	boolean remove(Object source, Object o);
97
98
	/**
99
	 * Returns whether the source's collection property contains all elements in
100
	 * the given collection
101
	 * 
102
	 * @param source
103
	 *            the property source
104
	 * @param c
105
	 *            the collection of elements to test for
106
	 * @return whether the source's collection property contains all elements in
107
	 *         the given collection
108
	 */
109
	boolean containsAll(Object source, Collection c);
110
111
	/**
112
	 * Adds all elements in the specified collection to the source's collection
113
	 * property.
114
	 * 
115
	 * @param source
116
	 *            the property source
117
	 * @param c
118
	 *            the collection of elements to add.
119
	 * @return whether the source's collection property was changed
120
	 */
121
	boolean addAll(Object source, Collection c);
122
123
	/**
124
	 * Removes all elements from the source's collection property which are
125
	 * contained in the specified collection.
126
	 * 
127
	 * @param source
128
	 *            the property source
129
	 * @param c
130
	 *            the collection of elements to be removed
131
	 * @return whether the source's collection property was changed
132
	 */
133
	boolean removeAll(Object source, Collection c);
134
135
	/**
136
	 * Removes all elements from the source's collection property which are not
137
	 * contained in the specified collection.
138
	 * 
139
	 * @param source
140
	 *            the property source
141
	 * @param c
142
	 *            the collection of elements to retain
143
	 * @return whether the source's collection property was changed
144
	 */
145
	boolean retainAll(Object source, Collection c);
146
147
	/**
148
	 * Removes all elements from the source's collection property.
149
	 * 
150
	 * @param source
151
	 *            the property source
152
	 */
153
	void clear(Object source);
154
155
	/**
156
	 * Returns whether the source's collection property is equal to the
157
	 * argument.
158
	 * 
159
	 * @param source
160
	 *            the property source
161
	 * @param o
162
	 *            the object to test for equality to the source's collection
163
	 *            property
164
	 * @return whether the source's collection property is equal to the argument
165
	 */
166
	boolean equals(Object source, Object o);
167
168
	/**
169
	 * Returns the hash code of the source's collection property.
170
	 * 
171
	 * @param source
172
	 *            the property source
173
	 * @return the hash code of the source's collection property
174
	 */
175
	int hashCode(Object source);
176
177
	/**
178
	 * Returns the type of the elements in the collection or <code>null</code>
179
	 * if untyped
180
	 * 
181
	 * @return the type of the elements in the collection or <code>null</code>
182
	 *         if untyped
183
	 */
184
	Object getElementType();
185
}
(-)src/org/eclipse/core/databinding/property/ListPropertyChangeEvent.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.core.databinding.property;
13
14
import org.eclipse.core.databinding.observable.list.ListDiff;
15
import org.eclipse.core.runtime.Assert;
16
17
/**
18
 * List change event describing an incremental change of a list property on a
19
 * particular property source.
20
 * 
21
 * @since 1.2
22
 */
23
public class ListPropertyChangeEvent extends PropertyChangeEvent {
24
	private static final long serialVersionUID = 1L;
25
26
	/**
27
	 * The list property that changed
28
	 */
29
	public final IListProperty property;
30
31
	/**
32
	 * ListDiff enumerating the added and removed elements in the list.
33
	 */
34
	public final ListDiff diff;
35
36
	/**
37
	 * Constructs a ListPropertyChangeEvent with the given attributes
38
	 * 
39
	 * @param source
40
	 *            the property source
41
	 * @param property
42
	 *            the property that changed on the source
43
	 * @param diff
44
	 *            a ListDiff describing the changes to the list property
45
	 */
46
	public ListPropertyChangeEvent(Object source, IListProperty property,
47
			ListDiff diff) {
48
		super(source);
49
		this.property = property;
50
		Assert.isNotNull(diff, "diff cannot be null"); //$NON-NLS-1$
51
		this.diff = diff;
52
	}
53
54
	void dispatch(IPropertyChangeListener listener) {
55
		((IListPropertyChangeListener) listener).handleListPropertyChange(this);
56
	}
57
}
(-)src/org/eclipse/core/databinding/property/PropertyChangeSupport.java (+118 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.Collections;
15
import java.util.IdentityHashMap;
16
import java.util.Iterator;
17
import java.util.Map;
18
19
import org.eclipse.core.runtime.ListenerList;
20
21
class PropertyChangeSupport {
22
	private Property property;
23
	private Map changeListeners;
24
25
	PropertyChangeSupport(Property property) {
26
		this.property = property;
27
		this.changeListeners = null;
28
	}
29
30
	protected final void addListener(Object source,
31
			IPropertyChangeListener listener) {
32
		boolean hadListeners = hasListeners(source);
33
34
		if (changeListeners == null) {
35
			synchronized (this) {
36
				if (changeListeners == null) {
37
					changeListeners = Collections
38
							.synchronizedMap(new IdentityHashMap());
39
				}
40
			}
41
		}
42
43
		ListenerList listeners;
44
		synchronized (changeListeners) {
45
			if (changeListeners.containsKey(source)) {
46
				listeners = (ListenerList) changeListeners.get(source);
47
			} else {
48
				changeListeners.put(source, listeners = new ListenerList());
49
			}
50
		}
51
52
		synchronized (listeners) {
53
			listeners.add(listener);
54
		}
55
56
		if (!hadListeners)
57
			property.addListenerTo(source);
58
	}
59
60
	/**
61
	 * Returns whether the change support has listeners registered for the given
62
	 * property source
63
	 * 
64
	 * @param source
65
	 *            the property source
66
	 * @return whether the change support has listeners registered for the given
67
	 *         property source
68
	 */
69
	public boolean hasListeners(Object source) {
70
		return changeListeners != null && changeListeners.containsKey(source);
71
	}
72
73
	protected final void removeListener(Object source,
74
			IPropertyChangeListener listener) {
75
		if (changeListeners != null) {
76
			ListenerList listeners = (ListenerList) changeListeners.get(source);
77
			if (listeners != null) {
78
				listeners.remove(listener);
79
				if (listeners.isEmpty()) {
80
					synchronized (listeners) {
81
						if (listeners.isEmpty()) {
82
							changeListeners.remove(source);
83
							property.removeListenerFrom(source);
84
						}
85
					}
86
				}
87
			}
88
		}
89
	}
90
91
	protected final void firePropertyChange(PropertyChangeEvent event) {
92
		ListenerList listenerList;
93
		synchronized (this) {
94
			if (changeListeners == null)
95
				return;
96
			listenerList = (ListenerList) changeListeners
97
					.get(event.getSource());
98
		}
99
		if (listenerList != null) {
100
			Object[] listeners = listenerList.getListeners();
101
			for (int i = 0; i < listeners.length; i++) {
102
				event.dispatch((IPropertyChangeListener) listeners[i]);
103
			}
104
		}
105
	}
106
107
	void dispose() {
108
		if (changeListeners != null) {
109
			for (Iterator iterator = changeListeners.keySet().iterator(); iterator
110
					.hasNext();) {
111
				Object source = iterator.next();
112
				property.removeListenerFrom(source);
113
				iterator.remove();
114
			}
115
			changeListeners = null;
116
		}
117
	}
118
}
(-)src/org/eclipse/core/databinding/property/PropertyObservables.java (+371 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 org.eclipse.core.databinding.observable.IObservable;
15
import org.eclipse.core.databinding.observable.Realm;
16
import org.eclipse.core.databinding.observable.list.IObservableList;
17
import org.eclipse.core.databinding.observable.map.IObservableMap;
18
import org.eclipse.core.databinding.observable.masterdetail.IObservableFactory;
19
import org.eclipse.core.databinding.observable.masterdetail.MasterDetailObservables;
20
import org.eclipse.core.databinding.observable.set.IObservableSet;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.core.internal.databinding.property.ListValuePropertyObservableList;
23
import org.eclipse.core.internal.databinding.property.MapValuePropertyObservableMap;
24
import org.eclipse.core.internal.databinding.property.PropertyObservableList;
25
import org.eclipse.core.internal.databinding.property.PropertyObservableMap;
26
import org.eclipse.core.internal.databinding.property.PropertyObservableSet;
27
import org.eclipse.core.internal.databinding.property.PropertyObservableValue;
28
import org.eclipse.core.internal.databinding.property.SetValuePropertyObservableMap;
29
30
/**
31
 * A factory for creating observables observing properties of source objects
32
 * 
33
 * @since 1.2
34
 */
35
public class PropertyObservables {
36
	/**
37
	 * Returns an observable value on the default realm that tracks the given
38
	 * property of the source object.
39
	 * 
40
	 * @param source
41
	 *            the property value
42
	 * @param property
43
	 *            the value property to observe
44
	 * @return an observable value on the default realm that tracks the given
45
	 *         property of the source object.
46
	 * @since 1.2
47
	 */
48
	public static IObservableValue observeValue(Object source,
49
			IValueProperty property) {
50
		return observeValue(Realm.getDefault(), source, property);
51
	}
52
53
	/**
54
	 * Returns an observable value on the given realm that tracks the given
55
	 * property of the source object
56
	 * 
57
	 * @param realm
58
	 *            the realm
59
	 * @param source
60
	 *            the property source
61
	 * @param property
62
	 *            the value property observe
63
	 * @return an observable value that tracks the given property of the source
64
	 *         object
65
	 * @since 1.2
66
	 */
67
	public static IObservableValue observeValue(Realm realm, Object source,
68
			IValueProperty property) {
69
		return new PropertyObservableValue(realm, source, property);
70
	}
71
72
	/**
73
	 * Returns a factory for creating observable values on the default realm
74
	 * tracking the given property of a particular source object
75
	 * 
76
	 * @param property
77
	 *            the property to observe
78
	 * @return a factory for creating observable values on the default realm
79
	 *         tracking the given property of a particular source object
80
	 */
81
	public static IObservableFactory valueFactory(IValueProperty property) {
82
		return valueFactory(Realm.getDefault(), property);
83
	}
84
85
	/**
86
	 * Returns a factory for creating observable values on the given realm
87
	 * tracking the given property of a particular source object
88
	 * 
89
	 * @param realm
90
	 *            the realm
91
	 * @param property
92
	 *            the property to observe
93
	 * @return a factory for creating observable values on the given realm
94
	 *         tracking the given property of a particular source object
95
	 */
96
	public static IObservableFactory valueFactory(final Realm realm,
97
			final IValueProperty property) {
98
		return new IObservableFactory() {
99
			public IObservable createObservable(Object target) {
100
				return observeValue(realm, target, property);
101
			}
102
		};
103
	}
104
105
	/**
106
	 * Returns an observable value on the master observable's realm which tracks
107
	 * the given property of the current value of <code>master</code>.
108
	 * 
109
	 * @param master
110
	 *            the master observable
111
	 * @param property
112
	 *            the property to observe
113
	 * @return an observable value which tracks the given property of the
114
	 *         current value of <code>master</code>.
115
	 */
116
	public static IObservableValue observeDetailValue(IObservableValue master,
117
			IValueProperty property) {
118
		return MasterDetailObservables.detailValue(master, valueFactory(master
119
				.getRealm(), property), property.getValueType());
120
	}
121
122
	/**
123
	 * Returns an observable set on the default realm that tracks the given
124
	 * property of the source object
125
	 * 
126
	 * @param source
127
	 *            the property source
128
	 * @param property
129
	 *            the property to observe
130
	 * @return an observable set on the default realm that tracks the given
131
	 *         property of the source object
132
	 */
133
	public static IObservableSet observeSet(Object source, ISetProperty property) {
134
		return observeSet(Realm.getDefault(), source, property);
135
	}
136
137
	/**
138
	 * Returns an observable set on the given realm that tracks the given
139
	 * property of the source object
140
	 * 
141
	 * @param realm
142
	 *            the realm
143
	 * @param source
144
	 *            the property source
145
	 * @param property
146
	 *            the property to observe
147
	 * @return an observable set on the given realm that tracks the given
148
	 *         property of the source object
149
	 */
150
	public static IObservableSet observeSet(Realm realm, Object source,
151
			ISetProperty property) {
152
		return new PropertyObservableSet(realm, source, property);
153
	}
154
155
	/**
156
	 * Returns a factory for creating observable sets on the default realm
157
	 * tracking the given property of a particular source object
158
	 * 
159
	 * @param property
160
	 *            the property to be observed
161
	 * @return a factory for creating observable sets on the default realm
162
	 *         tracking the given property of a particular source object
163
	 */
164
	public static IObservableFactory setFactory(ISetProperty property) {
165
		return setFactory(Realm.getDefault(), property);
166
	}
167
168
	/**
169
	 * Returns a factory for creating obervable sets on the given realm tracking
170
	 * the given property of a particular source object
171
	 * 
172
	 * @param realm
173
	 *            the realm
174
	 * @param property
175
	 *            the property to be observed
176
	 * @return a factory for creating obervable sets on the given realm tracking
177
	 *         the given property of a particular source object
178
	 */
179
	public static IObservableFactory setFactory(final Realm realm,
180
			final ISetProperty property) {
181
		return new IObservableFactory() {
182
			public IObservable createObservable(Object target) {
183
				return observeSet(realm, target, property);
184
			}
185
		};
186
	}
187
188
	/**
189
	 * Returns an observable set on the master observable's realm which tracks
190
	 * the given property of the current value of <code>master</code>.
191
	 * 
192
	 * @param master
193
	 *            the master observable
194
	 * @param property
195
	 *            the property to observe
196
	 * @return an observable set on the given realm which tracks the given
197
	 *         property of the current value of <code>master</code>.
198
	 */
199
	public static IObservableSet observeDetailSet(IObservableValue master,
200
			ISetProperty property) {
201
		return MasterDetailObservables.detailSet(master, setFactory(master
202
				.getRealm(), property), property.getElementType());
203
	}
204
205
	/**
206
	 * Returns an observable list on the default realm that tracks the given
207
	 * property of the source object
208
	 * 
209
	 * @param source
210
	 *            the property source
211
	 * @param property
212
	 *            the property to observe
213
	 * @return an observable list on the default realm that tracks the given
214
	 *         property of the source object
215
	 */
216
	public static IObservableList observeList(Object source,
217
			IListProperty property) {
218
		return observeList(Realm.getDefault(), source, property);
219
	}
220
221
	/**
222
	 * Returns an observable list on the given realm that tracks the given
223
	 * property of the source object
224
	 * 
225
	 * @param realm
226
	 *            the realm
227
	 * @param source
228
	 *            the property source
229
	 * @param property
230
	 *            the property to observe
231
	 * @return an observable list on the given realm that tracks the given
232
	 *         property of the source object
233
	 */
234
	public static IObservableList observeList(Realm realm, Object source,
235
			IListProperty property) {
236
		return new PropertyObservableList(realm, source, property);
237
	}
238
239
	/**
240
	 * Returns a factory for creating observable lists on the default realm
241
	 * tracking the given property of a particular source object
242
	 * 
243
	 * @param property
244
	 *            the property to be observed
245
	 * @return an observable value factory on
246
	 */
247
	public static IObservableFactory listFactory(IListProperty property) {
248
		return listFactory(Realm.getDefault(), property);
249
	}
250
251
	/**
252
	 * Returns a factory for creating obervable lists on the given realm
253
	 * tracking the given property of a particular source object
254
	 * 
255
	 * @param realm
256
	 *            the realm
257
	 * @param property
258
	 *            the property to be observed
259
	 * @return an observable value factory on
260
	 */
261
	public static IObservableFactory listFactory(final Realm realm,
262
			final IListProperty property) {
263
		return new IObservableFactory() {
264
			public IObservable createObservable(Object target) {
265
				return observeList(realm, target, property);
266
			}
267
		};
268
	}
269
270
	/**
271
	 * Returns an observable list on the master observable's realm which tracks
272
	 * the given property of the current value of <code>master</code>.
273
	 * 
274
	 * @param master
275
	 *            the master observable
276
	 * @param property
277
	 *            the property to observe
278
	 * @return an observable list on the given realm which tracks the given
279
	 *         property of the current value of <code>master</code>.
280
	 */
281
	public static IObservableList observeDetailList(IObservableValue master,
282
			IListProperty property) {
283
		return MasterDetailObservables.detailList(master, listFactory(master
284
				.getRealm(), property), property.getElementType());
285
	}
286
287
	/**
288
	 * Returns an observable list on the master observable's realm which tracks
289
	 * the given property of each element of <code>master</code>.
290
	 * 
291
	 * @param master
292
	 *            the master observable
293
	 * @param property
294
	 *            the property to observe on each element in the master
295
	 *            observable
296
	 * @return an observable
297
	 */
298
	public static IObservableList observeDetailValues(IObservableList master,
299
			IValueProperty property) {
300
		return new ListValuePropertyObservableList(master, property);
301
	}
302
303
	/**
304
	 * Returns an observable map on the default realm which tracks the given
305
	 * property of the source object
306
	 * 
307
	 * @param source
308
	 *            the property source
309
	 * @param property
310
	 *            the property to observe
311
	 * @return an observable map on the default realm which tracks the given
312
	 *         property of the source object
313
	 */
314
	public static IObservableMap observeMap(Object source, IMapProperty property) {
315
		return observeMap(Realm.getDefault(), source, property);
316
	}
317
318
	/**
319
	 * Returns an observable map on the given realm which tracks the given
320
	 * property of the source object
321
	 * 
322
	 * @param realm
323
	 *            the realm
324
	 * @param source
325
	 *            the property source
326
	 * @param property
327
	 *            the property to observe
328
	 * @return an observable map on the given realm which tracks the given
329
	 *         property of the source object
330
	 */
331
	public static IObservableMap observeMap(Realm realm, Object source,
332
			IMapProperty property) {
333
		return new PropertyObservableMap(realm, source, property);
334
	}
335
336
	/**
337
	 * Returns an observable map on the master observable's realm tracking the
338
	 * current values of the given property for the elements in the given set.
339
	 * 
340
	 * @param keySet
341
	 *            the master observable
342
	 * @param valueProperty
343
	 *            the property to observe on each element of the master
344
	 *            observable
345
	 * @return an observable map that tracks the current value of the given
346
	 *         property for the elements in the given set.
347
	 */
348
	public static IObservableMap observeDetailValues(IObservableSet keySet,
349
			IValueProperty valueProperty) {
350
		return new SetValuePropertyObservableMap(keySet, valueProperty);
351
	}
352
353
	/**
354
	 * Returns an observable map on the master observable's realm which tracks
355
	 * the current values of the given property for the entry values in the
356
	 * given map.
357
	 * 
358
	 * @param map
359
	 *            the master observable
360
	 * @param valueProperty
361
	 *            the property to observe on each entry value in the master
362
	 *            observable
363
	 * @return an observable map on the master observable's realm which tracks
364
	 *         the current value of the given property for the elements in the
365
	 *         given map's values collection
366
	 */
367
	public static IObservableMap observeDetailValues(IObservableMap map,
368
			IValueProperty valueProperty) {
369
		return new MapValuePropertyObservableMap(map, valueProperty);
370
	}
371
}
(-)src/org/eclipse/core/internal/databinding/property/masterdetail/ValuePropertyDetailSet.java (+150 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.masterdetail;
13
14
import java.util.Collection;
15
import java.util.HashMap;
16
import java.util.Map;
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.ISetProperty;
22
import org.eclipse.core.databinding.property.ISetPropertyChangeListener;
23
import org.eclipse.core.databinding.property.IValueProperty;
24
import org.eclipse.core.databinding.property.IValuePropertyChangeListener;
25
import org.eclipse.core.databinding.property.SetProperty;
26
import org.eclipse.core.databinding.property.SetPropertyChangeEvent;
27
import org.eclipse.core.databinding.property.ValuePropertyChangeEvent;
28
29
/**
30
 * @since 3.3
31
 * 
32
 */
33
public class ValuePropertyDetailSet extends SetProperty {
34
	private final IValueProperty masterProperty;
35
	private final ISetProperty detailProperty;
36
37
	private Map sourceToDetailListener = new HashMap();
38
39
	private IValuePropertyChangeListener masterListener = new MasterPropertyListener();
40
41
	private class MasterPropertyListener implements
42
			IValuePropertyChangeListener {
43
		public void handleValuePropertyChange(ValuePropertyChangeEvent event) {
44
			Object oldSource = event.diff.getOldValue();
45
			Object newSource = event.diff.getNewValue();
46
47
			Set oldSet = detailProperty.getSet(oldSource);
48
			Set newSet = detailProperty.getSet(newSource);
49
			SetDiff diff = Diffs.computeSetDiff(oldSet, newSet);
50
51
			Object source = event.getSource();
52
53
			removeDetailPropertyListener(source);
54
			addDetailPropertyListener(source);
55
56
			fireSetChange(source, diff);
57
		}
58
	}
59
60
	private class DetailPropertyListener implements ISetPropertyChangeListener {
61
		private Object source;
62
		private Object masterValue;
63
64
		DetailPropertyListener(Object source, Object masterValue) {
65
			this.source = source;
66
			this.masterValue = masterValue;
67
		}
68
69
		public void handleSetPropertyChange(SetPropertyChangeEvent event) {
70
			fireSetChange(source, event.diff);
71
		}
72
	}
73
74
	/**
75
	 * @param masterProperty
76
	 * @param detailProperty
77
	 */
78
	public ValuePropertyDetailSet(IValueProperty masterProperty,
79
			ISetProperty detailProperty) {
80
		this.masterProperty = masterProperty;
81
		this.detailProperty = detailProperty;
82
	}
83
84
	protected void addListenerTo(Object source) {
85
		masterProperty.addValueChangeListener(source, masterListener);
86
		addDetailPropertyListener(source);
87
	}
88
89
	protected void removeListenerFrom(Object source) {
90
		masterProperty.removeValueChangeListener(source, masterListener);
91
		removeDetailPropertyListener(source);
92
	}
93
94
	private void addDetailPropertyListener(Object source) {
95
		Object masterValue = masterProperty.getValue(source);
96
		DetailPropertyListener detailListener = new DetailPropertyListener(
97
				source, masterValue);
98
		detailProperty.addSetChangeListener(masterValue, detailListener);
99
		sourceToDetailListener.put(source, detailListener);
100
	}
101
102
	private void removeDetailPropertyListener(Object source) {
103
		DetailPropertyListener detailListener = (DetailPropertyListener) sourceToDetailListener
104
				.remove(source);
105
		if (detailListener != null) {
106
			detailProperty.removeSetChangeListener(detailListener.masterValue,
107
					detailListener);
108
		}
109
		sourceToDetailListener.remove(source);
110
	}
111
112
	public Set getSet(Object source) {
113
		Object masterValue = masterProperty.getValue(source);
114
		return detailProperty.getSet(masterValue);
115
	}
116
117
	public boolean add(Object source, Object o) {
118
		Object masterValue = masterProperty.getValue(source);
119
		return detailProperty.add(masterValue, o);
120
	}
121
122
	public boolean addAll(Object source, Collection c) {
123
		Object masterValue = masterProperty.getValue(source);
124
		return detailProperty.addAll(masterValue, c);
125
	}
126
127
	public void clear(Object source) {
128
		Object masterValue = masterProperty.getValue(source);
129
		detailProperty.clear(masterValue);
130
	}
131
132
	public Object getElementType() {
133
		return detailProperty.getElementType();
134
	}
135
136
	public boolean remove(Object source, Object o) {
137
		Object masterValue = masterProperty.getValue(source);
138
		return detailProperty.remove(masterValue, o);
139
	}
140
141
	public boolean removeAll(Object source, Collection c) {
142
		Object masterValue = masterProperty.getValue(source);
143
		return detailProperty.removeAll(masterValue, c);
144
	}
145
146
	public boolean retainAll(Object source, Collection c) {
147
		Object masterValue = masterProperty.getValue(source);
148
		return detailProperty.retainAll(masterValue, c);
149
	}
150
}
(-)src/org/eclipse/core/internal/databinding/property/PropertyObservableList.java (+374 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 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.ObservableTracker;
23
import org.eclipse.core.databinding.observable.Realm;
24
import org.eclipse.core.databinding.observable.list.AbstractObservableList;
25
import org.eclipse.core.databinding.property.IListProperty;
26
import org.eclipse.core.databinding.property.IListPropertyChangeListener;
27
import org.eclipse.core.databinding.property.IProperty;
28
import org.eclipse.core.databinding.property.IPropertyObservable;
29
import org.eclipse.core.databinding.property.ListPropertyChangeEvent;
30
31
/**
32
 * @since 3.3
33
 * 
34
 */
35
public class PropertyObservableList extends AbstractObservableList implements
36
		IPropertyObservable {
37
	private Object source;
38
	private IListProperty property;
39
40
	private volatile boolean updating = false;
41
42
	private boolean disposed = false;
43
44
	private transient volatile int modCount = 0;
45
46
	private IListPropertyChangeListener listener = new IListPropertyChangeListener() {
47
		public void handleListPropertyChange(final ListPropertyChangeEvent event) {
48
			if (!disposed && !updating) {
49
				getRealm().exec(new Runnable() {
50
					public void run() {
51
						getRealm().exec(new Runnable() {
52
							public void run() {
53
								modCount++;
54
								fireListChange(event.diff);
55
							}
56
						});
57
					}
58
				});
59
			}
60
		}
61
	};
62
63
	/**
64
	 * @param realm
65
	 * @param source
66
	 * @param property
67
	 */
68
	public PropertyObservableList(Realm realm, Object source,
69
			IListProperty property) {
70
		super(realm);
71
		this.source = source;
72
		this.property = property;
73
	}
74
75
	protected void firstListenerAdded() {
76
		if (!disposed) {
77
			property.addListChangeListener(source, listener);
78
		}
79
	}
80
81
	protected void lastListenerRemoved() {
82
		if (!disposed) {
83
			property.removeListChangeListener(source, listener);
84
		}
85
	}
86
87
	private void getterCalled() {
88
		ObservableTracker.getterCalled(this);
89
	}
90
91
	public Object getElementType() {
92
		return property.getElementType();
93
	}
94
95
	// Queries
96
97
	protected int doGetSize() {
98
		return property.size(source);
99
	}
100
101
	public boolean contains(Object o) {
102
		getterCalled();
103
		return property.contains(source, o);
104
	}
105
106
	public boolean containsAll(Collection c) {
107
		getterCalled();
108
		return property.containsAll(source, c);
109
	}
110
111
	public Object get(int index) {
112
		getterCalled();
113
		return property.get(source, index);
114
	}
115
116
	public int indexOf(Object o) {
117
		getterCalled();
118
		return property.indexOf(source, o);
119
	}
120
121
	public boolean isEmpty() {
122
		getterCalled();
123
		return property.isEmpty(source);
124
	}
125
126
	public int lastIndexOf(Object o) {
127
		getterCalled();
128
		return property.lastIndexOf(source, o);
129
	}
130
131
	public Object[] toArray() {
132
		getterCalled();
133
		return property.toArray(source);
134
	}
135
136
	public Object[] toArray(Object[] a) {
137
		getterCalled();
138
		return property.toArray(source, a);
139
	}
140
141
	// Single change operations
142
143
	public boolean add(Object o) {
144
		checkRealm();
145
		return property.add(source, o);
146
	}
147
148
	public Iterator iterator() {
149
		getterCalled();
150
		return new Iterator() {
151
			int lastReturned = -1;
152
			int expectedModCount = modCount;
153
			ListIterator delegate = new ArrayList(property.getList(source))
154
					.listIterator();
155
156
			public boolean hasNext() {
157
				getterCalled();
158
				checkForComodification();
159
				return delegate.hasNext();
160
			}
161
162
			public Object next() {
163
				getterCalled();
164
				checkForComodification();
165
				Object next = delegate.next();
166
				lastReturned = delegate.previousIndex();
167
				return next;
168
			}
169
170
			public void remove() {
171
				checkRealm();
172
				checkForComodification();
173
				if (lastReturned == -1)
174
					throw new IllegalStateException();
175
176
				delegate.remove(); // stay in sync
177
178
				property.remove(source, lastReturned);
179
180
				lastReturned = -1;
181
				expectedModCount = modCount;
182
			}
183
184
			private void checkForComodification() {
185
				if (expectedModCount != modCount)
186
					throw new ConcurrentModificationException();
187
			}
188
		};
189
	}
190
191
	public Object move(int oldIndex, int newIndex) {
192
		getterCalled();
193
		return property.move(source, oldIndex, newIndex);
194
	}
195
196
	public boolean remove(Object o) {
197
		getterCalled();
198
		return property.remove(source, o);
199
	}
200
201
	public void add(int index, Object o) {
202
		getterCalled();
203
		property.add(source, index, o);
204
	}
205
206
	public ListIterator listIterator() {
207
		return listIterator(0);
208
	}
209
210
	public ListIterator listIterator(final int index) {
211
		getterCalled();
212
		return new ListIterator() {
213
			int lastReturned = -1;
214
			int expectedModCount = modCount;
215
			ListIterator delegate = new ArrayList(property.getList(source))
216
					.listIterator(index);
217
218
			public boolean hasNext() {
219
				getterCalled();
220
				checkForComodification();
221
				return delegate.hasNext();
222
			}
223
224
			public int nextIndex() {
225
				getterCalled();
226
				checkForComodification();
227
				return delegate.nextIndex();
228
			}
229
230
			public Object next() {
231
				getterCalled();
232
				checkForComodification();
233
				Object next = delegate.next();
234
				lastReturned = delegate.previousIndex();
235
				return next;
236
			}
237
238
			public boolean hasPrevious() {
239
				getterCalled();
240
				checkForComodification();
241
				return delegate.hasPrevious();
242
			}
243
244
			public int previousIndex() {
245
				getterCalled();
246
				checkForComodification();
247
				return delegate.previousIndex();
248
			}
249
250
			public Object previous() {
251
				getterCalled();
252
				checkForComodification();
253
				Object previous = delegate.previous();
254
				lastReturned = delegate.nextIndex();
255
				return previous;
256
			}
257
258
			public void add(Object o) {
259
				checkRealm();
260
				checkForComodification();
261
				int index = delegate.nextIndex();
262
263
				delegate.add(o); // keep in sync
264
265
				property.add(source, index, o);
266
267
				lastReturned = -1;
268
				expectedModCount = modCount;
269
			}
270
271
			public void set(Object o) {
272
				checkRealm();
273
				checkForComodification();
274
275
				delegate.set(o);
276
277
				property.set(source, lastReturned, o);
278
279
				expectedModCount = modCount;
280
			}
281
282
			public void remove() {
283
				checkRealm();
284
				checkForComodification();
285
				if (lastReturned == -1)
286
					throw new IllegalStateException();
287
288
				delegate.remove(); // keep in sync
289
290
				property.remove(source, lastReturned);
291
292
				lastReturned = -1;
293
				expectedModCount = modCount;
294
			}
295
296
			private void checkForComodification() {
297
				if (expectedModCount != modCount)
298
					throw new ConcurrentModificationException();
299
			}
300
301
		};
302
	}
303
304
	public Object remove(int index) {
305
		getterCalled();
306
		return property.remove(source, index);
307
	}
308
309
	public Object set(int index, Object o) {
310
		getterCalled();
311
		return property.set(source, index, o);
312
	}
313
314
	public List subList(int fromIndex, int toIndex) {
315
		getterCalled();
316
		return Collections.unmodifiableList(property.getList(source).subList(
317
				fromIndex, toIndex));
318
	}
319
320
	// Bulk change operations
321
322
	public boolean addAll(Collection c) {
323
		getterCalled();
324
		return property.addAll(source, c);
325
	}
326
327
	public boolean addAll(int index, Collection c) {
328
		getterCalled();
329
		return property.addAll(source, index, c);
330
	}
331
332
	public boolean removeAll(Collection c) {
333
		getterCalled();
334
		return property.removeAll(source, c);
335
	}
336
337
	public boolean retainAll(Collection c) {
338
		getterCalled();
339
		return property.retainAll(source, c);
340
	}
341
342
	public void clear() {
343
		getterCalled();
344
		property.clear(source);
345
	}
346
347
	public boolean equals(Object o) {
348
		getterCalled();
349
		return property.equals(source, o);
350
	}
351
352
	public int hashCode() {
353
		getterCalled();
354
		return property.hashCode(source);
355
	}
356
357
	public Object getObserved() {
358
		return source;
359
	}
360
361
	public IProperty getProperty() {
362
		return property;
363
	}
364
365
	public synchronized void dispose() {
366
		if (!disposed) {
367
			disposed = true;
368
			property.removeListChangeListener(source, listener);
369
			property = null;
370
			source = null;
371
		}
372
		super.dispose();
373
	}
374
}
(-)src/org/eclipse/core/internal/databinding/property/ListValuePropertyObservableList.java (+379 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 java.lang.reflect.Array;
15
import java.util.ArrayList;
16
import java.util.Collection;
17
import java.util.Iterator;
18
import java.util.List;
19
import java.util.ListIterator;
20
21
import org.eclipse.core.databinding.observable.Diffs;
22
import org.eclipse.core.databinding.observable.IObserving;
23
import org.eclipse.core.databinding.observable.ObservableTracker;
24
import org.eclipse.core.databinding.observable.list.AbstractObservableList;
25
import org.eclipse.core.databinding.observable.list.IListChangeListener;
26
import org.eclipse.core.databinding.observable.list.IObservableList;
27
import org.eclipse.core.databinding.observable.list.ListChangeEvent;
28
import org.eclipse.core.databinding.observable.list.ListDiff;
29
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
30
import org.eclipse.core.databinding.observable.set.IObservableSet;
31
import org.eclipse.core.databinding.observable.set.ISetChangeListener;
32
import org.eclipse.core.databinding.observable.set.SetChangeEvent;
33
import org.eclipse.core.databinding.observable.set.WritableSet;
34
import org.eclipse.core.databinding.property.IValueProperty;
35
import org.eclipse.core.databinding.property.IValuePropertyChangeListener;
36
import org.eclipse.core.databinding.property.ValuePropertyChangeEvent;
37
import org.eclipse.core.internal.databinding.Util;
38
39
/**
40
 * @since 3.3
41
 * 
42
 */
43
public class ListValuePropertyObservableList extends AbstractObservableList
44
		implements IObserving {
45
	private IObservableList masterList;
46
	private IValueProperty detailProperty;
47
48
	private IObservableSet knownMasterElements;
49
50
	private boolean disposed = false;
51
52
	private IListChangeListener masterListener = new IListChangeListener() {
53
		public void handleListChange(ListChangeEvent event) {
54
			ListDiffEntry[] masterEntries = event.diff.getDifferences();
55
			ListDiffEntry[] detailEntries = new ListDiffEntry[masterEntries.length];
56
			for (int i = 0; i < masterEntries.length; i++) {
57
				ListDiffEntry masterDifference = masterEntries[i];
58
				int index = masterDifference.getPosition();
59
				boolean addition = masterDifference.isAddition();
60
				Object masterElement = masterDifference.getElement();
61
				Object elementDetailValue = detailProperty
62
						.getValue(masterElement);
63
				detailEntries[i] = Diffs.createListDiffEntry(index, addition,
64
						elementDetailValue);
65
			}
66
67
			// Removes listeners from elements removed from list
68
			knownMasterElements.retainAll(masterList);
69
70
			// Adds listeners to elements added to list
71
			knownMasterElements.addAll(masterList);
72
73
			fireListChange(Diffs.createListDiff(detailEntries));
74
		}
75
	};
76
77
	private IValuePropertyChangeListener detailListener = new IValuePropertyChangeListener() {
78
		public void handleValuePropertyChange(
79
				final ValuePropertyChangeEvent event) {
80
			if (!disposed) {
81
				Object source = event.getSource();
82
				int[] indices = findIndices(source);
83
				Object oldValue = event.diff.getOldValue();
84
				Object newValue = event.diff.getNewValue();
85
				ListDiffEntry[] entries = new ListDiffEntry[indices.length * 2];
86
				for (int i = 0; i < indices.length; i++) {
87
					int index = indices[i];
88
					entries[i * 2] = Diffs.createListDiffEntry(index, false,
89
							oldValue);
90
					entries[i * 2 + 1] = Diffs.createListDiffEntry(index, true,
91
							newValue);
92
				}
93
94
				ListDiff diff = Diffs.createListDiff(entries);
95
				fireListChange(diff);
96
			}
97
		}
98
99
		private int[] findIndices(Object masterElement) {
100
			List indices = new ArrayList();
101
102
			for (ListIterator it = masterList.listIterator(); it.hasNext();) {
103
				if (Util.equals(masterElement, it.next()))
104
					indices.add(Integer.valueOf(it.previousIndex()));
105
			}
106
107
			int[] result = new int[indices.size()];
108
			for (int i = 0; i < result.length; i++) {
109
				result[i] = ((Integer) indices.get(i)).intValue();
110
			}
111
			return result;
112
		}
113
	};
114
115
	/**
116
	 * @param masterList
117
	 * @param valueProperty
118
	 */
119
	public ListValuePropertyObservableList(IObservableList masterList,
120
			IValueProperty valueProperty) {
121
		super(masterList.getRealm());
122
		this.masterList = masterList;
123
		this.detailProperty = valueProperty;
124
	}
125
126
	protected void firstListenerAdded() {
127
		knownMasterElements = new WritableSet(getRealm());
128
		knownMasterElements.addSetChangeListener(new ISetChangeListener() {
129
			public void handleSetChange(SetChangeEvent event) {
130
				for (Iterator it = event.diff.getRemovals().iterator(); it
131
						.hasNext();) {
132
					detailProperty.addValueChangeListener(it.next(),
133
							detailListener);
134
				}
135
				for (Iterator it = event.diff.getAdditions().iterator(); it
136
						.hasNext();) {
137
					detailProperty.removeValueChangeListener(it.next(),
138
							detailListener);
139
				}
140
			}
141
		});
142
		knownMasterElements.addAll(masterList);
143
		masterList.addListChangeListener(masterListener);
144
	}
145
146
	protected void lastListenerRemoved() {
147
		masterList.removeListChangeListener(masterListener);
148
		if (knownMasterElements != null) {
149
			knownMasterElements.clear();
150
			knownMasterElements.dispose();
151
			knownMasterElements = null;
152
		}
153
	}
154
155
	protected int doGetSize() {
156
		getterCalled();
157
		return masterList.size();
158
	}
159
160
	private void getterCalled() {
161
		ObservableTracker.getterCalled(this);
162
	}
163
164
	public Object getElementType() {
165
		return detailProperty.getValueType();
166
	}
167
168
	public Object get(int index) {
169
		getterCalled();
170
		Object masterElement = masterList.get(index);
171
		return detailProperty.getValue(masterElement);
172
	}
173
174
	public boolean add(Object o) {
175
		throw new UnsupportedOperationException();
176
	}
177
178
	public boolean addAll(Collection c) {
179
		throw new UnsupportedOperationException();
180
	}
181
182
	public boolean addAll(int index, Collection c) {
183
		throw new UnsupportedOperationException();
184
	}
185
186
	public boolean contains(Object o) {
187
		getterCalled();
188
189
		Iterator it;
190
		if (knownMasterElements != null)
191
			it = knownMasterElements.iterator();
192
		else
193
			it = masterList.iterator();
194
		for (; it.hasNext();) {
195
			if (Util.equals(detailProperty.getValue(it.next()), o))
196
				return true;
197
		}
198
		return false;
199
	}
200
201
	public boolean isEmpty() {
202
		getterCalled();
203
		return masterList.isEmpty();
204
	}
205
206
	public boolean isStale() {
207
		getterCalled();
208
		return masterList.isStale();
209
	}
210
211
	public Iterator iterator() {
212
		getterCalled();
213
		return new Iterator() {
214
			Iterator it = masterList.iterator();
215
216
			public boolean hasNext() {
217
				getterCalled();
218
				return it.hasNext();
219
			}
220
221
			public Object next() {
222
				getterCalled();
223
				Object masterElement = it.next();
224
				return detailProperty.getValue(masterElement);
225
			}
226
227
			public void remove() {
228
				throw new UnsupportedOperationException();
229
			}
230
		};
231
	}
232
233
	public Object move(int oldIndex, int newIndex) {
234
		throw new UnsupportedOperationException();
235
	}
236
237
	public boolean remove(Object o) {
238
		throw new UnsupportedOperationException();
239
	}
240
241
	public boolean removeAll(Collection c) {
242
		throw new UnsupportedOperationException();
243
	}
244
245
	public boolean retainAll(Collection c) {
246
		throw new UnsupportedOperationException();
247
	}
248
249
	public Object[] toArray() {
250
		getterCalled();
251
		Object[] masterElements = masterList.toArray();
252
		Object[] result = new Object[masterElements.length];
253
		for (int i = 0; i < result.length; i++) {
254
			result[i] = detailProperty.getValue(masterElements[i]);
255
		}
256
		return result;
257
	}
258
259
	public Object[] toArray(Object[] a) {
260
		getterCalled();
261
		Object[] masterElements = masterList.toArray();
262
		if (a.length < masterElements.length)
263
			a = (Object[]) Array.newInstance(a.getClass().getComponentType(),
264
					masterElements.length);
265
		for (int i = 0; i < masterElements.length; i++) {
266
			a[i] = detailProperty.getValue(masterElements[i]);
267
		}
268
		return a;
269
	}
270
271
	public void add(int index, Object o) {
272
		throw new UnsupportedOperationException();
273
	}
274
275
	public void clear() {
276
		throw new UnsupportedOperationException();
277
	}
278
279
	public ListIterator listIterator() {
280
		return listIterator(0);
281
	}
282
283
	public ListIterator listIterator(final int index) {
284
		getterCalled();
285
		return new ListIterator() {
286
			ListIterator it = masterList.listIterator(index);
287
			Object lastReturned;
288
			boolean haveIterated = false;
289
290
			public void add(Object arg0) {
291
				throw new UnsupportedOperationException();
292
			}
293
294
			public boolean hasNext() {
295
				getterCalled();
296
				return it.hasNext();
297
			}
298
299
			public boolean hasPrevious() {
300
				getterCalled();
301
				return it.hasPrevious();
302
			}
303
304
			public Object next() {
305
				getterCalled();
306
				Object result = lastReturned = detailProperty.getValue(it
307
						.next());
308
				haveIterated = true;
309
				return result;
310
			}
311
312
			public int nextIndex() {
313
				getterCalled();
314
				return it.nextIndex();
315
			}
316
317
			public Object previous() {
318
				getterCalled();
319
				Object result = lastReturned = detailProperty.getValue(it
320
						.previous());
321
				haveIterated = true;
322
				return result;
323
			}
324
325
			public int previousIndex() {
326
				getterCalled();
327
				return it.previousIndex();
328
			}
329
330
			public void remove() {
331
				throw new UnsupportedOperationException();
332
			}
333
334
			public void set(Object o) {
335
				checkRealm();
336
				if (!haveIterated)
337
					throw new IllegalStateException();
338
				// if listeners are registered, the value property listener will
339
				// cause the list change event to fire. otherwise it doesn't
340
				// matter because noone is listening
341
				detailProperty.setValue(lastReturned, o);
342
			}
343
		};
344
	}
345
346
	public Object remove(int index) {
347
		throw new UnsupportedOperationException();
348
	}
349
350
	public Object set(int index, Object o) {
351
		checkRealm();
352
		Object masterElement = masterList.get(index);
353
		Object oldValue = detailProperty.getValue(masterElement);
354
		detailProperty.setValue(masterElement, o);
355
		return oldValue;
356
	}
357
358
	public Object getObserved() {
359
		return masterList;
360
	}
361
362
	public synchronized void dispose() {
363
		if (masterList != null) {
364
			masterList.removeListChangeListener(masterListener);
365
			masterList = null;
366
			masterListener = null;
367
		}
368
		if (knownMasterElements != null) {
369
			knownMasterElements.clear();
370
			knownMasterElements.dispose();
371
			knownMasterElements = null;
372
			detailListener = null;
373
		}
374
		detailProperty = null;
375
		disposed = true;
376
377
		super.dispose();
378
	}
379
}
(-)src/org/eclipse/core/internal/databinding/property/PropertyUpdateHelper.java (+65 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 java.util.HashMap;
15
import java.util.Map;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class PropertyUpdateHelper {
22
	private ThreadLocal localMap = new ThreadLocal() {
23
		protected Object initialValue() {
24
			return new HashMap();
25
		}
26
	};
27
28
	/**
29
	 * @param source
30
	 * @return blah
31
	 */
32
	public boolean isUpdating(Object source) {
33
		Map map = (Map) localMap.get();
34
		return map.containsKey(source);
35
	}
36
37
	/**
38
	 * @param source
39
	 * @param state
40
	 */
41
	public void setUpdating(Object source, boolean state) {
42
		Map map = (Map) localMap.get();
43
		Integer count = (Integer) map.get(source);
44
		int newCount = (count == null ? 0 : count.intValue())
45
				+ (state ? 1 : -1);
46
		if (newCount > 0)
47
			map.put(source, new Integer(newCount));
48
		else
49
			map.remove(source);
50
	}
51
52
	/**
53
	 * 
54
	 */
55
	public void dispose() {
56
		if (localMap != null) {
57
			Map map = (Map) localMap.get();
58
			if (map != null) {
59
				map.clear();
60
				localMap.set(null);
61
			}
62
			localMap = null;
63
		}
64
	}
65
}
(-)src/org/eclipse/core/internal/databinding/property/PropertyObservableSet.java (+197 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 java.util.Collection;
15
import java.util.ConcurrentModificationException;
16
import java.util.HashSet;
17
import java.util.Iterator;
18
import java.util.Set;
19
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.set.AbstractObservableSet;
22
import org.eclipse.core.databinding.property.IProperty;
23
import org.eclipse.core.databinding.property.IPropertyObservable;
24
import org.eclipse.core.databinding.property.ISetProperty;
25
import org.eclipse.core.databinding.property.ISetPropertyChangeListener;
26
import org.eclipse.core.databinding.property.SetPropertyChangeEvent;
27
28
/**
29
 * @since 3.3
30
 * 
31
 */
32
public class PropertyObservableSet extends AbstractObservableSet implements
33
		IPropertyObservable {
34
	private Object source;
35
	private ISetProperty property;
36
37
	private boolean updating = false;
38
39
	private boolean disposed = false;
40
41
	private transient volatile int modCount = 0;
42
43
	private ISetPropertyChangeListener listener = new ISetPropertyChangeListener() {
44
		public void handleSetPropertyChange(final SetPropertyChangeEvent event) {
45
			if (!disposed && !updating) {
46
				getRealm().exec(new Runnable() {
47
					public void run() {
48
						getRealm().exec(new Runnable() {
49
							public void run() {
50
								modCount++;
51
								fireSetChange(event.diff);
52
							}
53
						});
54
					}
55
				});
56
			}
57
		}
58
	};
59
60
	/**
61
	 * @param realm
62
	 * @param source
63
	 * @param property
64
	 */
65
	public PropertyObservableSet(Realm realm, Object source,
66
			ISetProperty property) {
67
		super(realm);
68
		this.source = source;
69
		this.property = property;
70
	}
71
72
	protected void firstListenerAdded() {
73
		if (!disposed) {
74
			property.addSetChangeListener(source, listener);
75
		}
76
	}
77
78
	protected void lastListenerRemoved() {
79
		if (!disposed) {
80
			property.removeSetChangeListener(source, listener);
81
		}
82
	}
83
84
	protected Set getWrappedSet() {
85
		return property.getSet(source);
86
	}
87
88
	public Object getElementType() {
89
		return property.getElementType();
90
	}
91
92
	// Queries
93
94
	protected int doGetSize() {
95
		return property.size(source);
96
	}
97
98
	// Single change operations
99
100
	public boolean add(Object o) {
101
		checkRealm();
102
		return property.add(source, o);
103
	}
104
105
	public Iterator iterator() {
106
		getterCalled();
107
		return new Iterator() {
108
			int expectedModCount = modCount;
109
			Iterator delegate = new HashSet(property.getSet(source)).iterator();
110
			Object last = null;
111
112
			public boolean hasNext() {
113
				getterCalled();
114
				checkForComodification();
115
				return delegate.hasNext();
116
			}
117
118
			public Object next() {
119
				getterCalled();
120
				checkForComodification();
121
				Object next = delegate.next();
122
				last = next;
123
				return next;
124
			}
125
126
			public void remove() {
127
				checkRealm();
128
				checkForComodification();
129
130
				delegate.remove(); // stay in sync
131
132
				property.remove(source, last);
133
134
				last = null;
135
				expectedModCount = modCount;
136
			}
137
138
			private void checkForComodification() {
139
				if (expectedModCount != modCount)
140
					throw new ConcurrentModificationException();
141
			}
142
		};
143
	}
144
145
	public boolean remove(Object o) {
146
		getterCalled();
147
		return property.remove(source, o);
148
	}
149
150
	// Bulk change operations
151
152
	public boolean addAll(Collection c) {
153
		getterCalled();
154
		return property.addAll(source, c);
155
	}
156
157
	public boolean removeAll(Collection c) {
158
		getterCalled();
159
		return property.removeAll(source, c);
160
	}
161
162
	public boolean retainAll(Collection c) {
163
		getterCalled();
164
		return property.retainAll(source, c);
165
	}
166
167
	public void clear() {
168
		getterCalled();
169
		property.clear(source);
170
	}
171
172
	public boolean equals(Object o) {
173
		return property.equals(source, o);
174
	}
175
176
	public int hashCode() {
177
		return property.hashCode(source);
178
	}
179
180
	public Object getObserved() {
181
		return source;
182
	}
183
184
	public IProperty getProperty() {
185
		return property;
186
	}
187
188
	public synchronized void dispose() {
189
		if (!disposed) {
190
			disposed = true;
191
			property.removeSetChangeListener(source, listener);
192
			property = null;
193
			source = null;
194
		}
195
		super.dispose();
196
	}
197
}
(-)src/org/eclipse/core/databinding/property/IPropertyChangeListener.java (+23 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
/**
15
 * Marker interface for all listener types in the properties framework.
16
 * 
17
 * @noimplement This interface is not intended to be implemented by clients.
18
 * 
19
 * @since 1.2
20
 */
21
public interface IPropertyChangeListener {
22
23
}
(-)src/org/eclipse/core/databinding/property/CollectionProperty.java (+56 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.Collection;
15
16
/**
17
 * Abstract implementation of ICollectionProperty.
18
 * 
19
 * @since 1.2
20
 */
21
public abstract class CollectionProperty extends Property implements
22
		ICollectionProperty {
23
	abstract Collection getCollection(Object source);
24
25
	public boolean contains(Object source, Object o) {
26
		return getCollection(source).contains(o);
27
	}
28
29
	public boolean containsAll(Object source, Collection c) {
30
		return getCollection(source).containsAll(c);
31
	}
32
33
	public boolean equals(Object source, Object o) {
34
		return getCollection(source).equals(o);
35
	}
36
37
	public int hashCode(Object source) {
38
		return getCollection(source).hashCode();
39
	}
40
41
	public boolean isEmpty(Object source) {
42
		return getCollection(source).isEmpty();
43
	}
44
45
	public int size(Object source) {
46
		return getCollection(source).size();
47
	}
48
49
	public Object[] toArray(Object source, Object[] array) {
50
		return getCollection(source).toArray(array);
51
	}
52
53
	public Object[] toArray(Object source) {
54
		return getCollection(source).toArray();
55
	}
56
}
(-)src/org/eclipse/core/internal/databinding/property/SetValuePropertyObservableMap.java (+92 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.Diffs;
15
import org.eclipse.core.databinding.observable.IObserving;
16
import org.eclipse.core.databinding.observable.map.ComputedObservableMap;
17
import org.eclipse.core.databinding.observable.set.IObservableSet;
18
import org.eclipse.core.databinding.property.IValueProperty;
19
import org.eclipse.core.databinding.property.IValuePropertyChangeListener;
20
import org.eclipse.core.databinding.property.ValuePropertyChangeEvent;
21
22
/**
23
 * @since 3.3
24
 * 
25
 */
26
public class SetValuePropertyObservableMap extends ComputedObservableMap
27
		implements IObserving {
28
	private IValueProperty property;
29
30
	private boolean disposed = false;
31
32
	private IValuePropertyChangeListener listener = new IValuePropertyChangeListener() {
33
		public void handleValuePropertyChange(
34
				final ValuePropertyChangeEvent event) {
35
			if (!disposed) {
36
				getRealm().exec(new Runnable() {
37
					public void run() {
38
						Object key = event.getSource();
39
						Object oldValue = event.diff.getOldValue();
40
						Object newValue = event.diff.getNewValue();
41
						fireMapChange(Diffs.createMapDiffSingleChange(key,
42
								oldValue, newValue));
43
					}
44
				});
45
			}
46
		}
47
	};
48
49
	/**
50
	 * @param keySet
51
	 * @param valueProperty
52
	 */
53
	public SetValuePropertyObservableMap(IObservableSet keySet,
54
			IValueProperty valueProperty) {
55
		super(keySet);
56
		this.property = valueProperty;
57
	}
58
59
	protected Object doGet(Object key) {
60
		return property.getValue(key);
61
	}
62
63
	protected Object doPut(Object key, Object value) {
64
		Object result = property.getValue(key);
65
		property.setValue(key, value);
66
		return result;
67
	}
68
69
	protected void hookListener(Object addedKey) {
70
		if (!disposed)
71
			property.addValueChangeListener(addedKey, listener);
72
	}
73
74
	protected void unhookListener(Object removedKey) {
75
		if (!disposed)
76
			property.removeValueChangeListener(removedKey, listener);
77
	}
78
79
	public Object getObserved() {
80
		return keySet();
81
	}
82
83
	public synchronized void dispose() {
84
		super.dispose();
85
86
		if (!disposed) {
87
			disposed = true;
88
			property = null;
89
			listener = null;
90
		}
91
	}
92
}
(-)src/org/eclipse/core/databinding/property/BasicValueProperty.java (+93 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;
13
14
import org.eclipse.core.databinding.observable.Diffs;
15
import org.eclipse.core.databinding.observable.value.ValueDiff;
16
import org.eclipse.core.internal.databinding.Util;
17
import org.eclipse.core.internal.databinding.property.PropertyUpdateHelper;
18
19
/**
20
 * Abstract IValueProperty implementation.
21
 * <p>
22
 * This class automatically fires value change events when changes are initiated
23
 * through a call to {@link #setValue(Object, Object)}.
24
 * 
25
 * @since 1.2
26
 */
27
public abstract class BasicValueProperty extends ValueProperty {
28
	private PropertyUpdateHelper updateHelper = new PropertyUpdateHelper();
29
30
	/**
31
	 * Returns whether this property is currently being updated on the source.
32
	 * Implementors should query this value to avoid unnecessary calculations,
33
	 * such as computing a diff.
34
	 * 
35
	 * @param source
36
	 *            the property source
37
	 * @return whether this property is currently being updated on the source.
38
	 */
39
	protected boolean isUpdating(Object source) {
40
		return updateHelper.isUpdating(source);
41
	}
42
43
	/**
44
	 * Sets the value property on the source to the specified value, then fires
45
	 * a value change.
46
	 * 
47
	 * @param source
48
	 *            the property source
49
	 * @param value
50
	 *            the new value
51
	 */
52
	public void setValue(Object source, Object value) {
53
		Object oldValue = getValue(source);
54
55
		updateHelper.setUpdating(source, true);
56
		try {
57
			doSetValue(source, value);
58
		} finally {
59
			updateHelper.setUpdating(source, false);
60
		}
61
62
		if (hasListeners(source)) {
63
			Object newValue = getValue(source);
64
			if (!Util.equals(oldValue, newValue)) {
65
				fireValueChange(source, Diffs.createValueDiff(oldValue,
66
						newValue));
67
			}
68
		}
69
	}
70
71
	protected void fireValueChange(Object source, ValueDiff diff) {
72
		if (!isUpdating(source))
73
			super.fireValueChange(source, diff);
74
	}
75
76
	/**
77
	 * Sets the value property on the source to the specified list.
78
	 * 
79
	 * @param source
80
	 *            the property source
81
	 * @param value
82
	 *            the new value
83
	 */
84
	protected abstract void doSetValue(Object source, Object value);
85
86
	public synchronized void dispose() {
87
		if (updateHelper != null) {
88
			updateHelper.dispose();
89
			updateHelper = null;
90
		}
91
		super.dispose();
92
	}
93
}
(-)src/org/eclipse/core/internal/databinding/property/PropertyObservableValue.java (+115 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.Diffs;
15
import org.eclipse.core.databinding.observable.Realm;
16
import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
17
import org.eclipse.core.databinding.property.IProperty;
18
import org.eclipse.core.databinding.property.IPropertyObservable;
19
import org.eclipse.core.databinding.property.IValueProperty;
20
import org.eclipse.core.databinding.property.IValuePropertyChangeListener;
21
import org.eclipse.core.databinding.property.ValuePropertyChangeEvent;
22
import org.eclipse.core.internal.databinding.Util;
23
24
/**
25
 * @since 1.2
26
 * 
27
 */
28
public class PropertyObservableValue extends AbstractObservableValue implements
29
		IPropertyObservable {
30
	private Object source;
31
	private IValueProperty property;
32
33
	private boolean updating = false;
34
35
	private boolean disposed = false;
36
37
	private IValuePropertyChangeListener listener = new IValuePropertyChangeListener() {
38
		public void handleValuePropertyChange(
39
				final ValuePropertyChangeEvent event) {
40
			if (!disposed && !updating) {
41
				getRealm().exec(new Runnable() {
42
					public void run() {
43
						fireValueChange(event.diff);
44
					}
45
				});
46
			}
47
		}
48
	};
49
50
	/**
51
	 * @param realm
52
	 * @param source
53
	 * @param property
54
	 */
55
	public PropertyObservableValue(Realm realm, Object source,
56
			IValueProperty property) {
57
		super(realm);
58
		this.source = source;
59
		this.property = property;
60
	}
61
62
	protected void firstListenerAdded() {
63
		if (!disposed) {
64
			property.addValueChangeListener(source, listener);
65
		}
66
	}
67
68
	protected void lastListenerRemoved() {
69
		if (!disposed) {
70
			property.removeValueChangeListener(source, listener);
71
		}
72
	}
73
74
	protected Object doGetValue() {
75
		return property.getValue(source);
76
	}
77
78
	protected void doSetValue(Object value) {
79
		Object oldValue = doGetValue();
80
81
		updating = true;
82
		try {
83
			property.setValue(source, value);
84
		} finally {
85
			updating = false;
86
		}
87
88
		Object newValue = doGetValue();
89
		if (!Util.equals(oldValue, newValue)) {
90
			fireValueChange(Diffs.createValueDiff(oldValue, newValue));
91
		}
92
	}
93
94
	public Object getValueType() {
95
		return property.getValueType();
96
	}
97
98
	public Object getObserved() {
99
		return source;
100
	}
101
102
	public IProperty getProperty() {
103
		return property;
104
	}
105
106
	public synchronized void dispose() {
107
		if (!disposed) {
108
			disposed = true;
109
			property.removeValueChangeListener(source, listener);
110
			property = null;
111
			source = null;
112
		}
113
		super.dispose();
114
	}
115
}
(-)src/org/eclipse/core/databinding/property/IListPropertyChangeListener.java (+27 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
/**
15
 * Listener for changes to list properties on a property source
16
 * 
17
 * @since 1.2
18
 */
19
public interface IListPropertyChangeListener extends IPropertyChangeListener {
20
	/**
21
	 * Handle a change to a list property on a specific property source.
22
	 * 
23
	 * @param event
24
	 *            an event describing the list change that occured.
25
	 */
26
	public void handleListPropertyChange(ListPropertyChangeEvent event);
27
}
(-)src/org/eclipse/core/databinding/property/Property.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.databinding.property;
13
14
/**
15
 * Abstract implementation of IProperty
16
 * 
17
 * @since 1.2
18
 */
19
public abstract class Property implements IProperty {
20
	private PropertyChangeSupport changeSupport;
21
22
	synchronized PropertyChangeSupport getChangeSupport() {
23
		if (changeSupport == null)
24
			changeSupport = new PropertyChangeSupport(this);
25
		return changeSupport;
26
	}
27
28
	/**
29
	 * Notifies the property that the first listener has been added for the
30
	 * given source object. Implementers should register a listener on the
31
	 * source object which fires an appropriate change event when a property
32
	 * change is observed on the source.
33
	 * 
34
	 * @param source
35
	 *            the source object to observe for property changes.
36
	 * @see #removeListenerFrom(Object)
37
	 */
38
	protected abstract void addListenerTo(Object source);
39
40
	/**
41
	 * Notifies the property that the last listener has been removed for the
42
	 * given source object. Implementers should unregister any previously
43
	 * registered listeners from the source.
44
	 * 
45
	 * @param source
46
	 *            the source object to stop observing for property changes.
47
	 * @see #addListenerTo(Object)
48
	 */
49
	protected abstract void removeListenerFrom(Object source);
50
51
	/**
52
	 * Returns whether this property has listeners registered for the given
53
	 * property source
54
	 * 
55
	 * @param source
56
	 *            the property source
57
	 * @return whether this property has listeners registered for the given
58
	 *         property source
59
	 */
60
	protected boolean hasListeners(Object source) {
61
		return changeSupport != null && changeSupport.hasListeners(source);
62
	}
63
64
	public synchronized void dispose() {
65
		if (changeSupport != null) {
66
			changeSupport.dispose();
67
			changeSupport = null;
68
		}
69
	}
70
}
(-)src/org/eclipse/jface/tests/databinding/swt/SWTObservablesTest.java (-71 / +123 lines)
Lines 7-39 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
 ******************************************************************************/
11
 ******************************************************************************/
11
12
12
package org.eclipse.jface.tests.databinding.swt;
13
package org.eclipse.jface.tests.databinding.swt;
13
14
15
import org.eclipse.core.databinding.observable.IDecoratingObservable;
14
import org.eclipse.core.databinding.observable.list.IObservableList;
16
import org.eclipse.core.databinding.observable.list.IObservableList;
17
import org.eclipse.core.databinding.property.IPropertyObservable;
18
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
15
import org.eclipse.jface.databinding.conformance.util.RealmTester;
19
import org.eclipse.jface.databinding.conformance.util.RealmTester;
20
import org.eclipse.jface.databinding.swt.ISWTObservable;
16
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
21
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
17
import org.eclipse.jface.databinding.swt.SWTObservables;
22
import org.eclipse.jface.databinding.swt.SWTObservables;
18
import org.eclipse.jface.internal.databinding.swt.ButtonObservableValue;
23
import org.eclipse.jface.internal.databinding.swt.CComboSelectionProperty;
19
import org.eclipse.jface.internal.databinding.swt.CComboObservableList;
24
import org.eclipse.jface.internal.databinding.swt.CComboTextProperty;
20
import org.eclipse.jface.internal.databinding.swt.CComboObservableValue;
25
import org.eclipse.jface.internal.databinding.swt.CLabelTextProperty;
21
import org.eclipse.jface.internal.databinding.swt.CLabelObservableValue;
26
import org.eclipse.jface.internal.databinding.swt.ComboSelectionProperty;
22
import org.eclipse.jface.internal.databinding.swt.ComboObservableList;
27
import org.eclipse.jface.internal.databinding.swt.ComboTextProperty;
23
import org.eclipse.jface.internal.databinding.swt.ComboObservableValue;
28
import org.eclipse.jface.internal.databinding.swt.LabelTextProperty;
24
import org.eclipse.jface.internal.databinding.swt.LabelObservableValue;
29
import org.eclipse.jface.internal.databinding.swt.ScaleMaximumProperty;
25
import org.eclipse.jface.internal.databinding.swt.ListObservableList;
30
import org.eclipse.jface.internal.databinding.swt.ScaleMinimumProperty;
26
import org.eclipse.jface.internal.databinding.swt.ListObservableValue;
31
import org.eclipse.jface.internal.databinding.swt.ScaleSelectionProperty;
27
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
32
import org.eclipse.jface.internal.databinding.swt.SpinnerMaximumProperty;
28
import org.eclipse.jface.internal.databinding.swt.ScaleObservableValue;
33
import org.eclipse.jface.internal.databinding.swt.SpinnerMinimumProperty;
29
import org.eclipse.jface.internal.databinding.swt.SpinnerObservableValue;
34
import org.eclipse.jface.internal.databinding.swt.SpinnerSelectionProperty;
30
import org.eclipse.jface.internal.databinding.swt.TableSingleSelectionObservableValue;
35
import org.eclipse.jface.internal.databinding.swt.StyledTextTextProperty;
31
import org.eclipse.jface.internal.databinding.swt.TextEditableObservableValue;
36
import org.eclipse.jface.internal.databinding.swt.TableSingleSelectionIndexProperty;
32
import org.eclipse.jface.internal.databinding.swt.TextObservableValue;
37
import org.eclipse.jface.internal.databinding.swt.TextEditableProperty;
38
import org.eclipse.jface.internal.databinding.swt.TextTextProperty;
33
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
39
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
34
import org.eclipse.swt.SWT;
40
import org.eclipse.swt.SWT;
35
import org.eclipse.swt.custom.CCombo;
41
import org.eclipse.swt.custom.CCombo;
36
import org.eclipse.swt.custom.CLabel;
42
import org.eclipse.swt.custom.CLabel;
43
import org.eclipse.swt.custom.StyledText;
37
import org.eclipse.swt.graphics.Color;
44
import org.eclipse.swt.graphics.Color;
38
import org.eclipse.swt.graphics.Font;
45
import org.eclipse.swt.graphics.Font;
39
import org.eclipse.swt.widgets.Button;
46
import org.eclipse.swt.widgets.Button;
Lines 55-68 Link Here
55
62
56
	protected void setUp() throws Exception {
63
	protected void setUp() throws Exception {
57
		super.setUp();
64
		super.setUp();
58
		
65
59
		shell = getShell();
66
		shell = getShell();
60
		RealmTester.setDefault(SWTObservables.getRealm(shell.getDisplay()));
67
		RealmTester.setDefault(SWTObservables.getRealm(shell.getDisplay()));
61
	}
68
	}
62
69
63
	protected void tearDown() throws Exception {
70
	protected void tearDown() throws Exception {
64
		super.tearDown();
71
		super.tearDown();
65
		
72
66
		RealmTester.setDefault(null);
73
		RealmTester.setDefault(null);
67
	}
74
	}
68
75
Lines 88-141 Link Here
88
		Spinner spinner = new Spinner(shell, SWT.NONE);
95
		Spinner spinner = new Spinner(shell, SWT.NONE);
89
		ISWTObservableValue value = SWTObservables.observeSelection(spinner);
96
		ISWTObservableValue value = SWTObservables.observeSelection(spinner);
90
		assertNotNull(value);
97
		assertNotNull(value);
91
		assertTrue(value instanceof SpinnerObservableValue);
98
		assertTrue(value.getWidget() == spinner);
92
99
93
		SpinnerObservableValue spinnerObservable = (SpinnerObservableValue) value;
100
		IPropertyObservable propertyObservable = getPropertyObservable(value);
94
		assertEquals(SWTProperties.SELECTION, spinnerObservable.getAttribute());
101
		assertTrue(propertyObservable.getProperty() instanceof SpinnerSelectionProperty);
95
	}
102
	}
96
103
97
	public void testObserveSelectionOfButton() throws Exception {
104
	public void testObserveSelectionOfButton() throws Exception {
98
		Button button = new Button(shell, SWT.PUSH);
105
		Button button = new Button(shell, SWT.PUSH);
99
		ISWTObservableValue value = SWTObservables.observeSelection(button);
106
		ISWTObservableValue value = SWTObservables.observeSelection(button);
100
		assertNotNull(value);
107
		assertNotNull(value);
101
		assertTrue(value instanceof ButtonObservableValue);
108
		assertTrue(value.getWidget() == button);
102
	}
109
	}
103
110
104
	public void testObserveSelectionOfCombo() throws Exception {
111
	public void testObserveSelectionOfCombo() throws Exception {
105
		Combo combo = new Combo(shell, SWT.NONE);
112
		Combo combo = new Combo(shell, SWT.NONE);
106
		ISWTObservableValue value = SWTObservables.observeSelection(combo);
113
		ISWTObservableValue value = SWTObservables.observeSelection(combo);
107
		assertNotNull(value);
114
		assertNotNull(value);
108
		assertTrue(value instanceof ComboObservableValue);
115
		assertTrue(value.getWidget() == combo);
109
116
110
		ComboObservableValue comboObservable = (ComboObservableValue) value;
117
		IPropertyObservable propertyObservable = getPropertyObservable(value);
111
		assertEquals(SWTProperties.SELECTION, comboObservable.getAttribute());
118
		assertTrue(propertyObservable.getProperty() instanceof ComboSelectionProperty);
112
	}
119
	}
113
120
114
	public void testObserveSelectionOfCCombo() throws Exception {
121
	public void testObserveSelectionOfCCombo() throws Exception {
115
		CCombo combo = new CCombo(shell, SWT.NONE);
122
		CCombo combo = new CCombo(shell, SWT.NONE);
116
		ISWTObservableValue value = SWTObservables.observeSelection(combo);
123
		ISWTObservableValue value = SWTObservables.observeSelection(combo);
117
		assertNotNull(value);
124
		assertNotNull(value);
118
		assertTrue(value instanceof CComboObservableValue);
125
		assertTrue(value.getWidget() == combo);
119
126
120
		CComboObservableValue ccomboObservable = (CComboObservableValue) value;
127
		IPropertyObservable property = getPropertyObservable(value);
121
		assertEquals(SWTProperties.SELECTION, ccomboObservable.getAttribute());
128
		assertTrue(property.getProperty() instanceof CComboSelectionProperty);
122
	}
129
	}
123
130
124
	public void testObserveSelectionOfList() throws Exception {
131
	public void testObserveSelectionOfList() throws Exception {
125
		List list = new List(shell, SWT.NONE);
132
		List list = new List(shell, SWT.NONE);
126
		ISWTObservableValue value = SWTObservables.observeSelection(list);
133
		ISWTObservableValue value = SWTObservables.observeSelection(list);
127
		assertNotNull(value);
134
		assertNotNull(value);
128
		assertTrue(value instanceof ListObservableValue);
135
		assertTrue(value.getWidget() == list);
129
	}
136
	}
130
	
137
131
	public void testObserveSelectionOfScale() throws Exception {
138
	public void testObserveSelectionOfScale() throws Exception {
132
		Scale scale = new Scale(shell, SWT.NONE);
139
		Scale scale = new Scale(shell, SWT.NONE);
133
		ISWTObservableValue value = SWTObservables.observeSelection(scale);
140
		ISWTObservableValue value = SWTObservables.observeSelection(scale);
134
		assertNotNull(value);
141
		assertNotNull(value);
135
		assertTrue(value instanceof ScaleObservableValue);
142
		assertTrue(value.getWidget() == scale);
136
		
143
137
		ScaleObservableValue scaleObservable = (ScaleObservableValue) value;
144
		IPropertyObservable property = getPropertyObservable(value);
138
		assertEquals(SWTProperties.SELECTION, scaleObservable.getAttribute());
145
		assertTrue(property.getProperty() instanceof ScaleSelectionProperty);
139
	}
146
	}
140
147
141
	public void testObserveSelectionOfUnsupportedControl() throws Exception {
148
	public void testObserveSelectionOfUnsupportedControl() throws Exception {
Lines 154-160 Link Here
154
		ISWTObservableValue value = SWTObservables.observeText(text,
161
		ISWTObservableValue value = SWTObservables.observeText(text,
155
				SWT.FocusOut);
162
				SWT.FocusOut);
156
		assertNotNull(value);
163
		assertNotNull(value);
157
		assertTrue(value instanceof TextObservableValue);
164
		assertTrue(value.getWidget() == text);
165
		IPropertyObservable propertyObservable = getPropertyObservable(value);
166
		assertTrue(propertyObservable.getProperty() instanceof TextTextProperty);
167
168
		assertFalse(text.isListening(SWT.FocusOut));
169
		ChangeEventTracker.observe(value);
170
		assertTrue(text.isListening(SWT.FocusOut));
171
	}
172
173
	public void testObserveTextOfStyledText() throws Exception {
174
		StyledText text = new StyledText(shell, SWT.NONE);
175
		assertFalse(text.isListening(SWT.FocusOut));
176
177
		ISWTObservableValue value = SWTObservables.observeText(text,
178
				SWT.FocusOut);
179
		assertNotNull(value);
180
		assertTrue(value.getWidget() == text);
181
		IPropertyObservable propertyObservable = getPropertyObservable(value);
182
		assertTrue(propertyObservable.getProperty() instanceof StyledTextTextProperty);
183
184
		assertFalse(text.isListening(SWT.FocusOut));
185
		ChangeEventTracker.observe(value);
158
		assertTrue(text.isListening(SWT.FocusOut));
186
		assertTrue(text.isListening(SWT.FocusOut));
159
	}
187
	}
160
188
Lines 171-204 Link Here
171
		Label label = new Label(shell, SWT.NONE);
199
		Label label = new Label(shell, SWT.NONE);
172
		ISWTObservableValue value = SWTObservables.observeText(label);
200
		ISWTObservableValue value = SWTObservables.observeText(label);
173
		assertNotNull(label);
201
		assertNotNull(label);
174
		assertTrue(value instanceof LabelObservableValue);
202
		assertTrue(value.getWidget() == label);
203
		IPropertyObservable propertyObservable = getPropertyObservable(value);
204
		assertTrue(propertyObservable.getProperty() instanceof LabelTextProperty);
175
	}
205
	}
176
206
177
	public void testObserveTextOfCLabel() throws Exception {
207
	public void testObserveTextOfCLabel() throws Exception {
178
		CLabel label = new CLabel(shell, SWT.NONE);
208
		CLabel label = new CLabel(shell, SWT.NONE);
179
		ISWTObservableValue value = SWTObservables.observeText(label);
209
		ISWTObservableValue value = SWTObservables.observeText(label);
180
		assertNotNull(label);
210
		assertNotNull(label);
181
		assertTrue(value instanceof CLabelObservableValue);
211
		assertTrue(value.getWidget() == label);
212
		IPropertyObservable propertyObservable = getPropertyObservable(value);
213
		assertTrue(propertyObservable.getProperty() instanceof CLabelTextProperty);
182
	}
214
	}
183
215
184
	public void testObserveTextOfCombo() throws Exception {
216
	public void testObserveTextOfCombo() throws Exception {
185
		Combo combo = new Combo(shell, SWT.NONE);
217
		Combo combo = new Combo(shell, SWT.NONE);
186
		ISWTObservableValue value = SWTObservables.observeText(combo);
218
		ISWTObservableValue value = SWTObservables.observeText(combo);
187
		assertNotNull(value);
219
		assertNotNull(value);
188
		assertTrue(value instanceof ComboObservableValue);
220
		assertTrue(value.getWidget() == combo);
221
222
		assertTrue(getPropertyObservable(value).getProperty() instanceof ComboTextProperty);
223
	}
189
224
190
		ComboObservableValue comboObservable = (ComboObservableValue) value;
225
	/**
191
		assertEquals(SWTProperties.TEXT, comboObservable.getAttribute());
226
	 * @param observable
227
	 * @return
228
	 */
229
	private IPropertyObservable getPropertyObservable(
230
			ISWTObservableValue observable) {
231
		IDecoratingObservable decoratingObservable = (IDecoratingObservable) observable;
232
		IPropertyObservable propertyObservable = (IPropertyObservable) decoratingObservable
233
				.getDecorated();
234
		return propertyObservable;
192
	}
235
	}
193
236
194
	public void testObserveTextOfCCombo() throws Exception {
237
	public void testObserveTextOfCCombo() throws Exception {
195
		CCombo combo = new CCombo(shell, SWT.NONE);
238
		CCombo combo = new CCombo(shell, SWT.NONE);
196
		ISWTObservableValue value = SWTObservables.observeText(combo);
239
		ISWTObservableValue value = SWTObservables.observeText(combo);
197
		assertNotNull(value);
240
		assertNotNull(value);
198
		assertTrue(value instanceof CComboObservableValue);
241
		assertTrue(value.getWidget() == combo);
199
242
200
		CComboObservableValue ccomboObservable = (CComboObservableValue) value;
243
		IDecoratingObservable decorating = (IDecoratingObservable) value;
201
		assertEquals(SWTProperties.TEXT, ccomboObservable.getAttribute());
244
		IPropertyObservable property = (IPropertyObservable) decorating
245
				.getDecorated();
246
		assertTrue(property.getProperty() instanceof CComboTextProperty);
202
	}
247
	}
203
248
204
	public void testObserveTextOfUnsupportedControl() throws Exception {
249
	public void testObserveTextOfUnsupportedControl() throws Exception {
Lines 214-234 Link Here
214
		Combo combo = new Combo(shell, SWT.NONE);
259
		Combo combo = new Combo(shell, SWT.NONE);
215
		IObservableList list = SWTObservables.observeItems(combo);
260
		IObservableList list = SWTObservables.observeItems(combo);
216
		assertNotNull(list);
261
		assertNotNull(list);
217
		assertTrue(list instanceof ComboObservableList);
262
		assertTrue(list instanceof ISWTObservable);
263
		assertTrue(((ISWTObservable) list).getWidget() == combo);
218
	}
264
	}
219
265
220
	public void testObserveItemsOfCCombo() throws Exception {
266
	public void testObserveItemsOfCCombo() throws Exception {
221
		CCombo ccombo = new CCombo(shell, SWT.NONE);
267
		CCombo ccombo = new CCombo(shell, SWT.NONE);
222
		IObservableList list = SWTObservables.observeItems(ccombo);
268
		IObservableList list = SWTObservables.observeItems(ccombo);
223
		assertNotNull(list);
269
		assertNotNull(list);
224
		assertTrue(list instanceof CComboObservableList);
270
		ISWTObservable swtObservable = (ISWTObservable) list;
271
		assertTrue(swtObservable.getWidget() == ccombo);
225
	}
272
	}
226
273
227
	public void testObserveItemsOfList() throws Exception {
274
	public void testObserveItemsOfList() throws Exception {
228
		List list = new List(shell, SWT.NONE);
275
		List list = new List(shell, SWT.NONE);
229
		IObservableList observableList = SWTObservables.observeItems(list);
276
		IObservableList observableList = SWTObservables.observeItems(list);
230
		assertNotNull(observableList);
277
		assertNotNull(observableList);
231
		assertTrue(observableList instanceof ListObservableList);
278
		ISWTObservable swtObservable = (ISWTObservable) observableList;
279
		assertTrue(swtObservable.getWidget() == list);
232
	}
280
	}
233
281
234
	public void testObserveItemsOfUnsupportedControl() throws Exception {
282
	public void testObserveItemsOfUnsupportedControl() throws Exception {
Lines 245-251 Link Here
245
		ISWTObservableValue value = SWTObservables
293
		ISWTObservableValue value = SWTObservables
246
				.observeSingleSelectionIndex(table);
294
				.observeSingleSelectionIndex(table);
247
		assertNotNull(value);
295
		assertNotNull(value);
248
		assertTrue(value instanceof TableSingleSelectionObservableValue);
296
		assertTrue(value.getWidget() == table);
297
		IPropertyObservable propertyObservable = getPropertyObservable(value);
298
		assertTrue(propertyObservable.getProperty() instanceof TableSingleSelectionIndexProperty);
249
	}
299
	}
250
300
251
	public void testObserveSingleSelectionIndexOfUnsupportedControl()
301
	public void testObserveSingleSelectionIndexOfUnsupportedControl()
Lines 258-282 Link Here
258
308
259
		}
309
		}
260
	}
310
	}
261
	
311
262
	public void testObserveMinOfSpinner() throws Exception {
312
	public void testObserveMinOfSpinner() throws Exception {
263
		Spinner spinner = new Spinner(shell, SWT.NONE);
313
		Spinner spinner = new Spinner(shell, SWT.NONE);
264
		ISWTObservableValue value = SWTObservables.observeMin(spinner);
314
		ISWTObservableValue value = SWTObservables.observeMin(spinner);
265
		assertNotNull(value);
315
		assertNotNull(value);
266
		assertTrue(value instanceof SpinnerObservableValue);
316
		assertTrue(value.getWidget() == spinner);
267
		
317
268
		SpinnerObservableValue spinnerObservable = (SpinnerObservableValue) value;
318
		IPropertyObservable propertyObservable = getPropertyObservable(value);
269
		assertEquals(SWTProperties.MIN, spinnerObservable.getAttribute());
319
		assertTrue(propertyObservable.getProperty() instanceof SpinnerMinimumProperty);
270
	}
320
	}
271
	
321
272
	public void testObserveMinOfScale() throws Exception {
322
	public void testObserveMinOfScale() throws Exception {
273
		Scale scale = new Scale(shell, SWT.NONE);
323
		Scale scale = new Scale(shell, SWT.NONE);
274
		ISWTObservableValue value = SWTObservables.observeMin(scale);
324
		ISWTObservableValue value = SWTObservables.observeMin(scale);
275
		assertNotNull(value);
325
		assertNotNull(value);
276
		assertTrue(value instanceof ScaleObservableValue);
326
		assertTrue(value.getWidget() == scale);
277
		
327
278
		ScaleObservableValue scaleObservable = (ScaleObservableValue) value;
328
		IPropertyObservable propertyObservable = getPropertyObservable(value);
279
		assertEquals(SWTProperties.MIN, scaleObservable.getAttribute());
329
		assertTrue(propertyObservable.getProperty() instanceof ScaleMinimumProperty);
280
	}
330
	}
281
331
282
	public void testObserveMinOfUnsupportedControl() throws Exception {
332
	public void testObserveMinOfUnsupportedControl() throws Exception {
Lines 284-313 Link Here
284
		try {
334
		try {
285
			SWTObservables.observeMin(text);
335
			SWTObservables.observeMin(text);
286
			fail("Exception should have been thrown");
336
			fail("Exception should have been thrown");
287
		} catch (IllegalArgumentException e) {	
337
		} catch (IllegalArgumentException e) {
288
		}
338
		}
289
	}
339
	}
290
	
340
291
	public void testObserveMaxOfSpinner() throws Exception {
341
	public void testObserveMaxOfSpinner() throws Exception {
292
		Spinner spinner = new Spinner(shell, SWT.NONE);
342
		Spinner spinner = new Spinner(shell, SWT.NONE);
293
		ISWTObservableValue value = SWTObservables.observeMax(spinner);
343
		ISWTObservableValue value = SWTObservables.observeMax(spinner);
294
		assertNotNull(value);
344
		assertNotNull(value);
295
		assertTrue(value instanceof SpinnerObservableValue);
345
		assertTrue(value.getWidget() == spinner);
296
		
346
297
		SpinnerObservableValue spinnerObservable = (SpinnerObservableValue) value;
347
		IPropertyObservable propertyObservable = getPropertyObservable(value);
298
		assertEquals(SWTProperties.MAX, spinnerObservable.getAttribute());
348
		assertTrue(propertyObservable.getProperty() instanceof SpinnerMaximumProperty);
299
	}
349
	}
300
	
350
301
	public void testObserveMaxOfScale() throws Exception {
351
	public void testObserveMaxOfScale() throws Exception {
302
		Scale scale = new Scale(shell, SWT.NONE);
352
		Scale scale = new Scale(shell, SWT.NONE);
303
		ISWTObservableValue value = SWTObservables.observeMax(scale);
353
		ISWTObservableValue value = SWTObservables.observeMax(scale);
304
		assertNotNull(value);
354
		assertNotNull(value);
305
		assertTrue(value instanceof ScaleObservableValue);
355
		assertTrue(value.getWidget() == scale);
306
		
356
307
		ScaleObservableValue scaleObservable = (ScaleObservableValue) value;
357
		IPropertyObservable propertyObservable = getPropertyObservable(value);
308
		assertEquals(SWTProperties.MAX, scaleObservable.getAttribute());
358
		assertTrue(propertyObservable.getProperty() instanceof ScaleMaximumProperty);
309
	}
359
	}
310
	
360
311
	public void testObserveMaxOfUnsupportedControl() throws Exception {
361
	public void testObserveMaxOfUnsupportedControl() throws Exception {
312
		Text text = new Text(shell, SWT.NONE);
362
		Text text = new Text(shell, SWT.NONE);
313
		try {
363
		try {
Lines 316-329 Link Here
316
		} catch (IllegalArgumentException e) {
366
		} catch (IllegalArgumentException e) {
317
		}
367
		}
318
	}
368
	}
319
	
369
320
	public void testObserveEditableOfText() throws Exception {
370
	public void testObserveEditableOfText() throws Exception {
321
		Text text = new Text(shell, SWT.NONE);
371
		Text text = new Text(shell, SWT.NONE);
322
		ISWTObservableValue value = SWTObservables.observeEditable(text);
372
		ISWTObservableValue value = SWTObservables.observeEditable(text);
323
		assertNotNull(value);
373
		assertNotNull(value);
324
		assertTrue(value instanceof TextEditableObservableValue);
374
		assertTrue(value.getWidget() == text);
375
		IPropertyObservable propertyObservable = getPropertyObservable(value);
376
		assertTrue(propertyObservable.getProperty() instanceof TextEditableProperty);
325
	}
377
	}
326
	
378
327
	public void testObserveEditableOfUnsupportedControl() throws Exception {
379
	public void testObserveEditableOfUnsupportedControl() throws Exception {
328
		Label label = new Label(shell, SWT.NONE);
380
		Label label = new Label(shell, SWT.NONE);
329
		try {
381
		try {
(-)src/org/eclipse/jface/tests/internal/databinding/swt/ScaleObservableValueMaxTest.java (-5 / +8 lines)
Lines 18-29 Link Here
18
import org.eclipse.core.databinding.observable.IObservable;
18
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.Realm;
19
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.property.PropertyObservables;
21
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
26
import org.eclipse.jface.databinding.swt.ScaleProperties;
26
import org.eclipse.jface.internal.databinding.swt.ScaleObservableValue;
27
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.Display;
29
import org.eclipse.swt.widgets.Scale;
29
import org.eclipse.swt.widgets.Scale;
Lines 73-81 Link Here
73
	}
73
	}
74
74
75
	public static Test suite() {
75
	public static Test suite() {
76
		TestSuite suite = new TestSuite(ScaleObservableValueMaxTest.class.toString());
76
		TestSuite suite = new TestSuite(ScaleObservableValueMaxTest.class
77
				.toString());
77
		suite.addTestSuite(ScaleObservableValueMaxTest.class);
78
		suite.addTestSuite(ScaleObservableValueMaxTest.class);
78
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
79
		suite.addTest(SWTMutableObservableValueContractTest
80
				.suite(new Delegate()));
79
		return suite;
81
		return suite;
80
	}
82
	}
81
83
Lines 96-102 Link Here
96
		}
98
		}
97
99
98
		public IObservableValue createObservableValue(Realm realm) {
100
		public IObservableValue createObservableValue(Realm realm) {
99
			return new ScaleObservableValue(realm, scale, SWTProperties.MAX);
101
			return PropertyObservables.observeValue(realm, scale,
102
					ScaleProperties.maximum());
100
		}
103
		}
101
104
102
		public void change(IObservable observable) {
105
		public void change(IObservable observable) {
(-)src/org/eclipse/jface/tests/internal/databinding/swt/TableSingleSelectionObservableValueTest.java (-16 / +23 lines)
Lines 18-29 Link Here
18
import org.eclipse.core.databinding.observable.IObservable;
18
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.Realm;
19
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.property.PropertyObservables;
21
import org.eclipse.core.runtime.Assert;
22
import org.eclipse.core.runtime.Assert;
22
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
23
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
24
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
25
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
25
import org.eclipse.jface.databinding.swt.SWTObservables;
26
import org.eclipse.jface.databinding.swt.SWTObservables;
26
import org.eclipse.jface.internal.databinding.swt.TableSingleSelectionObservableValue;
27
import org.eclipse.jface.databinding.swt.TableProperties;
27
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.widgets.Display;
29
import org.eclipse.swt.widgets.Display;
29
import org.eclipse.swt.widgets.Shell;
30
import org.eclipse.swt.widgets.Shell;
Lines 33-66 Link Here
33
/**
34
/**
34
 * @since 3.2
35
 * @since 3.2
35
 */
36
 */
36
public class TableSingleSelectionObservableValueTest extends ObservableDelegateTest {
37
public class TableSingleSelectionObservableValueTest extends
38
		ObservableDelegateTest {
37
	private Delegate delegate;
39
	private Delegate delegate;
38
	private IObservableValue observable;
40
	private IObservableValue observable;
39
	private Table table;
41
	private Table table;
40
	
42
41
	public TableSingleSelectionObservableValueTest() {
43
	public TableSingleSelectionObservableValueTest() {
42
		this(null);
44
		this(null);
43
	}
45
	}
44
	
46
45
	public TableSingleSelectionObservableValueTest(String testName) {
47
	public TableSingleSelectionObservableValueTest(String testName) {
46
		super(testName, new Delegate());
48
		super(testName, new Delegate());
47
	}
49
	}
48
	
50
49
	protected void setUp() throws Exception {
51
	protected void setUp() throws Exception {
50
		super.setUp();
52
		super.setUp();
51
		
53
52
		observable = (IObservableValue) getObservable();
54
		observable = (IObservableValue) getObservable();
53
		delegate = (Delegate) getObservableContractDelegate();
55
		delegate = (Delegate) getObservableContractDelegate();
54
		table = delegate.table;
56
		table = delegate.table;
55
	}
57
	}
56
	
58
57
	protected IObservable doCreateObservable() {
59
	protected IObservable doCreateObservable() {
58
		Delegate delegate = (Delegate) getObservableContractDelegate();
60
		Delegate delegate = (Delegate) getObservableContractDelegate();
59
		return delegate.createObservableValue(SWTObservables.getRealm(Display.getDefault()));
61
		return delegate.createObservableValue(SWTObservables.getRealm(Display
62
				.getDefault()));
60
	}
63
	}
61
	
64
62
	public void testSetValue() throws Exception {
65
	public void testSetValue() throws Exception {
63
		//preconditions
66
		// preconditions
64
		assertEquals(-1, table.getSelectionIndex());
67
		assertEquals(-1, table.getSelectionIndex());
65
		assertEquals(-1, ((Integer) observable.getValue()).intValue());
68
		assertEquals(-1, ((Integer) observable.getValue()).intValue());
66
69
Lines 70-88 Link Here
70
				.getSelectionIndex());
73
				.getSelectionIndex());
71
		assertEquals("observable value", value, observable.getValue());
74
		assertEquals("observable value", value, observable.getValue());
72
	}
75
	}
73
	
76
74
	public void testGetValue() throws Exception {
77
	public void testGetValue() throws Exception {
75
		int value = 1;
78
		int value = 1;
76
		table.setSelection(value);
79
		table.setSelection(value);
77
		
80
78
		assertEquals("table selection index", value, table.getSelectionIndex());
81
		assertEquals("table selection index", value, table.getSelectionIndex());
79
		assertEquals("observable value", new Integer(value), observable.getValue());
82
		assertEquals("observable value", new Integer(value), observable
83
				.getValue());
80
	}
84
	}
81
85
82
	public static Test suite() {
86
	public static Test suite() {
83
		TestSuite suite = new TestSuite(TableSingleSelectionObservableValueTest.class.toString());
87
		TestSuite suite = new TestSuite(
88
				TableSingleSelectionObservableValueTest.class.toString());
84
		suite.addTestSuite(TableSingleSelectionObservableValueTest.class);
89
		suite.addTestSuite(TableSingleSelectionObservableValueTest.class);
85
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
90
		suite.addTest(SWTMutableObservableValueContractTest
91
				.suite(new Delegate()));
86
		return suite;
92
		return suite;
87
	}
93
	}
88
94
Lines 104-110 Link Here
104
		}
110
		}
105
111
106
		public IObservableValue createObservableValue(Realm realm) {
112
		public IObservableValue createObservableValue(Realm realm) {
107
			return new TableSingleSelectionObservableValue(realm, table);
113
			return PropertyObservables.observeValue(realm, table,
114
					TableProperties.singleSelectionIndex());
108
		}
115
		}
109
116
110
		public Object getValueType(IObservableValue observable) {
117
		public Object getValueType(IObservableValue observable) {
(-)src/org/eclipse/jface/tests/internal/databinding/swt/CLabelObservableValueTest.java (-23 / +28 lines)
Lines 19-28 Link Here
19
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.IObservable;
20
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.core.databinding.property.PropertyObservables;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
25
import org.eclipse.jface.databinding.swt.CLabelProperties;
24
import org.eclipse.jface.databinding.swt.SWTObservables;
26
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.internal.databinding.swt.CLabelObservableValue;
26
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.custom.CLabel;
28
import org.eclipse.swt.custom.CLabel;
28
import org.eclipse.swt.widgets.Display;
29
import org.eclipse.swt.widgets.Display;
Lines 36-73 Link Here
36
	private Delegate delegate;
37
	private Delegate delegate;
37
	private IObservableValue observable;
38
	private IObservableValue observable;
38
	private CLabel label;
39
	private CLabel label;
39
	
40
40
	protected void setUp() throws Exception {
41
	protected void setUp() throws Exception {
41
		super.setUp();
42
		super.setUp();
42
		
43
43
		delegate = new Delegate();
44
		delegate = new Delegate();
44
		delegate.setUp();
45
		delegate.setUp();
45
		label = delegate.label;
46
		label = delegate.label;
46
		observable = delegate.createObservableValue(SWTObservables.getRealm(Display.getDefault()));
47
		observable = delegate.createObservableValue(SWTObservables
48
				.getRealm(Display.getDefault()));
47
	}
49
	}
48
	
50
49
	protected void tearDown() throws Exception {
51
	protected void tearDown() throws Exception {
50
		super.tearDown();
52
		super.tearDown();
51
		
53
52
		delegate.tearDown();
54
		delegate.tearDown();
53
		observable.dispose();
55
		observable.dispose();
54
	}
56
	}
55
	
57
56
    public void testSetValue() throws Exception {
58
	public void testSetValue() throws Exception {
57
    	//preconditions
59
		// preconditions
58
        assertEquals(null, label.getText());
60
		assertEquals(null, label.getText());
59
        assertEquals(null, observable.getValue());
61
		assertEquals(null, observable.getValue());
60
        
62
61
        String value = "value";
63
		String value = "value";
62
        observable.setValue(value);
64
		observable.setValue(value);
63
        assertEquals("label text", value, label.getText());
65
		assertEquals("label text", value, label.getText());
64
        assertEquals("observable value", value, observable.getValue());
66
		assertEquals("observable value", value, observable.getValue());
65
    }
67
	}
66
	
68
67
	public static Test suite() {
69
	public static Test suite() {
68
		TestSuite suite = new TestSuite(CLabelObservableValueTest.class.getName());
70
		TestSuite suite = new TestSuite(CLabelObservableValueTest.class
71
				.getName());
69
		suite.addTestSuite(CLabelObservableValueTest.class);
72
		suite.addTestSuite(CLabelObservableValueTest.class);
70
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
73
		suite.addTest(SWTMutableObservableValueContractTest
74
				.suite(new Delegate()));
71
		return suite;
75
		return suite;
72
	}
76
	}
73
77
Lines 87-104 Link Here
87
		}
91
		}
88
92
89
		public IObservableValue createObservableValue(Realm realm) {
93
		public IObservableValue createObservableValue(Realm realm) {
90
			return new CLabelObservableValue(realm, label);
94
			return PropertyObservables.observeValue(realm, label,
95
					CLabelProperties.text());
91
		}
96
		}
92
97
93
		public void change(IObservable observable) {
98
		public void change(IObservable observable) {
94
			IObservableValue value = (IObservableValue) observable;
99
			IObservableValue value = (IObservableValue) observable;
95
			value.setValue(value.getValue() + "a");
100
			value.setValue(value.getValue() + "a");
96
		}
101
		}
97
		
102
98
		public Object getValueType(IObservableValue observable) {
103
		public Object getValueType(IObservableValue observable) {
99
			return String.class;
104
			return String.class;
100
		}
105
		}
101
		
106
102
		public Object createValue(IObservableValue observable) {
107
		public Object createValue(IObservableValue observable) {
103
			return observable.getValue() + "a";
108
			return observable.getValue() + "a";
104
		}
109
		}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/SpinnerObservableValueTest.java (-4 / +3 lines)
Lines 13-20 Link Here
13
package org.eclipse.jface.tests.internal.databinding.swt;
13
package org.eclipse.jface.tests.internal.databinding.swt;
14
14
15
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
15
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
16
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
16
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
17
import org.eclipse.jface.internal.databinding.swt.SpinnerObservableValue;
17
import org.eclipse.jface.databinding.swt.SWTObservables;
18
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
18
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
19
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.widgets.Spinner;
20
import org.eclipse.swt.widgets.Spinner;
Lines 26-33 Link Here
26
public class SpinnerObservableValueTest extends AbstractSWTTestCase {
26
public class SpinnerObservableValueTest extends AbstractSWTTestCase {
27
	public void testDispose() throws Exception {
27
	public void testDispose() throws Exception {
28
		Spinner spinner = new Spinner(getShell(), SWT.NONE);
28
		Spinner spinner = new Spinner(getShell(), SWT.NONE);
29
		SpinnerObservableValue observableValue = new SpinnerObservableValue(
29
		ISWTObservableValue observableValue = SWTObservables.observeSelection(spinner);
30
				spinner, SWTProperties.SELECTION);
31
		ValueChangeEventTracker testCounterValueChangeListener = new ValueChangeEventTracker();
30
		ValueChangeEventTracker testCounterValueChangeListener = new ValueChangeEventTracker();
32
		observableValue.addValueChangeListener(testCounterValueChangeListener);
31
		observableValue.addValueChangeListener(testCounterValueChangeListener);
33
32
(-)src/org/eclipse/jface/tests/internal/databinding/swt/ScaleObservableValueSelectionTest.java (-6 / +8 lines)
Lines 18-29 Link Here
18
import org.eclipse.core.databinding.observable.IObservable;
18
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.Realm;
19
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.property.PropertyObservables;
21
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
26
import org.eclipse.jface.databinding.swt.ScaleProperties;
26
import org.eclipse.jface.internal.databinding.swt.ScaleObservableValue;
27
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.Display;
29
import org.eclipse.swt.widgets.Scale;
29
import org.eclipse.swt.widgets.Scale;
Lines 42-52 Link Here
42
	public ScaleObservableValueSelectionTest() {
42
	public ScaleObservableValueSelectionTest() {
43
		this(null);
43
		this(null);
44
	}
44
	}
45
	
45
46
	public ScaleObservableValueSelectionTest(String testName) {
46
	public ScaleObservableValueSelectionTest(String testName) {
47
		super(testName, new Delegate());
47
		super(testName, new Delegate());
48
	}
48
	}
49
	
49
50
	protected void setUp() throws Exception {
50
	protected void setUp() throws Exception {
51
		super.setUp();
51
		super.setUp();
52
52
Lines 56-62 Link Here
56
	}
56
	}
57
57
58
	protected IObservable doCreateObservable() {
58
	protected IObservable doCreateObservable() {
59
		return getObservableContractDelegate().createObservable(SWTObservables.getRealm(Display.getDefault()));
59
		return getObservableContractDelegate().createObservable(
60
				SWTObservables.getRealm(Display.getDefault()));
60
	}
61
	}
61
62
62
	public void testGetValue() throws Exception {
63
	public void testGetValue() throws Exception {
Lines 97-103 Link Here
97
		}
98
		}
98
99
99
		public IObservableValue createObservableValue(Realm realm) {
100
		public IObservableValue createObservableValue(Realm realm) {
100
			return new ScaleObservableValue(realm, scale, SWTProperties.SELECTION);
101
			return PropertyObservables.observeValue(realm, scale,
102
					ScaleProperties.selection());
101
		}
103
		}
102
104
103
		public void change(IObservable observable) {
105
		public void change(IObservable observable) {
(-)src/org/eclipse/jface/tests/internal/databinding/swt/DelayedObservableValueTest.java (-5 / +20 lines)
Lines 18-29 Link Here
18
import org.eclipse.core.databinding.observable.Diffs;
18
import org.eclipse.core.databinding.observable.Diffs;
19
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.IObservable;
20
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
25
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
24
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
26
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
27
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
25
import org.eclipse.jface.databinding.swt.SWTObservables;
28
import org.eclipse.jface.databinding.swt.SWTObservables;
26
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
27
import org.eclipse.jface.internal.databinding.swt.DelayedObservableValue;
29
import org.eclipse.jface.internal.databinding.swt.DelayedObservableValue;
28
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
30
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
29
import org.eclipse.swt.SWT;
31
import org.eclipse.swt.SWT;
Lines 55-60 Link Here
55
		newValue = new Object();
57
		newValue = new Object();
56
		target.setValue(oldValue);
58
		target.setValue(oldValue);
57
		delayed = new DelayedObservableValue(1, target);
59
		delayed = new DelayedObservableValue(1, target);
60
61
		// force listeners to be added to target
62
		ChangeEventTracker.observe(delayed);
58
	}
63
	}
59
64
60
	protected void tearDown() throws Exception {
65
	protected void tearDown() throws Exception {
Lines 193-206 Link Here
193
		assertEquals(newValue, tracker.event.diff.getNewValue());
198
		assertEquals(newValue, tracker.event.diff.getNewValue());
194
	}
199
	}
195
200
196
	static class SWTObservableValueStub extends AbstractSWTObservableValue {
201
	static class SWTObservableValueStub extends AbstractObservableValue
202
			implements ISWTObservableValue {
203
		private Widget widget;
204
197
		private Object value;
205
		private Object value;
198
		private boolean stale;
206
		private boolean stale;
199
207
200
		Object overrideValue;
208
		Object overrideValue;
201
209
202
		public SWTObservableValueStub(Realm realm, Widget widget) {
210
		public SWTObservableValueStub(Realm realm, Widget widget) {
203
			super(realm, widget);
211
			super(realm);
212
			this.widget = widget;
204
		}
213
		}
205
214
206
		protected Object doGetValue() {
215
		protected Object doGetValue() {
Lines 228-239 Link Here
228
		public boolean isStale() {
237
		public boolean isStale() {
229
			return stale;
238
			return stale;
230
		}
239
		}
240
241
		public Widget getWidget() {
242
			return widget;
243
		}
231
	}
244
	}
232
245
233
	public static Test suite() {
246
	public static Test suite() {
234
		TestSuite suite = new TestSuite(DelayedObservableValueTest.class.getName());
247
		TestSuite suite = new TestSuite(DelayedObservableValueTest.class
248
				.getName());
235
		suite.addTestSuite(DelayedObservableValueTest.class);
249
		suite.addTestSuite(DelayedObservableValueTest.class);
236
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
250
		suite.addTest(SWTMutableObservableValueContractTest
251
				.suite(new Delegate()));
237
		return suite;
252
		return suite;
238
	}
253
	}
239
254
(-)src/org/eclipse/jface/tests/internal/databinding/swt/SWTObservableListTest.java (-115 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 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 208858)
10
 *     Matthew Hall - bug 213145
11
 ******************************************************************************/
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
14
15
import junit.framework.Test;
16
import junit.framework.TestSuite;
17
18
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.IObservableCollection;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.jface.databinding.conformance.MutableObservableListContractTest;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
23
import org.eclipse.jface.internal.databinding.swt.SWTObservableList;
24
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
25
26
/**
27
 * @since 3.3
28
 */
29
public class SWTObservableListTest extends AbstractDefaultRealmTestCase {
30
	SWTObservableListStub list;
31
32
	protected void setUp() throws Exception {
33
		super.setUp();
34
		list = new SWTObservableListStub(Realm.getDefault(), 0);
35
	}
36
37
	public void testMove_ForwardAndBackward() {
38
		String element0 = "element0";
39
		String element1 = "element1";
40
41
		list.add(element0);
42
		list.add(element1);
43
44
		// move forward
45
		assertEquals(element0, list.move(0, 1));
46
		assertEquals(element1, list.move(0, 1));
47
48
		// move backward
49
		assertEquals(element1, list.move(1, 0));
50
		assertEquals(element0, list.move(1, 0));
51
	}
52
53
	public static Test suite() {
54
		TestSuite suite = new TestSuite(SWTObservableListTest.class.toString());
55
		suite.addTestSuite(SWTObservableListTest.class);
56
		suite.addTest(MutableObservableListContractTest.suite(new Delegate()));
57
		return suite;
58
	}
59
60
	static class Delegate extends AbstractObservableCollectionContractDelegate {
61
		public IObservableCollection createObservableCollection(Realm realm,
62
				int elementCount) {
63
			return new SWTObservableListStub(realm, elementCount);
64
		}
65
66
		private int counter;
67
68
		public Object createElement(IObservableCollection collection) {
69
			return "Item" + counter++;
70
		}
71
72
		public Object getElementType(IObservableCollection collection) {
73
			return String.class;
74
		}
75
76
		public void change(IObservable observable) {
77
			((SWTObservableListStub) observable).fireChange();
78
		}
79
	}
80
81
	static class SWTObservableListStub extends SWTObservableList {
82
		String[] items;
83
84
		public SWTObservableListStub(Realm realm, int elementCount) {
85
			super(realm);
86
			items = new String[elementCount];
87
			for (int i = 0; i < items.length; i++)
88
				items[i] = Integer.toString(i);
89
		}
90
91
		protected String getItem(int index) {
92
			return items[index];
93
		}
94
95
		protected int getItemCount() {
96
			return items.length;
97
		}
98
99
		protected String[] getItems() {
100
			return items;
101
		}
102
103
		protected void setItem(int index, String string) {
104
			items[index] = string;
105
		}
106
107
		protected void setItems(String[] newItems) {
108
			items = newItems;
109
		}
110
111
		protected void fireChange() {
112
			super.fireChange();
113
		}
114
	}
115
}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/TableObservableValueTest.java (-3 / +4 lines)
Lines 12-18 Link Here
12
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
13
package org.eclipse.jface.tests.internal.databinding.swt;
14
14
15
import org.eclipse.jface.internal.databinding.swt.TableSingleSelectionObservableValue;
15
import org.eclipse.core.databinding.observable.value.IObservableValue;
16
import org.eclipse.jface.databinding.swt.SWTObservables;
16
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
17
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.widgets.Table;
19
import org.eclipse.swt.widgets.Table;
Lines 24-31 Link Here
24
public class TableObservableValueTest extends AbstractSWTTestCase {
25
public class TableObservableValueTest extends AbstractSWTTestCase {
25
	public void testDispose() throws Exception {
26
	public void testDispose() throws Exception {
26
		Table table = new Table(getShell(), SWT.NONE);
27
		Table table = new Table(getShell(), SWT.NONE);
27
		TableSingleSelectionObservableValue observableValue = new TableSingleSelectionObservableValue(
28
		IObservableValue observableValue = SWTObservables
28
				table);
29
				.observeSingleSelectionIndex(table);
29
30
30
		TableItem item1 = new TableItem(table, SWT.NONE);
31
		TableItem item1 = new TableItem(table, SWT.NONE);
31
		item1.setText("Item1");
32
		item1.setText("Item1");
(-)src/org/eclipse/jface/tests/internal/databinding/swt/TextObservableValueFocusOutTest.java (-7 / +11 lines)
Lines 19-27 Link Here
19
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.IObservable;
20
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.core.databinding.property.PropertyObservables;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.internal.databinding.swt.TextObservableValue;
25
import org.eclipse.jface.databinding.swt.TextProperties;
25
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.widgets.Shell;
27
import org.eclipse.swt.widgets.Shell;
27
import org.eclipse.swt.widgets.Text;
28
import org.eclipse.swt.widgets.Text;
Lines 31-38 Link Here
31
 */
32
 */
32
public class TextObservableValueFocusOutTest extends TestCase {
33
public class TextObservableValueFocusOutTest extends TestCase {
33
	public static Test suite() {
34
	public static Test suite() {
34
		TestSuite suite = new TestSuite(TextObservableValueFocusOutTest.class.toString());
35
		TestSuite suite = new TestSuite(TextObservableValueFocusOutTest.class
35
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
36
				.toString());
37
		suite.addTest(SWTMutableObservableValueContractTest
38
				.suite(new Delegate()));
36
		return suite;
39
		return suite;
37
	}
40
	}
38
41
Lines 41-47 Link Here
41
		private Shell shell;
44
		private Shell shell;
42
45
43
		private Text text;
46
		private Text text;
44
		
47
45
		public void setUp() {
48
		public void setUp() {
46
			shell = new Shell();
49
			shell = new Shell();
47
			text = new Text(shell, SWT.NONE);
50
			text = new Text(shell, SWT.NONE);
Lines 52-58 Link Here
52
		}
55
		}
53
56
54
		public IObservableValue createObservableValue(Realm realm) {
57
		public IObservableValue createObservableValue(Realm realm) {
55
			return new TextObservableValue(realm, text, SWT.FocusOut);
58
			return PropertyObservables.observeValue(realm, text, TextProperties
59
					.text(SWT.FocusOut));
56
		}
60
		}
57
61
58
		public Object getValueType(IObservableValue observable) {
62
		public Object getValueType(IObservableValue observable) {
Lines 61-70 Link Here
61
65
62
		public void change(IObservable observable) {
66
		public void change(IObservable observable) {
63
			text.setFocus();
67
			text.setFocus();
64
			
68
65
			IObservableValue observableValue = (IObservableValue) observable;
69
			IObservableValue observableValue = (IObservableValue) observable;
66
			text.setText((String) createValue(observableValue));
70
			text.setText((String) createValue(observableValue));
67
			
71
68
			text.notifyListeners(SWT.FocusOut, null);
72
			text.notifyListeners(SWT.FocusOut, null);
69
		}
73
		}
70
74
(-)src/org/eclipse/jface/tests/internal/databinding/swt/TextEditableObservableValueTest.java (-27 / +35 lines)
Lines 18-27 Link Here
18
import org.eclipse.core.databinding.observable.IObservable;
18
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.Realm;
19
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.property.PropertyObservables;
21
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.internal.databinding.swt.TextEditableObservableValue;
25
import org.eclipse.jface.databinding.swt.TextProperties;
25
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.widgets.Shell;
27
import org.eclipse.swt.widgets.Shell;
27
import org.eclipse.swt.widgets.Text;
28
import org.eclipse.swt.widgets.Text;
Lines 29-37 Link Here
29
/**
30
/**
30
 * @since 1.1
31
 * @since 1.1
31
 */
32
 */
32
public class TextEditableObservableValueTest extends
33
public class TextEditableObservableValueTest extends ObservableDelegateTest {
33
		ObservableDelegateTest {
34
34
	
35
	private Delegate delegate;
35
	private Delegate delegate;
36
	private Text text;
36
	private Text text;
37
	private IObservableValue observable;
37
	private IObservableValue observable;
Lines 39-116 Link Here
39
	public TextEditableObservableValueTest() {
39
	public TextEditableObservableValueTest() {
40
		this(null);
40
		this(null);
41
	}
41
	}
42
	
42
43
	public TextEditableObservableValueTest(String testName) {
43
	public TextEditableObservableValueTest(String testName) {
44
		super(testName, new Delegate());
44
		super(testName, new Delegate());
45
	}
45
	}
46
46
47
	/* (non-Javadoc)
47
	/*
48
	 * @see org.eclipse.jface.conformance.databinding.ObservableDelegateTest#setUp()
48
	 * (non-Javadoc)
49
	 * 
50
	 * @see
51
	 * org.eclipse.jface.conformance.databinding.ObservableDelegateTest#setUp()
49
	 */
52
	 */
50
	protected void setUp() throws Exception {
53
	protected void setUp() throws Exception {
51
		super.setUp();
54
		super.setUp();
52
		
55
53
		delegate = (Delegate) getObservableContractDelegate();
56
		delegate = (Delegate) getObservableContractDelegate();
54
		observable = (IObservableValue) getObservable();
57
		observable = (IObservableValue) getObservable();
55
		text = delegate.text;
58
		text = delegate.text;
56
	}
59
	}
57
	
60
58
	protected IObservable doCreateObservable() {
61
	protected IObservable doCreateObservable() {
59
		return super.doCreateObservable();
62
		return super.doCreateObservable();
60
	}
63
	}
61
	
64
62
	public void testGetValue() throws Exception {
65
	public void testGetValue() throws Exception {
63
		text.setEditable(false);
66
		text.setEditable(false);
64
		assertEquals(Boolean.valueOf(text.getEditable()), observable.getValue());
67
		assertEquals(Boolean.valueOf(text.getEditable()), observable.getValue());
65
		
68
66
		text.setEditable(true);
69
		text.setEditable(true);
67
		assertEquals(Boolean.valueOf(text.getEditable()), observable.getValue());
70
		assertEquals(Boolean.valueOf(text.getEditable()), observable.getValue());
68
	}
71
	}
69
	
72
70
	public void testSetValue() throws Exception {
73
	public void testSetValue() throws Exception {
71
		text.setEditable(false);
74
		text.setEditable(false);
72
		observable.setValue(Boolean.TRUE);
75
		observable.setValue(Boolean.TRUE);
73
		assertEquals(Boolean.TRUE, Boolean.valueOf(text.getEditable()));
76
		assertEquals(Boolean.TRUE, Boolean.valueOf(text.getEditable()));
74
		
77
75
		observable.setValue(Boolean.FALSE);
78
		observable.setValue(Boolean.FALSE);
76
		assertEquals(Boolean.FALSE, Boolean.valueOf(text.getEditable()));
79
		assertEquals(Boolean.FALSE, Boolean.valueOf(text.getEditable()));
77
	}
80
	}
78
	
81
79
	public static Test suite() {
82
	public static Test suite() {
80
		TestSuite suite = new TestSuite(TextEditableObservableValueTest.class.toString());
83
		TestSuite suite = new TestSuite(TextEditableObservableValueTest.class
84
				.toString());
81
		suite.addTestSuite(TextEditableObservableValueTest.class);
85
		suite.addTestSuite(TextEditableObservableValueTest.class);
82
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
86
		suite.addTest(SWTMutableObservableValueContractTest
87
				.suite(new Delegate()));
83
		return suite;
88
		return suite;
84
	}
89
	}
85
	
90
86
	/*package*/ static class Delegate extends AbstractObservableValueContractDelegate {
91
	/* package */static class Delegate extends
92
			AbstractObservableValueContractDelegate {
87
		private Shell shell;
93
		private Shell shell;
88
		Text text;
94
		Text text;
89
		
95
90
		public void setUp() {			
96
		public void setUp() {
91
			shell = new Shell();
97
			shell = new Shell();
92
			text = new Text(shell, SWT.NONE);
98
			text = new Text(shell, SWT.NONE);
93
		}
99
		}
94
100
95
		public void tearDown() {			
101
		public void tearDown() {
96
			shell.dispose();
102
			shell.dispose();
97
		}
103
		}
98
		
104
99
		public IObservableValue createObservableValue(Realm realm) {
105
		public IObservableValue createObservableValue(Realm realm) {
100
			return new TextEditableObservableValue(realm, text);
106
			return PropertyObservables.observeValue(realm, text, TextProperties
107
					.editable());
101
		}
108
		}
102
		
109
103
		public Object getValueType(IObservableValue observable) {
110
		public Object getValueType(IObservableValue observable) {
104
			return Boolean.TYPE;
111
			return Boolean.TYPE;
105
		}
112
		}
106
		
113
107
		public void change(IObservable observable) {
114
		public void change(IObservable observable) {
108
			IObservableValue observableValue = (IObservableValue) observable;
115
			IObservableValue observableValue = (IObservableValue) observable;
109
			observableValue.setValue(createValue(observableValue));
116
			observableValue.setValue(createValue(observableValue));
110
		}
117
		}
111
		
118
112
		public Object createValue(IObservableValue observable) {
119
		public Object createValue(IObservableValue observable) {
113
			return (Boolean.TRUE.equals(observable.getValue()) ? Boolean.FALSE: Boolean.TRUE);
120
			return (Boolean.TRUE.equals(observable.getValue()) ? Boolean.FALSE
121
					: Boolean.TRUE);
114
		}
122
		}
115
	}
123
	}
116
}
124
}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/ControlObservableValueTest.java (-41 / +23 lines)
Lines 12-19 Link Here
12
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
13
package org.eclipse.jface.tests.internal.databinding.swt;
14
14
15
import org.eclipse.jface.internal.databinding.swt.ControlObservableValue;
15
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
16
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
16
import org.eclipse.jface.databinding.swt.SWTObservables;
17
import org.eclipse.jface.resource.JFaceResources;
17
import org.eclipse.jface.resource.JFaceResources;
18
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
18
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
19
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.SWT;
Lines 30-36 Link Here
30
30
31
	protected void setUp() throws Exception {
31
	protected void setUp() throws Exception {
32
		super.setUp();
32
		super.setUp();
33
		
33
34
		shell = new Shell();
34
		shell = new Shell();
35
	}
35
	}
36
36
Lines 47-95 Link Here
47
	}
47
	}
48
48
49
	public void testSetValueEnabled() throws Exception {
49
	public void testSetValueEnabled() throws Exception {
50
		ControlObservableValue observableValue = new ControlObservableValue(
50
		ISWTObservableValue observableValue = SWTObservables
51
				shell, SWTProperties.ENABLED);
51
				.observeEnabled(shell);
52
		Boolean value = Boolean.FALSE;
52
		Boolean value = Boolean.FALSE;
53
		observableValue.setValue(value);
53
		observableValue.setValue(value);
54
		assertFalse(shell.isEnabled());
54
		assertFalse(shell.isEnabled());
55
	}
55
	}
56
56
57
	public void testGetValueEnabled() throws Exception {
57
	public void testGetValueEnabled() throws Exception {
58
		ControlObservableValue value = new ControlObservableValue(shell,
58
		ISWTObservableValue value = SWTObservables.observeEnabled(shell);
59
				SWTProperties.ENABLED);
60
		shell.setEnabled(false);
59
		shell.setEnabled(false);
61
		assertEquals(Boolean.FALSE, value.getValue());
60
		assertEquals(Boolean.FALSE, value.getValue());
62
	}
61
	}
63
62
64
	public void testGetValueTypeEnabled() throws Exception {
63
	public void testGetValueTypeEnabled() throws Exception {
65
		ControlObservableValue value = new ControlObservableValue(shell,
64
		ISWTObservableValue value = SWTObservables.observeEnabled(shell);
66
				SWTProperties.ENABLED);
67
		assertEquals(boolean.class, value.getValueType());
65
		assertEquals(boolean.class, value.getValueType());
68
	}
66
	}
69
67
70
	public void testSetValueVisible() throws Exception {
68
	public void testSetValueVisible() throws Exception {
71
		ControlObservableValue value = new ControlObservableValue(shell,
69
		ISWTObservableValue value = SWTObservables.observeVisible(shell);
72
				SWTProperties.VISIBLE);
73
		value.setValue(Boolean.FALSE);
70
		value.setValue(Boolean.FALSE);
74
		assertFalse(shell.isVisible());
71
		assertFalse(shell.isVisible());
75
	}
72
	}
76
73
77
	public void testGetValueVisible() throws Exception {
74
	public void testGetValueVisible() throws Exception {
78
		ControlObservableValue value = new ControlObservableValue(shell,
75
		ISWTObservableValue value = SWTObservables.observeVisible(shell);
79
				SWTProperties.VISIBLE);
80
		shell.setVisible(false);
76
		shell.setVisible(false);
81
		assertEquals(Boolean.FALSE, value.getValue());
77
		assertEquals(Boolean.FALSE, value.getValue());
82
	}
78
	}
83
79
84
	public void testGetValueTypeVisible() throws Exception {
80
	public void testGetValueTypeVisible() throws Exception {
85
		ControlObservableValue value = new ControlObservableValue(shell,
81
		ISWTObservableValue value = SWTObservables.observeVisible(shell);
86
				SWTProperties.VISIBLE);
87
		assertEquals(Boolean.TYPE, value.getValueType());
82
		assertEquals(Boolean.TYPE, value.getValueType());
88
	}
83
	}
89
84
90
	public void testSetValueForeground() throws Exception {
85
	public void testSetValueForeground() throws Exception {
91
		ControlObservableValue value = new ControlObservableValue(shell,
86
		ISWTObservableValue value = SWTObservables.observeForeground(shell);
92
				SWTProperties.FOREGROUND);
93
87
94
		Color color = shell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
88
		Color color = shell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
95
89
Lines 98-105 Link Here
98
	}
92
	}
99
93
100
	public void testGetValueForeground() throws Exception {
94
	public void testGetValueForeground() throws Exception {
101
		ControlObservableValue value = new ControlObservableValue(shell,
95
		ISWTObservableValue value = SWTObservables.observeForeground(shell);
102
				SWTProperties.FOREGROUND);
103
96
104
		Color color = shell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
97
		Color color = shell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
105
		shell.setForeground(color);
98
		shell.setForeground(color);
Lines 107-120 Link Here
107
	}
100
	}
108
101
109
	public void testGetValueTypeForgroundColor() throws Exception {
102
	public void testGetValueTypeForgroundColor() throws Exception {
110
		ControlObservableValue value = new ControlObservableValue(shell,
103
		ISWTObservableValue value = SWTObservables.observeForeground(shell);
111
				SWTProperties.FOREGROUND);
112
		assertEquals(Color.class, value.getValueType());
104
		assertEquals(Color.class, value.getValueType());
113
	}
105
	}
114
106
115
	public void testGetValueBackground() throws Exception {
107
	public void testGetValueBackground() throws Exception {
116
		ControlObservableValue value = new ControlObservableValue(shell,
108
		ISWTObservableValue value = SWTObservables.observeBackground(shell);
117
				SWTProperties.BACKGROUND);
118
109
119
		Color color = shell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
110
		Color color = shell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
120
		shell.setBackground(color);
111
		shell.setBackground(color);
Lines 122-129 Link Here
122
	}
113
	}
123
114
124
	public void testSetValueBackground() throws Exception {
115
	public void testSetValueBackground() throws Exception {
125
		ControlObservableValue value = new ControlObservableValue(shell,
116
		ISWTObservableValue value = SWTObservables.observeBackground(shell);
126
				SWTProperties.BACKGROUND);
127
117
128
		Color color = shell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
118
		Color color = shell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
129
119
Lines 132-151 Link Here
132
	}
122
	}
133
123
134
	public void testGetValueTypeBackgroundColor() throws Exception {
124
	public void testGetValueTypeBackgroundColor() throws Exception {
135
		ControlObservableValue value = new ControlObservableValue(shell,
125
		ISWTObservableValue value = SWTObservables.observeBackground(shell);
136
				SWTProperties.BACKGROUND);
137
		assertEquals(Color.class, value.getValueType());
126
		assertEquals(Color.class, value.getValueType());
138
	}
127
	}
139
128
140
	public void testGetValueTypeTooltip() throws Exception {
129
	public void testGetValueTypeTooltip() throws Exception {
141
		ControlObservableValue value = new ControlObservableValue(shell,
130
		ISWTObservableValue value = SWTObservables.observeTooltipText(shell);
142
				SWTProperties.TOOLTIP_TEXT);
143
		assertEquals(String.class, value.getValueType());
131
		assertEquals(String.class, value.getValueType());
144
	}
132
	}
145
133
146
	public void testSetValueFont() throws Exception {
134
	public void testSetValueFont() throws Exception {
147
		ControlObservableValue value = new ControlObservableValue(shell,
135
		ISWTObservableValue value = SWTObservables.observeFont(shell);
148
				SWTProperties.FONT);
149
136
150
		Font font = JFaceResources.getDialogFont();
137
		Font font = JFaceResources.getDialogFont();
151
138
Lines 154-161 Link Here
154
	}
141
	}
155
142
156
	public void testGetValueFont() throws Exception {
143
	public void testGetValueFont() throws Exception {
157
		ControlObservableValue value = new ControlObservableValue(shell,
144
		ISWTObservableValue value = SWTObservables.observeFont(shell);
158
				SWTProperties.FONT);
159
145
160
		Font font = JFaceResources.getDialogFont();
146
		Font font = JFaceResources.getDialogFont();
161
		shell.setFont(font);
147
		shell.setFont(font);
Lines 163-192 Link Here
163
	}
149
	}
164
150
165
	public void testGetValueTypeFont() throws Exception {
151
	public void testGetValueTypeFont() throws Exception {
166
		ControlObservableValue value = new ControlObservableValue(shell,
152
		ISWTObservableValue value = SWTObservables.observeFont(shell);
167
				SWTProperties.FONT);
168
		assertEquals(Font.class, value.getValueType());
153
		assertEquals(Font.class, value.getValueType());
169
	}
154
	}
170
155
171
	public void testSetValueTooltipText() throws Exception {
156
	public void testSetValueTooltipText() throws Exception {
172
		ControlObservableValue value = new ControlObservableValue(shell,
157
		ISWTObservableValue value = SWTObservables.observeTooltipText(shell);
173
				SWTProperties.TOOLTIP_TEXT);
174
		String text = "text";
158
		String text = "text";
175
		value.setValue(text);
159
		value.setValue(text);
176
		assertEquals(text, shell.getToolTipText());
160
		assertEquals(text, shell.getToolTipText());
177
	}
161
	}
178
162
179
	public void testGetValueTooltipText() throws Exception {
163
	public void testGetValueTooltipText() throws Exception {
180
		ControlObservableValue value = new ControlObservableValue(shell,
164
		ISWTObservableValue value = SWTObservables.observeTooltipText(shell);
181
				SWTProperties.TOOLTIP_TEXT);
182
		String text = "text";
165
		String text = "text";
183
		shell.setToolTipText(text);
166
		shell.setToolTipText(text);
184
		assertEquals(text, value.getValue());
167
		assertEquals(text, value.getValue());
185
	}
168
	}
186
169
187
	public void testGetValueTypeTooltipText() throws Exception {
170
	public void testGetValueTypeTooltipText() throws Exception {
188
		ControlObservableValue value = new ControlObservableValue(shell,
171
		ISWTObservableValue value = SWTObservables.observeTooltipText(shell);
189
				SWTProperties.TOOLTIP_TEXT);
190
		assertEquals(String.class, value.getValueType());
172
		assertEquals(String.class, value.getValueType());
191
	}
173
	}
192
}
174
}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/LabelObservableValueTest.java (-27 / +32 lines)
Lines 18-28 Link Here
18
import org.eclipse.core.databinding.observable.IObservable;
18
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.Realm;
19
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.property.PropertyObservables;
21
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
25
import org.eclipse.jface.databinding.swt.LabelProperties;
24
import org.eclipse.jface.databinding.swt.SWTObservables;
26
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.internal.databinding.swt.LabelObservableValue;
26
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.Label;
29
import org.eclipse.swt.widgets.Label;
Lines 35-77 Link Here
35
	private Delegate delegate;
36
	private Delegate delegate;
36
	private IObservableValue observable;
37
	private IObservableValue observable;
37
	private Label label;
38
	private Label label;
38
	
39
39
	public LabelObservableValueTest() {
40
	public LabelObservableValueTest() {
40
		this(null);
41
		this(null);
41
	}
42
	}
42
	
43
43
	public LabelObservableValueTest(String testName) {
44
	public LabelObservableValueTest(String testName) {
44
		super(testName, new Delegate());
45
		super(testName, new Delegate());
45
	}
46
	}
46
	
47
47
	protected void setUp() throws Exception {
48
	protected void setUp() throws Exception {
48
		super.setUp();
49
		super.setUp();
49
		
50
50
		delegate = (Delegate) getObservableContractDelegate();
51
		delegate = (Delegate) getObservableContractDelegate();
51
		observable = (IObservableValue) getObservable();
52
		observable = (IObservableValue) getObservable();
52
		label = delegate.label;
53
		label = delegate.label;
53
	}
54
	}
54
	
55
55
	protected IObservable doCreateObservable() {
56
	protected IObservable doCreateObservable() {
56
		return getObservableContractDelegate().createObservable(SWTObservables.getRealm(Display.getDefault()));
57
		return getObservableContractDelegate().createObservable(
58
				SWTObservables.getRealm(Display.getDefault()));
57
	}
59
	}
58
	
60
59
    public void testSetValue() throws Exception {
61
	public void testSetValue() throws Exception {
60
    	//preconditions
62
		// preconditions
61
        assertEquals("", label.getText());
63
		assertEquals("", label.getText());
62
        assertEquals("", observable.getValue());
64
		assertEquals("", observable.getValue());
63
        
65
64
        String value = "value";
66
		String value = "value";
65
        observable.setValue(value);
67
		observable.setValue(value);
66
        assertEquals("label text", value, label.getText());
68
		assertEquals("label text", value, label.getText());
67
        assertEquals("observable value", value, observable.getValue());
69
		assertEquals("observable value", value, observable.getValue());
68
    }
70
	}
69
    
71
70
    public static Test suite() {
72
	public static Test suite() {
71
    	TestSuite suite = new TestSuite(LabelObservableValueTest.class.toString());
73
		TestSuite suite = new TestSuite(LabelObservableValueTest.class
72
    	suite.addTestSuite(LabelObservableValueTest.class);
74
				.toString());
73
    	suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
75
		suite.addTestSuite(LabelObservableValueTest.class);
74
    	return suite;
76
		suite.addTest(SWTMutableObservableValueContractTest
77
				.suite(new Delegate()));
78
		return suite;
75
	}
79
	}
76
80
77
	/* package */static class Delegate extends
81
	/* package */static class Delegate extends
Lines 90-107 Link Here
90
		}
94
		}
91
95
92
		public IObservableValue createObservableValue(Realm realm) {
96
		public IObservableValue createObservableValue(Realm realm) {
93
			return new LabelObservableValue(realm, label);
97
			return PropertyObservables.observeValue(realm, label,
98
					LabelProperties.text());
94
		}
99
		}
95
100
96
		public void change(IObservable observable) {
101
		public void change(IObservable observable) {
97
			IObservableValue value = (IObservableValue) observable;
102
			IObservableValue value = (IObservableValue) observable;
98
			value.setValue(value.getValue() + "a");
103
			value.setValue(value.getValue() + "a");
99
		}
104
		}
100
		
105
101
		public Object getValueType(IObservableValue observable) {
106
		public Object getValueType(IObservableValue observable) {
102
			return String.class;
107
			return String.class;
103
		}
108
		}
104
		
109
105
		public Object createValue(IObservableValue observable) {
110
		public Object createValue(IObservableValue observable) {
106
			return observable.getValue() + "a";
111
			return observable.getValue() + "a";
107
		}
112
		}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/TextObservableValueTest.java (-16 / +26 lines)
Lines 14-21 Link Here
14
14
15
package org.eclipse.jface.tests.internal.databinding.swt;
15
package org.eclipse.jface.tests.internal.databinding.swt;
16
16
17
import org.eclipse.core.databinding.observable.value.IObservableValue;
18
import org.eclipse.core.databinding.property.PropertyObservables;
17
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
19
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
18
import org.eclipse.jface.internal.databinding.swt.TextObservableValue;
20
import org.eclipse.jface.databinding.swt.TextProperties;
19
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
21
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
20
import org.eclipse.swt.SWT;
22
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.widgets.Shell;
23
import org.eclipse.swt.widgets.Shell;
Lines 35-41 Link Here
35
37
36
		Shell shell = new Shell();
38
		Shell shell = new Shell();
37
		text = new Text(shell, SWT.NONE);
39
		text = new Text(shell, SWT.NONE);
38
		
40
39
		listener = new ValueChangeEventTracker();
41
		listener = new ValueChangeEventTracker();
40
	}
42
	}
41
43
Lines 45-92 Link Here
45
	 */
47
	 */
46
	public void testConstructorUpdateEventTypes() {
48
	public void testConstructorUpdateEventTypes() {
47
		try {
49
		try {
48
			new TextObservableValue(text, SWT.NONE);
50
			TextProperties.text(SWT.None);
49
			new TextObservableValue(text, SWT.FocusOut);
51
			TextProperties.text(SWT.FocusOut);
50
			new TextObservableValue(text, SWT.Modify);
52
			TextProperties.text(SWT.Modify);
51
			assertTrue(true);
53
			assertTrue(true);
52
		} catch (IllegalArgumentException e) {
54
		} catch (IllegalArgumentException e) {
53
			fail();
55
			fail();
54
		}
56
		}
55
57
56
		try {
58
		try {
57
			new TextObservableValue(text, SWT.Verify);
59
			TextProperties.text(SWT.Verify);
58
			fail();
60
			fail();
59
		} catch (IllegalArgumentException e) {
61
		} catch (IllegalArgumentException e) {
60
			assertTrue(true);
62
			assertTrue(true);
61
		}
63
		}
62
	}
64
	}
63
	
65
64
	/**
66
	/**
65
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=171132
67
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=171132
66
	 * 
68
	 * 
67
	 * @throws Exception
69
	 * @throws Exception
68
	 */
70
	 */
69
	public void testGetValueBeforeFocusOutChangeEventsFire() throws Exception {
71
	public void testGetValueBeforeFocusOutChangeEventsFire() throws Exception {
70
		TextObservableValue observableValue = new TextObservableValue(text, SWT.FocusOut);
72
		IObservableValue observableValue = PropertyObservables.observeValue(
73
				text, TextProperties.text(SWT.FocusOut));
71
		observableValue.addValueChangeListener(listener);
74
		observableValue.addValueChangeListener(listener);
72
		
75
73
		String a = "a";
76
		String a = "a";
74
		String b = "b";
77
		String b = "b";
75
		
78
76
		text.setText(a);
79
		text.setText(a);
77
		assertEquals(a, observableValue.getValue()); //fetch the value updating the buffered value
80
78
		
81
		assertEquals(0, listener.count);
82
		assertEquals(a, observableValue.getValue()); // fetch the value updating
83
		// the buffered value
84
		assertEquals(1, listener.count);
85
79
		text.setText(b);
86
		text.setText(b);
80
		text.notifyListeners(SWT.FocusOut, null);
87
81
		
82
		assertEquals(1, listener.count);
88
		assertEquals(1, listener.count);
89
90
		text.notifyListeners(SWT.FocusOut, null);
91
92
		assertEquals(2, listener.count);
83
		assertEquals(a, listener.event.diff.getOldValue());
93
		assertEquals(a, listener.event.diff.getOldValue());
84
		assertEquals(b, listener.event.diff.getNewValue());
94
		assertEquals(b, listener.event.diff.getNewValue());
85
	}
95
	}
86
96
87
	public void testDispose() throws Exception {
97
	public void testDispose() throws Exception {
88
		TextObservableValue observableValue = new TextObservableValue(text,
98
		IObservableValue observableValue = PropertyObservables.observeValue(
89
				SWT.Modify);
99
				text, TextProperties.text(SWT.Modify));
90
		ValueChangeEventTracker testCounterValueChangeListener = new ValueChangeEventTracker();
100
		ValueChangeEventTracker testCounterValueChangeListener = new ValueChangeEventTracker();
91
		observableValue.addValueChangeListener(testCounterValueChangeListener);
101
		observableValue.addValueChangeListener(testCounterValueChangeListener);
92
102
(-)src/org/eclipse/jface/tests/internal/databinding/swt/CComboSingleSelectionObservableValueTest.java (-15 / +18 lines)
Lines 19-28 Link Here
19
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.IObservable;
20
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.core.databinding.property.PropertyObservables;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.swt.ISWTObservable;
25
import org.eclipse.jface.databinding.swt.CComboProperties;
25
import org.eclipse.jface.internal.databinding.swt.CComboSingleSelectionObservableValue;
26
import org.eclipse.jface.databinding.swt.SWTObservables;
26
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
27
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
27
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.custom.CCombo;
29
import org.eclipse.swt.custom.CCombo;
Lines 31-41 Link Here
31
/**
32
/**
32
 * @since 3.2
33
 * @since 3.2
33
 */
34
 */
34
public class CComboSingleSelectionObservableValueTest extends AbstractSWTTestCase {
35
public class CComboSingleSelectionObservableValueTest extends
36
		AbstractSWTTestCase {
35
	public void testSetValue() throws Exception {
37
	public void testSetValue() throws Exception {
36
		CCombo combo = new CCombo(getShell(), SWT.NONE);
38
		CCombo combo = new CCombo(getShell(), SWT.NONE);
37
		CComboSingleSelectionObservableValue observableValue = new CComboSingleSelectionObservableValue(
39
		IObservableValue observableValue = SWTObservables
38
				combo);
40
				.observeSingleSelectionIndex(combo);
39
		combo.add("Item1");
41
		combo.add("Item1");
40
		combo.add("Item2");
42
		combo.add("Item2");
41
43
Lines 52-60 Link Here
52
	}
54
	}
53
55
54
	public static Test suite() {
56
	public static Test suite() {
55
		TestSuite suite = new TestSuite(CComboSingleSelectionObservableValueTest.class.getName());
57
		TestSuite suite = new TestSuite(
58
				CComboSingleSelectionObservableValueTest.class.getName());
56
		suite.addTestSuite(CComboSingleSelectionObservableValueTest.class);
59
		suite.addTestSuite(CComboSingleSelectionObservableValueTest.class);
57
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
60
		suite.addTest(SWTMutableObservableValueContractTest
61
				.suite(new Delegate()));
58
		return suite;
62
		return suite;
59
	}
63
	}
60
64
Lines 75-87 Link Here
75
		}
79
		}
76
80
77
		public IObservableValue createObservableValue(Realm realm) {
81
		public IObservableValue createObservableValue(Realm realm) {
78
			return new CComboSingleSelectionObservableValue(realm, combo);
82
			return PropertyObservables.observeValue(realm, combo,
83
					CComboProperties.singleSelectionIndex());
79
		}
84
		}
80
85
81
		public void change(IObservable observable) {
86
		public void change(IObservable observable) {
82
			int index = _createValue((IObservableValue) observable);
87
			IObservableValue value = (IObservableValue)observable;
83
			combo.select(index);
88
			value.setValue(createValue(value));
84
			combo.notifyListeners(SWT.Selection, null);
85
		}
89
		}
86
90
87
		public Object getValueType(IObservableValue observable) {
91
		public Object getValueType(IObservableValue observable) {
Lines 91-102 Link Here
91
		public Object createValue(IObservableValue observable) {
95
		public Object createValue(IObservableValue observable) {
92
			return new Integer(_createValue(observable));
96
			return new Integer(_createValue(observable));
93
		}
97
		}
94
		
98
95
		private int _createValue(IObservableValue observable) {
99
		private int _createValue(IObservableValue observable) {
96
			CCombo combo = ((CCombo) ((ISWTObservable) observable).getWidget());
97
			int value = Math.max(0, combo.getSelectionIndex());
100
			int value = Math.max(0, combo.getSelectionIndex());
98
			
101
99
			//returns either 0 or 1 depending upon current value
102
			// returns either 0 or 1 depending upon current value
100
			return Math.abs(value - 1);
103
			return Math.abs(value - 1);
101
		}
104
		}
102
	}
105
	}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/SpinnerObservableValueMinTest.java (-9 / +13 lines)
Lines 18-29 Link Here
18
import org.eclipse.core.databinding.observable.IObservable;
18
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.Realm;
19
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.property.PropertyObservables;
21
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
26
import org.eclipse.jface.databinding.swt.SpinnerProperties;
26
import org.eclipse.jface.internal.databinding.swt.SpinnerObservableValue;
27
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.Display;
29
import org.eclipse.swt.widgets.Shell;
29
import org.eclipse.swt.widgets.Shell;
Lines 46-62 Link Here
46
	public SpinnerObservableValueMinTest(String testName) {
46
	public SpinnerObservableValueMinTest(String testName) {
47
		super(testName, new Delegate());
47
		super(testName, new Delegate());
48
	}
48
	}
49
	
49
50
	protected void setUp() throws Exception {
50
	protected void setUp() throws Exception {
51
		super.setUp();
51
		super.setUp();
52
		
52
53
		delegate = (Delegate) getObservableContractDelegate();
53
		delegate = (Delegate) getObservableContractDelegate();
54
		observable = (IObservableValue) getObservable();
54
		observable = (IObservableValue) getObservable();
55
		spinner = delegate.spinner;
55
		spinner = delegate.spinner;
56
	}
56
	}
57
	
57
58
	protected IObservable doCreateObservable() {
58
	protected IObservable doCreateObservable() {
59
		return getObservableContractDelegate().createObservable(SWTObservables.getRealm(Display.getDefault()));
59
		return getObservableContractDelegate().createObservable(
60
				SWTObservables.getRealm(Display.getDefault()));
60
	}
61
	}
61
62
62
	public void testGetValue() throws Exception {
63
	public void testGetValue() throws Exception {
Lines 72-80 Link Here
72
	}
73
	}
73
74
74
	public static Test suite() {
75
	public static Test suite() {
75
		TestSuite suite = new TestSuite(SpinnerObservableValueMinTest.class.toString());
76
		TestSuite suite = new TestSuite(SpinnerObservableValueMinTest.class
77
				.toString());
76
		suite.addTestSuite(SpinnerObservableValueMinTest.class);
78
		suite.addTestSuite(SpinnerObservableValueMinTest.class);
77
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
79
		suite.addTest(SWTMutableObservableValueContractTest
80
				.suite(new Delegate()));
78
		return suite;
81
		return suite;
79
	}
82
	}
80
83
Lines 95-101 Link Here
95
		}
98
		}
96
99
97
		public IObservableValue createObservableValue(Realm realm) {
100
		public IObservableValue createObservableValue(Realm realm) {
98
			return new SpinnerObservableValue(realm, spinner, SWTProperties.MIN);
101
			return PropertyObservables.observeValue(realm, spinner,
102
					SpinnerProperties.minimum());
99
		}
103
		}
100
104
101
		public void change(IObservable observable) {
105
		public void change(IObservable observable) {
(-)src/org/eclipse/jface/tests/internal/databinding/swt/SpinnerObservableValueMaxTest.java (-9 / +13 lines)
Lines 18-29 Link Here
18
import org.eclipse.core.databinding.observable.IObservable;
18
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.Realm;
19
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.property.PropertyObservables;
21
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
26
import org.eclipse.jface.databinding.swt.SpinnerProperties;
26
import org.eclipse.jface.internal.databinding.swt.SpinnerObservableValue;
27
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.Display;
29
import org.eclipse.swt.widgets.Shell;
29
import org.eclipse.swt.widgets.Shell;
Lines 46-52 Link Here
46
	public SpinnerObservableValueMaxTest(String testName) {
46
	public SpinnerObservableValueMaxTest(String testName) {
47
		super(testName, new Delegate());
47
		super(testName, new Delegate());
48
	}
48
	}
49
	
49
50
	protected void setUp() throws Exception {
50
	protected void setUp() throws Exception {
51
		super.setUp();
51
		super.setUp();
52
52
Lines 54-62 Link Here
54
		observable = (IObservableValue) getObservable();
54
		observable = (IObservableValue) getObservable();
55
		spinner = delegate.spinner;
55
		spinner = delegate.spinner;
56
	}
56
	}
57
	
57
58
	protected IObservable doCreateObservable() {
58
	protected IObservable doCreateObservable() {
59
		return getObservableContractDelegate().createObservable(SWTObservables.getRealm(Display.getDefault()));
59
		return getObservableContractDelegate().createObservable(
60
				SWTObservables.getRealm(Display.getDefault()));
60
	}
61
	}
61
62
62
	public void testGetValue() throws Exception {
63
	public void testGetValue() throws Exception {
Lines 70-80 Link Here
70
		observable.setValue(new Integer(max));
71
		observable.setValue(new Integer(max));
71
		assertEquals(max, spinner.getMaximum());
72
		assertEquals(max, spinner.getMaximum());
72
	}
73
	}
73
	
74
74
	public static Test suite() {
75
	public static Test suite() {
75
		TestSuite suite = new TestSuite(SpinnerObservableValueMaxTest.class.toString());
76
		TestSuite suite = new TestSuite(SpinnerObservableValueMaxTest.class
77
				.toString());
76
		suite.addTestSuite(SpinnerObservableValueMaxTest.class);
78
		suite.addTestSuite(SpinnerObservableValueMaxTest.class);
77
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
79
		suite.addTest(SWTMutableObservableValueContractTest
80
				.suite(new Delegate()));
78
		return suite;
81
		return suite;
79
	}
82
	}
80
83
Lines 95-101 Link Here
95
		}
98
		}
96
99
97
		public IObservableValue createObservableValue(Realm realm) {
100
		public IObservableValue createObservableValue(Realm realm) {
98
			return new SpinnerObservableValue(realm, spinner, SWTProperties.MAX);
101
			return PropertyObservables.observeValue(realm, spinner,
102
					SpinnerProperties.maximum());
99
		}
103
		}
100
104
101
		public void change(IObservable observable) {
105
		public void change(IObservable observable) {
(-)src/org/eclipse/jface/tests/internal/databinding/swt/ButtonObservableValueTest.java (-23 / +27 lines)
Lines 19-28 Link Here
19
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.IObservable;
20
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.core.databinding.property.PropertyObservables;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
25
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
25
import org.eclipse.jface.internal.databinding.swt.ButtonObservableValue;
26
import org.eclipse.jface.databinding.swt.ButtonProperties;
27
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
28
import org.eclipse.jface.databinding.swt.SWTObservables;
26
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
29
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
27
import org.eclipse.swt.SWT;
30
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.widgets.Button;
31
import org.eclipse.swt.widgets.Button;
Lines 33-54 Link Here
33
 */
36
 */
34
public class ButtonObservableValueTest extends AbstractSWTTestCase {
37
public class ButtonObservableValueTest extends AbstractSWTTestCase {
35
	private Button button;
38
	private Button button;
36
	private ButtonObservableValue observableValue;
39
	private ISWTObservableValue observableValue;
37
	private ValueChangeEventTracker listener;
40
	private ValueChangeEventTracker listener;
38
	
41
39
	/* (non-Javadoc)
40
	 * @see junit.framework.TestCase#setUp()
41
	 */
42
	protected void setUp() throws Exception {
42
	protected void setUp() throws Exception {
43
		super.setUp();
43
		super.setUp();
44
		
44
45
		Shell shell = getShell();
45
		Shell shell = getShell();
46
		button = new Button(shell, SWT.CHECK);
46
		button = new Button(shell, SWT.CHECK);
47
		observableValue = new ButtonObservableValue(
47
		observableValue = SWTObservables.observeSelection(button);
48
				button);
49
		listener = new ValueChangeEventTracker();
48
		listener = new ValueChangeEventTracker();
50
	}
49
	}
51
	
50
52
	public void testSelection_ChangeNotifiesObservable() throws Exception {
51
	public void testSelection_ChangeNotifiesObservable() throws Exception {
53
		observableValue.addValueChangeListener(listener);
52
		observableValue.addValueChangeListener(listener);
54
		button.setSelection(true);
53
		button.setSelection(true);
Lines 60-78 Link Here
60
		assertEquals("Selection event should notify observable.", 1,
59
		assertEquals("Selection event should notify observable.", 1,
61
				listener.count);
60
				listener.count);
62
	}
61
	}
63
	
62
64
	public void testSelection_NoChange() throws Exception {
63
	public void testSelection_NoChange() throws Exception {
65
		button.setSelection(true);
64
		button.setSelection(true);
66
		button.notifyListeners(SWT.Selection, null);
65
		button.notifyListeners(SWT.Selection, null);
67
		observableValue.addValueChangeListener(listener);
66
		observableValue.addValueChangeListener(listener);
68
		
67
69
		//precondition
68
		// precondition
70
		assertEquals(0, listener.count);
69
		assertEquals(0, listener.count);
71
		
70
72
		button.notifyListeners(SWT.Selection, null);
71
		button.notifyListeners(SWT.Selection, null);
73
		assertEquals("Value did not change.  Listeners should not have been notified.", 0, listener.count);
72
		assertEquals(
73
				"Value did not change.  Listeners should not have been notified.",
74
				0, listener.count);
74
	}
75
	}
75
	
76
76
	public void testSetValue_NullConvertedToFalse() {
77
	public void testSetValue_NullConvertedToFalse() {
77
		button.setSelection(true);
78
		button.setSelection(true);
78
		assertEquals(Boolean.TRUE, observableValue.getValue());
79
		assertEquals(Boolean.TRUE, observableValue.getValue());
Lines 104-112 Link Here
104
	}
105
	}
105
106
106
	public static Test suite() {
107
	public static Test suite() {
107
		TestSuite suite = new TestSuite(ButtonObservableValueTest.class.getName());
108
		TestSuite suite = new TestSuite(ButtonObservableValueTest.class
109
				.getName());
108
		suite.addTestSuite(ButtonObservableValueTest.class);
110
		suite.addTestSuite(ButtonObservableValueTest.class);
109
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
111
		suite.addTest(SWTMutableObservableValueContractTest
112
				.suite(new Delegate()));
110
		return suite;
113
		return suite;
111
	}
114
	}
112
115
Lines 130-136 Link Here
130
		}
133
		}
131
134
132
		public IObservableValue createObservableValue(Realm realm) {
135
		public IObservableValue createObservableValue(Realm realm) {
133
			return new ButtonObservableValue(realm, button);
136
			return PropertyObservables.observeValue(realm, button,
137
					ButtonProperties.selection());
134
		}
138
		}
135
139
136
		public Object getValueType(IObservableValue observable) {
140
		public Object getValueType(IObservableValue observable) {
Lines 138-151 Link Here
138
		}
142
		}
139
143
140
		public void change(IObservable observable) {
144
		public void change(IObservable observable) {
141
			button.setSelection(changeValue(button));
145
			((IObservableValue) observable).setValue(Boolean
142
			button.notifyListeners(SWT.Selection, null);
146
					.valueOf(changeValue(button)));
143
		}
147
		}
144
		
148
145
		public Object createValue(IObservableValue observable) {
149
		public Object createValue(IObservableValue observable) {
146
			return Boolean.valueOf(changeValue(button));
150
			return Boolean.valueOf(changeValue(button));
147
		}
151
		}
148
		
152
149
		private boolean changeValue(Button button) {
153
		private boolean changeValue(Button button) {
150
			return !button.getSelection();
154
			return !button.getSelection();
151
		}
155
		}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/ScaleObservableValueMinTest.java (-3 / +3 lines)
Lines 18-29 Link Here
18
import org.eclipse.core.databinding.observable.IObservable;
18
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.Realm;
19
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.property.PropertyObservables;
21
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
26
import org.eclipse.jface.databinding.swt.ScaleProperties;
26
import org.eclipse.jface.internal.databinding.swt.ScaleObservableValue;
27
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.Display;
29
import org.eclipse.swt.widgets.Scale;
29
import org.eclipse.swt.widgets.Scale;
Lines 95-101 Link Here
95
		}
95
		}
96
96
97
		public IObservableValue createObservableValue(Realm realm) {
97
		public IObservableValue createObservableValue(Realm realm) {
98
			return new ScaleObservableValue(realm, scale, SWTProperties.MIN);
98
			return PropertyObservables.observeValue(realm, scale, ScaleProperties.minimum());
99
		}
99
		}
100
100
101
		public void change(IObservable observable) {
101
		public void change(IObservable observable) {
(-)src/org/eclipse/jface/tests/internal/databinding/swt/ListSingleSelectionObservableValueTest.java (-6 / +7 lines)
Lines 11-30 Link Here
11
11
12
package org.eclipse.jface.tests.internal.databinding.swt;
12
package org.eclipse.jface.tests.internal.databinding.swt;
13
13
14
import org.eclipse.jface.internal.databinding.swt.ListSingleSelectionObservableValue;
14
import org.eclipse.core.databinding.observable.value.IObservableValue;
15
import org.eclipse.jface.databinding.swt.SWTObservables;
15
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
16
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
16
import org.eclipse.swt.SWT;
17
import org.eclipse.swt.SWT;
17
import org.eclipse.swt.widgets.List;
18
import org.eclipse.swt.widgets.List;
18
19
19
/**
20
/**
20
 * @since 3.2
21
 * @since 3.2
21
 *
22
 * 
22
 */
23
 */
23
public class ListSingleSelectionObservableValueTest extends AbstractSWTTestCase {
24
public class ListSingleSelectionObservableValueTest extends AbstractSWTTestCase {
24
	public void testSetValue() throws Exception {
25
	public void testSetValue() throws Exception {
25
		List list = new List(getShell(), SWT.NONE);
26
		List list = new List(getShell(), SWT.NONE);
26
		ListSingleSelectionObservableValue observableValue = new ListSingleSelectionObservableValue(
27
		IObservableValue observableValue = SWTObservables
27
				list);
28
				.observeSingleSelectionIndex(list);
28
		list.add("Item1");
29
		list.add("Item1");
29
30
30
		assertEquals(-1, list.getSelectionIndex());
31
		assertEquals(-1, list.getSelectionIndex());
Lines 39-46 Link Here
39
40
40
	public void testDispose() throws Exception {
41
	public void testDispose() throws Exception {
41
		List list = new List(getShell(), SWT.NONE);
42
		List list = new List(getShell(), SWT.NONE);
42
		ListSingleSelectionObservableValue observableValue = new ListSingleSelectionObservableValue(
43
		IObservableValue observableValue = SWTObservables
43
				list);
44
				.observeSingleSelectionIndex(list);
44
		list.add("Item1");
45
		list.add("Item1");
45
		list.add("Item2");
46
		list.add("Item2");
46
47
(-)src/org/eclipse/jface/tests/internal/databinding/swt/ShellObservableValueTest.java (-6 / +11 lines)
Lines 18-27 Link Here
18
import org.eclipse.core.databinding.observable.IObservable;
18
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.Realm;
19
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.property.PropertyObservables;
21
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
22
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
23
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
24
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
24
import org.eclipse.jface.internal.databinding.swt.ShellObservableValue;
25
import org.eclipse.jface.databinding.swt.SWTObservables;
26
import org.eclipse.jface.databinding.swt.ShellProperties;
25
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
27
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
26
import org.eclipse.swt.widgets.Shell;
28
import org.eclipse.swt.widgets.Shell;
27
29
Lines 34-46 Link Here
34
	String oldValue;
36
	String oldValue;
35
	String newValue;
37
	String newValue;
36
	Shell shell;
38
	Shell shell;
37
	ShellObservableValue observable;
39
	IObservableValue observable;
38
	ValueChangeEventTracker tracker;
40
	ValueChangeEventTracker tracker;
39
41
40
	protected void setUp() throws Exception {
42
	protected void setUp() throws Exception {
41
		super.setUp();
43
		super.setUp();
42
		shell = new Shell();
44
		shell = new Shell();
43
		observable = new ShellObservableValue(shell);
45
		observable = SWTObservables.observeText(shell);
44
		oldValue = "old";
46
		oldValue = "old";
45
		newValue = "new";
47
		newValue = "new";
46
		shell.setText(oldValue);
48
		shell.setText(oldValue);
Lines 88-96 Link Here
88
	}
90
	}
89
91
90
	public static Test suite() {
92
	public static Test suite() {
91
		TestSuite suite = new TestSuite(ShellObservableValueTest.class.toString());
93
		TestSuite suite = new TestSuite(ShellObservableValueTest.class
94
				.toString());
92
		suite.addTestSuite(ShellObservableValueTest.class);
95
		suite.addTestSuite(ShellObservableValueTest.class);
93
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
96
		suite.addTest(SWTMutableObservableValueContractTest
97
				.suite(new Delegate()));
94
		return suite;
98
		return suite;
95
	}
99
	}
96
100
Lines 109-115 Link Here
109
		}
113
		}
110
114
111
		public IObservableValue createObservableValue(Realm realm) {
115
		public IObservableValue createObservableValue(Realm realm) {
112
			return new ShellObservableValue(realm, shell);
116
			return PropertyObservables.observeValue(realm, shell,
117
					ShellProperties.text());
113
		}
118
		}
114
119
115
		public Object getValueType(IObservableValue observable) {
120
		public Object getValueType(IObservableValue observable) {
(-)src/org/eclipse/jface/tests/internal/databinding/swt/ComboSingleSelectionObservableValueTest.java (-4 / +5 lines)
Lines 10-30 Link Here
10
 ******************************************************************************/
10
 ******************************************************************************/
11
package org.eclipse.jface.tests.internal.databinding.swt;
11
package org.eclipse.jface.tests.internal.databinding.swt;
12
12
13
import org.eclipse.jface.internal.databinding.swt.ComboSingleSelectionObservableValue;
13
import org.eclipse.core.databinding.observable.value.IObservableValue;
14
import org.eclipse.jface.databinding.swt.SWTObservables;
14
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
15
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.widgets.Combo;
17
import org.eclipse.swt.widgets.Combo;
17
18
18
/**
19
/**
19
 * @since 3.2
20
 * @since 3.2
20
 *
21
 * 
21
 */
22
 */
22
public class ComboSingleSelectionObservableValueTest extends
23
public class ComboSingleSelectionObservableValueTest extends
23
		AbstractSWTTestCase {
24
		AbstractSWTTestCase {
24
	public void testSetValue() throws Exception {
25
	public void testSetValue() throws Exception {
25
		Combo combo = new Combo(getShell(), SWT.NONE);
26
		Combo combo = new Combo(getShell(), SWT.NONE);
26
		ComboSingleSelectionObservableValue observableValue = new ComboSingleSelectionObservableValue(
27
		IObservableValue observableValue = SWTObservables
27
				combo);
28
				.observeSingleSelectionIndex(combo);
28
		combo.add("Item1");
29
		combo.add("Item1");
29
		combo.add("Item2");
30
		combo.add("Item2");
30
31
(-)src/org/eclipse/jface/tests/internal/databinding/swt/CComboObservableValueTextTest.java (-8 / +12 lines)
Lines 19-31 Link Here
19
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.IObservable;
20
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.core.databinding.property.PropertyObservables;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
25
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
25
import org.eclipse.jface.databinding.swt.ISWTObservable;
26
import org.eclipse.jface.databinding.swt.CComboProperties;
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 66-74 Link Here
66
	}
66
	}
67
67
68
	public static Test suite() {
68
	public static Test suite() {
69
		TestSuite suite = new TestSuite(CComboObservableValueTextTest.class.getName());
69
		TestSuite suite = new TestSuite(CComboObservableValueTextTest.class
70
				.getName());
70
		suite.addTestSuite(CComboObservableValueTextTest.class);
71
		suite.addTestSuite(CComboObservableValueTextTest.class);
71
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
72
		suite.addTest(SWTMutableObservableValueContractTest
73
				.suite(new Delegate()));
72
		return suite;
74
		return suite;
73
	}
75
	}
74
76
Lines 88-99 Link Here
88
		}
90
		}
89
91
90
		public IObservableValue createObservableValue(Realm realm) {
92
		public IObservableValue createObservableValue(Realm realm) {
91
			return new CComboObservableValue(realm, combo, SWTProperties.TEXT);
93
			return new SWTObservableValueDecorator(
94
					PropertyObservables.observeValue(realm, combo,
95
							CComboProperties.text()), combo);
92
		}
96
		}
93
97
94
		public void change(IObservable observable) {
98
		public void change(IObservable observable) {
95
			CCombo combo = (CCombo) ((ISWTObservable) observable).getWidget();
99
			IObservableValue ov = (IObservableValue) observable;
96
			combo.setText(combo.getText() + "a");
100
			ov.setValue(createValue(ov));
97
		}
101
		}
98
102
99
		public Object getValueType(IObservableValue observable) {
103
		public Object getValueType(IObservableValue observable) {
(-)src/org/eclipse/jface/tests/internal/databinding/swt/ComboObservableValueTextTest.java (-8 / +12 lines)
Lines 19-31 Link Here
19
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.IObservable;
20
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.core.databinding.property.PropertyObservables;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
25
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
25
import org.eclipse.jface.databinding.swt.ISWTObservable;
26
import org.eclipse.jface.databinding.swt.ComboProperties;
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 67-75 Link Here
67
	}
67
	}
68
68
69
	public static Test suite() {
69
	public static Test suite() {
70
		TestSuite suite = new TestSuite(ComboObservableValueTextTest.class.toString());
70
		TestSuite suite = new TestSuite(ComboObservableValueTextTest.class
71
				.toString());
71
		suite.addTestSuite(ComboObservableValueTextTest.class);
72
		suite.addTestSuite(ComboObservableValueTextTest.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 89-100 Link Here
89
		}
91
		}
90
92
91
		public IObservableValue createObservableValue(Realm realm) {
93
		public IObservableValue createObservableValue(Realm realm) {
92
			return new ComboObservableValue(realm, combo, SWTProperties.TEXT);
94
			return new SWTObservableValueDecorator(
95
					PropertyObservables.observeValue(realm, combo,
96
							ComboProperties.text()), combo);
93
		}
97
		}
94
98
95
		public void change(IObservable observable) {
99
		public void change(IObservable observable) {
96
			Combo combo = (Combo) ((ISWTObservable) observable).getWidget();
100
			((IObservableValue) observable)
97
			combo.setText(combo.getText() + "a");
101
					.setValue(createValue((IObservableValue) observable));
98
		}
102
		}
99
103
100
		public Object getValueType(IObservableValue observable) {
104
		public Object getValueType(IObservableValue observable) {
(-)src/org/eclipse/jface/tests/internal/databinding/swt/SpinnerObservableValueSelectionTest.java (-10 / +13 lines)
Lines 18-29 Link Here
18
import org.eclipse.core.databinding.observable.IObservable;
18
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.Realm;
19
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.property.PropertyObservables;
21
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.ObservableDelegateTest;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
26
import org.eclipse.jface.databinding.swt.SpinnerProperties;
26
import org.eclipse.jface.internal.databinding.swt.SpinnerObservableValue;
27
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.SWT;
28
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.Display;
29
import org.eclipse.swt.widgets.Shell;
29
import org.eclipse.swt.widgets.Shell;
Lines 38-44 Link Here
38
	private Spinner spinner;
38
	private Spinner spinner;
39
39
40
	private IObservableValue observable;
40
	private IObservableValue observable;
41
	
41
42
	public SpinnerObservableValueSelectionTest() {
42
	public SpinnerObservableValueSelectionTest() {
43
		this(null);
43
		this(null);
44
	}
44
	}
Lines 56-62 Link Here
56
	}
56
	}
57
57
58
	protected IObservable doCreateObservable() {
58
	protected IObservable doCreateObservable() {
59
		return getObservableContractDelegate().createObservable(SWTObservables.getRealm(Display.getDefault()));
59
		return getObservableContractDelegate().createObservable(
60
				SWTObservables.getRealm(Display.getDefault()));
60
	}
61
	}
61
62
62
	public void testGetValue() throws Exception {
63
	public void testGetValue() throws Exception {
Lines 72-80 Link Here
72
	}
73
	}
73
74
74
	public static Test suite() {
75
	public static Test suite() {
75
		TestSuite suite = new TestSuite(SpinnerObservableValueSelectionTest.class.toString());
76
		TestSuite suite = new TestSuite(
77
				SpinnerObservableValueSelectionTest.class.toString());
76
		suite.addTestSuite(SpinnerObservableValueSelectionTest.class);
78
		suite.addTestSuite(SpinnerObservableValueSelectionTest.class);
77
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
79
		suite.addTest(SWTMutableObservableValueContractTest
80
				.suite(new Delegate()));
78
		return suite;
81
		return suite;
79
	}
82
	}
80
83
Lines 95-107 Link Here
95
		}
98
		}
96
99
97
		public IObservableValue createObservableValue(Realm realm) {
100
		public IObservableValue createObservableValue(Realm realm) {
98
			return new SpinnerObservableValue(realm, spinner, SWTProperties.SELECTION);
101
			return PropertyObservables.observeValue(realm, spinner,
102
					SpinnerProperties.selection());
99
		}
103
		}
100
104
101
		public void change(IObservable observable) {
105
		public void change(IObservable observable) {
102
			spinner
106
			spinner.setSelection(createIntegerValue(
103
					.setSelection(createIntegerValue(
107
					(IObservableValue) observable).intValue());
104
							(IObservableValue) observable).intValue());
105
			spinner.notifyListeners(SWT.Selection, null);
108
			spinner.notifyListeners(SWT.Selection, null);
106
		}
109
		}
107
110
(-)src/org/eclipse/jface/tests/internal/databinding/swt/ComboObservableValueTest.java (-16 / +21 lines)
Lines 12-20 Link Here
12
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
13
package org.eclipse.jface.tests.internal.databinding.swt;
14
14
15
import org.eclipse.core.databinding.observable.value.IObservableValue;
16
import org.eclipse.core.databinding.property.IValueProperty;
17
import org.eclipse.core.databinding.property.PropertyObservables;
15
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
18
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
16
import org.eclipse.jface.internal.databinding.swt.ComboObservableValue;
19
import org.eclipse.jface.databinding.swt.ComboProperties;
17
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
20
import org.eclipse.jface.databinding.swt.SWTObservables;
21
import org.eclipse.jface.internal.databinding.swt.ComboSelectionProperty;
22
import org.eclipse.jface.internal.databinding.swt.ComboTextProperty;
18
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
23
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
19
import org.eclipse.swt.SWT;
24
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.widgets.Combo;
25
import org.eclipse.swt.widgets.Combo;
Lines 26-33 Link Here
26
public class ComboObservableValueTest extends AbstractSWTTestCase {
31
public class ComboObservableValueTest extends AbstractSWTTestCase {
27
	public void testDispose() throws Exception {
32
	public void testDispose() throws Exception {
28
		Combo combo = new Combo(getShell(), SWT.NONE);
33
		Combo combo = new Combo(getShell(), SWT.NONE);
29
		ComboObservableValue observableValue = new ComboObservableValue(combo,
34
		IObservableValue observableValue = SWTObservables.observeText(combo);
30
				SWTProperties.TEXT);
31
		ValueChangeEventTracker testCounterValueChangeListener = new ValueChangeEventTracker();
35
		ValueChangeEventTracker testCounterValueChangeListener = new ValueChangeEventTracker();
32
		observableValue.addValueChangeListener(testCounterValueChangeListener);
36
		observableValue.addValueChangeListener(testCounterValueChangeListener);
33
37
Lines 51-75 Link Here
51
	}
55
	}
52
56
53
	public void testSetValueWithNull() {
57
	public void testSetValueWithNull() {
54
		testSetValueWithNull(SWTProperties.TEXT);
58
		testSetValueWithNull(ComboProperties.text());
55
		testSetValueWithNull(SWTProperties.SELECTION);
59
		testSetValueWithNull(ComboProperties.selection());
56
	}
60
	}
57
61
58
	protected void testSetValueWithNull(String observableMode) {
62
	protected void testSetValueWithNull(IValueProperty property) {
59
		Combo combo = new Combo(getShell(), SWT.NONE);
63
		Combo combo = new Combo(getShell(), SWT.NONE);
60
		combo.setItems(new String[] {"one", "two", "three"});
64
		combo.setItems(new String[] { "one", "two", "three" });
61
		ComboObservableValue observable = new ComboObservableValue(
65
		IObservableValue observable = PropertyObservables.observeValue(combo,
62
				combo, observableMode);
66
				property);
63
67
64
		observable.doSetValue("two");
68
		observable.setValue("two");
65
		assertEquals("two", combo.getText());
69
		assertEquals("two", combo.getText());
66
		if (observableMode.equals(SWTProperties.SELECTION)) {
70
		if (property instanceof ComboSelectionProperty) {
67
			assertEquals("expect selection at index 1 in mode " + observableMode, 1, combo.getSelectionIndex());
71
			assertEquals("expect selection at index 1 in selection mode", 1,
72
					combo.getSelectionIndex());
68
		}
73
		}
69
74
70
		if (observableMode.equals(SWTProperties.TEXT)) {
75
		if (property instanceof ComboTextProperty) {
71
			observable.doSetValue(null);
76
			observable.setValue(null);
72
			assertEquals("expect empty text in mode " + observableMode, "", combo.getText());
77
			assertEquals("expect empty text in text mode", "", combo.getText());
73
		}
78
		}
74
	}
79
	}
75
}
80
}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/CComboObservableValueSelectionTest.java (-11 / +14 lines)
Lines 19-31 Link Here
19
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.IObservable;
20
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.core.databinding.property.PropertyObservables;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
25
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
26
import org.eclipse.jface.databinding.swt.CComboProperties;
25
import org.eclipse.jface.databinding.swt.ISWTObservable;
27
import org.eclipse.jface.databinding.swt.ISWTObservable;
26
import org.eclipse.jface.databinding.swt.SWTObservables;
28
import org.eclipse.jface.databinding.swt.SWTObservables;
27
import org.eclipse.jface.internal.databinding.swt.CComboObservableValue;
29
import org.eclipse.jface.internal.databinding.swt.SWTObservableValueDecorator;
28
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
29
import org.eclipse.swt.SWT;
30
import org.eclipse.swt.SWT;
30
import org.eclipse.swt.custom.CCombo;
31
import org.eclipse.swt.custom.CCombo;
31
import org.eclipse.swt.widgets.Display;
32
import org.eclipse.swt.widgets.Display;
Lines 57-72 Link Here
57
		IObservableValue observable = (IObservableValue) delegate
58
		IObservableValue observable = (IObservableValue) delegate
58
				.createObservable(SWTObservables.getRealm(Display.getDefault()));
59
				.createObservable(SWTObservables.getRealm(Display.getDefault()));
59
60
60
		ValueChangeEventTracker listener = ValueChangeEventTracker.observe(observable);
61
		ValueChangeEventTracker listener = ValueChangeEventTracker
62
				.observe(observable);
61
		combo.select(0);
63
		combo.select(0);
62
64
63
		assertEquals("Observable was not notified.", 1, listener.count);
65
		assertEquals("Observable was not notified.", 1, listener.count);
64
	}
66
	}
65
67
66
	public static Test suite() {
68
	public static Test suite() {
67
		TestSuite suite = new TestSuite(CComboObservableValueSelectionTest.class.getName());
69
		TestSuite suite = new TestSuite(
70
				CComboObservableValueSelectionTest.class.getName());
68
		suite.addTestSuite(CComboObservableValueSelectionTest.class);
71
		suite.addTestSuite(CComboObservableValueSelectionTest.class);
69
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
72
		suite.addTest(SWTMutableObservableValueContractTest
73
				.suite(new Delegate()));
70
		return suite;
74
		return suite;
71
	}
75
	}
72
76
Lines 88-102 Link Here
88
		}
92
		}
89
93
90
		public IObservableValue createObservableValue(Realm realm) {
94
		public IObservableValue createObservableValue(Realm realm) {
91
			return new CComboObservableValue(realm, combo,
95
			return new SWTObservableValueDecorator(PropertyObservables
92
					SWTProperties.SELECTION);
96
					.observeValue(realm, combo, CComboProperties
97
							.selection()), combo);
93
		}
98
		}
94
99
95
		public void change(IObservable observable) {
100
		public void change(IObservable observable) {
96
			int index = combo
101
			IObservableValue ov = (IObservableValue) observable;
97
					.indexOf((String) createValue((IObservableValue) observable));
102
			ov.setValue(createValue(ov));
98
99
			combo.select(index);
100
		}
103
		}
101
104
102
		public Object getValueType(IObservableValue observable) {
105
		public Object getValueType(IObservableValue observable) {
(-)src/org/eclipse/jface/tests/internal/databinding/swt/CComboObservableValueTest.java (-12 / +15 lines)
Lines 13-21 Link Here
13
13
14
package org.eclipse.jface.tests.internal.databinding.swt;
14
package org.eclipse.jface.tests.internal.databinding.swt;
15
15
16
import org.eclipse.core.databinding.observable.value.IObservableValue;
17
import org.eclipse.core.databinding.property.IValueProperty;
18
import org.eclipse.core.databinding.property.PropertyObservables;
16
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
19
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
17
import org.eclipse.jface.internal.databinding.swt.CComboObservableValue;
20
import org.eclipse.jface.databinding.swt.CComboProperties;
18
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
21
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
22
import org.eclipse.jface.databinding.swt.SWTObservables;
19
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
23
import org.eclipse.jface.tests.databinding.AbstractSWTTestCase;
20
import org.eclipse.swt.SWT;
24
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.custom.CCombo;
25
import org.eclipse.swt.custom.CCombo;
Lines 26-33 Link Here
26
public class CComboObservableValueTest extends AbstractSWTTestCase {
30
public class CComboObservableValueTest extends AbstractSWTTestCase {
27
	public void testDispose() throws Exception {
31
	public void testDispose() throws Exception {
28
		CCombo combo = new CCombo(getShell(), SWT.NONE);
32
		CCombo combo = new CCombo(getShell(), SWT.NONE);
29
		CComboObservableValue observableValue = new CComboObservableValue(
33
		ISWTObservableValue observableValue = SWTObservables.observeText(combo);
30
				combo, SWTProperties.TEXT);
31
34
32
		ValueChangeEventTracker testCounterValueChangeListener = new ValueChangeEventTracker();
35
		ValueChangeEventTracker testCounterValueChangeListener = new ValueChangeEventTracker();
33
		observableValue.addValueChangeListener(testCounterValueChangeListener);
36
		observableValue.addValueChangeListener(testCounterValueChangeListener);
Lines 52-72 Link Here
52
	}
55
	}
53
56
54
	public void testSetValueWithNull() {
57
	public void testSetValueWithNull() {
55
		testSetValueWithNull(SWTProperties.TEXT);
58
		testSetValueWithNull(CComboProperties.text());
56
		testSetValueWithNull(SWTProperties.SELECTION);
59
		testSetValueWithNull(CComboProperties.selection());
57
	}
60
	}
58
61
59
	protected void testSetValueWithNull(String observableMode) {
62
	protected void testSetValueWithNull(IValueProperty property) {
60
		CCombo combo = new CCombo(getShell(), SWT.NONE);
63
		CCombo combo = new CCombo(getShell(), SWT.NONE);
61
		combo.setItems(new String[] {"one", "two", "three"});
64
		combo.setItems(new String[] { "one", "two", "three" });
62
		CComboObservableValue observable = new CComboObservableValue(
65
		IObservableValue observable = PropertyObservables.observeValue(
63
				combo, observableMode);
66
				combo, property);
64
67
65
		observable.doSetValue("two");
68
		observable.setValue("two");
66
		assertEquals("two", combo.getText());
69
		assertEquals("two", combo.getText());
67
		assertEquals(1, combo.getSelectionIndex());
70
		assertEquals(1, combo.getSelectionIndex());
68
71
69
		observable.doSetValue(null);
72
		observable.setValue(null);
70
		assertEquals("", combo.getText());
73
		assertEquals("", combo.getText());
71
		assertEquals(-1, combo.getSelectionIndex());
74
		assertEquals(-1, combo.getSelectionIndex());
72
	}
75
	}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/TextObservableValueModifyTest.java (-6 / +10 lines)
Lines 19-27 Link Here
19
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.IObservable;
20
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.core.databinding.property.PropertyObservables;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.internal.databinding.swt.TextObservableValue;
25
import org.eclipse.jface.databinding.swt.TextProperties;
25
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.SWT;
26
import org.eclipse.swt.widgets.Shell;
27
import org.eclipse.swt.widgets.Shell;
27
import org.eclipse.swt.widgets.Text;
28
import org.eclipse.swt.widgets.Text;
Lines 31-38 Link Here
31
 */
32
 */
32
public class TextObservableValueModifyTest extends TestCase {
33
public class TextObservableValueModifyTest extends TestCase {
33
	public static Test suite() {
34
	public static Test suite() {
34
		TestSuite suite = new TestSuite(TextObservableValueModifyTest.class.toString());
35
		TestSuite suite = new TestSuite(TextObservableValueModifyTest.class
35
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
36
				.toString());
37
		suite.addTest(SWTMutableObservableValueContractTest
38
				.suite(new Delegate()));
36
		return suite;
39
		return suite;
37
	}
40
	}
38
41
Lines 41-47 Link Here
41
		private Shell shell;
44
		private Shell shell;
42
45
43
		private Text text;
46
		private Text text;
44
		
47
45
		public void setUp() {
48
		public void setUp() {
46
			shell = new Shell();
49
			shell = new Shell();
47
			text = new Text(shell, SWT.NONE);
50
			text = new Text(shell, SWT.NONE);
Lines 52-58 Link Here
52
		}
55
		}
53
56
54
		public IObservableValue createObservableValue(Realm realm) {
57
		public IObservableValue createObservableValue(Realm realm) {
55
			return new TextObservableValue(realm, text, SWT.Modify);
58
			return PropertyObservables.observeValue(realm, text, TextProperties
59
					.text(SWT.Modify));
56
		}
60
		}
57
61
58
		public Object getValueType(IObservableValue observable) {
62
		public Object getValueType(IObservableValue observable) {
Lines 61-67 Link Here
61
65
62
		public void change(IObservable observable) {
66
		public void change(IObservable observable) {
63
			text.setFocus();
67
			text.setFocus();
64
			
68
65
			IObservableValue observableValue = (IObservableValue) observable;
69
			IObservableValue observableValue = (IObservableValue) observable;
66
			text.setText((String) createValue(observableValue));
70
			text.setText((String) createValue(observableValue));
67
		}
71
		}
(-)src/org/eclipse/jface/tests/internal/databinding/swt/ComboObservableValueSelectionTest.java (-9 / +12 lines)
Lines 19-31 Link Here
19
import org.eclipse.core.databinding.observable.IObservable;
19
import org.eclipse.core.databinding.observable.IObservable;
20
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.core.databinding.property.PropertyObservables;
22
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
23
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.swt.SWTMutableObservableValueContractTest;
24
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
25
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
26
import org.eclipse.jface.databinding.swt.ComboProperties;
25
import org.eclipse.jface.databinding.swt.ISWTObservable;
27
import org.eclipse.jface.databinding.swt.ISWTObservable;
26
import org.eclipse.jface.databinding.swt.SWTObservables;
28
import org.eclipse.jface.databinding.swt.SWTObservables;
27
import org.eclipse.jface.internal.databinding.swt.ComboObservableValue;
29
import org.eclipse.jface.internal.databinding.swt.SWTObservableValueDecorator;
28
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
29
import org.eclipse.swt.SWT;
30
import org.eclipse.swt.SWT;
30
import org.eclipse.swt.widgets.Combo;
31
import org.eclipse.swt.widgets.Combo;
31
import org.eclipse.swt.widgets.Display;
32
import org.eclipse.swt.widgets.Display;
Lines 33-39 Link Here
33
34
34
/**
35
/**
35
 * @since 3.2
36
 * @since 3.2
36
 *
37
 * 
37
 */
38
 */
38
public class ComboObservableValueSelectionTest extends TestCase {
39
public class ComboObservableValueSelectionTest extends TestCase {
39
	private Delegate delegate;
40
	private Delegate delegate;
Lines 67-75 Link Here
67
	}
68
	}
68
69
69
	public static Test suite() {
70
	public static Test suite() {
70
		TestSuite suite = new TestSuite(ComboObservableValueSelectionTest.class.toString());
71
		TestSuite suite = new TestSuite(ComboObservableValueSelectionTest.class
72
				.toString());
71
		suite.addTestSuite(ComboObservableValueSelectionTest.class);
73
		suite.addTestSuite(ComboObservableValueSelectionTest.class);
72
		suite.addTest(SWTMutableObservableValueContractTest.suite(new Delegate()));
74
		suite.addTest(SWTMutableObservableValueContractTest
75
				.suite(new Delegate()));
73
		return suite;
76
		return suite;
74
	}
77
	}
75
78
Lines 91-106 Link Here
91
		}
94
		}
92
95
93
		public IObservableValue createObservableValue(Realm realm) {
96
		public IObservableValue createObservableValue(Realm realm) {
94
			return new ComboObservableValue(realm, combo,
97
			return new SWTObservableValueDecorator(PropertyObservables
95
					SWTProperties.SELECTION);
98
					.observeValue(realm, combo, ComboProperties
99
							.selection()), combo);
96
		}
100
		}
97
101
98
		public void change(IObservable observable) {
102
		public void change(IObservable observable) {
99
			int index = combo
103
			int index = combo
100
					.indexOf((String) createValue((IObservableValue) observable));
104
					.indexOf((String) createValue((IObservableValue) observable));
101
105
102
			combo.select(index);
106
			((IObservableValue) observable).setValue(combo.getItem(index));
103
			combo.notifyListeners(SWT.Selection, null);
104
		}
107
		}
105
108
106
		public Object getValueType(IObservableValue observable) {
109
		public Object getValueType(IObservableValue observable) {
(-)src/org/eclipse/core/tests/internal/databinding/beans/JavaBeanObservableArrayBasedListTest.java (-32 / +25 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
10
 *     Matthew Hall - bugs 221351, 213145, 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 23-35 Link Here
23
import junit.framework.Test;
22
import junit.framework.Test;
24
import junit.framework.TestSuite;
23
import junit.framework.TestSuite;
25
24
25
import org.eclipse.core.databinding.beans.BeanProperties;
26
import org.eclipse.core.databinding.beans.BeansObservables;
27
import org.eclipse.core.databinding.beans.IBeanObservable;
28
import org.eclipse.core.databinding.beans.IBeanProperty;
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.list.IObservableList;
32
import org.eclipse.core.databinding.observable.list.IObservableList;
30
import org.eclipse.core.databinding.observable.list.ListChangeEvent;
33
import org.eclipse.core.databinding.observable.list.ListChangeEvent;
31
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
34
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
32
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableList;
33
import org.eclipse.jface.databinding.conformance.MutableObservableListContractTest;
35
import org.eclipse.jface.databinding.conformance.MutableObservableListContractTest;
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.ListChangeEventTracker;
37
import org.eclipse.jface.databinding.conformance.util.ListChangeEventTracker;
Lines 42-48 Link Here
42
 */
44
 */
43
public class JavaBeanObservableArrayBasedListTest extends
45
public class JavaBeanObservableArrayBasedListTest extends
44
		AbstractDefaultRealmTestCase {
46
		AbstractDefaultRealmTestCase {
45
	private JavaBeanObservableList list;
47
	private IObservableList list;
48
	private IBeanObservable beanObservable;
46
49
47
	private PropertyDescriptor propertyDescriptor;
50
	private PropertyDescriptor propertyDescriptor;
48
51
Lines 59-77 Link Here
59
		super.setUp();
62
		super.setUp();
60
63
61
		propertyName = "array";
64
		propertyName = "array";
62
		propertyDescriptor = new PropertyDescriptor(propertyName, Bean.class);
65
		propertyDescriptor = ((IBeanProperty) BeanProperties.listProperty(
66
				Bean.class, propertyName)).getPropertyDescriptor();
63
		bean = new Bean(new Object[0]);
67
		bean = new Bean(new Object[0]);
64
68
65
		list = new JavaBeanObservableList(SWTObservables.getRealm(Display
69
		list = BeansObservables.observeList(SWTObservables.getRealm(Display
66
				.getDefault()), bean, propertyDescriptor, Bean.class);
70
				.getDefault()), bean, propertyName);
71
		beanObservable = (IBeanObservable) list;
67
	}
72
	}
68
73
69
	public void testGetObserved() throws Exception {
74
	public void testGetObserved() throws Exception {
70
		assertSame(bean, list.getObserved());
75
		assertSame(bean, beanObservable.getObserved());
71
	}
76
	}
72
77
73
	public void testGetPropertyDescriptor() throws Exception {
78
	public void testGetPropertyDescriptor() throws Exception {
74
		assertSame(propertyDescriptor, list.getPropertyDescriptor());
79
		assertEquals(propertyDescriptor, beanObservable.getPropertyDescriptor());
75
	}
80
	}
76
81
77
	public void testRegistersListenerAfterFirstListenerIsAdded()
82
	public void testRegistersListenerAfterFirstListenerIsAdded()
Lines 308-323 Link Here
308
	}
313
	}
309
314
310
	public void testRemoveAll() throws Exception {
315
	public void testRemoveAll() throws Exception {
311
		List elements = Arrays.asList(new String[] { "1", "2" });
316
		list.addAll(Arrays.asList(new String[] { "1", "2", "3", "4" }));
312
		list.addAll(elements);
313
		list.addAll(elements);
314
315
		assertEquals(4, bean.getArray().length);
317
		assertEquals(4, bean.getArray().length);
316
		list.removeAll(elements);
318
319
		list.removeAll(Arrays.asList(new String[] { "2", "4" }));
317
320
318
		assertEquals(2, bean.getArray().length);
321
		assertEquals(2, bean.getArray().length);
319
		assertEquals(elements.get(0), bean.getArray()[0]);
322
		assertEquals("1", bean.getArray()[0]);
320
		assertEquals(elements.get(1), bean.getArray()[1]);
323
		assertEquals("3", bean.getArray()[1]);
321
	}
324
	}
322
325
323
	public void testRemoveAllListChangeEvent() throws Exception {
326
	public void testRemoveAllListChangeEvent() throws Exception {
Lines 471-478 Link Here
471
		PropertyChangeEvent event = listener.evt;
474
		PropertyChangeEvent event = listener.evt;
472
		assertEquals("event did not fire", 1, listener.count);
475
		assertEquals("event did not fire", 1, listener.count);
473
		assertEquals("array", event.getPropertyName());
476
		assertEquals("array", event.getPropertyName());
474
		assertTrue("old value", Arrays.equals(old, (Object[]) event.getOldValue()));
477
		assertTrue("old value", Arrays.equals(old, (Object[]) event
475
		assertTrue("new value", Arrays.equals(bean.getArray(), (Object[]) event.getNewValue()));
478
				.getOldValue()));
479
		assertTrue("new value", Arrays.equals(bean.getArray(), (Object[]) event
480
				.getNewValue()));
476
		assertFalse("lists are equal", Arrays.equals(bean.getArray(), old));
481
		assertFalse("lists are equal", Arrays.equals(bean.getArray(), old));
477
	}
482
	}
478
483
Lines 482-492 Link Here
482
487
483
		PropertyChangeEvent evt;
488
		PropertyChangeEvent evt;
484
489
485
		/*
486
		 * (non-Javadoc)
487
		 * 
488
		 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
489
		 */
490
		public void propertyChange(PropertyChangeEvent evt) {
490
		public void propertyChange(PropertyChangeEvent evt) {
491
			count++;
491
			count++;
492
			this.evt = evt;
492
			this.evt = evt;
Lines 504-520 Link Here
504
		public IObservableCollection createObservableCollection(Realm realm,
504
		public IObservableCollection createObservableCollection(Realm realm,
505
				int elementCount) {
505
				int elementCount) {
506
			String propertyName = "array";
506
			String propertyName = "array";
507
			PropertyDescriptor propertyDescriptor;
508
			try {
509
				propertyDescriptor = new PropertyDescriptor(propertyName,
510
						Bean.class);
511
			} catch (IntrospectionException e) {
512
				throw new RuntimeException(e);
513
			}
514
			Object bean = new Bean(new Object[0]);
507
			Object bean = new Bean(new Object[0]);
515
508
516
			IObservableList list = new JavaBeanObservableList(realm, bean,
509
			IObservableList list = BeansObservables.observeList(realm, bean,
517
					propertyDescriptor, String.class);
510
					propertyName, String.class);
518
			for (int i = 0; i < elementCount; i++)
511
			for (int i = 0; i < elementCount; i++)
519
				list.add(createElement(list));
512
				list.add(createElement(list));
520
			return list;
513
			return list;
(-)src/org/eclipse/core/tests/internal/databinding/beans/BeanObservableListDecoratorTest.java (-13 / +7 lines)
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/core/tests/internal/databinding/beans/JavaBeanObservableListTest.java (-36 / +26 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
10
 *     Matthew Hall - bugs 221351, 213145, 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 24-36 Link Here
24
import junit.framework.Test;
23
import junit.framework.Test;
25
import junit.framework.TestSuite;
24
import junit.framework.TestSuite;
26
25
26
import org.eclipse.core.databinding.beans.BeanProperties;
27
import org.eclipse.core.databinding.beans.BeansObservables;
28
import org.eclipse.core.databinding.beans.IBeanObservable;
29
import org.eclipse.core.databinding.beans.IBeanProperty;
30
import org.eclipse.core.databinding.beans.PojoObservables;
27
import org.eclipse.core.databinding.observable.IObservable;
31
import org.eclipse.core.databinding.observable.IObservable;
28
import org.eclipse.core.databinding.observable.IObservableCollection;
32
import org.eclipse.core.databinding.observable.IObservableCollection;
29
import org.eclipse.core.databinding.observable.Realm;
33
import org.eclipse.core.databinding.observable.Realm;
30
import org.eclipse.core.databinding.observable.list.IObservableList;
34
import org.eclipse.core.databinding.observable.list.IObservableList;
31
import org.eclipse.core.databinding.observable.list.ListChangeEvent;
35
import org.eclipse.core.databinding.observable.list.ListChangeEvent;
32
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
36
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
33
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableList;
34
import org.eclipse.jface.databinding.conformance.MutableObservableListContractTest;
37
import org.eclipse.jface.databinding.conformance.MutableObservableListContractTest;
35
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
38
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
36
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
39
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
Lines 43-49 Link Here
43
 * @since 1.1
46
 * @since 1.1
44
 */
47
 */
45
public class JavaBeanObservableListTest extends AbstractDefaultRealmTestCase {
48
public class JavaBeanObservableListTest extends AbstractDefaultRealmTestCase {
46
	private JavaBeanObservableList list;
49
	private IObservableList list;
50
	private IBeanObservable beanObservable;
47
51
48
	private PropertyDescriptor propertyDescriptor;
52
	private PropertyDescriptor propertyDescriptor;
49
53
Lines 60-78 Link Here
60
		super.setUp();
64
		super.setUp();
61
65
62
		propertyName = "list";
66
		propertyName = "list";
63
		propertyDescriptor = new PropertyDescriptor(propertyName, Bean.class);
67
		propertyDescriptor = ((IBeanProperty) BeanProperties.listProperty(
68
				Bean.class, propertyName)).getPropertyDescriptor();
64
		bean = new Bean(new ArrayList());
69
		bean = new Bean(new ArrayList());
65
70
66
		list = new JavaBeanObservableList(SWTObservables.getRealm(Display
71
		list = BeansObservables.observeList(SWTObservables.getRealm(Display
67
				.getDefault()), bean, propertyDescriptor, String.class);
72
				.getDefault()), bean, propertyName);
73
		beanObservable = (IBeanObservable) list;
68
	}
74
	}
69
75
70
	public void testGetObserved() throws Exception {
76
	public void testGetObserved() throws Exception {
71
		assertEquals(bean, list.getObserved());
77
		assertEquals(bean, beanObservable.getObserved());
72
	}
78
	}
73
79
74
	public void testGetPropertyDescriptor() throws Exception {
80
	public void testGetPropertyDescriptor() throws Exception {
75
		assertEquals(propertyDescriptor, list.getPropertyDescriptor());
81
		assertEquals(propertyDescriptor, beanObservable.getPropertyDescriptor());
76
	}
82
	}
77
83
78
	public void testRegistersListenerAfterFirstListenerIsAdded()
84
	public void testRegistersListenerAfterFirstListenerIsAdded()
Lines 322-337 Link Here
322
	}
328
	}
323
329
324
	public void testRemoveAll() throws Exception {
330
	public void testRemoveAll() throws Exception {
325
		List elements = Arrays.asList(new String[] { "1", "2" });
331
		list.addAll(Arrays.asList(new String[] { "1", "2", "3", "4" }));
326
		list.addAll(elements);
327
		list.addAll(elements);
328
329
		assertEquals(4, bean.getList().size());
332
		assertEquals(4, bean.getList().size());
330
		list.removeAll(elements);
333
334
		list.removeAll(Arrays.asList(new String[] { "2", "4" }));
331
335
332
		assertEquals(2, bean.getList().size());
336
		assertEquals(2, bean.getList().size());
333
		assertEquals(elements.get(0), bean.getList().get(0));
337
		assertEquals("1", bean.getList().get(0));
334
		assertEquals(elements.get(1), bean.getList().get(1));
338
		assertEquals("3", bean.getList().get(1));
335
	}
339
	}
336
340
337
	public void testRemoveAllListChangeEvent() throws Exception {
341
	public void testRemoveAllListChangeEvent() throws Exception {
Lines 467-475 Link Here
467
471
468
	public void testConstructor_RegistersListener() throws Exception {
472
	public void testConstructor_RegistersListener() throws Exception {
469
		Bean bean = new Bean();
473
		Bean bean = new Bean();
470
		JavaBeanObservableList observable = new JavaBeanObservableList(Realm
474
		IObservableList observable = BeansObservables.observeList(Realm
471
				.getDefault(), bean,
475
				.getDefault(), bean, "list");
472
				new PropertyDescriptor("list", Bean.class), Bean.class);
473
476
474
		assertFalse(bean.hasListeners("list"));
477
		assertFalse(bean.hasListeners("list"));
475
		ChangeEventTracker.observe(observable);
478
		ChangeEventTracker.observe(observable);
Lines 478-486 Link Here
478
481
479
	public void testConstructor_SkipsRegisterListener() throws Exception {
482
	public void testConstructor_SkipsRegisterListener() throws Exception {
480
		Bean bean = new Bean();
483
		Bean bean = new Bean();
481
		JavaBeanObservableList observable = new JavaBeanObservableList(Realm
484
		IObservableList observable = PojoObservables.observeList(Realm
482
				.getDefault(), bean,
485
				.getDefault(), bean, "list");
483
				new PropertyDescriptor("list", Bean.class), Bean.class, false);
484
486
485
		assertFalse(bean.hasListeners("list"));
487
		assertFalse(bean.hasListeners("list"));
486
		ChangeEventTracker.observe(observable);
488
		ChangeEventTracker.observe(observable);
Lines 517-527 Link Here
517
519
518
		PropertyChangeEvent evt;
520
		PropertyChangeEvent evt;
519
521
520
		/*
521
		 * (non-Javadoc)
522
		 * 
523
		 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
524
		 */
525
		public void propertyChange(PropertyChangeEvent evt) {
522
		public void propertyChange(PropertyChangeEvent evt) {
526
			count++;
523
			count++;
527
			this.evt = evt;
524
			this.evt = evt;
Lines 539-555 Link Here
539
		public IObservableCollection createObservableCollection(Realm realm,
536
		public IObservableCollection createObservableCollection(Realm realm,
540
				int elementCount) {
537
				int elementCount) {
541
			String propertyName = "list";
538
			String propertyName = "list";
542
			PropertyDescriptor propertyDescriptor;
543
			try {
544
				propertyDescriptor = new PropertyDescriptor(propertyName,
545
						Bean.class);
546
			} catch (IntrospectionException e) {
547
				throw new RuntimeException(e);
548
			}
549
			Object bean = new Bean(new ArrayList());
539
			Object bean = new Bean(new ArrayList());
550
540
551
			IObservableList list = new JavaBeanObservableList(realm, bean,
541
			IObservableList list = BeansObservables.observeList(realm, bean,
552
					propertyDescriptor, String.class);
542
					propertyName, String.class);
553
			for (int i = 0; i < elementCount; i++)
543
			for (int i = 0; i < elementCount; i++)
554
				list.add(createElement(list));
544
				list.add(createElement(list));
555
			return list;
545
			return list;
(-)src/org/eclipse/core/tests/internal/databinding/beans/BeanObservableValueDecoratorTest.java (-21 / +15 lines)
Lines 14-21 Link Here
14
14
15
import java.beans.PropertyDescriptor;
15
import java.beans.PropertyDescriptor;
16
16
17
import org.eclipse.core.databinding.beans.BeansObservables;
18
import org.eclipse.core.databinding.observable.value.IObservableValue;
17
import org.eclipse.core.internal.databinding.beans.BeanObservableValueDecorator;
19
import org.eclipse.core.internal.databinding.beans.BeanObservableValueDecorator;
18
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableValue;
19
import org.eclipse.jface.databinding.swt.SWTObservables;
20
import org.eclipse.jface.databinding.swt.SWTObservables;
20
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
21
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
21
import org.eclipse.swt.widgets.Display;
22
import org.eclipse.swt.widgets.Display;
Lines 23-62 Link Here
23
/**
24
/**
24
 * @since 3.3
25
 * @since 3.3
25
 */
26
 */
26
public class BeanObservableValueDecoratorTest extends AbstractDefaultRealmTestCase {
27
public class BeanObservableValueDecoratorTest extends
28
		AbstractDefaultRealmTestCase {
27
	private Bean bean;
29
	private Bean bean;
28
	private JavaBeanObservableValue observableValue;
30
	private IObservableValue observableValue;
29
	private BeanObservableValueDecorator decorator;
31
	private BeanObservableValueDecorator decorator;
30
	private PropertyDescriptor propertyDescriptor;
32
	private PropertyDescriptor propertyDescriptor;
31
	
33
32
	/*
33
	 * (non-Javadoc)
34
	 * 
35
	 * @see junit.framework.TestCase#setUp()
36
	 */
37
	protected void setUp() throws Exception {
34
	protected void setUp() throws Exception {
38
		super.setUp();
35
		super.setUp();
39
		
36
40
		bean = new Bean();
37
		bean = new Bean();
41
		propertyDescriptor = new PropertyDescriptor("value",
38
		propertyDescriptor = new PropertyDescriptor("value", Bean.class);
42
				Bean.class);
39
		observableValue = BeansObservables.observeValue(SWTObservables
43
		observableValue = new JavaBeanObservableValue(
40
				.getRealm(Display.getDefault()), bean, "value");
44
				SWTObservables.getRealm(Display.getDefault()), bean,
41
		decorator = new BeanObservableValueDecorator(observableValue,
45
				propertyDescriptor);
42
				propertyDescriptor);
46
		decorator = new BeanObservableValueDecorator(
47
				observableValue, observableValue
48
						.getPropertyDescriptor());
49
	}
43
	}
50
44
51
	public void testGetDelegate() throws Exception {
45
	public void testGetDelegate() throws Exception {
52
		assertEquals(observableValue, decorator.getDecorated());
46
		assertSame(observableValue, 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/JavaBeanObservableMapTest.java (-9 / +22 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 - bugs 213145, 241585
10
 *     Matthew Hall - bugs 213145, 241585, 194734
11
 *******************************************************************************/
11
 *******************************************************************************/
12
12
13
package org.eclipse.core.tests.internal.databinding.beans;
13
package org.eclipse.core.tests.internal.databinding.beans;
Lines 19-30 Link Here
19
import junit.framework.TestCase;
19
import junit.framework.TestCase;
20
import junit.framework.TestSuite;
20
import junit.framework.TestSuite;
21
21
22
import org.eclipse.core.databinding.beans.BeanProperties;
23
import org.eclipse.core.databinding.beans.BeansObservables;
24
import org.eclipse.core.databinding.beans.IBeanObservable;
25
import org.eclipse.core.databinding.beans.IBeanProperty;
26
import org.eclipse.core.databinding.beans.PojoObservables;
22
import org.eclipse.core.databinding.observable.Realm;
27
import org.eclipse.core.databinding.observable.Realm;
23
import org.eclipse.core.databinding.observable.map.IMapChangeListener;
28
import org.eclipse.core.databinding.observable.map.IMapChangeListener;
29
import org.eclipse.core.databinding.observable.map.IObservableMap;
24
import org.eclipse.core.databinding.observable.map.MapChangeEvent;
30
import org.eclipse.core.databinding.observable.map.MapChangeEvent;
25
import org.eclipse.core.databinding.observable.map.MapDiff;
31
import org.eclipse.core.databinding.observable.map.MapDiff;
26
import org.eclipse.core.databinding.observable.set.WritableSet;
32
import org.eclipse.core.databinding.observable.set.WritableSet;
27
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableMap;
28
import org.eclipse.core.tests.databinding.observable.ThreadRealm;
33
import org.eclipse.core.tests.databinding.observable.ThreadRealm;
29
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
34
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
30
import org.eclipse.jface.databinding.conformance.util.CurrentRealm;
35
import org.eclipse.jface.databinding.conformance.util.CurrentRealm;
Lines 42-48 Link Here
42
47
43
	private PropertyDescriptor propertyDescriptor;
48
	private PropertyDescriptor propertyDescriptor;
44
49
45
	private JavaBeanObservableMap map;
50
	private IObservableMap map;
51
	private IBeanObservable beanObservable;
46
52
47
	protected void setUp() throws Exception {
53
	protected void setUp() throws Exception {
48
		ThreadRealm realm = new ThreadRealm();
54
		ThreadRealm realm = new ThreadRealm();
Lines 54-61 Link Here
54
		set.add(model1);
60
		set.add(model1);
55
		set.add(model2);
61
		set.add(model2);
56
62
57
		propertyDescriptor = new PropertyDescriptor("value", Bean.class);
63
		String propertyName = "value";
58
		map = new JavaBeanObservableMap(set, propertyDescriptor);
64
		propertyDescriptor = ((IBeanProperty) BeanProperties.valueProperty(
65
				Bean.class, propertyName)).getPropertyDescriptor();
66
		map = BeansObservables.observeMap(set, Bean.class, propertyName);
67
		beanObservable = (IBeanObservable) map;
59
	}
68
	}
60
69
61
	public void testGetValue() throws Exception {
70
	public void testGetValue() throws Exception {
Lines 135-145 Link Here
135
	}
144
	}
136
	
145
	
137
	public void testGetObserved() throws Exception {
146
	public void testGetObserved() throws Exception {
138
		assertEquals(set, map.getObserved());
147
		assertEquals(set, beanObservable.getObserved());
139
	}
148
	}
140
	
149
	
141
	public void testGetPropertyDescriptor() throws Exception {
150
	public void testGetPropertyDescriptor() throws Exception {
142
		assertEquals(propertyDescriptor, map.getPropertyDescriptor());
151
		assertEquals(propertyDescriptor, beanObservable.getPropertyDescriptor());
143
	}
152
	}
144
	
153
	
145
	public void testConstructor_SkipRegisterListeners() throws Exception {
154
	public void testConstructor_SkipRegisterListeners() throws Exception {
Lines 148-154 Link Here
148
		Bean bean = new Bean();
157
		Bean bean = new Bean();
149
		set.add(bean);
158
		set.add(bean);
150
		
159
		
151
		JavaBeanObservableMap observable = new JavaBeanObservableMap(set, new PropertyDescriptor("value", Bean.class), false);
160
		IObservableMap observable = PojoObservables.observeMap(set, Bean.class,
161
				"value");
162
		assertFalse(bean.hasListeners("value"));
152
		ChangeEventTracker.observe(observable);
163
		ChangeEventTracker.observe(observable);
153
164
154
		assertFalse(bean.hasListeners("value"));
165
		assertFalse(bean.hasListeners("value"));
Lines 160-166 Link Here
160
		Bean bean = new Bean();
171
		Bean bean = new Bean();
161
		set.add(bean);
172
		set.add(bean);
162
		
173
		
163
		JavaBeanObservableMap observable = new JavaBeanObservableMap(set, new PropertyDescriptor("value", Bean.class));
174
		IObservableMap observable = BeansObservables.observeMap(set,
175
				Bean.class, "value");
176
		assertFalse(bean.hasListeners("value"));
164
		ChangeEventTracker.observe(observable);
177
		ChangeEventTracker.observe(observable);
165
178
166
		assertTrue(bean.hasListeners("value"));
179
		assertTrue(bean.hasListeners("value"));
(-)src/org/eclipse/core/tests/internal/databinding/beans/JavaBeanObservableValueTest.java (-26 / +22 lines)
Lines 9-33 Link Here
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Brad Reynolds - bug 171616
10
 *     Brad Reynolds - bug 171616
11
 *     Katarzyna Marszalek - test case for bug 198519
11
 *     Katarzyna Marszalek - test case for bug 198519
12
 *     Matthew Hall - bug 213145
12
 *     Matthew Hall - bugs 213145, 194734
13
 ******************************************************************************/
13
 ******************************************************************************/
14
14
15
package org.eclipse.core.tests.internal.databinding.beans;
15
package org.eclipse.core.tests.internal.databinding.beans;
16
16
17
import java.beans.IntrospectionException;
18
import java.beans.PropertyDescriptor;
17
import java.beans.PropertyDescriptor;
19
18
20
import junit.framework.Test;
19
import junit.framework.Test;
21
import junit.framework.TestSuite;
20
import junit.framework.TestSuite;
22
21
22
import org.eclipse.core.databinding.beans.BeanProperties;
23
import org.eclipse.core.databinding.beans.BeansObservables;
23
import org.eclipse.core.databinding.beans.BeansObservables;
24
import org.eclipse.core.databinding.beans.IBeanObservable;
25
import org.eclipse.core.databinding.beans.IBeanProperty;
26
import org.eclipse.core.databinding.beans.PojoObservables;
24
import org.eclipse.core.databinding.observable.ChangeEvent;
27
import org.eclipse.core.databinding.observable.ChangeEvent;
25
import org.eclipse.core.databinding.observable.IChangeListener;
28
import org.eclipse.core.databinding.observable.IChangeListener;
26
import org.eclipse.core.databinding.observable.IObservable;
29
import org.eclipse.core.databinding.observable.IObservable;
27
import org.eclipse.core.databinding.observable.Realm;
30
import org.eclipse.core.databinding.observable.Realm;
28
import org.eclipse.core.databinding.observable.value.ComputedValue;
31
import org.eclipse.core.databinding.observable.value.ComputedValue;
29
import org.eclipse.core.databinding.observable.value.IObservableValue;
32
import org.eclipse.core.databinding.observable.value.IObservableValue;
30
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableValue;
31
import org.eclipse.jface.databinding.conformance.MutableObservableValueContractTest;
33
import org.eclipse.jface.databinding.conformance.MutableObservableValueContractTest;
32
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
34
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
33
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
35
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
Lines 39-73 Link Here
39
 */
41
 */
40
public class JavaBeanObservableValueTest extends AbstractDefaultRealmTestCase {
42
public class JavaBeanObservableValueTest extends AbstractDefaultRealmTestCase {
41
	private Bean bean;
43
	private Bean bean;
42
	private JavaBeanObservableValue observableValue;
44
	private IObservableValue observableValue;
45
	private IBeanObservable beanObservable;
43
	private PropertyDescriptor propertyDescriptor;
46
	private PropertyDescriptor propertyDescriptor;
44
	private String propertyName;
47
	private String propertyName;
45
48
46
	/* (non-Javadoc)
47
	 * @see junit.framework.TestCase#setUp()
48
	 */
49
	protected void setUp() throws Exception {
49
	protected void setUp() throws Exception {
50
		super.setUp();
50
		super.setUp();
51
		
51
		
52
		bean = new Bean();
52
		bean = new Bean();
53
		propertyName = "value";
53
		propertyName = "value";
54
		propertyDescriptor = new PropertyDescriptor(propertyName, Bean.class);
54
		propertyDescriptor = ((IBeanProperty) BeanProperties.valueProperty(
55
		observableValue = new JavaBeanObservableValue(Realm.getDefault(), bean, propertyDescriptor);
55
				Bean.class, propertyName)).getPropertyDescriptor();
56
		observableValue = BeansObservables.observeValue(bean, propertyName);
57
		beanObservable = (IBeanObservable) observableValue;
56
	}
58
	}
57
59
58
	public void testGetObserved() throws Exception {
60
	public void testGetObserved() throws Exception {
59
		assertEquals(bean, observableValue.getObserved());
61
		assertEquals(bean, beanObservable.getObserved());
60
	}
62
	}
61
63
62
	public void testGetPropertyDescriptor() throws Exception {
64
	public void testGetPropertyDescriptor() throws Exception {
63
    	assertEquals(propertyDescriptor, observableValue.getPropertyDescriptor());
65
    	assertEquals(propertyDescriptor, beanObservable.getPropertyDescriptor());
64
	}
66
	}
65
67
66
	public void testSetValueThrowsExceptionThrownByBean() throws Exception {
68
	public void testSetValueThrowsExceptionThrownByBean() throws Exception {
67
		ThrowsSetException temp = new ThrowsSetException();
69
		ThrowsSetException temp = new ThrowsSetException();
68
		JavaBeanObservableValue observable = new JavaBeanObservableValue(Realm
70
		IObservableValue observable = BeansObservables.observeValue(temp,
69
				.getDefault(), temp,
71
				"value");
70
				new PropertyDescriptor("value", ThrowsSetException.class));
71
72
72
		try {
73
		try {
73
			observable.setValue("");
74
			observable.setValue("");
Lines 79-87 Link Here
79
	
80
	
80
	public void testGetValueThrowsExceptionThrownByBean() throws Exception {
81
	public void testGetValueThrowsExceptionThrownByBean() throws Exception {
81
		ThrowsGetException temp = new ThrowsGetException();
82
		ThrowsGetException temp = new ThrowsGetException();
82
		JavaBeanObservableValue observable = new JavaBeanObservableValue(Realm
83
		IObservableValue observable = BeansObservables.observeValue(temp,
83
				.getDefault(), temp,
84
				"value");
84
				new PropertyDescriptor("value", ThrowsGetException.class));
85
85
86
		try {
86
		try {
87
			observable.getValue();
87
			observable.getValue();
Lines 108-121 Link Here
108
	}
108
	}
109
109
110
	public void testConstructor_RegistersListeners() throws Exception {
110
	public void testConstructor_RegistersListeners() throws Exception {
111
		JavaBeanObservableValue observable = new JavaBeanObservableValue(Realm.getDefault(), bean, propertyDescriptor);
111
		IObservableValue observable = BeansObservables.observeValue(bean,
112
				propertyName);
112
		ChangeEventTracker.observe(observable);
113
		ChangeEventTracker.observe(observable);
113
		
114
		
114
		assertTrue(bean.hasListeners(propertyName));
115
		assertTrue(bean.hasListeners(propertyName));
115
	}
116
	}
116
	
117
	
117
	public void testConstructor_SkipRegisterListeners() throws Exception {
118
	public void testConstructor_SkipRegisterListeners() throws Exception {
118
		JavaBeanObservableValue observable = new JavaBeanObservableValue(Realm.getDefault(), bean, propertyDescriptor, false);
119
		IObservableValue observable = PojoObservables.observeValue(bean,
120
				propertyName);
119
		ChangeEventTracker.observe(observable);
121
		ChangeEventTracker.observe(observable);
120
		
122
		
121
		assertFalse(bean.hasListeners(propertyName));
123
		assertFalse(bean.hasListeners(propertyName));
Lines 138-150 Link Here
138
		}
140
		}
139
		
141
		
140
		public IObservableValue createObservableValue(Realm realm) {
142
		public IObservableValue createObservableValue(Realm realm) {
141
			try {
143
			return BeansObservables.observeValue(realm, bean, "value");
142
				PropertyDescriptor propertyDescriptor = new PropertyDescriptor("value", Bean.class);
143
				return new JavaBeanObservableValue(realm, bean,
144
						propertyDescriptor);					
145
			} catch (IntrospectionException e) {
146
				throw new RuntimeException(e);
147
			}
148
		}
144
		}
149
		
145
		
150
		public void change(IObservable observable) {
146
		public void change(IObservable observable) {
(-)src/org/eclipse/core/tests/internal/databinding/beans/JavaBeanObservableSetTest.java (-28 / +22 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
10
 *     Matthew Hall - bugs 221351, 213145, 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.HashSet;
17
import java.util.HashSet;
Lines 21-33 Link Here
21
import junit.framework.TestCase;
20
import junit.framework.TestCase;
22
import junit.framework.TestSuite;
21
import junit.framework.TestSuite;
23
22
23
import org.eclipse.core.databinding.beans.BeanProperties;
24
import org.eclipse.core.databinding.beans.BeansObservables;
25
import org.eclipse.core.databinding.beans.IBeanObservable;
26
import org.eclipse.core.databinding.beans.IBeanProperty;
27
import org.eclipse.core.databinding.beans.PojoObservables;
24
import org.eclipse.core.databinding.observable.IObservable;
28
import org.eclipse.core.databinding.observable.IObservable;
25
import org.eclipse.core.databinding.observable.IObservableCollection;
29
import org.eclipse.core.databinding.observable.IObservableCollection;
26
import org.eclipse.core.databinding.observable.Realm;
30
import org.eclipse.core.databinding.observable.Realm;
27
import org.eclipse.core.databinding.observable.set.IObservableSet;
31
import org.eclipse.core.databinding.observable.set.IObservableSet;
28
import org.eclipse.core.databinding.observable.set.ISetChangeListener;
32
import org.eclipse.core.databinding.observable.set.ISetChangeListener;
29
import org.eclipse.core.databinding.observable.set.SetChangeEvent;
33
import org.eclipse.core.databinding.observable.set.SetChangeEvent;
30
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableSet;
31
import org.eclipse.jface.databinding.conformance.MutableObservableSetContractTest;
34
import org.eclipse.jface.databinding.conformance.MutableObservableSetContractTest;
32
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
35
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
33
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
36
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
Lines 39-72 Link Here
39
 * @since 3.3
42
 * @since 3.3
40
 */
43
 */
41
public class JavaBeanObservableSetTest extends TestCase {
44
public class JavaBeanObservableSetTest extends TestCase {
42
	private JavaBeanObservableSet observableSet;
45
	private IObservableSet observableSet;
46
	private IBeanObservable beanObservable;
43
	private Bean bean;
47
	private Bean bean;
44
	private PropertyDescriptor propertyDescriptor;
48
	private PropertyDescriptor propertyDescriptor;
45
	private String propertyName;
49
	private String propertyName;
46
	private SetChangeListener listener;
50
	private SetChangeListener listener;
47
51
48
	/*
49
	 * (non-Javadoc)
50
	 * 
51
	 * @see junit.framework.TestCase#setUp()
52
	 */
53
	protected void setUp() throws Exception {
52
	protected void setUp() throws Exception {
54
		bean = new Bean();
53
		bean = new Bean();
55
		propertyName = "set";
54
		propertyName = "set";
56
		propertyDescriptor = new PropertyDescriptor(propertyName, Bean.class);
55
		propertyDescriptor = ((IBeanProperty) BeanProperties.setProperty(
56
				Bean.class, propertyName)).getPropertyDescriptor();
57
57
58
		observableSet = new JavaBeanObservableSet(SWTObservables
58
		observableSet = BeansObservables
59
				.getRealm(Display.getDefault()), bean, propertyDescriptor,
59
				.observeSet(SWTObservables.getRealm(Display.getDefault()),
60
				Bean.class);
60
						bean, propertyName, Bean.class);
61
		beanObservable = (IBeanObservable) observableSet;
61
		listener = new SetChangeListener();
62
		listener = new SetChangeListener();
62
	}
63
	}
63
64
64
	public void testGetObserved() throws Exception {
65
	public void testGetObserved() throws Exception {
65
		assertEquals(bean, observableSet.getObserved());
66
		assertEquals(bean, beanObservable.getObserved());
66
	}
67
	}
67
68
68
	public void testGetPropertyDescriptor() throws Exception {
69
	public void testGetPropertyDescriptor() throws Exception {
69
		assertEquals(propertyDescriptor, observableSet.getPropertyDescriptor());
70
		assertEquals(propertyDescriptor, beanObservable.getPropertyDescriptor());
70
	}
71
	}
71
	
72
	
72
	public void testGetElementType() throws Exception {
73
	public void testGetElementType() throws Exception {
Lines 97-104 Link Here
97
	public void testConstructor_RegisterListeners() throws Exception {
98
	public void testConstructor_RegisterListeners() throws Exception {
98
		bean = new Bean();
99
		bean = new Bean();
99
100
100
		observableSet = new JavaBeanObservableSet(new CurrentRealm(true), bean,
101
		observableSet = BeansObservables.observeSet(new CurrentRealm(true),
101
				propertyDescriptor, Bean.class);
102
				bean, propertyName);
102
		assertFalse(bean.hasListeners(propertyName));
103
		assertFalse(bean.hasListeners(propertyName));
103
		ChangeEventTracker.observe(observableSet);
104
		ChangeEventTracker.observe(observableSet);
104
		assertTrue(bean.hasListeners(propertyName));
105
		assertTrue(bean.hasListeners(propertyName));
Lines 107-114 Link Here
107
	public void testConstructor_SkipsRegisterListeners() throws Exception {
108
	public void testConstructor_SkipsRegisterListeners() throws Exception {
108
		bean = new Bean();
109
		bean = new Bean();
109
110
110
		observableSet = new JavaBeanObservableSet(new CurrentRealm(true), bean,
111
		observableSet = PojoObservables.observeSet(new CurrentRealm(true),
111
				propertyDescriptor, Bean.class, false);
112
				bean, propertyName);
112
		assertFalse(bean.hasListeners(propertyName));
113
		assertFalse(bean.hasListeners(propertyName));
113
		ChangeEventTracker.observe(observableSet);
114
		ChangeEventTracker.observe(observableSet);
114
		assertFalse(bean.hasListeners(propertyName));
115
		assertFalse(bean.hasListeners(propertyName));
Lines 134-149 Link Here
134
				int elementCount) {
135
				int elementCount) {
135
			Bean bean = new Bean();
136
			Bean bean = new Bean();
136
			String propertyName = "set";
137
			String propertyName = "set";
137
			PropertyDescriptor propertyDescriptor;
138
			try {
139
				propertyDescriptor = new PropertyDescriptor(propertyName,
140
						Bean.class);
141
			} catch (IntrospectionException e) {
142
				throw new RuntimeException(e);
143
			}
144
138
145
			IObservableSet set = new JavaBeanObservableSet(realm,
139
			IObservableSet set = BeansObservables.observeSet(realm, bean,
146
					bean, propertyDescriptor, String.class);
140
					propertyName, String.class);
147
			for (int i = 0; i < elementCount; i++)
141
			for (int i = 0; i < elementCount; i++)
148
				set.add(createElement(set));
142
				set.add(createElement(set));
149
			return set;
143
			return set;
(-)src/org/eclipse/core/tests/internal/databinding/beans/BeanObservableSetDecoratorTest.java (-18 / +12 lines)
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/JavaBeanObservableArrayBasedSetTest.java (-24 / +18 lines)
Lines 8-19 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation (bug 221351)
9
 *     Matthew Hall - initial API and implementation (bug 221351)
10
 *     Brad Reynolds - through JavaBeanObservableArrayBasedListTest.java
10
 *     Brad Reynolds - through JavaBeanObservableArrayBasedListTest.java
11
 *     Matthew Hall - bug 213145
11
 *     Matthew Hall - bugs 213145, 194734
12
 ******************************************************************************/
12
 ******************************************************************************/
13
13
14
package org.eclipse.core.tests.internal.databinding.beans;
14
package org.eclipse.core.tests.internal.databinding.beans;
15
15
16
import java.beans.IntrospectionException;
17
import java.beans.PropertyChangeEvent;
16
import java.beans.PropertyChangeEvent;
18
import java.beans.PropertyChangeListener;
17
import java.beans.PropertyChangeListener;
19
import java.beans.PropertyDescriptor;
18
import java.beans.PropertyDescriptor;
Lines 25-36 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;
29
import org.eclipse.core.databinding.beans.IBeanObservable;
30
import org.eclipse.core.databinding.beans.IBeanProperty;
28
import org.eclipse.core.databinding.observable.IObservable;
31
import org.eclipse.core.databinding.observable.IObservable;
29
import org.eclipse.core.databinding.observable.IObservableCollection;
32
import org.eclipse.core.databinding.observable.IObservableCollection;
30
import org.eclipse.core.databinding.observable.Realm;
33
import org.eclipse.core.databinding.observable.Realm;
31
import org.eclipse.core.databinding.observable.set.IObservableSet;
34
import org.eclipse.core.databinding.observable.set.IObservableSet;
32
import org.eclipse.core.databinding.observable.set.SetChangeEvent;
35
import org.eclipse.core.databinding.observable.set.SetChangeEvent;
33
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableSet;
34
import org.eclipse.jface.databinding.conformance.MutableObservableSetContractTest;
36
import org.eclipse.jface.databinding.conformance.MutableObservableSetContractTest;
35
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
37
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
36
import org.eclipse.jface.databinding.conformance.util.SetChangeEventTracker;
38
import org.eclipse.jface.databinding.conformance.util.SetChangeEventTracker;
Lines 43-49 Link Here
43
 */
45
 */
44
public class JavaBeanObservableArrayBasedSetTest extends
46
public class JavaBeanObservableArrayBasedSetTest extends
45
		AbstractDefaultRealmTestCase {
47
		AbstractDefaultRealmTestCase {
46
	private JavaBeanObservableSet set;
48
	private IObservableSet set;
49
	private IBeanObservable beanObservable;
47
50
48
	private PropertyDescriptor propertyDescriptor;
51
	private PropertyDescriptor propertyDescriptor;
49
52
Lines 55-73 Link Here
55
		super.setUp();
58
		super.setUp();
56
59
57
		propertyName = "array";
60
		propertyName = "array";
58
		propertyDescriptor = new PropertyDescriptor(propertyName, Bean.class);
61
		propertyDescriptor = ((IBeanProperty) BeanProperties.setProperty(
62
				Bean.class, propertyName)).getPropertyDescriptor();
59
		bean = new Bean(new HashSet());
63
		bean = new Bean(new HashSet());
60
64
61
		set = new JavaBeanObservableSet(SWTObservables.getRealm(Display
65
		set = BeansObservables.observeSet(SWTObservables.getRealm(Display
62
				.getDefault()), bean, propertyDescriptor, String.class);
66
				.getDefault()), bean, propertyName);
67
		beanObservable = (IBeanObservable) set;
63
	}
68
	}
64
69
65
	public void testGetObserved() throws Exception {
70
	public void testGetObserved() throws Exception {
66
		assertEquals(bean, set.getObserved());
71
		assertEquals(bean, beanObservable.getObserved());
67
	}
72
	}
68
73
69
	public void testGetPropertyDescriptor() throws Exception {
74
	public void testGetPropertyDescriptor() throws Exception {
70
		assertEquals(propertyDescriptor, set.getPropertyDescriptor());
75
		assertEquals(propertyDescriptor, beanObservable.getPropertyDescriptor());
71
	}
76
	}
72
77
73
	public void testRegistersListenerAfterFirstListenerIsAdded()
78
	public void testRegistersListenerAfterFirstListenerIsAdded()
Lines 292-298 Link Here
292
		assertEquals("array", event.getPropertyName());
297
		assertEquals("array", event.getPropertyName());
293
		assertTrue("old value", Arrays.equals(old, (Object[]) event
298
		assertTrue("old value", Arrays.equals(old, (Object[]) event
294
				.getOldValue()));
299
				.getOldValue()));
295
		assertTrue("new value", Arrays.equals(bean.getArray(), (Object[]) event.getNewValue()));
300
		assertTrue("new value", Arrays.equals(bean.getArray(), (Object[]) event
301
				.getNewValue()));
296
		assertFalse("lists are equal", Arrays.equals(bean.getArray(), old));
302
		assertFalse("lists are equal", Arrays.equals(bean.getArray(), old));
297
	}
303
	}
298
304
Lines 302-312 Link Here
302
308
303
		PropertyChangeEvent evt;
309
		PropertyChangeEvent evt;
304
310
305
		/*
306
		 * (non-Javadoc)
307
		 * 
308
		 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
309
		 */
310
		public void propertyChange(PropertyChangeEvent evt) {
311
		public void propertyChange(PropertyChangeEvent evt) {
311
			count++;
312
			count++;
312
			this.evt = evt;
313
			this.evt = evt;
Lines 324-340 Link Here
324
		public IObservableCollection createObservableCollection(Realm realm,
325
		public IObservableCollection createObservableCollection(Realm realm,
325
				int elementCount) {
326
				int elementCount) {
326
			String propertyName = "array";
327
			String propertyName = "array";
327
			PropertyDescriptor propertyDescriptor;
328
			try {
329
				propertyDescriptor = new PropertyDescriptor(propertyName,
330
						Bean.class);
331
			} catch (IntrospectionException e) {
332
				throw new RuntimeException(e);
333
			}
334
			Object bean = new Bean(new Object[0]);
328
			Object bean = new Bean(new Object[0]);
335
329
336
			IObservableSet list = new JavaBeanObservableSet(realm, bean,
330
			IObservableSet list = BeansObservables.observeSet(realm, bean,
337
					propertyDescriptor, String.class);
331
					propertyName, String.class);
338
			for (int i = 0; i < elementCount; i++)
332
			for (int i = 0; i < elementCount; i++)
339
				list.add(createElement(list));
333
				list.add(createElement(list));
340
			return list;
334
			return list;
(-)src/org/eclipse/jface/tests/databinding/BindingTestSuite.java (-25 / +29 lines)
Lines 15-20 Link Here
15
 *                    212518, 215531, 221351, 184830, 213145, 218269, 239015,
15
 *                    212518, 215531, 221351, 184830, 213145, 218269, 239015,
16
 *                    237703, 237718, 222289
16
 *                    237703, 237718, 222289
17
 *     Ovidio Mallo - bug 237163, bug 235195
17
 *     Ovidio Mallo - bug 237163, bug 235195
18
 *     Chris Aniszczyk - bug 131435
18
 *******************************************************************************/
19
 *******************************************************************************/
19
package org.eclipse.jface.tests.databinding;
20
package org.eclipse.jface.tests.databinding;
20
21
Lines 155-161 Link Here
155
import org.eclipse.jface.tests.internal.databinding.swt.DelayedObservableValueTest;
156
import org.eclipse.jface.tests.internal.databinding.swt.DelayedObservableValueTest;
156
import org.eclipse.jface.tests.internal.databinding.swt.LabelObservableValueTest;
157
import org.eclipse.jface.tests.internal.databinding.swt.LabelObservableValueTest;
157
import org.eclipse.jface.tests.internal.databinding.swt.ListSingleSelectionObservableValueTest;
158
import org.eclipse.jface.tests.internal.databinding.swt.ListSingleSelectionObservableValueTest;
158
import org.eclipse.jface.tests.internal.databinding.swt.SWTObservableListTest;
159
import org.eclipse.jface.tests.internal.databinding.swt.ScaleObservableValueMaxTest;
159
import org.eclipse.jface.tests.internal.databinding.swt.ScaleObservableValueMaxTest;
160
import org.eclipse.jface.tests.internal.databinding.swt.ScaleObservableValueMinTest;
160
import org.eclipse.jface.tests.internal.databinding.swt.ScaleObservableValueMinTest;
161
import org.eclipse.jface.tests.internal.databinding.swt.ScaleObservableValueSelectionTest;
161
import org.eclipse.jface.tests.internal.databinding.swt.ScaleObservableValueSelectionTest;
Lines 164-169 Link Here
164
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueMinTest;
164
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueMinTest;
165
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueSelectionTest;
165
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueSelectionTest;
166
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueTest;
166
import org.eclipse.jface.tests.internal.databinding.swt.SpinnerObservableValueTest;
167
import org.eclipse.jface.tests.internal.databinding.swt.StyledTextObservableValueFocusOutTest;
168
import org.eclipse.jface.tests.internal.databinding.swt.StyledTextObservableValueModifyTest;
169
import org.eclipse.jface.tests.internal.databinding.swt.StyledTextObservableValueTest;
167
import org.eclipse.jface.tests.internal.databinding.swt.TableObservableValueTest;
170
import org.eclipse.jface.tests.internal.databinding.swt.TableObservableValueTest;
168
import org.eclipse.jface.tests.internal.databinding.swt.TableSingleSelectionObservableValueTest;
171
import org.eclipse.jface.tests.internal.databinding.swt.TableSingleSelectionObservableValueTest;
169
import org.eclipse.jface.tests.internal.databinding.swt.TextEditableObservableValueTest;
172
import org.eclipse.jface.tests.internal.databinding.swt.TextEditableObservableValueTest;
Lines 238-255 Link Here
238
		addTest(ObservableSetTest.suite());
241
		addTest(ObservableSetTest.suite());
239
		addTest(UnionSetTest.suite());
242
		addTest(UnionSetTest.suite());
240
		addTest(WritableSetTest.suite());
243
		addTest(WritableSetTest.suite());
241
		
244
242
		//org.eclipse.core.tests.databinding.observable.value
245
		// org.eclipse.core.tests.databinding.observable.value
243
		addTestSuite(AbstractObservableValueTest.class);
246
		addTestSuite(AbstractObservableValueTest.class);
244
		addTestSuite(AbstractVetoableValueTest.class);
247
		addTestSuite(AbstractVetoableValueTest.class);
245
		addTestSuite(ComputedValueTest.class);
248
		addTestSuite(ComputedValueTest.class);
246
		addTest(DecoratingObservableValueTest.suite());
249
		addTest(DecoratingObservableValueTest.suite());
247
		addTest(WritableValueTest.suite());
250
		addTest(WritableValueTest.suite());
248
		
251
249
		//org.eclipse.core.tests.databinding.validation
252
		// org.eclipse.core.tests.databinding.validation
250
		addTestSuite(MultiValidatorTest.class);
253
		addTestSuite(MultiValidatorTest.class);
251
		addTestSuite(ValidationStatusTest.class);
254
		addTestSuite(ValidationStatusTest.class);
252
		
255
253
		// org.eclipse.core.tests.internal.databinding
256
		// org.eclipse.core.tests.internal.databinding
254
		addTestSuite(BindingMessagesTest.class);
257
		addTestSuite(BindingMessagesTest.class);
255
		addTestSuite(BindingStatusTest.class);
258
		addTestSuite(BindingStatusTest.class);
Lines 282-288 Link Here
282
		addTestSuite(StringToNumberParserTest.class);
285
		addTestSuite(StringToNumberParserTest.class);
283
		addTestSuite(StringToShortConverterTest.class);
286
		addTestSuite(StringToShortConverterTest.class);
284
287
285
		//org.eclipse.core.tests.internal.databinding.internal.beans
288
		// org.eclipse.core.tests.internal.databinding.internal.beans
286
		addTest(BeanObservableListDecoratorTest.suite());
289
		addTest(BeanObservableListDecoratorTest.suite());
287
		addTestSuite(BeanObservableSetDecoratorTest.class);
290
		addTestSuite(BeanObservableSetDecoratorTest.class);
288
		addTestSuite(BeanObservableValueDecoratorTest.class);
291
		addTestSuite(BeanObservableValueDecoratorTest.class);
Lines 294-301 Link Here
294
		addTest(JavaBeanObservableSetTest.suite());
297
		addTest(JavaBeanObservableSetTest.suite());
295
		addTest(JavaBeanObservableValueTest.suite());
298
		addTest(JavaBeanObservableValueTest.suite());
296
		addTestSuite(ListenerSupportTest.class);
299
		addTestSuite(ListenerSupportTest.class);
297
		
300
298
		//org.eclipse.core.tests.internal.databinding.observable
301
		// org.eclipse.core.tests.internal.databinding.observable
299
		addTest(ConstantObservableValueTest.suite());
302
		addTest(ConstantObservableValueTest.suite());
300
		addTest(EmptyObservableListTest.suite());
303
		addTest(EmptyObservableListTest.suite());
301
		addTest(EmptyObservableSetTest.suite());
304
		addTest(EmptyObservableSetTest.suite());
Lines 307-314 Link Here
307
		addTest(ValidatedObservableValueTest.suite());
310
		addTest(ValidatedObservableValueTest.suite());
308
		addTest(ValidatedObservableListTest.suite());
311
		addTest(ValidatedObservableListTest.suite());
309
		addTest(ValidatedObservableSetTest.suite());
312
		addTest(ValidatedObservableSetTest.suite());
310
//		addTest(ValidatedObservableMapTest.suite());
313
		// addTest(ValidatedObservableMapTest.suite());
311
		
314
312
		// org.eclipse.core.tests.internal.databinding.observable.masterdetail
315
		// org.eclipse.core.tests.internal.databinding.observable.masterdetail
313
		addTest(DetailObservableListTest.suite());
316
		addTest(DetailObservableListTest.suite());
314
		addTest(DetailObservableSetTest.suite());
317
		addTest(DetailObservableSetTest.suite());
Lines 335-358 Link Here
335
		addTest(BindingScenariosTestSuite.suite());
338
		addTest(BindingScenariosTestSuite.suite());
336
		// The files in this package are in the above test suite
339
		// The files in this package are in the above test suite
337
340
338
		//org.eclipse.jface.tests.databinding.swt
341
		// org.eclipse.jface.tests.databinding.swt
339
		addTestSuite(SWTObservablesTest.class);
342
		addTestSuite(SWTObservablesTest.class);
340
		
343
341
		// org.eclipse.jface.tests.databinding.viewers
344
		// org.eclipse.jface.tests.databinding.viewers
342
		addTestSuite(ObservableListTreeContentProviderTest.class);
345
		addTestSuite(ObservableListTreeContentProviderTest.class);
343
		addTestSuite(ObservableMapLabelProviderTest.class);
346
		addTestSuite(ObservableMapLabelProviderTest.class);
344
		addTestSuite(ObservableSetContentProviderTest.class);
347
		addTestSuite(ObservableSetContentProviderTest.class);
345
		addTestSuite(ObservableSetTreeContentProviderTest.class);
348
		addTestSuite(ObservableSetTreeContentProviderTest.class);
346
		addTestSuite(ViewersObservablesTest.class);
349
		addTestSuite(ViewersObservablesTest.class);
347
		
350
348
		// org.eclipse.jface.tests.databinding.wizard
351
		// org.eclipse.jface.tests.databinding.wizard
349
		addTestSuite(WizardPageSupportTest.class);
352
		addTestSuite(WizardPageSupportTest.class);
350
		
353
351
		//org.eclipse.jface.tests.example.databinding.mask.internal
354
		// org.eclipse.jface.tests.example.databinding.mask.internal
352
		addTestSuite(EditMaskLexerAndTokenTest.class);
355
		addTestSuite(EditMaskLexerAndTokenTest.class);
353
		addTestSuite(EditMaskParserTest.class);
356
		addTestSuite(EditMaskParserTest.class);
354
357
355
		//org.eclipse.jface.tests.internal.databinding.internal.swt
358
		// org.eclipse.jface.tests.internal.databinding.internal.swt
356
		addTest(ButtonObservableValueTest.suite());
359
		addTest(ButtonObservableValueTest.suite());
357
		addTestSuite(CComboObservableValueTest.class);
360
		addTestSuite(CComboObservableValueTest.class);
358
		addTest(CComboObservableValueSelectionTest.suite());
361
		addTest(CComboObservableValueSelectionTest.suite());
Lines 365-395 Link Here
365
		addTest(ComboObservableValueTextTest.suite());
368
		addTest(ComboObservableValueTextTest.suite());
366
		addTestSuite(ComboSingleSelectionObservableValueTest.class);
369
		addTestSuite(ComboSingleSelectionObservableValueTest.class);
367
		addTest(DelayedObservableValueTest.suite());
370
		addTest(DelayedObservableValueTest.suite());
368
		
371
369
		addTest(SWTObservableListTest.suite());
370
		
371
		addTestSuite(ControlObservableValueTest.class);
372
		addTestSuite(ControlObservableValueTest.class);
372
		addTest(LabelObservableValueTest.suite());
373
		addTest(LabelObservableValueTest.suite());
373
		addTestSuite(ListSingleSelectionObservableValueTest.class);
374
		addTestSuite(ListSingleSelectionObservableValueTest.class);
374
		addTest(ScaleObservableValueMinTest.suite());
375
		addTest(ScaleObservableValueMinTest.suite());
375
		addTest(ScaleObservableValueMaxTest.suite());
376
		addTest(ScaleObservableValueMaxTest.suite());
376
		addTest(ScaleObservableValueSelectionTest.suite());
377
		addTest(ScaleObservableValueSelectionTest.suite());
377
		
378
378
		addTest(ShellObservableValueTest.suite());
379
		addTest(ShellObservableValueTest.suite());
379
		
380
380
		addTestSuite(SpinnerObservableValueTest.class);
381
		addTestSuite(SpinnerObservableValueTest.class);
381
		addTest(SpinnerObservableValueMinTest.suite());
382
		addTest(SpinnerObservableValueMinTest.suite());
382
		addTest(SpinnerObservableValueMaxTest.suite());
383
		addTest(SpinnerObservableValueMaxTest.suite());
383
		addTest(SpinnerObservableValueSelectionTest.suite());
384
		addTest(SpinnerObservableValueSelectionTest.suite());
384
		
385
385
		addTestSuite(TableObservableValueTest.class);
386
		addTestSuite(TableObservableValueTest.class);
386
		addTest(TableSingleSelectionObservableValueTest.suite());
387
		addTest(TableSingleSelectionObservableValueTest.suite());
387
		addTest(TextEditableObservableValueTest.suite());
388
		addTest(TextEditableObservableValueTest.suite());
388
		addTest(TextObservableValueFocusOutTest.suite());
389
		addTest(TextObservableValueFocusOutTest.suite());
389
		addTest(TextObservableValueModifyTest.suite());
390
		addTest(TextObservableValueModifyTest.suite());
390
		addTestSuite(TextObservableValueTest.class);
391
		addTestSuite(TextObservableValueTest.class);
391
		
392
		addTest(StyledTextObservableValueFocusOutTest.suite());
392
		//org.eclipse.jface.tests.internal.databinding.internal.viewers
393
		addTest(StyledTextObservableValueModifyTest.suite());
394
		addTestSuite(StyledTextObservableValueTest.class);
395
396
		// org.eclipse.jface.tests.internal.databinding.internal.viewers
393
		addTest(ObservableViewerElementSetTest.suite());
397
		addTest(ObservableViewerElementSetTest.suite());
394
		addTestSuite(ObservableCollectionTreeContentProviderTest.class);
398
		addTestSuite(ObservableCollectionTreeContentProviderTest.class);
395
		addTestSuite(SelectionProviderMultiSelectionObservableListTest.class);
399
		addTestSuite(SelectionProviderMultiSelectionObservableListTest.class);
(-)src/org/eclipse/core/tests/databinding/beans/PojoObservablesTest.java (-18 / +16 lines)
Lines 7-16 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 194734
10
 *******************************************************************************/
11
 *******************************************************************************/
11
12
12
package org.eclipse.core.tests.databinding.beans;
13
package org.eclipse.core.tests.databinding.beans;
13
14
15
import org.eclipse.core.databinding.beans.IBeanObservable;
14
import org.eclipse.core.databinding.beans.PojoObservables;
16
import org.eclipse.core.databinding.beans.PojoObservables;
15
import org.eclipse.core.databinding.observable.Realm;
17
import org.eclipse.core.databinding.observable.Realm;
16
import org.eclipse.core.databinding.observable.list.IObservableList;
18
import org.eclipse.core.databinding.observable.list.IObservableList;
Lines 19-28 Link Here
19
import org.eclipse.core.databinding.observable.set.IObservableSet;
21
import org.eclipse.core.databinding.observable.set.IObservableSet;
20
import org.eclipse.core.databinding.observable.set.WritableSet;
22
import org.eclipse.core.databinding.observable.set.WritableSet;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
23
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableList;
23
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableMap;
24
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableSet;
25
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableValue;
26
import org.eclipse.core.tests.internal.databinding.beans.Bean;
24
import org.eclipse.core.tests.internal.databinding.beans.Bean;
27
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
25
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
28
import org.eclipse.jface.databinding.conformance.util.CurrentRealm;
26
import org.eclipse.jface.databinding.conformance.util.CurrentRealm;
Lines 47-58 Link Here
47
		propertyName = "value";
45
		propertyName = "value";
48
	}
46
	}
49
47
50
	public void testObserveValue_ReturnsJavaBeanObservableValue()
48
	public void testObserveValue_ReturnsIBeanObservable()
51
			throws Exception {
49
			throws Exception {
52
		IObservableValue value = PojoObservables.observeValue(pojo, propertyName);
50
		IObservableValue value = PojoObservables.observeValue(pojo, propertyName);
53
51
54
		assertNotNull(value);
52
		assertNotNull(value);
55
		assertTrue(value instanceof JavaBeanObservableValue);
53
		assertTrue(value instanceof IBeanObservable);
56
	}
54
	}
57
55
58
	public void testObserveValue_DoesNotAttachListeners() throws Exception {
56
	public void testObserveValue_DoesNotAttachListeners() throws Exception {
Lines 62-78 Link Here
62
		assertFalse(pojo.hasListeners(propertyName));
60
		assertFalse(pojo.hasListeners(propertyName));
63
	}
61
	}
64
62
65
	public void testObservableValueWithRealm_ReturnsJavaBeanObservable()
63
	public void testObservableValueWithRealm_ReturnsIBeanObservable()
66
			throws Exception {
64
			throws Exception {
67
		CurrentRealm realm = new CurrentRealm(true);
65
		CurrentRealm realm = new CurrentRealm(true);
68
		IObservableValue value = PojoObservables.observeValue(realm, pojo,
66
		IObservableValue value = PojoObservables.observeValue(realm, pojo,
69
				propertyName);
67
				propertyName);
70
68
71
		assertNotNull(value);
69
		assertNotNull(value);
72
		assertTrue(value instanceof JavaBeanObservableValue);
70
		assertTrue(value instanceof IBeanObservable);
73
	}
71
	}
74
72
75
	public void testObservableMap_ReturnsJavaBeanObservableMap()
73
	public void testObservableMap_ReturnsIBeanObservable()
76
			throws Exception {
74
			throws Exception {
77
		IObservableSet set = new WritableSet();
75
		IObservableSet set = new WritableSet();
78
		set.add(new Bean());
76
		set.add(new Bean());
Lines 80-86 Link Here
80
		IObservableMap map = PojoObservables.observeMap(set, Bean.class,
78
		IObservableMap map = PojoObservables.observeMap(set, Bean.class,
81
				propertyName);
79
				propertyName);
82
		assertNotNull(map);
80
		assertNotNull(map);
83
		assertTrue(map instanceof JavaBeanObservableMap);
81
		assertTrue(map instanceof IBeanObservable);
84
	}
82
	}
85
	
83
	
86
	public void testObservableMap_DoesNotAttachListeners() throws Exception {
84
	public void testObservableMap_DoesNotAttachListeners() throws Exception {
Lines 101-109 Link Here
101
		assertEquals(2, maps.length);
99
		assertEquals(2, maps.length);
102
	}
100
	}
103
	
101
	
104
	public void testObserveListWithElementType_ReturnsJavaBeanObservableList() throws Exception {
102
	public void testObserveListWithElementType_ReturnsIBeanObservable() throws Exception {
105
		IObservableList list = PojoObservables.observeList(Realm.getDefault(), pojo, "list", String.class);
103
		IObservableList list = PojoObservables.observeList(Realm.getDefault(), pojo, "list", String.class);
106
		assertTrue(list instanceof JavaBeanObservableList);
104
		assertTrue(list instanceof IBeanObservable);
107
	}
105
	}
108
	
106
	
109
	public void testObserveListWithElementType_DoesNotAttachListeners() throws Exception {
107
	public void testObserveListWithElementType_DoesNotAttachListeners() throws Exception {
Lines 113-121 Link Here
113
		assertFalse(pojo.hasListeners("list"));
111
		assertFalse(pojo.hasListeners("list"));
114
	}
112
	}
115
	
113
	
116
	public void testObserveList_ReturnsJavaBeanObservableList() throws Exception {
114
	public void testObserveList_ReturnsIBeanObservable() throws Exception {
117
		IObservableList observable = PojoObservables.observeList(Realm.getDefault(), pojo, "list");
115
		IObservableList observable = PojoObservables.observeList(Realm.getDefault(), pojo, "list");
118
		assertTrue(observable instanceof JavaBeanObservableList);
116
		assertTrue(observable instanceof IBeanObservable);
119
	}
117
	}
120
	
118
	
121
	public void testObserveList_DoesNotAttachListeners() throws Exception {
119
	public void testObserveList_DoesNotAttachListeners() throws Exception {
Lines 125-133 Link Here
125
		assertFalse(pojo.hasListeners("list"));
123
		assertFalse(pojo.hasListeners("list"));
126
	}
124
	}
127
	
125
	
128
	public void testObserveSetWithElementType_ReturnsJavaBeanObservableList() throws Exception {
126
	public void testObserveSetWithElementType_ReturnsIBeanObservable() throws Exception {
129
		IObservableSet list = PojoObservables.observeSet(Realm.getDefault(), pojo, "set", String.class);
127
		IObservableSet list = PojoObservables.observeSet(Realm.getDefault(), pojo, "set", String.class);
130
		assertTrue(list instanceof JavaBeanObservableSet);
128
		assertTrue(list instanceof IBeanObservable);
131
	}
129
	}
132
	
130
	
133
	public void testObserveSetWithElementType_DoesNotAttachListeners() throws Exception {
131
	public void testObserveSetWithElementType_DoesNotAttachListeners() throws Exception {
Lines 137-145 Link Here
137
		assertFalse(pojo.hasListeners("set"));
135
		assertFalse(pojo.hasListeners("set"));
138
	}
136
	}
139
	
137
	
140
	public void testObserveSet_ReturnsJavaBeanObservableList() throws Exception {
138
	public void testObserveSet_ReturnsIBeanObservable() throws Exception {
141
		IObservableSet list = PojoObservables.observeSet(Realm.getDefault(), pojo, "set");
139
		IObservableSet list = PojoObservables.observeSet(Realm.getDefault(), pojo, "set");
142
		assertTrue(list instanceof JavaBeanObservableSet);
140
		assertTrue(list instanceof IBeanObservable);
143
	}
141
	}
144
	
142
	
145
	public void testObserveSet_DoesNotAttachListeners() throws Exception {
143
	public void testObserveSet_DoesNotAttachListeners() throws Exception {
(-)src/org/eclipse/jface/tests/databinding/scenarios/ButtonControlScenario.java (-1 lines)
Lines 66-72 Link Here
66
        adventure.setPetsAllowed(finalNewBoolean);
66
        adventure.setPetsAllowed(finalNewBoolean);
67
        spinEventLoop(0);
67
        spinEventLoop(0);
68
        assertEquals(newBoolean, button.getSelection());
68
        assertEquals(newBoolean, button.getSelection());
69
70
    }
69
    }
71
70
72
    public void testScenario02() {
71
    public void testScenario02() {
(-)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
 *******************************************************************************/
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
14
15
import junit.framework.Test;
16
import junit.framework.TestCase;
17
import junit.framework.TestSuite;
18
19
import org.eclipse.core.databinding.observable.IObservable;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.core.databinding.property.PropertyObservables;
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 PropertyObservables.observeValue(realm, text,
59
					StyledTextProperties.text(SWT.FocusOut));
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 (+110 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
 *******************************************************************************/
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
14
15
import org.eclipse.core.databinding.observable.value.IObservableValue;
16
import org.eclipse.jface.databinding.conformance.util.ValueChangeEventTracker;
17
import org.eclipse.jface.databinding.swt.SWTObservables;
18
import org.eclipse.jface.internal.databinding.swt.StyledTextTextProperty;
19
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.custom.StyledText;
22
import org.eclipse.swt.widgets.Shell;
23
24
/**
25
 * Tests to assert the inputs of the StyledTextObservableValue constructor.
26
 */
27
public class StyledTextObservableValueTest extends AbstractDefaultRealmTestCase {
28
	private StyledText text;
29
	private ValueChangeEventTracker listener;
30
31
	protected void setUp() throws Exception {
32
		super.setUp();
33
34
		Shell shell = new Shell();
35
		text = new StyledText(shell, SWT.NONE);
36
37
		listener = new ValueChangeEventTracker();
38
	}
39
40
	/**
41
	 * Asserts that only valid SWT event types are accepted on construction of
42
	 * StyledTextObservableValue.
43
	 */
44
	public void testConstructorUpdateEventTypes() {
45
		try {
46
			new StyledTextTextProperty(SWT.NONE);
47
			new StyledTextTextProperty(SWT.FocusOut);
48
			new StyledTextTextProperty(SWT.Modify);
49
			assertTrue(true);
50
		} catch (IllegalArgumentException e) {
51
			fail();
52
		}
53
54
		try {
55
			new StyledTextTextProperty(SWT.Verify);
56
			fail();
57
		} catch (IllegalArgumentException e) {
58
			assertTrue(true);
59
		}
60
	}
61
62
	/**
63
	 * s
64
	 * 
65
	 * @throws Exception
66
	 */
67
	public void testGetValueBeforeFocusOutChangeEventsFire() throws Exception {
68
		IObservableValue observableValue = SWTObservables.observeText(text,
69
				SWT.FocusOut);
70
		observableValue.addValueChangeListener(listener);
71
72
		String a = "a";
73
		String b = "b";
74
75
		text.setText(a);
76
		assertEquals(a, observableValue.getValue()); // fetch the value updating
77
		// the buffered value
78
		assertEquals(1, listener.count);
79
80
		text.setText(b);
81
82
		text.notifyListeners(SWT.FocusOut, null);
83
84
		assertEquals(2, listener.count);
85
		assertEquals(a, listener.event.diff.getOldValue());
86
		assertEquals(b, listener.event.diff.getNewValue());
87
	}
88
89
	public void testDispose() throws Exception {
90
		IObservableValue observableValue = SWTObservables.observeText(text,
91
				SWT.Modify);
92
		ValueChangeEventTracker testCounterValueChangeListener = new ValueChangeEventTracker();
93
		observableValue.addValueChangeListener(testCounterValueChangeListener);
94
95
		String expected1 = "Test123";
96
		text.setText(expected1);
97
98
		assertEquals(1, testCounterValueChangeListener.count);
99
		assertEquals(expected1, text.getText());
100
		assertEquals(expected1, observableValue.getValue());
101
102
		observableValue.dispose();
103
104
		String expected2 = "NewValue123";
105
		text.setText(expected2);
106
107
		assertEquals(1, testCounterValueChangeListener.count);
108
		assertEquals(expected2, text.getText());
109
	}
110
}
(-)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
 *******************************************************************************/
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
14
15
import junit.framework.Test;
16
import junit.framework.TestCase;
17
import junit.framework.TestSuite;
18
19
import org.eclipse.core.databinding.observable.IObservable;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.value.IObservableValue;
22
import org.eclipse.core.databinding.property.PropertyObservables;
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 PropertyObservables.observeValue(realm, text,
59
					StyledTextProperties.text(SWT.Modify));
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/internal/databinding/swt/ListObservableValue.java (-109 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
 *******************************************************************************/
13
package org.eclipse.jface.internal.databinding.swt;
14
15
import org.eclipse.core.databinding.observable.Diffs;
16
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.widgets.Event;
19
import org.eclipse.swt.widgets.List;
20
import org.eclipse.swt.widgets.Listener;
21
22
/**
23
 * @since 3.2
24
 * 
25
 */
26
public class ListObservableValue extends AbstractSWTObservableValue {
27
28
	private final List list;
29
30
	private boolean updating = false;
31
32
	private String currentValue;
33
34
	private Listener listener;
35
36
	/**
37
	 * @param list
38
	 */
39
	public ListObservableValue(List list) {
40
		super(list);
41
		this.list = list;
42
		this.currentValue = (String) doGetValue();
43
44
		if ((list.getStyle() & SWT.MULTI) > 0)
45
			throw new IllegalArgumentException(
46
					"SWT.SINGLE support only for a List selection"); //$NON-NLS-1$
47
48
		listener = new Listener() {
49
50
			public void handleEvent(Event event) {
51
				if (!updating) {
52
					Object oldValue = currentValue;
53
					currentValue = (String) doGetValue();
54
					fireValueChange(Diffs.createValueDiff(oldValue,
55
							currentValue));
56
				}
57
			}
58
59
		};
60
		list.addListener(SWT.Selection, listener);
61
	}
62
63
	public void doSetValue(Object value) {
64
		String oldValue = null;
65
		if (list.getSelection() != null && list.getSelection().length > 0)
66
			oldValue = list.getSelection()[0];
67
		try {
68
			updating = true;
69
			String items[] = list.getItems();
70
			int index = -1;
71
			if (items != null && value != null) {
72
				for (int i = 0; i < items.length; i++) {
73
					if (value.equals(items[i])) {
74
						index = i;
75
						break;
76
					}
77
				}
78
				list.select(index); // -1 will not "unselect"
79
			}
80
			currentValue = (String) value;
81
		} finally {
82
			updating = false;
83
		}
84
		fireValueChange(Diffs.createValueDiff(oldValue, value));
85
	}
86
87
	public Object doGetValue() {
88
		int index = list.getSelectionIndex();
89
		if (index >= 0)
90
			return list.getItem(index);
91
		return null;
92
	}
93
94
	public Object getValueType() {
95
		return String.class;
96
	}
97
98
	/*
99
	 * (non-Javadoc)
100
	 *
101
	 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
102
	 */
103
	public synchronized void dispose() {
104
		super.dispose();
105
		if (listener != null && !list.isDisposed()) {
106
			list.removeListener(SWT.Selection, listener);
107
		}
108
	}
109
}
(-)src/org/eclipse/jface/internal/databinding/swt/CComboSingleSelectionObservableValue.java (-81 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
 *******************************************************************************/
13
package org.eclipse.jface.internal.databinding.swt;
14
15
import org.eclipse.core.databinding.observable.Realm;
16
import org.eclipse.swt.custom.CCombo;
17
import org.eclipse.swt.events.SelectionEvent;
18
import org.eclipse.swt.events.SelectionListener;
19
20
/**
21
 * @since 1.0
22
 *
23
 */
24
public class CComboSingleSelectionObservableValue extends
25
		SingleSelectionObservableValue {
26
27
	private SelectionListener selectionListener;
28
29
	/**
30
	 * @param combo
31
	 */
32
	public CComboSingleSelectionObservableValue(CCombo combo) {
33
		super(combo);
34
	}
35
	
36
	/**
37
	 * @param realm
38
	 * @param combo
39
	 */
40
	public CComboSingleSelectionObservableValue(Realm realm, CCombo combo) {
41
		super(realm, combo);
42
	}
43
44
	private CCombo getCCombo() {
45
		return (CCombo) getWidget();
46
	}
47
48
	protected void doAddSelectionListener(final Runnable runnable) {
49
		selectionListener = new SelectionListener() {
50
			public void widgetDefaultSelected(SelectionEvent e) {
51
				runnable.run();
52
			}
53
54
			public void widgetSelected(SelectionEvent e) {
55
				runnable.run();
56
			}
57
		};
58
		getCCombo().addSelectionListener(selectionListener);
59
	}
60
61
	protected int doGetSelectionIndex() {
62
		return getCCombo().getSelectionIndex();
63
	}
64
65
	protected void doSetSelectionIndex(int index) {
66
		getCCombo().setText(getCCombo().getItem(index));
67
	}
68
69
	/*
70
	 * (non-Javadoc)
71
	 *
72
	 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
73
	 */
74
	public synchronized void dispose() {
75
		super.dispose();
76
		if (selectionListener != null && !getCCombo().isDisposed()) {
77
			getCCombo().removeSelectionListener(selectionListener);
78
		}
79
80
	}
81
}
(-)src/org/eclipse/jface/internal/databinding/swt/ShellObservableValue.java (-74 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 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 207844)
10
 *     IBM Corporation - initial API and implementation
11
 *     Brad Reynolds - initial API and implementation
12
 *     Matthew Hall - bug 212235
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.databinding.observable.value.IObservableValue;
19
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
20
import org.eclipse.swt.widgets.Shell;
21
22
/**
23
 * An {@link IObservableValue} that tracks the text of a Shell.
24
 * 
25
 * @since 1.2
26
 */
27
public class ShellObservableValue extends AbstractSWTObservableValue {
28
29
	private final Shell shell;
30
31
	/**
32
	 * Constructs a ShellObservableValue which tracks the text of the given
33
	 * Shell.
34
	 * 
35
	 * @param shell
36
	 *            the shell to track
37
	 */
38
	public ShellObservableValue(Shell shell) {
39
		super(shell);
40
		this.shell = shell;
41
	}
42
43
	/**
44
	 * Constructs a ShellObservableValue belonging to the given realm, which
45
	 * tracks the text of the given shell.
46
	 * 
47
	 * @param realm
48
	 *            the realm of the constructed observable
49
	 * @param shell
50
	 *            the shell to track
51
	 */
52
	public ShellObservableValue(Realm realm, Shell shell) {
53
		super(realm, shell);
54
		this.shell = shell;
55
	}
56
57
	protected void doSetValue(final Object value) {
58
		String oldValue = shell.getText();
59
		String newValue = value == null ? "" : value.toString(); //$NON-NLS-1$
60
		shell.setText(newValue);
61
62
		if (!newValue.equals(oldValue)) {
63
			fireValueChange(Diffs.createValueDiff(oldValue, newValue));
64
		}
65
	}
66
67
	protected Object doGetValue() {
68
		return shell.getText();
69
	}
70
71
	public Object getValueType() {
72
		return String.class;
73
	}
74
}
(-)src/org/eclipse/jface/internal/databinding/swt/CComboObservableList.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.custom.CCombo;
15
16
/**
17
 * @since 3.2
18
 * 
19
 */
20
public class CComboObservableList extends SWTObservableList {
21
22
	private final CCombo ccombo;
23
24
	/**
25
	 * @param ccombo
26
	 */
27
	public CComboObservableList(CCombo ccombo) {
28
		super(SWTObservables.getRealm(ccombo.getDisplay()));
29
		this.ccombo = ccombo;
30
	}
31
32
	protected int getItemCount() {
33
		return ccombo.getItemCount();
34
	}
35
36
	protected void setItems(String[] newItems) {
37
		ccombo.setItems(newItems);
38
	}
39
40
	protected String[] getItems() {
41
		return ccombo.getItems();
42
	}
43
44
	protected String getItem(int index) {
45
		return ccombo.getItem(index);
46
	}
47
48
	protected void setItem(int index, String string) {
49
		ccombo.setItem(index, string);
50
	}
51
}
(-)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/DelayedObservableValue.java (-57 / +39 lines)
Lines 16-32 Link Here
16
package org.eclipse.jface.internal.databinding.swt;
16
package org.eclipse.jface.internal.databinding.swt;
17
17
18
import org.eclipse.core.databinding.observable.Diffs;
18
import org.eclipse.core.databinding.observable.Diffs;
19
import org.eclipse.core.databinding.observable.IStaleListener;
20
import org.eclipse.core.databinding.observable.ObservableTracker;
21
import org.eclipse.core.databinding.observable.StaleEvent;
19
import org.eclipse.core.databinding.observable.StaleEvent;
22
import org.eclipse.core.databinding.observable.value.IObservableValue;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
23
import org.eclipse.core.databinding.observable.value.IValueChangeListener;
24
import org.eclipse.core.databinding.observable.value.IVetoableValue;
21
import org.eclipse.core.databinding.observable.value.IVetoableValue;
25
import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
22
import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
26
import org.eclipse.core.databinding.observable.value.ValueChangingEvent;
23
import org.eclipse.core.databinding.observable.value.ValueChangingEvent;
27
import org.eclipse.core.databinding.observable.value.ValueDiff;
24
import org.eclipse.core.databinding.observable.value.ValueDiff;
28
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
25
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
29
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
30
import org.eclipse.jface.util.Util;
26
import org.eclipse.jface.util.Util;
31
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.SWT;
32
import org.eclipse.swt.widgets.Control;
28
import org.eclipse.swt.widgets.Control;
Lines 49-55 Link Here
49
 * 
45
 * 
50
 * @since 1.2
46
 * @since 1.2
51
 */
47
 */
52
public class DelayedObservableValue extends AbstractSWTObservableValue {
48
public class DelayedObservableValue extends SWTObservableValueDecorator {
53
	class ValueUpdater implements Runnable {
49
	class ValueUpdater implements Runnable {
54
		private final Object oldValue;
50
		private final Object oldValue;
55
51
Lines 75-94 Link Here
75
		}
71
		}
76
	}
72
	}
77
73
78
	private IStaleListener staleListener = new IStaleListener() {
79
		public void handleStale(StaleEvent staleEvent) {
80
			if (!updating)
81
				fireStale();
82
		}
83
	};
84
85
	private IValueChangeListener valueChangeListener = new IValueChangeListener() {
86
		public void handleValueChange(ValueChangeEvent event) {
87
			if (!updating)
88
				makeDirty();
89
		}
90
	};
91
92
	private Listener focusOutListener = new Listener() {
74
	private Listener focusOutListener = new Listener() {
93
		public void handleEvent(Event event) {
75
		public void handleEvent(Event event) {
94
			// Force update on focus out
76
			// Force update on focus out
Lines 98-105 Link Here
98
	};
80
	};
99
81
100
	private final int delay;
82
	private final int delay;
101
	private ISWTObservableValue observable;
83
	private Widget widget;
102
	private Control control;
103
84
104
	private boolean dirty = true;
85
	private boolean dirty = true;
105
	private Object cachedValue = null;
86
	private Object cachedValue = null;
Lines 121-144 Link Here
121
	 */
102
	 */
122
	public DelayedObservableValue(int delayMillis,
103
	public DelayedObservableValue(int delayMillis,
123
			ISWTObservableValue observable) {
104
			ISWTObservableValue observable) {
124
		super(observable.getRealm(), observable.getWidget());
105
		super(observable, observable.getWidget());
125
		this.delay = delayMillis;
106
		this.delay = delayMillis;
126
		this.observable = observable;
127
107
128
		observable.addValueChangeListener(valueChangeListener);
108
		widget = observable.getWidget();
129
		observable.addStaleListener(staleListener);
130
		Widget widget = observable.getWidget();
131
		if (widget instanceof Control) {
109
		if (widget instanceof Control) {
132
			control = (Control) widget;
110
			widget.addListener(SWT.FocusOut, focusOutListener);
133
			control.addListener(SWT.FocusOut, focusOutListener);
134
		}
111
		}
135
112
136
		cachedValue = doGetValue();
113
		cachedValue = observable.getValue();
114
	}
115
116
	protected void handleValueChange(ValueChangeEvent event) {
117
		if (!updating)
118
			makeDirty();
119
	}
120
121
	protected void handleStaleEvent(StaleEvent event) {
122
		if (!updating)
123
			super.handleStaleEvent(event);
137
	}
124
	}
138
125
139
	protected Object doGetValue() {
126
	public Object getValue() {
127
		getterCalled();
128
129
		if (!hasListeners())
130
			return super.getValue();
131
140
		if (dirty) {
132
		if (dirty) {
141
			cachedValue = observable.getValue();
133
			cachedValue = super.getValue();
142
			dirty = false;
134
			dirty = false;
143
135
144
			if (updater != null && !updater.running) {
136
			if (updater != null && !updater.running) {
Lines 150-156 Link Here
150
		return cachedValue;
142
		return cachedValue;
151
	}
143
	}
152
144
153
	protected void doSetValue(Object value) {
145
	public void setValue(Object value) {
146
		checkRealm();
147
154
		updating = true;
148
		updating = true;
155
		try {
149
		try {
156
			// Principle of least surprise: setValue overrides any pending
150
			// Principle of least surprise: setValue overrides any pending
Lines 159-202 Link Here
159
			cancelScheduledUpdate();
153
			cancelScheduledUpdate();
160
154
161
			Object oldValue = cachedValue;
155
			Object oldValue = cachedValue;
162
			observable.setValue(value);
156
			super.setValue(value);
163
			// Bug 215297 - target observable could veto or override value
157
			// Bug 215297 - target observable could veto or override value
164
			// passed to setValue(). Make sure we cache whatever is set.
158
			// passed to setValue(). Make sure we cache whatever is set.
165
			cachedValue = observable.getValue();
159
			Object newValue = cachedValue = super.getValue();
166
160
167
			if (!Util.equals(oldValue, cachedValue))
161
			if (hasListeners() && !Util.equals(oldValue, newValue))
168
				fireValueChange(Diffs.createValueDiff(oldValue, cachedValue));
162
				fireValueChange(Diffs.createValueDiff(oldValue, newValue));
169
		} finally {
163
		} finally {
170
			updating = false;
164
			updating = false;
171
		}
165
		}
172
	}
166
	}
173
167
174
	public boolean isStale() {
168
	protected boolean hasListeners() {
175
		ObservableTracker.getterCalled(this);
169
		return super.hasListeners();
176
		return (dirty && updater != null) || observable.isStale();
177
	}
170
	}
178
171
179
	/**
172
	public boolean isStale() {
180
	 * Returns the type of the value from {@link #doGetValue()}, i.e.
173
		getterCalled();
181
	 * String.class
174
		return (dirty && updater != null) || super.isStale();
182
	 * 
183
	 * @see org.eclipse.core.databinding.observable.value.IObservableValue#getValueType()
184
	 */
185
	public Object getValueType() {
186
		return observable.getValueType();
187
	}
175
	}
188
176
189
	public void dispose() {
177
	public void dispose() {
190
		cancelScheduledUpdate();
178
		cancelScheduledUpdate();
191
		if (observable != null) {
179
		if (widget instanceof Control) {
192
			observable.dispose();
180
			widget.removeListener(SWT.FocusOut, focusOutListener);
193
			observable.removeValueChangeListener(valueChangeListener);
181
			widget = null;
194
			observable.removeStaleListener(staleListener);
195
			observable = null;
196
		}
197
		if (control != null) {
198
			control.removeListener(SWT.FocusOut, focusOutListener);
199
			control = null;
200
		}
182
		}
201
		super.dispose();
183
		super.dispose();
202
	}
184
	}
Lines 219-225 Link Here
219
201
220
	private void scheduleUpdate() {
202
	private void scheduleUpdate() {
221
		updater = new ValueUpdater(cachedValue);
203
		updater = new ValueUpdater(cachedValue);
222
		observable.getWidget().getDisplay().timerExec(delay, updater);
204
		widget.getDisplay().timerExec(delay, updater);
223
	}
205
	}
224
206
225
	private void internalFireValueChange(final Object oldValue) {
207
	private void internalFireValueChange(final Object oldValue) {
(-)src/org/eclipse/jface/internal/databinding/swt/TableSingleSelectionObservableValue.java (-80 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
 *******************************************************************************/
13
package org.eclipse.jface.internal.databinding.swt;
14
15
import org.eclipse.core.databinding.observable.Realm;
16
import org.eclipse.swt.events.SelectionEvent;
17
import org.eclipse.swt.events.SelectionListener;
18
import org.eclipse.swt.widgets.Table;
19
20
/**
21
 * @since 1.0
22
 * 
23
 */
24
public class TableSingleSelectionObservableValue extends
25
		SingleSelectionObservableValue {
26
27
	private SelectionListener selectionListener;
28
29
	/**
30
	 * @param table
31
	 */
32
	public TableSingleSelectionObservableValue(Table table) {
33
		super(table);
34
	}
35
	
36
	/**
37
	 * @param realm
38
	 * @param table
39
	 */
40
	public TableSingleSelectionObservableValue(Realm realm, Table table) {
41
		super(realm, table);
42
	}
43
44
	private Table getTable() {
45
		return (Table) getWidget();
46
	}
47
48
	protected void doAddSelectionListener(final Runnable runnable) {
49
		selectionListener = new SelectionListener() {
50
			public void widgetDefaultSelected(SelectionEvent e) {
51
				runnable.run();
52
			}
53
54
			public void widgetSelected(SelectionEvent e) {
55
				runnable.run();
56
			}
57
		};
58
		getTable().addSelectionListener(selectionListener);
59
	}
60
61
	protected int doGetSelectionIndex() {
62
		return getTable().getSelectionIndex();
63
	}
64
65
	protected void doSetSelectionIndex(int index) {
66
		getTable().setSelection(index);
67
	}
68
69
	/*
70
	 * (non-Javadoc)
71
	 *
72
	 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
73
	 */
74
	public synchronized void dispose() {
75
		super.dispose();
76
		if (selectionListener != null && !getTable().isDisposed()) {
77
			getTable().removeSelectionListener(selectionListener);
78
		}
79
	}
80
}
(-)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/SWTObservableList.java (-193 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 208858
11
 *******************************************************************************/
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import java.util.ArrayList;
15
import java.util.Arrays;
16
import java.util.Collection;
17
import java.util.List;
18
19
import org.eclipse.core.databinding.BindingException;
20
import org.eclipse.core.databinding.observable.Diffs;
21
import org.eclipse.core.databinding.observable.ObservableTracker;
22
import org.eclipse.core.databinding.observable.Realm;
23
import org.eclipse.core.databinding.observable.list.AbstractObservableList;
24
25
/**
26
 * Abstract base class of CComboObservableList, ComboObservableList, and
27
 * ListObservableList.
28
 * 
29
 * @since 3.2
30
 * 
31
 */
32
public abstract class SWTObservableList extends AbstractObservableList {
33
34
	/**
35
	 * 
36
	 */
37
	public SWTObservableList() {
38
		super();
39
	}
40
41
	/**
42
	 * @param realm
43
	 */
44
	public SWTObservableList(Realm realm) {
45
		super(realm);
46
	}
47
48
	public void add(int index, Object element) {
49
		int size = doGetSize();
50
		if (index < 0 || index > size)
51
			index = size;
52
		String[] newItems = new String[size + 1];
53
		System.arraycopy(getItems(), 0, newItems, 0, index);
54
		newItems[index] = (String) element;
55
		System.arraycopy(getItems(), index, newItems, index + 1, size - index);
56
		setItems(newItems);
57
		fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
58
				true, element)));
59
	}
60
61
	public int doGetSize() {
62
		return getItemCount();
63
	}
64
65
	public Object get(int index) {
66
		getterCalled();
67
		return getItem(index);
68
	}
69
70
	public Object getElementType() {
71
		return String.class;
72
	}
73
74
	/**
75
	 * @param index
76
	 * @return the item at the given index
77
	 */
78
	protected abstract String getItem(int index);
79
80
	/**
81
	 * @return the item count
82
	 */
83
	protected abstract int getItemCount();
84
85
	/**
86
	 * @return the items
87
	 */
88
	protected abstract String[] getItems();
89
90
	private void getterCalled() {
91
		ObservableTracker.getterCalled(this);
92
	}
93
94
	public Object remove(int index) {
95
		getterCalled();
96
		int size = doGetSize();
97
		if (index < 0 || index > size - 1)
98
			throw new BindingException(
99
					"Request to remove an element out of the collection bounds"); //$NON-NLS-1$
100
101
		String[] newItems = new String[size - 1];
102
		String oldElement = getItem(index);
103
		if (newItems.length > 0) {
104
			System.arraycopy(getItems(), 0, newItems, 0, index);
105
			if (size - 1 > index) {
106
				System.arraycopy(getItems(), index + 1, newItems, index, size
107
						- index - 1);
108
			}
109
		}
110
		setItems(newItems);
111
		fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
112
				false, oldElement)));
113
		return oldElement;
114
	}
115
116
	public Object set(int index, Object element) {
117
		String oldElement = getItem(index);
118
		setItem(index, (String) element);
119
		fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(index,
120
				false, oldElement), Diffs.createListDiffEntry(index, true,
121
				element)));
122
		return oldElement;
123
	}
124
125
	public Object move(int oldIndex, int newIndex) {
126
		checkRealm();
127
		if (oldIndex == newIndex)
128
			return get(oldIndex);
129
		int size = doGetSize();
130
		if (oldIndex < 0 || oldIndex >= size)
131
			throw new IndexOutOfBoundsException(
132
					"oldIndex: " + oldIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$
133
		if (newIndex < 0 || newIndex >= size)
134
			throw new IndexOutOfBoundsException(
135
					"newIndex: " + newIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$
136
137
		String[] items = getItems();
138
		String[] newItems = new String[size];
139
		String element = items[oldIndex];
140
		if (newItems.length > 0) {
141
			System.arraycopy(items, 0, newItems, 0, size);
142
			if (oldIndex < newIndex) {
143
				System.arraycopy(items, oldIndex + 1, newItems, oldIndex,
144
						newIndex - oldIndex);
145
			} else {
146
				System.arraycopy(items, newIndex, newItems, newIndex + 1,
147
						oldIndex - newIndex);
148
			}
149
			newItems[newIndex] = element;
150
		}
151
		setItems(newItems);
152
		fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(oldIndex,
153
				false, element), Diffs.createListDiffEntry(newIndex, true,
154
				element)));
155
		return element;
156
	}
157
158
	public boolean removeAll(Collection c) {
159
		checkRealm();
160
		List oldItems = Arrays.asList(getItems());
161
		List newItems = new ArrayList(oldItems);
162
		boolean removedAll = newItems.removeAll(c);
163
		if (removedAll) {
164
			setItems((String[]) newItems.toArray(new String[newItems.size()]));
165
			fireListChange(Diffs.computeListDiff(oldItems, newItems));
166
		}
167
		return removedAll;
168
	}
169
170
	public boolean retainAll(Collection c) {
171
		checkRealm();
172
		List oldItems = Arrays.asList(getItems());
173
		List newItems = new ArrayList(oldItems);
174
		boolean retainedAll = newItems.retainAll(c);
175
		if (retainedAll) {
176
			setItems((String[]) newItems.toArray(new String[newItems.size()]));
177
			fireListChange(Diffs.computeListDiff(oldItems, newItems));
178
		}
179
		return retainedAll;
180
	}
181
182
	/**
183
	 * @param index
184
	 * @param string
185
	 */
186
	protected abstract void setItem(int index, String string);
187
188
	/**
189
	 * @param newItems
190
	 */
191
	protected abstract void setItems(String[] newItems);
192
193
}
(-)src/org/eclipse/jface/internal/databinding/swt/TextEditableObservableValue.java (-75 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007, 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.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.Text;
18
19
/**
20
 * Observable value for the editable property of a Text.
21
 * 
22
 * @since 1.1
23
 */
24
public class TextEditableObservableValue extends AbstractSWTObservableValue {
25
	private Text text;
26
	
27
	/**
28
	 * @param text
29
	 */
30
	public TextEditableObservableValue(Text text) {
31
		super(text);	
32
		this.text = text;
33
	}
34
	
35
	/**
36
	 * @param realm
37
	 * @param text
38
	 */
39
	public TextEditableObservableValue(Realm realm, Text text) {
40
		super(realm, text);
41
		this.text = text;
42
	}
43
44
	/* (non-Javadoc)
45
	 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#doGetValue()
46
	 */
47
	protected Object doGetValue() {
48
		return (text.getEditable()) ? Boolean.TRUE : Boolean.FALSE;
49
	}
50
	
51
	/* (non-Javadoc)
52
	 * @see org.eclipse.core.databinding.observable.value.IObservableValue#getValueType()
53
	 */
54
	public Object getValueType() {
55
		return Boolean.TYPE;
56
	}
57
	
58
	/* (non-Javadoc)
59
	 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#doSetValue(java.lang.Object)
60
	 */
61
	protected void doSetValue(Object value) {
62
		if (value == null) {
63
			throw new IllegalArgumentException("Parameter value was null."); //$NON-NLS-1$
64
		}
65
		
66
		Boolean oldValue = new Boolean(text.getEditable());
67
		Boolean newValue = (Boolean) value;
68
		
69
		text.setEditable(newValue.booleanValue());
70
		
71
		if (!oldValue.equals(newValue)) {
72
			fireValueChange(Diffs.createValueDiff(oldValue, newValue));
73
		}
74
	}
75
}
(-)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/TextObservableValue.java (-198 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 135446)
11
 *     Brad Reynolds - bug 164653
12
 *******************************************************************************/
13
package org.eclipse.jface.internal.databinding.swt;
14
15
import org.eclipse.core.databinding.observable.Diffs;
16
import org.eclipse.core.databinding.observable.IObservable;
17
import org.eclipse.core.databinding.observable.Realm;
18
import org.eclipse.jface.databinding.swt.SWTObservables;
19
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTVetoableValue;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.events.VerifyEvent;
22
import org.eclipse.swt.events.VerifyListener;
23
import org.eclipse.swt.widgets.Event;
24
import org.eclipse.swt.widgets.Listener;
25
import org.eclipse.swt.widgets.Text;
26
27
/**
28
 * {@link IObservable} implementation that wraps a {@link Text} widget. The time
29
 * at which listeners should be notified about changes to the text is specified
30
 * on construction.
31
 * 
32
 * <dl>
33
 * <dt>Events:</dt>
34
 * <dd> If the update event type (specified on construction) is
35
 * <code>SWT.Modify</code> a value change event will be fired on every key
36
 * stroke. If the update event type is <code>SWT.FocusOut</code> a value
37
 * change event will be fired on focus out. When in either mode if the user is
38
 * entering text and presses [Escape] the value will be reverted back to the
39
 * last value set using doSetValue(). Regardless of the update event type a
40
 * value changing event will fire on verify to enable vetoing of changes.</dd>
41
 * </dl>
42
 * 
43
 * @since 1.0
44
 */
45
public class TextObservableValue extends AbstractSWTVetoableValue {
46
47
	/**
48
	 * {@link Text} widget that this is being observed.
49
	 */
50
	private final Text text;
51
52
	/**
53
	 * Flag to track when the model is updating the widget. When
54
	 * <code>true</code> the handlers for the SWT events should not process
55
	 * the event as this would cause an infinite loop.
56
	 */
57
	private boolean updating = false;
58
59
	/**
60
	 * SWT event that on firing this observable will fire change events to its
61
	 * listeners.
62
	 */
63
	private final int updateEventType;
64
65
	/**
66
	 * Valid types for the {@link #updateEventType}.
67
	 */
68
	private static final int[] validUpdateEventTypes = new int[] { SWT.Modify,
69
			SWT.FocusOut, SWT.None };
70
71
	/**
72
	 * Previous value of the Text.
73
	 */
74
	private String oldValue;
75
76
	private Listener updateListener = new Listener() {
77
		public void handleEvent(Event event) {
78
			if (!updating) {
79
				String newValue = text.getText();
80
81
				if (!newValue.equals(oldValue)) {
82
					fireValueChange(Diffs.createValueDiff(oldValue, newValue));					
83
					oldValue = newValue;
84
				}
85
			}
86
		}
87
	};
88
89
	private VerifyListener verifyListener;
90
91
	/**
92
	 * Constructs a new instance bound to the given <code>text</code> widget
93
	 * and configured to fire change events to its listeners at the time of the
94
	 * <code>updateEventType</code>.
95
	 * 
96
	 * @param text
97
	 * @param updateEventType
98
	 *            SWT event constant as to what SWT event to update the model in
99
	 *            response to. Appropriate values are: <code>SWT.Modify</code>,
100
	 *            <code>SWT.FocusOut</code>, <code>SWT.None</code>.
101
	 * @throws IllegalArgumentException
102
	 *             if <code>updateEventType</code> is an incorrect type.
103
	 */
104
	public TextObservableValue(final Text text, int updateEventType) {
105
		this(SWTObservables.getRealm(text.getDisplay()), text, updateEventType);
106
	}
107
	
108
	/**
109
	 * Constructs a new instance.
110
	 * 
111
	 * @param realm can not be <code>null</code>
112
	 * @param text
113
	 * @param updateEventType
114
	 */
115
	public TextObservableValue(final Realm realm, Text text, int updateEventType) {
116
		super(realm, text);
117
		
118
		boolean eventValid = false;
119
		for (int i = 0; !eventValid && i < validUpdateEventTypes.length; i++) {
120
			eventValid = (updateEventType == validUpdateEventTypes[i]);
121
		}
122
		if (!eventValid) {
123
			throw new IllegalArgumentException(
124
					"UpdateEventType [" + updateEventType + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
125
		}
126
		this.text = text;
127
		this.updateEventType = updateEventType;
128
		if (updateEventType != SWT.None) {
129
			text.addListener(updateEventType, updateListener);
130
		}
131
		
132
		oldValue = text.getText();
133
		
134
		verifyListener = new VerifyListener() {
135
			public void verifyText(VerifyEvent e) {
136
				if (!updating) {
137
					String currentText = TextObservableValue.this.text
138
							.getText();
139
					String newText = currentText.substring(0, e.start) + e.text
140
							+ currentText.substring(e.end);
141
					if (!fireValueChanging(Diffs.createValueDiff(currentText,
142
							newText))) {
143
						e.doit = false;
144
					}
145
				}
146
			}
147
		};
148
		text.addVerifyListener(verifyListener);
149
	}
150
151
	/**
152
	 * Sets the bound {@link Text Text's} text to the passed <code>value</code>.
153
	 * 
154
	 * @param value
155
	 *            new value, String expected
156
	 * @see org.eclipse.core.databinding.observable.value.AbstractVetoableValue#doSetApprovedValue(java.lang.Object)
157
	 * @throws ClassCastException
158
	 *             if the value is anything other than a String
159
	 */
160
	protected void doSetApprovedValue(final Object value) {
161
		try {
162
			updating = true;
163
			text.setText(value == null ? "" : value.toString()); //$NON-NLS-1$
164
			oldValue = text.getText();
165
		} finally {
166
			updating = false;
167
		}
168
	}
169
170
	/**
171
	 * Returns the current value of the {@link Text}.
172
	 * 
173
	 * @see org.eclipse.core.databinding.observable.value.AbstractVetoableValue#doGetValue()
174
	 */
175
	public Object doGetValue() {
176
		return oldValue = text.getText();
177
	}
178
179
	/**
180
	 * Returns the type of the value from {@link #doGetValue()}, i.e.
181
	 * String.class
182
	 * 
183
	 * @see org.eclipse.core.databinding.observable.value.IObservableValue#getValueType()
184
	 */
185
	public Object getValueType() {
186
		return String.class;
187
	}
188
189
	public void dispose() {
190
		if (!text.isDisposed()) {
191
			if (updateEventType != SWT.None) {
192
				text.removeListener(updateEventType, updateListener);
193
			}
194
			text.removeVerifyListener(verifyListener);
195
		}
196
		super.dispose();
197
	}
198
}
(-)src/org/eclipse/jface/internal/databinding/swt/SpinnerObservableValue.java (-151 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.Spinner;
23
24
/**
25
 * @since 1.0
26
 * 
27
 */
28
public class SpinnerObservableValue extends AbstractSWTObservableValue {
29
30
	private final Spinner spinner;
31
32
	private final String attribute;
33
34
	private boolean updating = false;
35
36
	private int currentSelection;
37
38
	private ModifyListener modifyListener;
39
40
	/**
41
	 * @param spinner
42
	 * @param attribute
43
	 */
44
	public SpinnerObservableValue(Spinner spinner, String attribute) {
45
		super(spinner);
46
		this.spinner = spinner;
47
		this.attribute = attribute;
48
		init();
49
	}
50
	
51
	/**
52
	 * @param realm
53
	 * @param spinner
54
	 * @param attribute
55
	 */
56
	public SpinnerObservableValue(Realm realm, Spinner spinner, String attribute) {
57
		super(realm, spinner);
58
		this.spinner = spinner;
59
		this.attribute = attribute;
60
		init();
61
	}
62
	
63
	private void init() {		
64
		if (attribute.equals(SWTProperties.SELECTION)) {
65
			currentSelection = spinner.getSelection();
66
			modifyListener = new ModifyListener() {
67
				public void modifyText(ModifyEvent e) {
68
					if (!updating) {
69
						int newSelection = SpinnerObservableValue.this.spinner
70
						.getSelection();
71
						notifyIfChanged(currentSelection, newSelection);
72
						currentSelection = newSelection;
73
					}
74
				}
75
			};
76
			spinner.addModifyListener(modifyListener);
77
		} else if (!attribute.equals(SWTProperties.MIN)
78
				&& !attribute.equals(SWTProperties.MAX)) {
79
			throw new IllegalArgumentException(
80
					"Attribute name not valid: " + attribute); //$NON-NLS-1$
81
		}
82
	}
83
84
	public void doSetValue(final Object value) {
85
		int oldValue;
86
		int newValue;
87
		try {
88
			updating = true;
89
			newValue = ((Integer) value).intValue();
90
			if (attribute.equals(SWTProperties.SELECTION)) {
91
				oldValue = spinner.getSelection();
92
				spinner.setSelection(newValue);
93
				currentSelection = newValue;
94
			} else if (attribute.equals(SWTProperties.MIN)) {
95
				oldValue = spinner.getMinimum();
96
				spinner.setMinimum(newValue);
97
			} else if (attribute.equals(SWTProperties.MAX)) {
98
				oldValue = spinner.getMaximum();
99
				spinner.setMaximum(newValue);
100
			} else {
101
				Assert.isTrue(false, "invalid attribute name:" + attribute); //$NON-NLS-1$
102
				return;
103
			}
104
			notifyIfChanged(oldValue, newValue);
105
		} finally {
106
			updating = false;
107
		}
108
	}
109
110
	public Object doGetValue() {
111
		int value = 0;
112
		if (attribute.equals(SWTProperties.SELECTION)) {
113
			value = spinner.getSelection();
114
		} else if (attribute.equals(SWTProperties.MIN)) {
115
			value = spinner.getMinimum();
116
		} else if (attribute.equals(SWTProperties.MAX)) {
117
			value = spinner.getMaximum();
118
		}
119
		return new Integer(value);
120
	}
121
122
	public Object getValueType() {
123
		return Integer.TYPE;
124
	}
125
126
	/**
127
	 * @return attribute being observed
128
	 */
129
	public String getAttribute() {
130
		return attribute;
131
	}
132
133
	/*
134
	 * (non-Javadoc)
135
	 *
136
	 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
137
	 */
138
	public synchronized void dispose() {
139
		super.dispose();
140
		if (modifyListener != null && !spinner.isDisposed()) {
141
			spinner.removeModifyListener(modifyListener);
142
		}
143
	}
144
	
145
	private void notifyIfChanged(int oldValue, int newValue) {
146
		if (oldValue != newValue) {
147
			fireValueChange(Diffs.createValueDiff(new Integer(oldValue),
148
					new Integer(newValue)));
149
		}
150
	}
151
}
(-)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/internal/databinding/swt/SWTProperties.java (-77 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
 *     Matt Carter - bug 170668
11
 *     Brad Reynolds - bug 170848
12
 *******************************************************************************/
13
package org.eclipse.jface.internal.databinding.swt;
14
15
/**
16
 * Constants used to describe properties of SWT controls.
17
 * 
18
 * @since 1.0
19
 *
20
 */
21
public interface SWTProperties {
22
23
	/**
24
	 * Applies to Control
25
	 */
26
	public static final String ENABLED = "enabled"; //$NON-NLS-1$
27
	/**
28
	 * Applies to Control
29
	 */
30
	public static final String VISIBLE = "visible"; //$NON-NLS-1$
31
	/**
32
	 * Applies to Control
33
	 */
34
	public static final String TOOLTIP_TEXT = "tooltip"; //$NON-NLS-1$	
35
	/**
36
	 * Applies to
37
	 */
38
	public static final String ITEMS = "items"; //$NON-NLS-1$
39
	/**
40
	 * Applies to Spinner
41
	 */
42
	public static final String MAX = "max"; //$NON-NLS-1$
43
	/**
44
	 * Applies to Spinner
45
	 */
46
	public static final String MIN = "min"; //$NON-NLS-1$
47
	/**
48
	 * Applies to Spinner, Button
49
	 */
50
	public static final String SELECTION = "selection"; //$NON-NLS-1$
51
	/**
52
	 * Applies to Spinner, Button
53
	 */
54
	public static final String SELECTION_INDEX = "index"; //$NON-NLS-1$
55
	/**
56
	 * Applies to Text, Label, Combo
57
	 */
58
	public static final String TEXT = "text"; //$NON-NLS-1$
59
	
60
	/**
61
	 * Applies to Label, CLabel.
62
	 */
63
	public static final String IMAGE = "image"; //$NON-NLS-1$
64
	/**
65
	 * Applies to Control
66
	 */
67
	public static final String FOREGROUND = "foreground"; //$NON-NLS-1$
68
	/**
69
	 * Applies to Control
70
	 */
71
	public static final String BACKGROUND = "background"; //$NON-NLS-1$
72
	/**
73
	 * Applies to Control
74
	 */
75
	public static final String FONT = "font"; //$NON-NLS-1$
76
77
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlObservableValue.java (-106 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
 *     Matt Carter - bug 170668
12
 *     Brad Reynolds - bug 170848
13
 *******************************************************************************/
14
package org.eclipse.jface.internal.databinding.swt;
15
16
import java.util.HashMap;
17
import java.util.Map;
18
19
import org.eclipse.core.databinding.observable.Diffs;
20
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
21
import org.eclipse.swt.graphics.Color;
22
import org.eclipse.swt.graphics.Font;
23
import org.eclipse.swt.widgets.Control;
24
25
/**
26
 * @since 1.0
27
 * 
28
 */
29
public class ControlObservableValue extends AbstractSWTObservableValue {
30
31
	private final Control control;
32
33
	private final String attribute;
34
35
	private Object valueType;
36
	
37
	private static final Map SUPPORTED_ATTRIBUTES = new HashMap();
38
	static {
39
		SUPPORTED_ATTRIBUTES.put(SWTProperties.ENABLED, Boolean.TYPE);
40
		SUPPORTED_ATTRIBUTES.put(SWTProperties.VISIBLE, Boolean.TYPE);
41
		SUPPORTED_ATTRIBUTES.put(SWTProperties.TOOLTIP_TEXT, String.class);
42
		SUPPORTED_ATTRIBUTES.put(SWTProperties.FOREGROUND, Color.class);
43
		SUPPORTED_ATTRIBUTES.put(SWTProperties.BACKGROUND, Color.class);
44
		SUPPORTED_ATTRIBUTES.put(SWTProperties.FONT, Font.class);
45
	}
46
	
47
	/**
48
	 * @param control
49
	 * @param attribute
50
	 */
51
	public ControlObservableValue(Control control, String attribute) {
52
		super(control);
53
		this.control = control;
54
		this.attribute = attribute;
55
		if (SUPPORTED_ATTRIBUTES.keySet().contains(attribute)) {
56
			this.valueType = SUPPORTED_ATTRIBUTES.get(attribute); 
57
		} else {
58
			throw new IllegalArgumentException();
59
		}
60
	}
61
62
	public void doSetValue(Object value) {
63
		Object oldValue = doGetValue();
64
		if (attribute.equals(SWTProperties.ENABLED)) {
65
			control.setEnabled(((Boolean) value).booleanValue());
66
		} else if (attribute.equals(SWTProperties.VISIBLE)) {
67
			control.setVisible(((Boolean) value).booleanValue());
68
		} else if (attribute.equals(SWTProperties.TOOLTIP_TEXT)) {
69
			control.setToolTipText((String) value);
70
		} else if (attribute.equals(SWTProperties.FOREGROUND)) {
71
			control.setForeground((Color) value);
72
		} else if (attribute.equals(SWTProperties.BACKGROUND)) {
73
			control.setBackground((Color) value);
74
		} else if (attribute.equals(SWTProperties.FONT)) {
75
			control.setFont((Font) value);
76
		}
77
		fireValueChange(Diffs.createValueDiff(oldValue, value));
78
	}
79
80
	public Object doGetValue() {
81
		if (attribute.equals(SWTProperties.ENABLED)) {
82
			return control.getEnabled() ? Boolean.TRUE : Boolean.FALSE;
83
		}
84
		if (attribute.equals(SWTProperties.VISIBLE)) {
85
			return control.getVisible() ? Boolean.TRUE : Boolean.FALSE;
86
		}
87
		if (attribute.equals(SWTProperties.TOOLTIP_TEXT)) {
88
			return control.getToolTipText();			
89
		}
90
		if (attribute.equals(SWTProperties.FOREGROUND))	 {
91
			return control.getForeground();
92
		}
93
		if (attribute.equals(SWTProperties.BACKGROUND)) {
94
			return control.getBackground();
95
		}
96
		if (attribute.equals(SWTProperties.FONT)) {
97
			return control.getFont();
98
		}
99
		
100
		return null;
101
	}
102
103
	public Object getValueType() {
104
		return valueType;
105
	}
106
}
(-)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/ButtonObservableValue.java (-110 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
 *******************************************************************************/
13
package org.eclipse.jface.internal.databinding.swt;
14
15
import org.eclipse.core.databinding.observable.Diffs;
16
import org.eclipse.core.databinding.observable.Realm;
17
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.widgets.Button;
20
import org.eclipse.swt.widgets.Event;
21
import org.eclipse.swt.widgets.Listener;
22
23
/**
24
 * @since 1.0
25
 * 
26
 */
27
public class ButtonObservableValue extends AbstractSWTObservableValue {
28
29
	private final Button button;
30
31
	private boolean selectionValue;
32
33
	private Listener updateListener = new Listener() {
34
		public void handleEvent(Event event) {
35
			boolean oldSelectionValue = selectionValue;
36
			selectionValue = button.getSelection();
37
						
38
			notifyIfChanged(oldSelectionValue, selectionValue);
39
		}
40
	};
41
42
	/**
43
	 * @param button
44
	 */
45
	public ButtonObservableValue(Button button) {
46
		super(button);
47
		this.button = button;
48
		init();
49
	}
50
	
51
	/**
52
	 * @param realm
53
	 * @param button
54
	 */
55
	public ButtonObservableValue(Realm realm, Button button) {
56
		super(realm, button);
57
		this.button = button;
58
		init();
59
	}
60
	
61
	private void init() {
62
		button.addListener(SWT.Selection, updateListener);
63
		button.addListener(SWT.DefaultSelection, updateListener);
64
		this.selectionValue = button.getSelection();
65
	}
66
67
	public void doSetValue(final Object value) {
68
		boolean oldSelectionValue = selectionValue;
69
		selectionValue = value == null ? false : ((Boolean) value)
70
				.booleanValue();
71
		
72
		button.setSelection(selectionValue);
73
		notifyIfChanged(oldSelectionValue, selectionValue);
74
	}
75
76
	public Object doGetValue() {
77
		return button.getSelection() ? Boolean.TRUE : Boolean.FALSE;
78
	}
79
80
	public Object getValueType() {
81
		return Boolean.TYPE;
82
	}
83
	
84
	/*
85
	 * (non-Javadoc)
86
	 *
87
	 * @see org.eclipse.core.databinding.observable.value.AbstractObservableValue#dispose()
88
	 */
89
	public synchronized void dispose() {
90
		super.dispose();
91
92
		if (!button.isDisposed()) {
93
			button.removeListener(SWT.Selection, updateListener);
94
			button.removeListener(SWT.DefaultSelection, updateListener);
95
		}
96
	}
97
98
	/**
99
	 * Notifies consumers with a value change event only if a change occurred.
100
	 * 
101
	 * @param oldValue
102
	 * @param newValue
103
	 */
104
	private void notifyIfChanged(boolean oldValue, boolean newValue) {
105
		if (oldValue != newValue) {
106
			fireValueChange(Diffs.createValueDiff(oldValue ? Boolean.TRUE : Boolean.FALSE,
107
					newValue ? Boolean.TRUE : Boolean.FALSE));
108
		}		
109
	}
110
}
(-)src/org/eclipse/jface/internal/databinding/swt/SingleSelectionObservableValue.java (-103 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.Control;
18
19
/**
20
 * @since 1.0
21
 * 
22
 */
23
abstract public class SingleSelectionObservableValue extends
24
		AbstractSWTObservableValue {
25
26
	private boolean updating = false;
27
28
	private int currentSelection;
29
30
	/**
31
	 * @param control
32
	 *            the control
33
	 */
34
	public SingleSelectionObservableValue(Control control) {
35
		super(control);
36
		init();
37
	}
38
	
39
	/**
40
	 * @param realm
41
	 * @param control
42
	 */
43
	public SingleSelectionObservableValue(Realm realm, Control control) {
44
		super(realm, control);
45
		init();
46
	}
47
	
48
	private void init() {		
49
		currentSelection = doGetSelectionIndex();
50
		doAddSelectionListener(new Runnable(){
51
			public void run() {
52
				if (!updating) {
53
					int newSelection = doGetSelectionIndex();
54
					notifyIfChanged(currentSelection, newSelection);
55
					currentSelection = newSelection;
56
				}
57
			}
58
		});
59
	}
60
61
	/**
62
	 * @param runnable
63
	 */
64
	protected abstract void doAddSelectionListener(Runnable runnable);
65
66
	public void doSetValue(Object value) {
67
		try {
68
			updating = true;
69
			int intValue = ((Integer) value).intValue();
70
			doSetSelectionIndex(intValue);
71
			notifyIfChanged(currentSelection, intValue);
72
			currentSelection = intValue;
73
		} finally {
74
			updating = false;
75
		}
76
	}
77
78
	/**
79
	 * @param intValue
80
	 *            the selection index
81
	 */
82
	protected abstract void doSetSelectionIndex(int intValue);
83
84
	public Object doGetValue() {
85
		return new Integer(doGetSelectionIndex());
86
	}
87
88
	/**
89
	 * @return the selection index
90
	 */
91
	protected abstract int doGetSelectionIndex();
92
93
	public Object getValueType() {
94
		return Integer.TYPE;
95
	}
96
97
	private void notifyIfChanged(int oldValue, int newValue) {
98
		if (oldValue != newValue) {
99
			fireValueChange(Diffs.createValueDiff(new Integer(
100
					oldValue), new Integer(newValue)));
101
		}
102
	}
103
}
(-)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/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/ListSingleSelectionObservableValue.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 - bug 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.List;
18
19
/**
20
 * @since 1.0
21
 * 
22
 */
23
public class ListSingleSelectionObservableValue extends
24
		SingleSelectionObservableValue {
25
26
	private SelectionListener selectionListener;
27
28
	/**
29
	 * @param combo
30
	 */
31
	public ListSingleSelectionObservableValue(List combo) {
32
		super(combo);
33
	}
34
35
	private List getList() {
36
		return (List) 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
		getList().addSelectionListener(selectionListener);
50
	}
51
52
	protected int doGetSelectionIndex() {
53
		return getList().getSelectionIndex();
54
	}
55
56
	protected void doSetSelectionIndex(int index) {
57
		getList().setSelection(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 && !getList().isDisposed()) {
68
			getList().removeSelectionListener(selectionListener);
69
		}
70
	}
71
}
(-)src/org/eclipse/jface/internal/databinding/internal/swt/LinkObservableValue.java (-47 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Michael Krauter, Catuno GmbH 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
 *     Michael Krauter, Catuno GmbH - initial API and implementation (bug 180223)
10
 *******************************************************************************/
11
package org.eclipse.jface.internal.databinding.internal.swt;
12
13
import org.eclipse.core.databinding.observable.Diffs;
14
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
15
import org.eclipse.swt.widgets.Link;
16
17
/**
18
 * @since 1.2
19
 * 
20
 */
21
public class LinkObservableValue extends AbstractSWTObservableValue {
22
23
	private final Link link;
24
25
	/**
26
	 * @param link
27
	 */
28
	public LinkObservableValue(Link link) {
29
		super(link);
30
		this.link = link;
31
	}
32
33
	public void doSetValue(final Object value) {
34
		String oldValue = link.getText();
35
		link.setText(value == null ? "" : value.toString()); //$NON-NLS-1$
36
		fireValueChange(Diffs.createValueDiff(oldValue, link.getText()));
37
	}
38
39
	public Object doGetValue() {
40
		return link.getText();
41
	}
42
43
	public Object getValueType() {
44
		return String.class;
45
	}
46
47
}
(-)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/internal/databinding/provisional/swt/AbstractSWTVetoableValue.java (-71 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.AbstractVetoableValue;
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 vetoable 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 AbstractSWTVetoableValue extends AbstractVetoableValue implements ISWTObservableValue {
30
31
	private final Widget widget;
32
33
	/**
34
	 * Standard constructor for an SWT VetoableValue.  Makes sure that
35
	 * the observable gets disposed when the SWT widget is disposed.
36
	 * 
37
	 * @param widget
38
	 */
39
	protected AbstractSWTVetoableValue(Widget widget) {
40
		this(SWTObservables.getRealm(widget.getDisplay()), widget);
41
	}
42
	
43
	/**
44
	 * Constructs a new instance for the provided <code>realm</code> and <code>widget</code>.
45
	 * 
46
	 * @param realm
47
	 * @param widget
48
	 * @since 1.2
49
	 */
50
	protected AbstractSWTVetoableValue(Realm realm, Widget widget) {
51
		super(realm);
52
		this.widget = widget;
53
		if (widget == null) {
54
			throw new IllegalArgumentException("The widget parameter is null."); //$NON-NLS-1$
55
		}
56
		widget.addDisposeListener(disposeListener);
57
	}
58
	
59
	private DisposeListener disposeListener = new DisposeListener() {
60
		public void widgetDisposed(DisposeEvent e) {
61
			AbstractSWTVetoableValue.this.dispose();
62
		}
63
	};
64
65
	/**
66
	 * @return Returns the widget.
67
	 */
68
	public Widget getWidget() {
69
		return widget;
70
	}
71
}
(-)src/org/eclipse/jface/databinding/swt/SWTObservables.java (-91 / +164 lines)
Lines 10-16 Link Here
10
 *     Matt Carter - bug 170668
10
 *     Matt Carter - bug 170668
11
 *     Brad Reynolds - bug 170848
11
 *     Brad Reynolds - bug 170848
12
 *     Matthew Hall - bug 180746, bug 207844
12
 *     Matthew Hall - bug 180746, bug 207844
13
 *     Michael Krauter, bug 180223
13
 *     Michael Krauter - bug 180223
14
 *     Tom Schindl - bug 246462
14
 *******************************************************************************/
15
 *******************************************************************************/
15
package org.eclipse.jface.databinding.swt;
16
package org.eclipse.jface.databinding.swt;
16
17
Lines 19-48 Link Here
19
20
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.list.IObservableList;
22
import org.eclipse.core.databinding.observable.list.IObservableList;
22
import org.eclipse.jface.internal.databinding.swt.ButtonObservableValue;
23
import org.eclipse.core.databinding.property.IListProperty;
23
import org.eclipse.jface.internal.databinding.swt.CComboObservableList;
24
import org.eclipse.core.databinding.property.IValueProperty;
24
import org.eclipse.jface.internal.databinding.swt.CComboObservableValue;
25
import org.eclipse.core.databinding.property.PropertyObservables;
25
import org.eclipse.jface.internal.databinding.swt.CComboSingleSelectionObservableValue;
26
import org.eclipse.jface.internal.databinding.swt.CLabelObservableValue;
27
import org.eclipse.jface.internal.databinding.swt.ComboObservableList;
28
import org.eclipse.jface.internal.databinding.swt.ComboObservableValue;
29
import org.eclipse.jface.internal.databinding.internal.swt.LinkObservableValue;
30
import org.eclipse.jface.internal.databinding.swt.ComboSingleSelectionObservableValue;
31
import org.eclipse.jface.internal.databinding.swt.ControlObservableValue;
32
import org.eclipse.jface.internal.databinding.swt.DelayedObservableValue;
26
import org.eclipse.jface.internal.databinding.swt.DelayedObservableValue;
33
import org.eclipse.jface.internal.databinding.swt.LabelObservableValue;
27
import org.eclipse.jface.internal.databinding.swt.SWTObservableListDecorator;
34
import org.eclipse.jface.internal.databinding.swt.ListObservableList;
28
import org.eclipse.jface.internal.databinding.swt.SWTObservableValueDecorator;
35
import org.eclipse.jface.internal.databinding.swt.ListObservableValue;
29
import org.eclipse.jface.internal.databinding.swt.SWTVetoableValueDecorator;
36
import org.eclipse.jface.internal.databinding.swt.ListSingleSelectionObservableValue;
37
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
38
import org.eclipse.jface.internal.databinding.swt.ScaleObservableValue;
39
import org.eclipse.jface.internal.databinding.swt.ShellObservableValue;
40
import org.eclipse.jface.internal.databinding.swt.SpinnerObservableValue;
41
import org.eclipse.jface.internal.databinding.swt.TableSingleSelectionObservableValue;
42
import org.eclipse.jface.internal.databinding.swt.TextEditableObservableValue;
43
import org.eclipse.jface.internal.databinding.swt.TextObservableValue;
44
import org.eclipse.swt.custom.CCombo;
30
import org.eclipse.swt.custom.CCombo;
45
import org.eclipse.swt.custom.CLabel;
31
import org.eclipse.swt.custom.CLabel;
32
import org.eclipse.swt.custom.StyledText;
46
import org.eclipse.swt.widgets.Button;
33
import org.eclipse.swt.widgets.Button;
47
import org.eclipse.swt.widgets.Combo;
34
import org.eclipse.swt.widgets.Combo;
48
import org.eclipse.swt.widgets.Control;
35
import org.eclipse.swt.widgets.Control;
Lines 88-100 Link Here
88
75
89
	/**
76
	/**
90
	 * Returns an observable which delays notification of value change events
77
	 * Returns an observable which delays notification of value change events
91
	 * from <code>observable</code> until <code>delay</code> milliseconds
78
	 * from <code>observable</code> until <code>delay</code> milliseconds have
92
	 * have passed since the last change event, or until a FocusOut event is
79
	 * passed since the last change event, or until a FocusOut event is received
93
	 * received from the underlying widget (whichever happens earlier). This
80
	 * from the underlying widget (whichever happens earlier). This class helps
94
	 * class helps to delay validation until the user stops typing. To notify
81
	 * to delay validation until the user stops typing. To notify about pending
95
	 * about pending changes, the returned observable value will fire a stale
82
	 * changes, the returned observable value will fire a stale event when the
96
	 * event when the wrapped observable value fires a change event, but this
83
	 * wrapped observable value fires a change event, but this change is being
97
	 * change is being delayed.
84
	 * delayed.
98
	 * 
85
	 * 
99
	 * @param delay
86
	 * @param delay
100
	 * @param observable
87
	 * @param observable
Lines 104-138 Link Here
104
	 * 
91
	 * 
105
	 * @since 1.2
92
	 * @since 1.2
106
	 */
93
	 */
107
	public static ISWTObservableValue observeDelayedValue(int delay, ISWTObservableValue observable) {
94
	public static ISWTObservableValue observeDelayedValue(int delay,
108
	  return new DelayedObservableValue(delay, observable);
95
			ISWTObservableValue observable) {
96
		return new DelayedObservableValue(delay, observable);
97
	}
98
99
	private static ISWTObservableValue observeControl(Control control,
100
			IValueProperty property) {
101
		return new SWTObservableValueDecorator(
102
				PropertyObservables.observeValue(
103
						getRealm(control.getDisplay()), control, property),
104
				control);
109
	}
105
	}
110
106
111
	/**
107
	/**
108
	 * Returns an observable value tracking the enabled state of the given
109
	 * control
110
	 * 
112
	 * @param control
111
	 * @param control
112
	 *            the control to observe
113
	 * @return an observable value tracking the enabled state of the given
113
	 * @return an observable value tracking the enabled state of the given
114
	 *         control
114
	 *         control
115
	 */
115
	 */
116
	public static ISWTObservableValue observeEnabled(Control control) {
116
	public static ISWTObservableValue observeEnabled(Control control) {
117
		return new ControlObservableValue(control, SWTProperties.ENABLED);
117
		return observeControl(control, ControlProperties.enabled());
118
	}
118
	}
119
119
120
	/**
120
	/**
121
	 * Returns an observable value tracking the visible state of the given
122
	 * control
123
	 * 
121
	 * @param control
124
	 * @param control
125
	 *            the control to observe
122
	 * @return an observable value tracking the visible state of the given
126
	 * @return an observable value tracking the visible state of the given
123
	 *         control
127
	 *         control
124
	 */
128
	 */
125
	public static ISWTObservableValue observeVisible(Control control) {
129
	public static ISWTObservableValue observeVisible(Control control) {
126
		return new ControlObservableValue(control, SWTProperties.VISIBLE);
130
		return observeControl(control, ControlProperties.visible());
127
	}
131
	}
128
132
129
	/**
133
	/**
134
	 * Returns an observable value tracking the tooltip text of the given
135
	 * control
136
	 * 
130
	 * @param control
137
	 * @param control
138
	 *            the control to observe
131
	 * @return an observable value tracking the tooltip text of the given
139
	 * @return an observable value tracking the tooltip text of the given
132
	 *         control
140
	 *         control
133
	 */
141
	 */
134
	public static ISWTObservableValue observeTooltipText(Control control) {
142
	public static ISWTObservableValue observeTooltipText(Control control) {
135
		return new ControlObservableValue(control, SWTProperties.TOOLTIP_TEXT);
143
		return observeControl(control, ControlProperties.toolTipText());
136
	}
144
	}
137
145
138
	/**
146
	/**
Lines 153-178 Link Here
153
	 *             if <code>control</code> type is unsupported
161
	 *             if <code>control</code> type is unsupported
154
	 */
162
	 */
155
	public static ISWTObservableValue observeSelection(Control control) {
163
	public static ISWTObservableValue observeSelection(Control control) {
164
		IValueProperty property;
156
		if (control instanceof Spinner) {
165
		if (control instanceof Spinner) {
157
			return new SpinnerObservableValue((Spinner) control,
166
			property = SpinnerProperties.selection();
158
					SWTProperties.SELECTION);
159
		} else if (control instanceof Button) {
167
		} else if (control instanceof Button) {
160
			return new ButtonObservableValue((Button) control);
168
			property = ButtonProperties.selection();
161
		} else if (control instanceof Combo) {
169
		} else if (control instanceof Combo) {
162
			return new ComboObservableValue((Combo) control,
170
			property = ComboProperties.selection();
163
					SWTProperties.SELECTION);
164
		} else if (control instanceof CCombo) {
171
		} else if (control instanceof CCombo) {
165
			return new CComboObservableValue((CCombo) control,
172
			property = CComboProperties.selection();
166
					SWTProperties.SELECTION);
167
		} else if (control instanceof List) {
173
		} else if (control instanceof List) {
168
			return new ListObservableValue((List) control);
174
			property = ListProperties.selection();
169
		} else if (control instanceof Scale) {
175
		} else if (control instanceof Scale) {
170
			return new ScaleObservableValue((Scale) control,
176
			property = ScaleProperties.selection();
171
					SWTProperties.SELECTION);
177
		} else {
178
			throw new IllegalArgumentException(
179
					"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
172
		}
180
		}
173
181
174
		throw new IllegalArgumentException(
182
		return observeControl(control, property);
175
				"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
176
	}
183
	}
177
184
178
	/**
185
	/**
Lines 189-203 Link Here
189
	 *             if <code>control</code> type is unsupported
196
	 *             if <code>control</code> type is unsupported
190
	 */
197
	 */
191
	public static ISWTObservableValue observeMin(Control control) {
198
	public static ISWTObservableValue observeMin(Control control) {
199
		IValueProperty property;
192
		if (control instanceof Spinner) {
200
		if (control instanceof Spinner) {
193
			return new SpinnerObservableValue((Spinner) control,
201
			property = SpinnerProperties.minimum();
194
					SWTProperties.MIN);
195
		} else if (control instanceof Scale) {
202
		} else if (control instanceof Scale) {
196
			return new ScaleObservableValue((Scale) control, SWTProperties.MIN);
203
			property = ScaleProperties.minimum();
204
		} else {
205
			throw new IllegalArgumentException(
206
					"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
197
		}
207
		}
198
208
199
		throw new IllegalArgumentException(
209
		return observeControl(control, property);
200
				"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
201
	}
210
	}
202
211
203
	/**
212
	/**
Lines 214-228 Link Here
214
	 *             if <code>control</code> type is unsupported
223
	 *             if <code>control</code> type is unsupported
215
	 */
224
	 */
216
	public static ISWTObservableValue observeMax(Control control) {
225
	public static ISWTObservableValue observeMax(Control control) {
226
		IValueProperty property;
217
		if (control instanceof Spinner) {
227
		if (control instanceof Spinner) {
218
			return new SpinnerObservableValue((Spinner) control,
228
			property = SpinnerProperties.maximum();
219
					SWTProperties.MAX);
220
		} else if (control instanceof Scale) {
229
		} else if (control instanceof Scale) {
221
			return new ScaleObservableValue((Scale) control, SWTProperties.MAX);
230
			property = ScaleProperties.maximum();
231
		} else {
232
			throw new IllegalArgumentException(
233
					"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
222
		}
234
		}
223
235
224
		throw new IllegalArgumentException(
236
		return observeControl(control, property);
225
				"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
226
	}
237
	}
227
238
228
	/**
239
	/**
Lines 230-251 Link Here
230
	 * <code>control</code>. The supported types are:
241
	 * <code>control</code>. The supported types are:
231
	 * <ul>
242
	 * <ul>
232
	 * <li>org.eclipse.swt.widgets.Text</li>
243
	 * <li>org.eclipse.swt.widgets.Text</li>
244
	 * <li>org.eclipse.swt.custom.StyledText (as of 1.3)</li>
233
	 * </ul>
245
	 * </ul>
234
	 * 
246
	 * 
235
	 * <li>org.eclipse.swt.widgets.Label</li>
236
	 * @param control
247
	 * @param control
237
	 * @param event event type to register for change events
248
	 * @param event
249
	 *            event type to register for change events
238
	 * @return observable value
250
	 * @return observable value
239
	 * @throws IllegalArgumentException
251
	 * @throws IllegalArgumentException
240
	 *             if <code>control</code> type is unsupported
252
	 *             if <code>control</code> type is unsupported
241
	 */
253
	 */
242
	public static ISWTObservableValue observeText(Control control, int event) {
254
	public static ISWTObservableValue observeText(Control control, int event) {
255
		IValueProperty property;
243
		if (control instanceof Text) {
256
		if (control instanceof Text) {
244
			return new TextObservableValue((Text) control, event);
257
			property = TextProperties.text(event);
258
		} else if (control instanceof StyledText) {
259
			property = StyledTextProperties.text(event);
260
		} else {
261
			throw new IllegalArgumentException(
262
					"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
245
		}
263
		}
246
264
247
		throw new IllegalArgumentException(
265
		return new SWTVetoableValueDecorator(PropertyObservables.observeValue(
248
				"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
266
				control, property), control);
249
	}
267
	}
250
268
251
	/**
269
	/**
Lines 266-288 Link Here
266
	 *             if <code>control</code> type is unsupported
284
	 *             if <code>control</code> type is unsupported
267
	 */
285
	 */
268
	public static ISWTObservableValue observeText(Control control) {
286
	public static ISWTObservableValue observeText(Control control) {
287
		IValueProperty property;
269
		if (control instanceof Label) {
288
		if (control instanceof Label) {
270
			return new LabelObservableValue((Label) control);
289
			property = LabelProperties.text();
271
		} else if (control instanceof Link) {
290
		} else if (control instanceof Link) {
272
			return new LinkObservableValue((Link) control);
291
			property = LinkProperties.text();
273
		} else if (control instanceof CLabel) {
292
		} else if (control instanceof CLabel) {
274
			return new CLabelObservableValue((CLabel) control);
293
			property = CLabelProperties.text();
275
		} else if (control instanceof Combo) {
294
		} else if (control instanceof Combo) {
276
			return new ComboObservableValue((Combo) control, SWTProperties.TEXT);
295
			property = ComboProperties.text();
277
		} else if (control instanceof CCombo) {
296
		} else if (control instanceof CCombo) {
278
			return new CComboObservableValue((CCombo) control,
297
			property = CComboProperties.text();
279
					SWTProperties.TEXT);
280
		} else if (control instanceof Shell) {
298
		} else if (control instanceof Shell) {
281
			return new ShellObservableValue((Shell) control);
299
			property = ShellProperties.text();
300
		} else {
301
			throw new IllegalArgumentException(
302
					"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
282
		}
303
		}
283
304
284
		throw new IllegalArgumentException(
305
		return observeControl(control, property);
285
				"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
286
	}
306
	}
287
307
288
	/**
308
	/**
Lines 300-315 Link Here
300
	 *             if <code>control</code> type is unsupported
320
	 *             if <code>control</code> type is unsupported
301
	 */
321
	 */
302
	public static IObservableList observeItems(Control control) {
322
	public static IObservableList observeItems(Control control) {
323
		IListProperty property;
303
		if (control instanceof Combo) {
324
		if (control instanceof Combo) {
304
			return new ComboObservableList((Combo) control);
325
			property = ComboProperties.items();
305
		} else if (control instanceof CCombo) {
326
		} else if (control instanceof CCombo) {
306
			return new CComboObservableList((CCombo) control);
327
			property = CComboProperties.items();
307
		} else if (control instanceof List) {
328
		} else if (control instanceof List) {
308
			return new ListObservableList((List) control);
329
			property = ListProperties.items();
330
		} else {
331
			throw new IllegalArgumentException(
332
					"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
309
		}
333
		}
310
334
311
		throw new IllegalArgumentException(
335
		return new SWTObservableListDecorator(PropertyObservables.observeList(
312
				"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
336
				control, property), control);
313
	}
337
	}
314
338
315
	/**
339
	/**
Lines 329-346 Link Here
329
	 */
353
	 */
330
	public static ISWTObservableValue observeSingleSelectionIndex(
354
	public static ISWTObservableValue observeSingleSelectionIndex(
331
			Control control) {
355
			Control control) {
356
		IValueProperty property;
332
		if (control instanceof Table) {
357
		if (control instanceof Table) {
333
			return new TableSingleSelectionObservableValue((Table) control);
358
			property = TableProperties.singleSelectionIndex();
334
		} else if (control instanceof Combo) {
359
		} else if (control instanceof Combo) {
335
			return new ComboSingleSelectionObservableValue((Combo) control);
360
			property = ComboProperties.singleSelectionIndex();
336
		} else if (control instanceof CCombo) {
361
		} else if (control instanceof CCombo) {
337
			return new CComboSingleSelectionObservableValue((CCombo) control);
362
			property = CComboProperties.singleSelectionIndex();
338
		} else if (control instanceof List) {
363
		} else if (control instanceof List) {
339
			return new ListSingleSelectionObservableValue((List) control);
364
			property = ListProperties.singleSelectionIndex();
365
		} else {
366
			throw new IllegalArgumentException(
367
					"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
340
		}
368
		}
341
369
342
		throw new IllegalArgumentException(
370
		return observeControl(control, property);
343
				"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
344
	}
371
	}
345
372
346
	/**
373
	/**
Lines 349-377 Link Here
349
	 *         control
376
	 *         control
350
	 */
377
	 */
351
	public static ISWTObservableValue observeForeground(Control control) {
378
	public static ISWTObservableValue observeForeground(Control control) {
352
		return new ControlObservableValue(control, SWTProperties.FOREGROUND);
379
		return observeControl(control, ControlProperties.foreground());
353
	}
380
	}
354
381
355
	/**
382
	/**
383
	 * Returns an observable value tracking the background color of the given
384
	 * control
385
	 * 
356
	 * @param control
386
	 * @param control
387
	 *            the control to observe
357
	 * @return an observable value tracking the background color of the given
388
	 * @return an observable value tracking the background color of the given
358
	 *         control
389
	 *         control
359
	 */
390
	 */
360
	public static ISWTObservableValue observeBackground(Control control) {
391
	public static ISWTObservableValue observeBackground(Control control) {
361
		return new ControlObservableValue(control, SWTProperties.BACKGROUND);
392
		return observeControl(control, ControlProperties.background());
362
	}
393
	}
363
394
364
	/**
395
	/**
396
	 * Returns an observable value tracking the font of the given control.
397
	 * 
365
	 * @param control
398
	 * @param control
399
	 *            the control to observe
366
	 * @return an observable value tracking the font of the given control
400
	 * @return an observable value tracking the font of the given control
367
	 */
401
	 */
368
	public static ISWTObservableValue observeFont(Control control) {
402
	public static ISWTObservableValue observeFont(Control control) {
369
		return new ControlObservableValue(control, SWTProperties.FONT);
403
		return observeControl(control, ControlProperties.font());
370
	}
404
	}
371
	
405
372
	/**
406
	/**
373
	 * Returns an observable observing the editable attribute of
407
	 * @param control
374
	 * the provided <code>control</code>. The supported types are:
408
	 * @return an observable value tracking the size of the given control
409
	 * @since 1.3
410
	 */
411
	public static ISWTObservableValue observeSize(Control control) {
412
		return observeControl(control, ControlProperties.size());
413
	}
414
415
	/**
416
	 * @param control
417
	 * @return an observable value tracking the location of the given control
418
	 * @since 1.3
419
	 */
420
	public static ISWTObservableValue observeLocation(Control control) {
421
		return observeControl(control, ControlProperties.location());
422
	}
423
424
	/**
425
	 * @param control
426
	 * @return an observable value tracking the focus of the given control
427
	 * @since 1.3
428
	 */
429
	public static ISWTObservableValue observeFocused(Control control) {
430
		return observeControl(control, ControlProperties.focused());
431
	}
432
433
	/**
434
	 * @param control
435
	 * @return an observable value tracking the bounds of the given control
436
	 * @since 1.3
437
	 */
438
	public static ISWTObservableValue observeBounds(Control control) {
439
		return observeControl(control, ControlProperties.bounds());
440
	}
441
442
	/**
443
	 * Returns an observable observing the editable attribute of the provided
444
	 * <code>control</code>. The supported types are:
375
	 * <ul>
445
	 * <ul>
376
	 * <li>org.eclipse.swt.widgets.Text</li>
446
	 * <li>org.eclipse.swt.widgets.Text</li>
377
	 * </ul>
447
	 * </ul>
Lines 382-393 Link Here
382
	 *             if <code>control</code> type is unsupported
452
	 *             if <code>control</code> type is unsupported
383
	 */
453
	 */
384
	public static ISWTObservableValue observeEditable(Control control) {
454
	public static ISWTObservableValue observeEditable(Control control) {
455
		IValueProperty property;
385
		if (control instanceof Text) {
456
		if (control instanceof Text) {
386
			return new TextEditableObservableValue((Text) control);
457
			property = TextProperties.editable();
458
		} else {
459
			throw new IllegalArgumentException(
460
					"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
387
		}
461
		}
388
		
462
389
		throw new IllegalArgumentException(
463
		return observeControl(control, property);
390
				"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
391
	}
464
	}
392
465
393
	private static class DisplayRealm extends Realm {
466
	private static class DisplayRealm extends Realm {
(-)src/org/eclipse/jface/internal/databinding/swt/SWTObservableListDecorator.java (+57 lines)
Added 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.swt;
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.swt.ISWTObservable;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.widgets.Event;
19
import org.eclipse.swt.widgets.Listener;
20
import org.eclipse.swt.widgets.Widget;
21
22
/**
23
 * @since 3.3
24
 * 
25
 */
26
public class SWTObservableListDecorator extends DecoratingObservableList
27
		implements ISWTObservable {
28
	private Widget widget;
29
30
	/**
31
	 * @param decorated
32
	 * @param widget
33
	 */
34
	public SWTObservableListDecorator(IObservableList decorated, Widget widget) {
35
		super(decorated, true);
36
		this.widget = widget;
37
		widget.addListener(SWT.Dispose, disposeListener);
38
	}
39
40
	private Listener disposeListener = new Listener() {
41
		public void handleEvent(Event event) {
42
			SWTObservableListDecorator.this.dispose();
43
		}
44
	};
45
46
	public void dispose() {
47
		this.widget = null;
48
		super.dispose();
49
	}
50
51
	/**
52
	 * @return Returns the widget.
53
	 */
54
	public Widget getWidget() {
55
		return widget;
56
	}
57
}
(-)src/org/eclipse/jface/databinding/swt/ButtonProperties.java (+31 lines)
Added Link Here
1
/*******************************************************************************
2
 * All rights reserved. This program and the accompanying materials
3
 * Copyright (c) 2008 IBM Corporation and others.
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.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.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/swt/CLabelProperties.java (+31 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.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.CLabelTextProperty;
16
17
/**
18
 * A factory for creating properties of SWT Labels.
19
 * 
20
 * @since 1.3
21
 */
22
public class CLabelProperties {
23
	/**
24
	 * Returns a value property for the text of a SWT Label.
25
	 * 
26
	 * @return a value property for the text of a SWT Label.
27
	 */
28
	public static IValueProperty text() {
29
		return new CLabelTextProperty();
30
	}
31
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlEnabledProperty.java (+28 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.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 ControlBooleanValueProperty {
21
	public boolean doGetBooleanValue(Control control) {
22
		return control.getEnabled();
23
	}
24
25
	public void doSetBooleanValue(Control control, boolean value) {
26
		control.setEnabled(value);
27
	}
28
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlFocusedProperty.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
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.widgets.Control;
17
18
/**
19
 * @since 3.3
20
 * 
21
 */
22
public class ControlFocusedProperty extends ControlBooleanValueProperty {
23
	/**
24
	 * 
25
	 */
26
	public ControlFocusedProperty() {
27
		super(new int[] { SWT.FocusIn, SWT.FocusOut });
28
	}
29
30
	public boolean doGetBooleanValue(Control control) {
31
		return control.isFocusControl();
32
	}
33
34
	public void doSetBooleanValue(Control control, boolean value) {
35
		if (value)
36
			control.setFocus();
37
	}
38
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlToolTipTextProperty.java (+28 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
16
/**
17
 * @since 3.3
18
 * 
19
 */
20
public class ControlToolTipTextProperty extends ControlStringValueProperty {
21
	String doGetStringValue(Control control) {
22
		return control.getToolTipText();
23
	}
24
25
	void doSetStringValue(Control control, String value) {
26
		control.setToolTipText(value);
27
	}
28
}
(-)src/org/eclipse/jface/databinding/swt/StyledTextProperties.java (+36 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.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.StyledTextTextProperty;
16
import org.eclipse.swt.SWT;
17
18
/**
19
 * A factory for creating properties of SWT StyledTexts.
20
 * 
21
 * @since 1.3
22
 */
23
public class StyledTextProperties {
24
	/**
25
	 * Returns a value property for the text of a SWT StyledText.
26
	 * 
27
	 * @param event
28
	 *            the SWT event type to register for change events. May be
29
	 *            {@link SWT#None}, {@link SWT#Modify} or {@link SWT#FocusOut}.
30
	 * 
31
	 * @return a value property for the text of a SWT StyledText.
32
	 */
33
	public static IValueProperty text(int event) {
34
		return new StyledTextTextProperty(event);
35
	}
36
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlLocationProperty.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
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 ControlLocationProperty extends ControlValueProperty {
24
	/**
25
	 * 
26
	 */
27
	public ControlLocationProperty() {
28
		super(SWT.Move, Point.class);
29
	}
30
31
	public Object doGetValue(Control control) {
32
		return control.getLocation();
33
	}
34
35
	public void doSetValue(Control control, Object value) {
36
		control.setLocation((Point) value);
37
	}
38
}
(-)src/org/eclipse/jface/databinding/swt/ScaleProperties.java (+51 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.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.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 (+37 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.Button;
16
import org.eclipse.swt.widgets.Control;
17
18
/**
19
 * @since 3.3
20
 * 
21
 */
22
public class ButtonSelectionProperty extends ControlBooleanValueProperty {
23
	/**
24
	 * 
25
	 */
26
	public ButtonSelectionProperty() {
27
		super(SWT.Selection);
28
	}
29
30
	boolean doGetBooleanValue(Control control) {
31
		return ((Button) control).getSelection();
32
	}
33
34
	void doSetBooleanValue(Control control, boolean value) {
35
		((Button) control).setSelection(value);
36
	}
37
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlFontProperty.java (+36 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.graphics.Font;
15
import org.eclipse.swt.widgets.Control;
16
17
/**
18
 * @since 3.3
19
 *
20
 */
21
public class ControlFontProperty extends ControlValueProperty {
22
	/**
23
	 * 
24
	 */
25
	public ControlFontProperty() {
26
		super(Font.class);
27
	}
28
29
	public Object doGetValue(Control control) {
30
		return control.getFont();
31
	}
32
33
	public void doSetValue(Control control, Object value) {
34
		control.setFont((Font) value);
35
	}
36
}
(-)src/org/eclipse/jface/internal/databinding/swt/ScaleMaximumProperty.java (+29 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
import org.eclipse.swt.widgets.Scale;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class ScaleMaximumProperty extends ControlIntValueProperty {
22
	int doGetIntValue(Control control) {
23
		return ((Scale) control).getMaximum();
24
	}
25
26
	void doSetIntValue(Control control, int value) {
27
		((Scale) control).setMaximum(value);
28
	}
29
}
(-)src/org/eclipse/jface/internal/databinding/swt/ListSelectionProperty.java (+52 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.Control;
16
import org.eclipse.swt.widgets.List;
17
18
/**
19
 * @since 3.3
20
 * 
21
 */
22
public class ListSelectionProperty extends ControlStringValueProperty {
23
	/**
24
	 * 
25
	 */
26
	public ListSelectionProperty() {
27
		super(SWT.Selection);
28
	}
29
30
	String doGetStringValue(Control control) {
31
		List list = (List) control;
32
		int index = list.getSelectionIndex();
33
		if (index >= 0)
34
			return list.getItem(index);
35
		return null;
36
	}
37
38
	void doSetStringValue(Control control, String value) {
39
		List list = (List) control;
40
		String items[] = list.getItems();
41
		int index = -1;
42
		if (items != null && value != null) {
43
			for (int i = 0; i < items.length; i++) {
44
				if (value.equals(items[i])) {
45
					index = i;
46
					break;
47
				}
48
			}
49
			list.select(index);
50
		}
51
	}
52
}
(-)src/org/eclipse/jface/databinding/swt/ControlProperties.java (+121 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.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.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.ControlVisibleProperty;
24
import org.eclipse.jface.internal.databinding.swt.ControlToolTipTextProperty;
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/internal/databinding/swt/ControlIntValueProperty.java (+44 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
16
/**
17
 * @since 3.3
18
 * 
19
 */
20
public abstract class ControlIntValueProperty extends ControlValueProperty {
21
	ControlIntValueProperty() {
22
		super(Integer.TYPE);
23
	}
24
25
	ControlIntValueProperty(int event) {
26
		super(event, Integer.TYPE);
27
	}
28
29
	ControlIntValueProperty(int[] events) {
30
		super(events, Integer.TYPE);
31
	}
32
33
	Object doGetValue(Control control) {
34
		return new Integer(doGetIntValue(control));
35
	}
36
37
	abstract int doGetIntValue(Control control);
38
39
	void doSetValue(Control control, Object value) {
40
		doSetIntValue(control, ((Integer) value).intValue());
41
	}
42
43
	abstract void doSetIntValue(Control control, int intValue);
44
}
(-)src/org/eclipse/jface/internal/databinding/swt/LinkTextProperty.java (+29 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
import org.eclipse.swt.widgets.Link;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class LinkTextProperty extends ControlStringValueProperty {
22
	String doGetStringValue(Control control) {
23
		return ((Link) control).getText();
24
	}
25
26
	void doSetStringValue(Control control, String value) {
27
		((Link) control).setText(value == null ? "" : value); //$NON-NLS-1$
28
	}
29
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlBackgroundProperty.java (+36 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.graphics.Color;
15
import org.eclipse.swt.widgets.Control;
16
17
/**
18
 * @since 3.3
19
 *
20
 */
21
public class ControlBackgroundProperty extends ControlValueProperty {
22
	/**
23
	 * 
24
	 */
25
	public ControlBackgroundProperty() {
26
		super(Color.class);
27
	}
28
29
	public Object doGetValue(Control control) {
30
		return control.getBackground();
31
	}
32
33
	public void doSetValue(Control control, Object value) {
34
		control.setBackground((Color) value);
35
	}
36
}
(-)src/org/eclipse/jface/internal/databinding/swt/ComboSelectionProperty.java (+52 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.Combo;
16
import org.eclipse.swt.widgets.Control;
17
18
/**
19
 * @since 3.3
20
 *
21
 */
22
public class ComboSelectionProperty extends ControlStringValueProperty {
23
	/**
24
	 * 
25
	 */
26
	public ComboSelectionProperty() {
27
		super(SWT.Modify);
28
	}
29
30
	String doGetStringValue(Control control) {
31
		return ((Combo)control).getText();
32
	}
33
34
	void doSetStringValue(Control control, String value) {
35
		Combo combo = (Combo) control;
36
		String items[] = combo.getItems();
37
		int index = -1;
38
		if (items != null && value != null) {
39
			for (int i = 0; i < items.length; i++) {
40
				if (value.equals(items[i])) {
41
					index = i;
42
					break;
43
				}
44
			}
45
			if (index == -1) {
46
				combo.setText(value);
47
			} else {
48
				combo.select(index); // -1 will not "unselect"
49
			}
50
		}
51
	}
52
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlStringListProperty.java (+41 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.jface.internal.databinding.swt;
13
14
import java.util.Arrays;
15
import java.util.List;
16
17
import org.eclipse.swt.widgets.Control;
18
19
/**
20
 * @since 3.3
21
 * 
22
 */
23
public abstract class ControlStringListProperty extends ControlListProperty {
24
	protected void doSetList(Object source, List list) {
25
		String[] strings = (String[]) list.toArray(new String[list.size()]);
26
		doSetStringList((Control) source, strings);
27
	}
28
29
	abstract void doSetStringList(Control control, String[] list);
30
31
	public List getList(Object source) {
32
		String[] list = doGetStringList((Control) source);
33
		return Arrays.asList(list);
34
	}
35
36
	abstract String[] doGetStringList(Control control);
37
38
	public Object getElementType() {
39
		return String.class;
40
	}
41
}
(-)src/org/eclipse/jface/internal/databinding/swt/SWTObservableValueDecorator.java (+57 lines)
Added 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.swt;
13
14
import org.eclipse.core.databinding.observable.value.DecoratingObservableValue;
15
import org.eclipse.core.databinding.observable.value.IObservableValue;
16
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.widgets.Event;
19
import org.eclipse.swt.widgets.Listener;
20
import org.eclipse.swt.widgets.Widget;
21
22
/**
23
 * @since 3.3
24
 * 
25
 */
26
public class SWTObservableValueDecorator extends DecoratingObservableValue
27
		implements ISWTObservableValue {
28
	private Widget widget;
29
30
	/**
31
	 * @param decorated
32
	 * @param widget
33
	 */
34
	public SWTObservableValueDecorator(IObservableValue decorated, Widget widget) {
35
		super(decorated, true);
36
		this.widget = widget;
37
		widget.addListener(SWT.Dispose, disposeListener);
38
	}
39
40
	private Listener disposeListener = new Listener() {
41
		public void handleEvent(Event event) {
42
			SWTObservableValueDecorator.this.dispose();
43
		}
44
	};
45
46
	public void dispose() {
47
		this.widget = null;
48
		super.dispose();
49
	}
50
51
	/**
52
	 * @return Returns the widget.
53
	 */
54
	public Widget getWidget() {
55
		return widget;
56
	}
57
}
(-)src/org/eclipse/jface/internal/databinding/swt/CLabelTextProperty.java (+29 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.custom.CLabel;
15
import org.eclipse.swt.widgets.Control;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class CLabelTextProperty extends ControlStringValueProperty {
22
	String doGetStringValue(Control control) {
23
		return ((CLabel) control).getText();
24
	}
25
26
	void doSetStringValue(Control control, String value) {
27
		((CLabel) control).setText(value == null ? "" : value); //$NON-NLS-1$
28
	}
29
}
(-)src/org/eclipse/jface/databinding/swt/SpinnerProperties.java (+51 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.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.SpinnerMaximumProperty;
16
import org.eclipse.jface.internal.databinding.swt.SpinnerMinimumProperty;
17
import org.eclipse.jface.internal.databinding.swt.SpinnerSelectionProperty;
18
19
/**
20
 * A factory for creating properties of SWT Spinners.
21
 * 
22
 * @since 1.3
23
 */
24
public class SpinnerProperties {
25
	/**
26
	 * Returns a value property for the selected value of a SWT Spinner.
27
	 * 
28
	 * @return a value property for the selected value of a SWT Spinner.
29
	 */
30
	public static IValueProperty selection() {
31
		return new SpinnerSelectionProperty();
32
	}
33
34
	/**
35
	 * Returns a value property for the minimum value of a SWT Spinner.
36
	 * 
37
	 * @return a value property for the minimum value of a SWT Spinner.
38
	 */
39
	public static IValueProperty minimum() {
40
		return new SpinnerMinimumProperty();
41
	}
42
43
	/**
44
	 * Returns a value property for the maximum value of a SWT Spinner.
45
	 * 
46
	 * @return a value property for the maximum value of a SWT Spinner.
47
	 */
48
	public static IValueProperty maximum() {
49
		return new SpinnerMaximumProperty();
50
	}
51
}
(-)src/org/eclipse/jface/internal/databinding/swt/CComboItemsProperty.java (+29 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.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
}
(-)src/org/eclipse/jface/internal/databinding/swt/ComboSingleSelectionIndexProperty.java (+37 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.Combo;
16
import org.eclipse.swt.widgets.Control;
17
18
/**
19
 * @since 3.3
20
 * 
21
 */
22
public class ComboSingleSelectionIndexProperty extends ControlIntValueProperty {
23
	/**
24
	 * 
25
	 */
26
	public ComboSingleSelectionIndexProperty() {
27
		super(new int[] { SWT.Selection, SWT.DefaultSelection });
28
	}
29
30
	int doGetIntValue(Control control) {
31
		return ((Combo) control).getSelectionIndex();
32
	}
33
34
	void doSetIntValue(Control control, int value) {
35
		((Combo) control).select(value);
36
	}
37
}
(-)src/org/eclipse/jface/internal/databinding/swt/SWTVetoableValueDecorator.java (+138 lines)
Added 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.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
		decorated.removeValueChangeListener(valueListener);
96
		decorated.removeStaleListener(staleListener);
97
		if (widget != null && !widget.isDisposed())
98
			widget.removeListener(SWT.Verify, verifyListener);
99
	}
100
101
	protected void doSetApprovedValue(Object value) {
102
		checkRealm();
103
		decorated.setValue(value);
104
	}
105
106
	protected Object doGetValue() {
107
		getterCalled();
108
		return decorated.getValue();
109
	}
110
111
	public Object getValueType() {
112
		return decorated.getValueType();
113
	}
114
115
	public boolean isStale() {
116
		getterCalled();
117
		return decorated.isStale();
118
	}
119
120
	public void dispose() {
121
		if (decorated != null) {
122
			decorated.dispose();
123
			decorated = null;
124
		}
125
		if (widget != null && !widget.isDisposed())
126
			widget.removeListener(SWT.Verify, verifyListener);
127
		this.widget = null;
128
		super.dispose();
129
	}
130
131
	public Widget getWidget() {
132
		return widget;
133
	}
134
135
	public IObservable getDecorated() {
136
		return decorated;
137
	}
138
}
(-)src/org/eclipse/jface/databinding/swt/TextProperties.java (+45 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.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.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/databinding/swt/ShellProperties.java (+31 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.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.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 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.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.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/internal/databinding/swt/ControlSizeProperty.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
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 ControlValueProperty {
24
	/**
25
	 * 
26
	 */
27
	public ControlSizeProperty() {
28
		super(SWT.Resize, Point.class);
29
	}
30
31
	public Object doGetValue(Control control) {
32
		return control.getSize();
33
	}
34
35
	public void doSetValue(Control control, Object value) {
36
		control.setSize((Point) value);
37
	}
38
}
(-)src/org/eclipse/jface/databinding/swt/CComboProperties.java (+62 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.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.IListProperty;
15
import org.eclipse.core.databinding.property.IValueProperty;
16
import org.eclipse.jface.internal.databinding.swt.CComboItemsProperty;
17
import org.eclipse.jface.internal.databinding.swt.CComboSelectionProperty;
18
import org.eclipse.jface.internal.databinding.swt.CComboSingleSelectionIndexProperty;
19
import org.eclipse.jface.internal.databinding.swt.CComboTextProperty;
20
21
/**
22
 * A factory for creating properties of SWT CCombos
23
 * 
24
 * @since 1.3
25
 */
26
public class CComboProperties {
27
	/**
28
	 * Returns a value property for the selection text of a SWT CCombo.
29
	 * 
30
	 * @return a value property for the selection text of a SWT CCombo.
31
	 */
32
	public static IValueProperty selection() {
33
		return new CComboSelectionProperty();
34
	}
35
36
	/**
37
	 * Returns a value property for the text of a SWT CCombo.
38
	 * 
39
	 * @return a value property for the text of a SWT CCombo.
40
	 */
41
	public static IValueProperty text() {
42
		return new CComboTextProperty();
43
	}
44
45
	/**
46
	 * Returns a list property for the items of a SWT CCombo.
47
	 * 
48
	 * @return a list property for the items of a SWT CCombo.
49
	 */
50
	public static IListProperty items() {
51
		return new CComboItemsProperty();
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 CComboSingleSelectionIndexProperty();
61
	}
62
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlVisibleProperty.java (+28 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
16
/**
17
 * @since 3.3
18
 *
19
 */
20
public class ControlVisibleProperty extends ControlBooleanValueProperty {
21
	boolean doGetBooleanValue(Control control) {
22
		return control.getVisible();
23
	}
24
25
	void doSetBooleanValue(Control control, boolean value) {
26
		control.setVisible(value);
27
	}
28
}
(-)src/org/eclipse/jface/internal/databinding/swt/ListSingleSelectionIndexProperty.java (+37 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.Control;
16
import org.eclipse.swt.widgets.List;
17
18
/**
19
 * @since 3.3
20
 * 
21
 */
22
public class ListSingleSelectionIndexProperty extends ControlIntValueProperty {
23
	/**
24
	 * 
25
	 */
26
	public ListSingleSelectionIndexProperty() {
27
		super(new int[] { SWT.Selection, SWT.DefaultSelection });
28
	}
29
30
	int doGetIntValue(Control control) {
31
		return ((List) control).getSelectionIndex();
32
	}
33
34
	void doSetIntValue(Control control, int value) {
35
		((List) control).setSelection(value);
36
	}
37
}
(-)src/org/eclipse/jface/internal/databinding/swt/CComboSelectionProperty.java (+54 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.custom.CCombo;
16
import org.eclipse.swt.widgets.Control;
17
18
/**
19
 * @since 3.3
20
 * 
21
 */
22
public class CComboSelectionProperty extends ControlStringValueProperty {
23
	/**
24
	 * 
25
	 */
26
	public CComboSelectionProperty() {
27
		super(SWT.Modify);
28
	}
29
30
	String doGetStringValue(Control control) {
31
		return ((CCombo) control).getText();
32
	}
33
34
	void doSetStringValue(Control control, String value) {
35
		CCombo ccombo = (CCombo) control;
36
		String items[] = ccombo.getItems();
37
		int index = -1;
38
		if (value == null) {
39
			ccombo.select(-1);
40
		} else if (items != null) {
41
			for (int i = 0; i < items.length; i++) {
42
				if (value.equals(items[i])) {
43
					index = i;
44
					break;
45
				}
46
			}
47
			if (index == -1) {
48
				ccombo.setText(value);
49
			} else {
50
				ccombo.select(index); // -1 will not "unselect"
51
			}
52
		}
53
	}
54
}
(-)src/org/eclipse/jface/databinding/swt/TableProperties.java (+31 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.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.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/internal/databinding/swt/SpinnerSelectionProperty.java (+37 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.Control;
16
import org.eclipse.swt.widgets.Spinner;
17
18
/**
19
 * @since 3.3
20
 * 
21
 */
22
public class SpinnerSelectionProperty extends ControlIntValueProperty {
23
	/**
24
	 * 
25
	 */
26
	public SpinnerSelectionProperty() {
27
		super(SWT.Modify);
28
	}
29
30
	int doGetIntValue(Control control) {
31
		return ((Spinner) control).getSelection();
32
	}
33
34
	void doSetIntValue(Control control, int value) {
35
		((Spinner) control).setSelection(value);
36
	}
37
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlBoundsProperty.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
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.Rectangle;
17
import org.eclipse.swt.widgets.Control;
18
19
/**
20
 * @since 3.3
21
 * 
22
 */
23
public class ControlBoundsProperty extends ControlValueProperty {
24
	/**
25
	 * 
26
	 */
27
	public ControlBoundsProperty() {
28
		super(new int[] { SWT.Resize, SWT.Move }, Rectangle.class);
29
	}
30
31
	public Object doGetValue(Control control) {
32
		return control.getBounds();
33
	}
34
35
	public void doSetValue(Control control, Object value) {
36
		control.setBounds((Rectangle) value);
37
	}
38
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlValueProperty.java (+125 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.jface.internal.databinding.swt;
13
14
import java.util.HashMap;
15
import java.util.Map;
16
17
import org.eclipse.core.databinding.observable.Diffs;
18
import org.eclipse.core.databinding.property.ValueProperty;
19
import org.eclipse.core.internal.databinding.property.PropertyUpdateHelper;
20
import org.eclipse.jface.util.Util;
21
import org.eclipse.swt.SWT;
22
import org.eclipse.swt.widgets.Control;
23
import org.eclipse.swt.widgets.Event;
24
import org.eclipse.swt.widgets.Listener;
25
import org.eclipse.swt.widgets.Widget;
26
27
abstract class ControlValueProperty extends ValueProperty implements Listener {
28
	private Map controlToValue = new HashMap();
29
30
	private PropertyUpdateHelper updateHelper = new PropertyUpdateHelper();
31
32
	private int[] events;
33
	private Object valueType;
34
35
	ControlValueProperty(Object valueType) {
36
		this(null, valueType);
37
	}
38
39
	ControlValueProperty(int event, Object valueType) {
40
		this(new int[] { event }, valueType);
41
	}
42
43
	ControlValueProperty(int[] events, Object valueType) {
44
		this.events = events;
45
		this.valueType = valueType;
46
	}
47
48
	protected final void addListenerTo(Object source) {
49
		controlToValue.put(source, doGetValue((Control) source));
50
		if (events != null) {
51
			for (int i = 0; i < events.length; i++) {
52
				int event = events[i];
53
				if (event != SWT.None) {
54
					((Widget) source).addListener(event, this);
55
				}
56
			}
57
		}
58
	}
59
60
	protected final void removeListenerFrom(Object source) {
61
		controlToValue.remove(source);
62
		Widget widget = (Widget) source;
63
		if (events != null) {
64
			for (int i = 0; i < events.length; i++) {
65
				int event = events[i];
66
				if (event != SWT.None && !widget.isDisposed())
67
					widget.removeListener(event, this);
68
			}
69
		}
70
	}
71
72
	public void handleEvent(Event event) {
73
		if (!updateHelper.isUpdating(event.widget)) {
74
			Object source = event.widget;
75
			Object oldValue = controlToValue.get(source);
76
			Object newValue = doGetValue((Control) source);
77
			controlToValue.put(source, newValue);
78
			if (!Util.equals(oldValue, newValue))
79
				fireValueChange(source, Diffs.createValueDiff(oldValue,
80
						newValue));
81
		}
82
	}
83
84
	public final Object getValueType() {
85
		return valueType;
86
	}
87
88
	public Object getValue(Object source) {
89
		Object oldValue = controlToValue.get(source);
90
		Object currentValue = doGetValue((Control) source);
91
92
		if (hasListeners(source)) {
93
			controlToValue.put(source, currentValue);
94
			if (!Util.equals(oldValue, currentValue)) {
95
				fireValueChange(source, Diffs.createValueDiff(oldValue,
96
						currentValue));
97
			}
98
		}
99
		return currentValue;
100
	}
101
102
	public void setValue(Object source, Object value) {
103
		Object oldValue = getValue(source);
104
105
		updateHelper.setUpdating(source, true);
106
		try {
107
			doSetValue((Control) source, value);
108
		} finally {
109
			updateHelper.setUpdating(source, false);
110
		}
111
112
		if (hasListeners(source)) {
113
			Object newValue = getValue(source);
114
			if (!Util.equals(oldValue, newValue)) {
115
				controlToValue.put(source, newValue);
116
				fireValueChange(source, Diffs.createValueDiff(oldValue,
117
						newValue));
118
			}
119
		}
120
	}
121
122
	abstract Object doGetValue(Control control);
123
124
	abstract void doSetValue(Control control, Object value);
125
}
(-)src/org/eclipse/jface/databinding/swt/ComboProperties.java (+62 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.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.IListProperty;
15
import org.eclipse.core.databinding.property.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/internal/databinding/swt/ComboTextProperty.java (+37 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.Combo;
16
import org.eclipse.swt.widgets.Control;
17
18
/**
19
 * @since 3.3
20
 * 
21
 */
22
public class ComboTextProperty extends ControlStringValueProperty {
23
	/**
24
	 * 
25
	 */
26
	public ComboTextProperty() {
27
		super(SWT.Modify);
28
	}
29
30
	String doGetStringValue(Control control) {
31
		return ((Combo) control).getText();
32
	}
33
34
	void doSetStringValue(Control control, String value) {
35
		((Combo) control).setText(value != null ? value : ""); //$NON-NLS-1$
36
	}
37
}
(-)src/org/eclipse/jface/internal/databinding/swt/StyledTextTextProperty.java (+49 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.custom.StyledText;
16
import org.eclipse.swt.widgets.Control;
17
18
/**
19
 * @since 3.3
20
 * 
21
 */
22
public class StyledTextTextProperty extends ControlStringValueProperty {
23
	/**
24
	 * @param event
25
	 */
26
	public StyledTextTextProperty(int event) {
27
		super(checkEvent(event));
28
	}
29
30
	private static int checkEvent(int event) {
31
		switch (event) {
32
		case SWT.None:
33
		case SWT.Modify:
34
		case SWT.FocusOut:
35
			return event;
36
		default:
37
			throw new IllegalArgumentException("UpdateEventType [" //$NON-NLS-1$
38
					+ event + "] is not supported."); //$NON-NLS-1$
39
		}
40
	}
41
42
	String doGetStringValue(Control control) {
43
		return ((StyledText) control).getText();
44
	}
45
46
	void doSetStringValue(Control control, String value) {
47
		((StyledText) control).setText(value == null ? "" : value); //$NON-NLS-1$
48
	}
49
}
(-)src/org/eclipse/jface/internal/databinding/swt/LabelTextProperty.java (+29 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
import org.eclipse.swt.widgets.Label;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class LabelTextProperty extends ControlStringValueProperty {
22
	String doGetStringValue(Control control) {
23
		return ((Label) control).getText();
24
	}
25
26
	void doSetStringValue(Control control, String value) {
27
		((Label) control).setText(value == null ? "" : value); //$NON-NLS-1$
28
	}
29
}
(-)src/org/eclipse/jface/databinding/swt/LabelProperties.java (+31 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.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.LabelTextProperty;
16
17
/**
18
 * A factory for creating properties of SWT Labels.
19
 * 
20
 * @since 1.3
21
 */
22
public class LabelProperties {
23
	/**
24
	 * Returns a value property for the text of a SWT Label.
25
	 * 
26
	 * @return a value property for the text of a SWT Label.
27
	 */
28
	public static IValueProperty text() {
29
		return new LabelTextProperty();
30
	}
31
}
(-)src/org/eclipse/jface/internal/databinding/swt/TextTextProperty.java (+49 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.Control;
16
import org.eclipse.swt.widgets.Text;
17
18
/**
19
 * @since 3.3
20
 * 
21
 */
22
public class TextTextProperty extends ControlStringValueProperty {
23
	/**
24
	 * @param event
25
	 */
26
	public TextTextProperty(int event) {
27
		super(checkEvent(event));
28
	}
29
30
	private static int checkEvent(int event) {
31
		switch (event) {
32
		case SWT.None:
33
		case SWT.Modify:
34
		case SWT.FocusOut:
35
			return event;
36
		default:
37
			throw new IllegalArgumentException("UpdateEventType [" //$NON-NLS-1$
38
					+ event + "] is not supported."); //$NON-NLS-1$
39
		}
40
	}
41
42
	String doGetStringValue(Control control) {
43
		return ((Text) control).getText();
44
	}
45
46
	void doSetStringValue(Control control, String value) {
47
		((Text) control).setText(value == null ? "" : value); //$NON-NLS-1$
48
	}
49
}
(-)src/org/eclipse/jface/internal/databinding/swt/SpinnerMaximumProperty.java (+29 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
import org.eclipse.swt.widgets.Spinner;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class SpinnerMaximumProperty extends ControlIntValueProperty {
22
	int doGetIntValue(Control control) {
23
		return ((Spinner) control).getMaximum();
24
	}
25
26
	void doSetIntValue(Control control, int value) {
27
		((Spinner) control).setMaximum(value);
28
	}
29
}
(-)src/org/eclipse/jface/databinding/swt/ListProperties.java (+52 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.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.IListProperty;
15
import org.eclipse.core.databinding.property.IValueProperty;
16
import org.eclipse.jface.internal.databinding.swt.ListItemsProperty;
17
import org.eclipse.jface.internal.databinding.swt.ListSelectionProperty;
18
import org.eclipse.jface.internal.databinding.swt.ListSingleSelectionIndexProperty;
19
20
/**
21
 * A factory for creating properties of SWT List controls.
22
 * 
23
 * @since 1.3
24
 */
25
public class ListProperties {
26
	/**
27
	 * Returns a value property for observing the selection text of a SWT List.
28
	 * 
29
	 * @return a value property for observing the selection text of a SWT List.
30
	 */
31
	public static IValueProperty selection() {
32
		return new ListSelectionProperty();
33
	}
34
35
	/**
36
	 * Returns a list property for observing the items in a SWT List.
37
	 * 
38
	 * @return a list property for observing the items in a SWT List.
39
	 */
40
	public static IListProperty items() {
41
		return new ListItemsProperty();
42
	}
43
44
	/**
45
	 * Returns a value property for the single selection index of a SWT List.
46
	 * 
47
	 * @return a value property for the single selection index of a SWT List.
48
	 */
49
	public static IValueProperty singleSelectionIndex() {
50
		return new ListSingleSelectionIndexProperty();
51
	}
52
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlBooleanValueProperty.java (+46 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
16
/**
17
 * @since 3.3
18
 * 
19
 */
20
public abstract class ControlBooleanValueProperty extends ControlValueProperty {
21
	ControlBooleanValueProperty(int[] events) {
22
		super(events, Boolean.TYPE);
23
	}
24
25
	ControlBooleanValueProperty(int event) {
26
		super(event, Boolean.TYPE);
27
	}
28
29
	ControlBooleanValueProperty() {
30
		super(Boolean.TYPE);
31
	}
32
33
	Object doGetValue(Control control) {
34
		return doGetBooleanValue(control) ? Boolean.TRUE : Boolean.FALSE;
35
	}
36
37
	abstract boolean doGetBooleanValue(Control control);
38
39
	void doSetValue(Control control, Object value) {
40
		if (value == null)
41
			value = Boolean.FALSE;
42
		doSetBooleanValue(control, ((Boolean) value).booleanValue());
43
	}
44
45
	abstract void doSetBooleanValue(Control control, boolean value);
46
}
(-)src/org/eclipse/jface/internal/databinding/swt/ComboItemsProperty.java (+29 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.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
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlStringValueProperty.java (+40 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
16
/**
17
 * @since 3.3
18
 * 
19
 */
20
public abstract class ControlStringValueProperty extends ControlValueProperty {
21
	ControlStringValueProperty(int event) {
22
		super(event, String.class);
23
	}
24
25
	ControlStringValueProperty() {
26
		super(String.class);
27
	}
28
29
	Object doGetValue(Control control) {
30
		return doGetStringValue(control);
31
	}
32
33
	abstract String doGetStringValue(Control control);
34
35
	void doSetValue(Control control, Object value) {
36
		doSetStringValue(control, (String) value);
37
	}
38
39
	abstract void doSetStringValue(Control control, String value);
40
}
(-)src/org/eclipse/jface/internal/databinding/swt/ListItemsProperty.java (+29 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
import org.eclipse.swt.widgets.List;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class ListItemsProperty extends ControlStringListProperty {
22
	protected void doSetStringList(Control control, String[] list) {
23
		((List) control).setItems(list);
24
	}
25
26
	public String[] doGetStringList(Control control) {
27
		return ((List) control).getItems();
28
	}
29
}
(-)src/org/eclipse/jface/internal/databinding/swt/CComboSingleSelectionIndexProperty.java (+37 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.custom.CCombo;
16
import org.eclipse.swt.widgets.Control;
17
18
/**
19
 * @since 3.3
20
 * 
21
 */
22
public class CComboSingleSelectionIndexProperty extends ControlIntValueProperty {
23
	/**
24
	 * 
25
	 */
26
	public CComboSingleSelectionIndexProperty() {
27
		super(new int[] { SWT.Selection, SWT.DefaultSelection });
28
	}
29
30
	int doGetIntValue(Control control) {
31
		return ((CCombo) control).getSelectionIndex();
32
	}
33
34
	void doSetIntValue(Control control, int value) {
35
		((CCombo) control).select(value);
36
	}
37
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlForegroundProperty.java (+36 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.graphics.Color;
15
import org.eclipse.swt.widgets.Control;
16
17
/**
18
 * @since 3.3
19
 *
20
 */
21
public class ControlForegroundProperty extends ControlValueProperty {
22
	/**
23
	 * 
24
	 */
25
	public ControlForegroundProperty() {
26
		super(Color.class);
27
	}
28
29
	public Object doGetValue(Control control) {
30
		return control.getForeground();
31
	}
32
33
	public void doSetValue(Control control, Object value) {
34
		control.setForeground((Color) value);
35
	}
36
}
(-)src/org/eclipse/jface/internal/databinding/swt/ScaleMinimumProperty.java (+29 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
import org.eclipse.swt.widgets.Scale;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class ScaleMinimumProperty extends ControlIntValueProperty {
22
	int doGetIntValue(Control control) {
23
		return ((Scale) control).getMinimum();
24
	}
25
26
	void doSetIntValue(Control control, int value) {
27
		((Scale) control).setMinimum(value);
28
	}
29
}
(-)src/org/eclipse/jface/internal/databinding/swt/SpinnerMinimumProperty.java (+29 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
import org.eclipse.swt.widgets.Spinner;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class SpinnerMinimumProperty extends ControlIntValueProperty {
22
	int doGetIntValue(Control control) {
23
		return ((Spinner) control).getMinimum();
24
	}
25
26
	void doSetIntValue(Control control, int value) {
27
		((Spinner) control).setMinimum(value);
28
	}
29
}
(-)src/org/eclipse/jface/internal/databinding/swt/ScaleSelectionProperty.java (+37 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.Control;
16
import org.eclipse.swt.widgets.Scale;
17
18
/**
19
 * @since 3.3
20
 * 
21
 */
22
public class ScaleSelectionProperty extends ControlIntValueProperty {
23
	/**
24
	 * 
25
	 */
26
	public ScaleSelectionProperty() {
27
		super(SWT.Selection);
28
	}
29
30
	int doGetIntValue(Control control) {
31
		return ((Scale) control).getSelection();
32
	}
33
34
	void doSetIntValue(Control control, int value) {
35
		((Scale) control).setSelection(value);
36
	}
37
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlListProperty.java (+24 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.jface.internal.databinding.swt;
13
14
import org.eclipse.core.databinding.property.BasicListProperty;
15
16
abstract class ControlListProperty extends BasicListProperty {
17
	protected void addListenerTo(Object source) {
18
		// no-op
19
	}
20
21
	protected void removeListenerFrom(Object source) {
22
		// no-op
23
	}
24
}
(-)src/org/eclipse/jface/internal/databinding/swt/TextEditableProperty.java (+29 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
import org.eclipse.swt.widgets.Text;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class TextEditableProperty extends ControlBooleanValueProperty {
22
	boolean doGetBooleanValue(Control control) {
23
		return ((Text) control).getEditable();
24
	}
25
26
	void doSetBooleanValue(Control control, boolean value) {
27
		((Text) control).setEditable(value);
28
	}
29
}
(-)src/org/eclipse/jface/internal/databinding/swt/ShellTextProperty.java (+29 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
import org.eclipse.swt.widgets.Shell;
16
17
/**
18
 * @since 3.3
19
 * 
20
 */
21
public class ShellTextProperty extends ControlStringValueProperty {
22
	String doGetStringValue(Control control) {
23
		return ((Shell) control).getText();
24
	}
25
26
	void doSetStringValue(Control control, String value) {
27
		((Shell) control).setText(value == null ? "" : value); //$NON-NLS-1$
28
	}
29
}
(-)src/org/eclipse/jface/internal/databinding/swt/TableSingleSelectionIndexProperty.java (+37 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.widgets.Control;
16
import org.eclipse.swt.widgets.Table;
17
18
/**
19
 * @since 3.3
20
 * 
21
 */
22
public class TableSingleSelectionIndexProperty extends ControlIntValueProperty {
23
	/**
24
	 * 
25
	 */
26
	public TableSingleSelectionIndexProperty() {
27
		super(new int[] { SWT.Selection, SWT.DefaultSelection });
28
	}
29
30
	int doGetIntValue(Control control) {
31
		return ((Table) control).getSelectionIndex();
32
	}
33
34
	void doSetIntValue(Control control, int value) {
35
		((Table) control).setSelection(value);
36
	}
37
}
(-)src/org/eclipse/jface/internal/databinding/swt/CComboTextProperty.java (+37 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.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.custom.CCombo;
16
import org.eclipse.swt.widgets.Control;
17
18
/**
19
 * @since 3.3
20
 * 
21
 */
22
public class CComboTextProperty extends ControlStringValueProperty {
23
	/**
24
	 * 
25
	 */
26
	public CComboTextProperty() {
27
		super(SWT.Modify);
28
	}
29
30
	String doGetStringValue(Control control) {
31
		return ((CCombo) control).getText();
32
	}
33
34
	void doSetStringValue(Control control, String value) {
35
		((CCombo) control).setText(value != null ? value : ""); //$NON-NLS-1$
36
	}
37
}
(-)src/org/eclipse/core/internal/databinding/beans/JavaBeanObservableMap.java (-142 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
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
20
import org.eclipse.core.databinding.beans.IBeanObservable;
21
import org.eclipse.core.databinding.observable.Diffs;
22
import org.eclipse.core.databinding.observable.map.ComputedObservableMap;
23
import org.eclipse.core.databinding.observable.set.IObservableSet;
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.0
30
 * 
31
 */
32
public class JavaBeanObservableMap extends ComputedObservableMap implements
33
		IBeanObservable {
34
35
	private PropertyDescriptor propertyDescriptor;
36
	
37
	private PropertyChangeListener elementListener = new PropertyChangeListener() {
38
		public void propertyChange(final java.beans.PropertyChangeEvent event) {
39
			if (!updating) {
40
				getRealm().exec(new Runnable() {
41
					public void run() {
42
						fireMapChange(Diffs.createMapDiffSingleChange(
43
								event.getSource(), event.getOldValue(), event
44
								.getNewValue()));
45
					}
46
				});
47
			}
48
		}
49
	};
50
51
	private ListenerSupport listenerSupport;
52
53
	private boolean updating = false;
54
55
	private boolean attachListeners;
56
57
	/**
58
	 * @param domain
59
	 * @param propertyDescriptor
60
	 */
61
	public JavaBeanObservableMap(IObservableSet domain,
62
			PropertyDescriptor propertyDescriptor) {
63
		this(domain, propertyDescriptor, true);
64
	}
65
66
	/**
67
	 * @param domain
68
	 * @param propertyDescriptor
69
	 * @param attachListeners
70
	 */
71
	public JavaBeanObservableMap(IObservableSet domain,
72
			PropertyDescriptor propertyDescriptor, boolean attachListeners) {
73
		super(domain);
74
75
		this.propertyDescriptor = propertyDescriptor;
76
		this.attachListeners = attachListeners;
77
		if (attachListeners) {
78
			this.listenerSupport = new ListenerSupport(elementListener,
79
					propertyDescriptor.getName());
80
		}
81
		init();
82
	}
83
84
	protected void hookListener(Object domainElement) {
85
		if (attachListeners && domainElement != null) {
86
			listenerSupport.hookListener(domainElement);
87
		}
88
	}
89
90
	protected void unhookListener(Object domainElement) {
91
		if (attachListeners && domainElement != null) {
92
			listenerSupport.unhookListener(domainElement);
93
		}
94
	}
95
96
	protected Object doGet(Object key) {
97
		if (key == null) {
98
			return null;
99
		}
100
		try {
101
			Method readMethod = propertyDescriptor.getReadMethod();
102
			if (!readMethod.isAccessible()) {
103
				readMethod.setAccessible(true);
104
			}
105
			return readMethod.invoke(key, new Object[0]);
106
		} catch (Exception e) {
107
			Policy.getLog().log(
108
					new Status(IStatus.ERROR, Policy.JFACE_DATABINDING,
109
							IStatus.ERROR, "cannot get value", e)); //$NON-NLS-1$
110
			throw new RuntimeException(e);
111
		}
112
	}
113
114
	protected Object doPut(Object key, Object value) {
115
		try {
116
			Object oldValue = get(key);
117
			propertyDescriptor.getWriteMethod().invoke(key,
118
					new Object[] { value });
119
			keySet().add(key);
120
			return oldValue;
121
		} catch (Exception e) {
122
			Policy.getLog().log(
123
					new Status(IStatus.ERROR, Policy.JFACE_DATABINDING,
124
							IStatus.ERROR, "cannot set value", e)); //$NON-NLS-1$
125
			throw new RuntimeException(e);
126
		}
127
	}
128
129
	/* (non-Javadoc)
130
	 * @see org.eclipse.core.databinding.beans.IBeanObservable#getObserved()
131
	 */
132
	public Object getObserved() {
133
		return keySet();
134
	}
135
136
	/* (non-Javadoc)
137
	 * @see org.eclipse.core.databinding.beans.IBeanObservable#getPropertyDescriptor()
138
	 */
139
	public PropertyDescriptor getPropertyDescriptor() {
140
		return propertyDescriptor;
141
	}
142
}
(-)src/org/eclipse/core/internal/databinding/beans/JavaBeanObservableList.java (-410 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
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 PropertyChangeListener collectionListener = new PropertyChangeListener() {
45
		public void propertyChange(java.beans.PropertyChangeEvent event) {
46
			if (!updating) {
47
				getRealm().exec(new Runnable() {
48
					public void run() {
49
						updateWrappedList( new ArrayList( Arrays.asList(getValues() ) ) );
50
					}
51
				});
52
			}
53
		}
54
	};
55
56
	private boolean updating = false;
57
58
	private PropertyDescriptor descriptor;
59
60
	private ListenerSupport collectionListenSupport;
61
62
	private boolean attachListeners;
63
64
	/**
65
	 * @param realm
66
	 * @param object
67
	 * @param descriptor
68
	 * @param elementType
69
	 */
70
	public JavaBeanObservableList(Realm realm, Object object,
71
			PropertyDescriptor descriptor, Class elementType) {
72
		this(realm, object, descriptor, elementType, true);
73
	}
74
75
	/**
76
	 * @param realm
77
	 * @param object
78
	 * @param descriptor
79
	 * @param elementType
80
	 * @param attachListeners
81
	 */
82
	public JavaBeanObservableList(Realm realm, Object object,
83
			PropertyDescriptor descriptor, Class elementType,
84
			boolean attachListeners) {
85
86
		super(realm, new ArrayList(), elementType);
87
		this.object = object;
88
		this.descriptor = descriptor;
89
		this.attachListeners = attachListeners;
90
91
		if (attachListeners) {
92
			this.collectionListenSupport = new ListenerSupport(
93
					collectionListener, descriptor.getName());
94
		}
95
96
		// initialize list without firing events
97
		wrappedList.addAll(Arrays.asList(getValues()));
98
	}
99
100
	protected void firstListenerAdded() {
101
		if (attachListeners) {
102
			collectionListenSupport.hookListener(this.object);
103
		}
104
	}
105
106
	protected void lastListenerRemoved() {
107
		if (collectionListenSupport != null) {
108
			collectionListenSupport.dispose();
109
		}
110
	}
111
112
	public void dispose() {
113
		super.dispose();
114
		lastListenerRemoved();
115
	}
116
117
	private Object primGetValues() {
118
		Exception ex = null;
119
		try {
120
			Method readMethod = descriptor.getReadMethod();
121
			if (!readMethod.isAccessible()) {
122
				readMethod.setAccessible(true);
123
			}
124
			return readMethod.invoke(object, new Object[0]);
125
		} catch (IllegalArgumentException e) {
126
			ex = e;
127
		} catch (IllegalAccessException e) {
128
			ex = e;
129
		} catch (InvocationTargetException e) {
130
			ex = e;
131
		}
132
		throw new BindingException("Could not read collection values", ex); //$NON-NLS-1$
133
	}
134
135
	private Object[] getValues() {
136
		Object[] values = null;
137
138
		Object result = primGetValues();
139
		if (descriptor.getPropertyType().isArray())
140
			values = (Object[]) result;
141
		else {
142
			// TODO add jUnit for POJO (var. SettableValue) collections
143
			Collection list = (Collection) result;
144
			if (list != null) {
145
				values = list.toArray();
146
			}
147
		}
148
		if (values == null)
149
			values = new Object[0];
150
		return values;
151
	}
152
153
	public Object getObserved() {
154
		return object;
155
	}
156
157
	public PropertyDescriptor getPropertyDescriptor() {
158
		return descriptor;
159
	}
160
161
	private void setValues() {
162
		if (descriptor.getPropertyType().isArray()) {
163
			Class componentType = descriptor.getPropertyType()
164
					.getComponentType();
165
			Object[] newArray = (Object[]) Array.newInstance(componentType,
166
					wrappedList.size());
167
			wrappedList.toArray(newArray);
168
			primSetValues(newArray);
169
		} else {
170
			// assume that it is a java.util.List
171
			primSetValues(new ArrayList(wrappedList));
172
		}
173
	}
174
175
	private void primSetValues(Object newValue) {
176
		Exception ex = null;
177
		try {
178
			Method writeMethod = descriptor.getWriteMethod();
179
			if (!writeMethod.isAccessible()) {
180
				writeMethod.setAccessible(true);
181
			}
182
			writeMethod.invoke(object, new Object[] { newValue });
183
			return;
184
		} catch (IllegalArgumentException e) {
185
			ex = e;
186
		} catch (IllegalAccessException e) {
187
			ex = e;
188
		} catch (InvocationTargetException e) {
189
			ex = e;
190
		}
191
		throw new BindingException("Could not write collection values", ex); //$NON-NLS-1$
192
	}
193
194
	public Object set(int index, Object element) {
195
		getterCalled();
196
		updating = true;
197
		try {
198
			Object oldElement = wrappedList.set(index, element);
199
			setValues();
200
			fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
201
					index, false, oldElement), Diffs.createListDiffEntry(index,
202
					true, element)));
203
			return oldElement;
204
		} finally {
205
			updating = false;
206
		}
207
	}
208
209
	public Object move(int oldIndex, int newIndex) {
210
		getterCalled();
211
		updating = true;
212
		try {
213
			int size = wrappedList.size();
214
			if (oldIndex < 0 || oldIndex >= size)
215
				throw new IndexOutOfBoundsException(
216
						"oldIndex: " + oldIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$
217
			if (newIndex < 0 || newIndex >= size)
218
				throw new IndexOutOfBoundsException(
219
						"newIndex: " + newIndex + ", size:" + size); //$NON-NLS-1$ //$NON-NLS-2$
220
			if (oldIndex == newIndex)
221
				return wrappedList.get(oldIndex);
222
			Object element = wrappedList.remove(oldIndex);
223
			wrappedList.add(newIndex, element);
224
			setValues();
225
			fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
226
					oldIndex, false, element), Diffs.createListDiffEntry(
227
					newIndex, true, element)));
228
			return element;
229
		} finally {
230
			updating = false;
231
		}
232
	}
233
234
	public Object remove(int index) {
235
		getterCalled();
236
		updating = true;
237
		try {
238
			Object oldElement = wrappedList.remove(index);
239
			setValues();
240
			fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
241
					index, false, oldElement)));
242
			return oldElement;
243
		} finally {
244
			updating = false;
245
		}
246
	}
247
248
	public boolean add(Object element) {
249
		updating = true;
250
		try {
251
			int index = wrappedList.size();
252
			boolean result = wrappedList.add(element);
253
			setValues();
254
			fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
255
					index, true, element)));
256
			return result;
257
		} finally {
258
			updating = false;
259
		}
260
	}
261
262
	public void add(int index, Object element) {
263
		updating = true;
264
		try {
265
			wrappedList.add(index, element);
266
			setValues();
267
			fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
268
					index, true, element)));
269
		} finally {
270
			updating = false;
271
		}
272
	}
273
274
	public boolean addAll(Collection c) {
275
		if (c.isEmpty()) {
276
			return false;
277
		}
278
		updating = true;
279
		try {
280
			int index = wrappedList.size();
281
			boolean result = wrappedList.addAll(c);
282
			setValues();
283
			ListDiffEntry[] entries = new ListDiffEntry[c.size()];
284
			int i = 0;
285
			for (Iterator it = c.iterator(); it.hasNext();) {
286
				Object o = it.next();
287
				entries[i++] = Diffs.createListDiffEntry(index++, true, o);
288
			}
289
			fireListChange(Diffs.createListDiff(entries));
290
			return result;
291
		} finally {
292
			updating = false;
293
		}
294
	}
295
296
	public boolean addAll(int index, Collection c) {
297
		if (c.isEmpty()) {
298
			return false;
299
		}
300
		updating = true;
301
		try {
302
			boolean result = wrappedList.addAll(index, c);
303
			setValues();
304
			ListDiffEntry[] entries = new ListDiffEntry[c.size()];
305
			int i = 0;
306
			for (Iterator it = c.iterator(); it.hasNext();) {
307
				Object o = it.next();
308
				entries[i++] = Diffs.createListDiffEntry(index++, true, o);
309
			}
310
			fireListChange(Diffs.createListDiff(entries));
311
			return result;
312
		} finally {
313
			updating = false;
314
		}
315
	}
316
317
	public boolean remove(Object o) {
318
		getterCalled();
319
		int index = wrappedList.indexOf(o);
320
		if (index == -1) {
321
			return false;
322
		}
323
		updating = true;
324
		try {
325
			Object oldElement = wrappedList.remove(index);
326
			setValues();
327
			fireListChange(Diffs.createListDiff(Diffs.createListDiffEntry(
328
					index, false, oldElement)));
329
			return true;
330
		} finally {
331
			updating = false;
332
		}
333
	}
334
335
	public boolean removeAll(Collection c) {
336
		getterCalled();
337
		boolean changed = false;
338
		updating = true;
339
		try {
340
			List diffEntries = new ArrayList();
341
			for (Iterator it = c.iterator(); it.hasNext();) {
342
				Object o = it.next();
343
				int index = wrappedList.indexOf(o);
344
				if (index != -1) {
345
					changed = true;
346
					Object oldElement = wrappedList.remove(index);
347
					diffEntries.add(Diffs.createListDiffEntry(index, false,
348
							oldElement));
349
				}
350
			}
351
			if (changed) {
352
				setValues();
353
				fireListChange(Diffs
354
						.createListDiff((ListDiffEntry[]) diffEntries
355
								.toArray(new ListDiffEntry[diffEntries.size()])));
356
			}
357
			return changed;
358
		} finally {
359
			updating = false;
360
		}
361
	}
362
363
	public boolean retainAll(Collection c) {
364
		getterCalled();
365
		boolean changed = false;
366
		updating = true;
367
		try {
368
			List diffEntries = new ArrayList();
369
			int index = 0;
370
			for (Iterator it = wrappedList.iterator(); it.hasNext();) {
371
				Object o = it.next();
372
				boolean retain = c.contains(o);
373
				if (retain) {
374
					index++;
375
				} else {
376
					changed = true;
377
					it.remove();
378
					diffEntries.add(Diffs.createListDiffEntry(index, false, o));
379
				}
380
			}
381
			if (changed) {
382
				setValues();
383
				fireListChange(Diffs
384
						.createListDiff((ListDiffEntry[]) diffEntries
385
								.toArray(new ListDiffEntry[diffEntries.size()])));
386
			}
387
			return changed;
388
		} finally {
389
			updating = false;
390
		}
391
	}
392
393
	public void clear() {
394
		updating = true;
395
		try {
396
			List diffEntries = new ArrayList();
397
			for (Iterator it = wrappedList.iterator(); it.hasNext();) {
398
				Object o = it.next();
399
				diffEntries.add(Diffs.createListDiffEntry(0, false, o));
400
			}
401
			wrappedList.clear();
402
			setValues();
403
			fireListChange(Diffs.createListDiff((ListDiffEntry[]) diffEntries
404
					.toArray(new ListDiffEntry[diffEntries.size()])));
405
		} finally {
406
			updating = false;
407
		}
408
	}
409
410
}
(-)src/org/eclipse/core/internal/databinding/beans/JavaBeanPropertyObservableMap.java (-241 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
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 PropertyChangeListener mapListener = new PropertyChangeListener() {
45
		public void propertyChange(final PropertyChangeEvent event) {
46
			if (!updating) {
47
				getRealm().exec(new Runnable() {
48
					public void run() {
49
						Map oldValue = wrappedMap;
50
						Map newValue = (Map) event.getNewValue();
51
						wrappedMap = new HashMap(newValue);
52
						
53
						fireMapChange(Diffs.computeMapDiff(oldValue, newValue));
54
					}
55
				});
56
			}
57
		}
58
	};
59
60
	private boolean updating = false;
61
62
	private PropertyDescriptor descriptor;
63
64
	private ListenerSupport collectionListenSupport;
65
66
	private boolean attachListeners;
67
68
	/**
69
	 * @param realm
70
	 * @param object
71
	 * @param descriptor
72
	 */
73
	public JavaBeanPropertyObservableMap(Realm realm, Object object,
74
			PropertyDescriptor descriptor) {
75
		this(realm, object, descriptor, true);
76
	}
77
78
	/**
79
	 * @param realm
80
	 * @param object
81
	 * @param descriptor
82
	 * @param attachListeners
83
	 */
84
	public JavaBeanPropertyObservableMap(Realm realm, Object object,
85
			PropertyDescriptor descriptor, boolean attachListeners) {
86
		super(realm, new HashMap());
87
		this.object = object;
88
		this.descriptor = descriptor;
89
		this.attachListeners = attachListeners;
90
		if (attachListeners) {
91
			this.collectionListenSupport = new ListenerSupport(mapListener,
92
					descriptor.getName());
93
		}
94
95
		wrappedMap.putAll(getMap());
96
	}
97
98
	protected void firstListenerAdded() {
99
		if (attachListeners) {
100
			collectionListenSupport.hookListener(this.object);
101
		}
102
	}
103
104
	protected void lastListenerRemoved() {
105
		if (collectionListenSupport != null) {
106
			collectionListenSupport.dispose();
107
		}
108
	}
109
110
	private Object primGetMap() {
111
		try {
112
			Method readMethod = descriptor.getReadMethod();
113
			if (!readMethod.isAccessible()) {
114
				readMethod.setAccessible(true);
115
			}
116
			return readMethod.invoke(object, new Object[0]);
117
		} catch (IllegalArgumentException e) {
118
		} catch (IllegalAccessException e) {
119
		} catch (InvocationTargetException e) {
120
		}
121
		Assert.isTrue(false, "Could not read collection values"); //$NON-NLS-1$
122
		return null;
123
	}
124
125
	private void primSetMap(Object newValue) {
126
		Exception ex = null;
127
		try {
128
			Method writeMethod = descriptor.getWriteMethod();
129
			if (!writeMethod.isAccessible()) {
130
				writeMethod.setAccessible(true);
131
			}
132
			writeMethod.invoke(object, new Object[] { newValue });
133
			return;
134
		} catch (IllegalArgumentException e) {
135
			ex = e;
136
		} catch (IllegalAccessException e) {
137
			ex = e;
138
		} catch (InvocationTargetException e) {
139
			ex = e;
140
		}
141
		throw new BindingException("Could not write collection values", ex); //$NON-NLS-1$
142
	}
143
144
	private Map getMap() {
145
		Map result = (Map) primGetMap();
146
147
		if (result == null)
148
			result = new HashMap();
149
		return result;
150
	}
151
152
	private void setMap() {
153
		primSetMap(new HashMap(wrappedMap));
154
	}
155
156
	public Object put(Object key, Object value) {
157
		checkRealm();
158
		updating = true;
159
		try {
160
			Object result = wrappedMap.put(key, value);
161
			if (!Util.equals(result, value)) {
162
				setMap();
163
				if (result == null) {
164
					fireMapChange(Diffs.createMapDiffSingleAdd(key, value));
165
				} else {
166
					fireMapChange(Diffs.createMapDiffSingleChange(key, result,
167
							value));
168
				}
169
			}
170
			return result;
171
		} finally {
172
			updating = false;
173
		}
174
	}
175
176
	public void putAll(Map map) {
177
		checkRealm();
178
		updating = true;
179
		try {
180
			Set addedKeys = new HashSet(map.size());
181
			Map changes = new HashMap(map.size());
182
			for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
183
				Map.Entry entry = (Entry) it.next();
184
				Object key = entry.getKey();
185
				Object newValue = entry.getValue();
186
				Object oldValue = wrappedMap.put(key, newValue);
187
				if (oldValue == null) {
188
					addedKeys.add(key);
189
				} else if (!Util.equals(oldValue, newValue)) {
190
					changes.put(key, oldValue);
191
				}
192
			}
193
			if (!addedKeys.isEmpty() || !changes.isEmpty()) {
194
				setMap();
195
				fireMapChange(Diffs.createMapDiff(addedKeys,
196
						Collections.EMPTY_SET, changes.keySet(), changes,
197
						wrappedMap));
198
			}
199
		} finally {
200
			updating = false;
201
		}
202
	}
203
204
	public Object remove(Object key) {
205
		checkRealm();
206
		updating = true;
207
		try {
208
			Object result = wrappedMap.remove(key);
209
			if (result!=null) {
210
				setMap();
211
				fireMapChange(Diffs.createMapDiffSingleRemove(key, result));
212
			}
213
			return result;
214
		} finally {
215
			updating = false;
216
		}
217
	}
218
219
	public void clear() {
220
		checkRealm();
221
		if (wrappedMap.isEmpty())
222
			return;
223
		updating = true;
224
		try {
225
			Map oldMap = wrappedMap;
226
			wrappedMap = new HashMap();
227
			setMap();
228
			fireMapChange(Diffs.computeMapDiff(oldMap, Collections.EMPTY_MAP));
229
		} finally {
230
			updating = false;
231
		}
232
	}
233
234
	public Object getObserved() {
235
		return object;
236
	}
237
238
	public PropertyDescriptor getPropertyDescriptor() {
239
		return descriptor;
240
	}
241
}
(-)src/org/eclipse/core/internal/databinding/beans/JavaBeanObservableSet.java (-306 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 - bug 221351, 223164
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 PropertyChangeListener collectionListener = new PropertyChangeListener() {
44
		public void propertyChange(java.beans.PropertyChangeEvent event) {
45
			if (!updating) {
46
				getRealm().exec(new Runnable() {
47
					public void run() {
48
						Set newElements = new HashSet(Arrays
49
								.asList(getValues()));
50
						Set addedElements = new HashSet(newElements);
51
						Set removedElements = new HashSet(wrappedSet);
52
						// remove all new elements from old elements to compute
53
						// the removed elements
54
						removedElements.removeAll(newElements);
55
						addedElements.removeAll(wrappedSet);
56
						wrappedSet = newElements;
57
						fireSetChange(Diffs.createSetDiff(addedElements,
58
								removedElements));
59
					}
60
				});
61
			}
62
		}
63
	};
64
65
	private boolean updating = false;
66
67
	private PropertyDescriptor descriptor;
68
69
	private ListenerSupport collectionListenSupport;
70
71
	private boolean attachListeners;
72
73
	/**
74
	 * @param realm
75
	 * @param object
76
	 * @param descriptor
77
	 * @param elementType
78
	 */
79
	public JavaBeanObservableSet(Realm realm, Object object,
80
			PropertyDescriptor descriptor, Class elementType) {
81
		this(realm, object, descriptor, elementType, true);
82
	}
83
84
	/**
85
	 * @param realm
86
	 * @param object
87
	 * @param descriptor
88
	 * @param elementType
89
	 * @param attachListeners
90
	 */
91
	public JavaBeanObservableSet(Realm realm, Object object,
92
			PropertyDescriptor descriptor, Class elementType,
93
			boolean attachListeners) {
94
		super(realm, new HashSet(), elementType);
95
		this.object = object;
96
		this.descriptor = descriptor;
97
		this.attachListeners = attachListeners;
98
		if (attachListeners) {
99
			this.collectionListenSupport = new ListenerSupport(
100
					collectionListener, descriptor.getName());
101
		}
102
103
		wrappedSet.addAll(Arrays.asList(getValues()));
104
	}
105
106
	protected void firstListenerAdded() {
107
		if (attachListeners) {
108
			collectionListenSupport.hookListener(this.object);
109
		}
110
	}
111
112
	protected void lastListenerRemoved() {
113
		if (collectionListenSupport != null) {
114
			collectionListenSupport.dispose();
115
		}
116
	}
117
118
	private Object primGetValues() {
119
		try {
120
			Method readMethod = descriptor.getReadMethod();
121
			if (!readMethod.isAccessible()) {
122
				readMethod.setAccessible(true);
123
			}
124
			return readMethod.invoke(object, new Object[0]);
125
		} catch (IllegalArgumentException e) {
126
		} catch (IllegalAccessException e) {
127
		} catch (InvocationTargetException e) {
128
		}
129
		Assert.isTrue(false, "Could not read collection values"); //$NON-NLS-1$
130
		return null;
131
	}
132
133
	private Object[] getValues() {
134
		Object[] values = null;
135
136
		Object result = primGetValues();
137
		if (descriptor.getPropertyType().isArray())
138
			values = (Object[]) result;
139
		else {
140
			// TODO add jUnit for POJO (var. SettableValue) collections
141
			Collection list = (Collection) result;
142
			if (list != null)
143
				values = list.toArray();
144
		}
145
		if (values == null)
146
			values = new Object[0];
147
		return values;
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
					wrappedSet.size());
156
			wrappedSet.toArray(newArray);
157
			primSetValues(newArray);
158
		} else {
159
			// assume that it is a java.util.Set
160
			primSetValues(new HashSet(wrappedSet));
161
		}
162
	}
163
164
	public boolean add(Object o) {
165
		getterCalled();
166
		updating = true;
167
		try {
168
			boolean added = wrappedSet.add(o);
169
			if (added) {
170
				setValues();
171
				fireSetChange(Diffs.createSetDiff(Collections.singleton(o),
172
						Collections.EMPTY_SET));
173
			}
174
			return added;
175
		} finally {
176
			updating = false;
177
		}
178
	}
179
180
	public boolean remove(Object o) {
181
		getterCalled();
182
		updating = true;
183
		try {
184
			boolean removed = wrappedSet.remove(o);
185
			if (removed) {
186
				setValues();
187
				fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET,
188
						Collections.singleton(o)));
189
			}
190
			return removed;
191
		} finally {
192
			updating = false;
193
		}
194
	}
195
196
	public boolean addAll(Collection c) {
197
		getterCalled();
198
		updating = true;
199
		try {
200
			Set additions = new HashSet();
201
			for (Iterator iterator = c.iterator(); iterator.hasNext();) {
202
				Object element = iterator.next();
203
				if (wrappedSet.add(element))
204
					additions.add(element);
205
			}
206
			boolean changed = !additions.isEmpty();
207
			if (changed) {
208
				setValues();
209
				fireSetChange(Diffs.createSetDiff(additions,
210
						Collections.EMPTY_SET));
211
			}
212
			return changed;
213
		} finally {
214
			updating = false;
215
		}
216
	}
217
218
	public boolean removeAll(Collection c) {
219
		getterCalled();
220
		updating = true;
221
		try {
222
			Set removals = new HashSet();
223
			for (Iterator iterator = c.iterator(); iterator.hasNext();) {
224
				Object element = iterator.next();
225
				if (wrappedSet.remove(element))
226
					removals.add(element);
227
			}
228
			boolean changed = !removals.isEmpty();
229
			if (changed) {
230
				setValues();
231
				fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET,
232
						removals));
233
			}
234
			return changed;
235
		} finally {
236
			updating = false;
237
		}
238
	}
239
240
	public boolean retainAll(Collection c) {
241
		getterCalled();
242
		updating = true;
243
		try {
244
			Set removals = new HashSet();
245
			for (Iterator iterator = wrappedSet.iterator(); iterator.hasNext();) {
246
				Object element = iterator.next();
247
				if (!c.contains(element)) {
248
					iterator.remove();
249
					removals.add(element);
250
				}
251
			}
252
			boolean changed = !removals.isEmpty();
253
			if (changed) {
254
				setValues();
255
				fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET,
256
						removals));
257
			}
258
			return changed;
259
		} finally {
260
			updating = false;
261
		}
262
	}
263
264
	public void clear() {
265
		getterCalled();
266
		if (wrappedSet.isEmpty())
267
			return;
268
269
		updating = true;
270
		try {
271
			Set removals = new HashSet(wrappedSet);
272
			wrappedSet.clear();
273
			setValues();
274
			fireSetChange(Diffs.createSetDiff(Collections.EMPTY_SET, removals));
275
		} finally {
276
			updating = false;
277
		}
278
	}
279
280
	private void primSetValues(Object newValue) {
281
		Exception ex = null;
282
		try {
283
			Method writeMethod = descriptor.getWriteMethod();
284
			if (!writeMethod.isAccessible()) {
285
				writeMethod.setAccessible(true);
286
			}
287
			writeMethod.invoke(object, new Object[] { newValue });
288
			return;
289
		} catch (IllegalArgumentException e) {
290
			ex = e;
291
		} catch (IllegalAccessException e) {
292
			ex = e;
293
		} catch (InvocationTargetException e) {
294
			ex = e;
295
		}
296
		throw new BindingException("Could not write collection values", ex); //$NON-NLS-1$
297
	}
298
299
	public Object getObserved() {
300
		return object;
301
	}
302
303
	public PropertyDescriptor getPropertyDescriptor() {
304
		return descriptor;
305
	}
306
}
(-)src/org/eclipse/core/internal/databinding/beans/JavaBeanObservableValue.java (-190 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
 *     Brad Reynolds - bug 164134, 171616
12
 *******************************************************************************/
13
package org.eclipse.core.internal.databinding.beans;
14
15
import java.beans.PropertyChangeListener;
16
import java.beans.PropertyDescriptor;
17
import java.lang.reflect.InvocationTargetException;
18
import java.lang.reflect.Method;
19
20
import org.eclipse.core.databinding.BindingException;
21
import org.eclipse.core.databinding.beans.BeansObservables;
22
import org.eclipse.core.databinding.beans.IBeanObservable;
23
import org.eclipse.core.databinding.observable.Diffs;
24
import org.eclipse.core.databinding.observable.Realm;
25
import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
26
import org.eclipse.core.databinding.observable.value.ValueDiff;
27
import org.eclipse.core.databinding.util.Policy;
28
import org.eclipse.core.internal.databinding.Util;
29
import org.eclipse.core.runtime.IStatus;
30
import org.eclipse.core.runtime.Status;
31
32
/**
33
 * @since 1.0
34
 * 
35
 */
36
public class JavaBeanObservableValue extends AbstractObservableValue implements IBeanObservable {
37
	private final Object object;
38
	private boolean updating = false;
39
40
	private final PropertyDescriptor propertyDescriptor;
41
	private ListenerSupport listenerSupport;
42
43
	private boolean attachListeners;
44
45
	/**
46
	 * @param realm
47
	 * @param object
48
	 * @param descriptor
49
	 */
50
	public JavaBeanObservableValue(Realm realm, Object object,
51
			PropertyDescriptor descriptor) {
52
		this(realm, object, descriptor, true);
53
	}
54
55
	/**
56
	 * @param realm
57
	 * @param object
58
	 * @param descriptor
59
	 * @param attachListeners
60
	 */
61
	public JavaBeanObservableValue(Realm realm, Object object,
62
			PropertyDescriptor descriptor, boolean attachListeners) {
63
		super(realm);
64
		this.object = object;
65
		this.propertyDescriptor = descriptor;
66
		this.attachListeners = attachListeners;
67
	}
68
69
	protected void firstListenerAdded() {
70
		if (!attachListeners) {
71
			return;
72
		}
73
			
74
		PropertyChangeListener listener = new PropertyChangeListener() {
75
			public void propertyChange(java.beans.PropertyChangeEvent event) {
76
				if (!updating) {
77
					final ValueDiff diff = Diffs.createValueDiff(event.getOldValue(),
78
											event.getNewValue());
79
					getRealm().exec(new Runnable(){
80
						public void run() {
81
							fireValueChange(diff);
82
						}});
83
				}
84
			}
85
		};
86
		
87
		if (listenerSupport == null) {
88
			listenerSupport = new ListenerSupport(listener, propertyDescriptor.getName());
89
		}
90
		
91
		listenerSupport.hookListener(object);
92
	}
93
94
	public void doSetValue(Object value) {
95
		updating = true;
96
		try {
97
			Object oldValue = doGetValue();
98
			
99
			if (Util.equals(oldValue, value)) {
100
				return;
101
			}
102
			
103
			Method writeMethod = propertyDescriptor.getWriteMethod();
104
			if (!writeMethod.isAccessible()) {
105
				writeMethod.setAccessible(true);
106
			}
107
			writeMethod.invoke(object, new Object[] { value });
108
			fireValueChange(Diffs.createValueDiff(oldValue, doGetValue()));
109
		} catch (InvocationTargetException e) {
110
			/*
111
			 * InvocationTargetException wraps any exception thrown by the
112
			 * invoked method.
113
			 */
114
			throw new RuntimeException(e.getCause());
115
		} catch (Exception e) {
116
			if (BeansObservables.DEBUG) {
117
				Policy
118
						.getLog()
119
						.log(
120
								new Status(
121
										IStatus.WARNING,
122
										Policy.JFACE_DATABINDING,
123
										IStatus.OK,
124
										"Could not change value of " + object + "." + propertyDescriptor.getName(), e)); //$NON-NLS-1$ //$NON-NLS-2$
125
			}
126
		} finally {
127
			updating = false;
128
		}
129
	}
130
131
	public Object doGetValue() {
132
		try {
133
			Method readMethod = propertyDescriptor.getReadMethod();
134
			if (readMethod == null) {
135
				throw new BindingException(propertyDescriptor.getName()
136
						+ " property does not have a read method."); //$NON-NLS-1$
137
			}
138
			if (!readMethod.isAccessible()) {
139
				readMethod.setAccessible(true);
140
			}
141
			return readMethod.invoke(object, null);
142
		} catch (InvocationTargetException e) {
143
			/*
144
			 * InvocationTargetException wraps any exception thrown by the
145
			 * invoked method.
146
			 */
147
			throw new RuntimeException(e.getCause());
148
		} catch (Exception e) {
149
			if (BeansObservables.DEBUG) {
150
				Policy
151
						.getLog()
152
						.log(
153
								new Status(
154
										IStatus.WARNING,
155
										Policy.JFACE_DATABINDING,
156
										IStatus.OK,
157
										"Could not read value of " + object + "." + propertyDescriptor.getName(), e)); //$NON-NLS-1$ //$NON-NLS-2$
158
			}
159
			return null;
160
		}
161
	}
162
163
	protected void lastListenerRemoved() {
164
		unhookListener();
165
	}
166
167
	private void unhookListener() {
168
		if (listenerSupport != null) {
169
			listenerSupport.dispose();
170
			listenerSupport = null;
171
		}
172
	}
173
174
	public Object getValueType() {
175
		return propertyDescriptor.getPropertyType();
176
	}
177
178
	public Object getObserved() {
179
		return object;
180
	}
181
182
	public PropertyDescriptor getPropertyDescriptor() {
183
		return propertyDescriptor;
184
	}
185
186
	public synchronized void dispose() {
187
		unhookListener();
188
		super.dispose();
189
	}
190
}
(-)src/org/eclipse/core/databinding/beans/BeansObservables.java (-114 / +60 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
11
 *     Matthew Hall - bug 221704, 234686, 246625, 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-41 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.IListProperty;
27
import org.eclipse.core.databinding.property.IMapProperty;
28
import org.eclipse.core.databinding.property.ISetProperty;
29
import org.eclipse.core.databinding.property.IValueProperty;
30
import org.eclipse.core.databinding.property.PropertyObservables;
30
import org.eclipse.core.internal.databinding.beans.BeanObservableListDecorator;
31
import org.eclipse.core.internal.databinding.beans.BeanObservableListDecorator;
31
import org.eclipse.core.internal.databinding.beans.BeanObservableMapDecorator;
32
import org.eclipse.core.internal.databinding.beans.BeanObservableMapDecorator;
32
import org.eclipse.core.internal.databinding.beans.BeanObservableSetDecorator;
33
import org.eclipse.core.internal.databinding.beans.BeanObservableSetDecorator;
33
import org.eclipse.core.internal.databinding.beans.BeanObservableValueDecorator;
34
import org.eclipse.core.internal.databinding.beans.BeanObservableValueDecorator;
34
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableList;
35
import org.eclipse.core.internal.databinding.beans.BeanPropertyHelper;
35
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableMap;
36
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableSet;
37
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableValue;
38
import org.eclipse.core.internal.databinding.beans.JavaBeanPropertyObservableMap;
39
import org.eclipse.core.runtime.Assert;
36
import org.eclipse.core.runtime.Assert;
40
37
41
/**
38
/**
Lines 83-91 Link Here
83
	 */
80
	 */
84
	public static IObservableValue observeValue(Realm realm, Object bean,
81
	public static IObservableValue observeValue(Realm realm, Object bean,
85
			String propertyName) {
82
			String propertyName) {
86
		PropertyDescriptor descriptor = getPropertyDescriptor(bean.getClass(),
83
		IValueProperty property = BeanProperties.valueProperty(bean.getClass(),
87
				propertyName);
84
				propertyName);
88
		return new JavaBeanObservableValue(realm, bean, descriptor);
85
		PropertyDescriptor propertyDescriptor = ((IBeanProperty) property)
86
				.getPropertyDescriptor();
87
		return new BeanObservableValueDecorator(PropertyObservables
88
				.observeValue(realm, bean, property), propertyDescriptor);
89
	}
89
	}
90
90
91
	/**
91
	/**
Lines 103-111 Link Here
103
	 */
103
	 */
104
	public static IObservableMap observeMap(IObservableSet domain,
104
	public static IObservableMap observeMap(IObservableSet domain,
105
			Class beanClass, String propertyName) {
105
			Class beanClass, String propertyName) {
106
		PropertyDescriptor descriptor = getPropertyDescriptor(beanClass,
106
		IValueProperty property = BeanProperties.valueProperty(beanClass,
107
				propertyName);
107
				propertyName);
108
		return new JavaBeanObservableMap(domain, descriptor);
108
		PropertyDescriptor propertyDescriptor = ((IBeanProperty) property)
109
				.getPropertyDescriptor();
110
		return new BeanObservableMapDecorator(PropertyObservables.observeDetailValues(
111
				domain, property), propertyDescriptor);
109
	}
112
	}
110
113
111
	/**
114
	/**
Lines 124-132 Link Here
124
	 */
127
	 */
125
	public static IObservableMap observeMap(Realm realm, Object bean,
128
	public static IObservableMap observeMap(Realm realm, Object bean,
126
			String propertyName) {
129
			String propertyName) {
127
		PropertyDescriptor descriptor = getPropertyDescriptor(bean.getClass(),
130
		IMapProperty property = BeanProperties.mapProperty(bean.getClass(),
128
				propertyName);
131
				propertyName);
129
		return new JavaBeanPropertyObservableMap(realm, bean, descriptor);
132
		PropertyDescriptor propertyDescriptor = ((IBeanProperty) property)
133
				.getPropertyDescriptor();
134
		return new BeanObservableMapDecorator(PropertyObservables.observeMap(
135
				bean, property), propertyDescriptor);
130
	}
136
	}
131
137
132
	/**
138
	/**
Lines 145-171 Link Here
145
		return observeMap(Realm.getDefault(), bean, propertyName);
151
		return observeMap(Realm.getDefault(), bean, propertyName);
146
	}
152
	}
147
153
148
	/*package*/ static PropertyDescriptor getPropertyDescriptor(Class beanClass,
149
			String propertyName) {
150
		BeanInfo beanInfo;
151
		try {
152
			beanInfo = Introspector.getBeanInfo(beanClass);
153
		} catch (IntrospectionException e) {
154
			// cannot introspect, give up
155
			return null;
156
		}
157
		PropertyDescriptor[] propertyDescriptors = beanInfo
158
				.getPropertyDescriptors();
159
		for (int i = 0; i < propertyDescriptors.length; i++) {
160
			PropertyDescriptor descriptor = propertyDescriptors[i];
161
			if (descriptor.getName().equals(propertyName)) {
162
				return descriptor;
163
			}
164
		}
165
		throw new BindingException(
166
				"Could not find property with name " + propertyName + " in class " + beanClass); //$NON-NLS-1$ //$NON-NLS-2$
167
	}
168
169
	/**
154
	/**
170
	 * Returns an array of observable maps in the default realm tracking the
155
	 * Returns an array of observable maps in the default realm tracking the
171
	 * current values of the named propertys for the beans in the given set.
156
	 * current values of the named propertys for the beans in the given set.
Lines 231-239 Link Here
231
	 * collection-typed named property of the given bean object. The returned
216
	 * collection-typed named property of the given bean object. The returned
232
	 * list is mutable. When an item is added or removed the setter is invoked
217
	 * list is mutable. When an item is added or removed the setter is invoked
233
	 * for the list on the parent bean to provide notification to other
218
	 * for the list on the parent bean to provide notification to other
234
	 * listeners via <code>PropertyChangeEvents</code>. This is done to
219
	 * listeners via <code>PropertyChangeEvents</code>. This is done to provide
235
	 * provide the same behavior as is expected from arrays as specified in the
220
	 * the same behavior as is expected from arrays as specified in the bean
236
	 * bean spec in section 7.2.
221
	 * spec in section 7.2.
237
	 * 
222
	 * 
238
	 * @param realm
223
	 * @param realm
239
	 *            the realm
224
	 *            the realm
Lines 242-249 Link Here
242
	 * @param propertyName
227
	 * @param propertyName
243
	 *            the name of the property
228
	 *            the name of the property
244
	 * @param elementType
229
	 * @param elementType
245
	 *            type of the elements in the list. If <code>null</code> and
230
	 *            type of the elements in the list. If <code>null</code> and the
246
	 *            the property is an array the type will be inferred. If
231
	 *            property is an array the type will be inferred. If
247
	 *            <code>null</code> and the property type cannot be inferred
232
	 *            <code>null</code> and the property type cannot be inferred
248
	 *            element type will be <code>null</code>.
233
	 *            element type will be <code>null</code>.
249
	 * @return an observable list tracking the collection-typed named property
234
	 * @return an observable list tracking the collection-typed named property
Lines 251-262 Link Here
251
	 */
236
	 */
252
	public static IObservableList observeList(Realm realm, Object bean,
237
	public static IObservableList observeList(Realm realm, Object bean,
253
			String propertyName, Class elementType) {
238
			String propertyName, Class elementType) {
254
		PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean
239
		IListProperty property = BeanProperties.listProperty(bean.getClass(),
255
				.getClass(), propertyName);
240
				propertyName, elementType);
256
		elementType = getCollectionElementType(elementType, propertyDescriptor);
241
		PropertyDescriptor propertyDescriptor = ((IBeanProperty) property)
257
242
				.getPropertyDescriptor();
258
		return new JavaBeanObservableList(realm, bean, propertyDescriptor,
243
		return new BeanObservableListDecorator(PropertyObservables.observeList(
259
				elementType);
244
				realm, bean, property), propertyDescriptor);
260
	}
245
	}
261
246
262
	/**
247
	/**
Lines 436-448 Link Here
436
	 */
421
	 */
437
	public static IObservableValue observeDetailValue(Realm realm,
422
	public static IObservableValue observeDetailValue(Realm realm,
438
			IObservableValue master, String propertyName, Class propertyType) {
423
			IObservableValue master, String propertyName, Class propertyType) {
439
440
		IObservableValue value = MasterDetailObservables.detailValue(master,
424
		IObservableValue value = MasterDetailObservables.detailValue(master,
441
				valueFactory(realm, propertyName), propertyType);
425
				valueFactory(realm, propertyName), propertyType);
442
		BeanObservableValueDecorator decorator = new BeanObservableValueDecorator(
426
		return new BeanObservableValueDecorator(value, BeanPropertyHelper
443
				value, getValueTypePropertyDescriptor(master, propertyName));
427
				.getValueTypePropertyDescriptor(master, propertyName));
444
445
		return decorator;
446
	}
428
	}
447
429
448
	/**
430
	/**
Lines 468-477 Link Here
468
	/**
450
	/**
469
	 * Helper method for
451
	 * Helper method for
470
	 * <code>MasterDetailObservables.detailValue(master, valueFactory(realm,
452
	 * <code>MasterDetailObservables.detailValue(master, valueFactory(realm,
471
	 * propertyName), propertyType)</code>.
453
	 * propertyName), propertyType)</code>. This method returns an
472
	 * This method returns an {@link IBeanObservable} with a
454
	 * {@link IBeanObservable} with a {@link PropertyDescriptor} based on the
473
	 * {@link PropertyDescriptor} based on the given master type and property
455
	 * given master type and property name.
474
	 * name.
475
	 * 
456
	 * 
476
	 * @param realm
457
	 * @param realm
477
	 *            the realm
458
	 *            the realm
Lines 491-504 Link Here
491
	 * @since 1.1
472
	 * @since 1.1
492
	 */
473
	 */
493
	public static IObservableValue observeDetailValue(Realm realm,
474
	public static IObservableValue observeDetailValue(Realm realm,
494
			IObservableValue master, Class masterType, String propertyName, Class propertyType) {
475
			IObservableValue master, Class masterType, String propertyName,
476
			Class propertyType) {
495
		Assert.isNotNull(masterType, "masterType cannot be null"); //$NON-NLS-1$
477
		Assert.isNotNull(masterType, "masterType cannot be null"); //$NON-NLS-1$
496
		IObservableValue value = MasterDetailObservables.detailValue(master,
478
		IObservableValue value = MasterDetailObservables.detailValue(master,
497
				valueFactory(realm, propertyName), propertyType);
479
				valueFactory(realm, propertyName), propertyType);
498
		BeanObservableValueDecorator decorator = new BeanObservableValueDecorator(
480
		return new BeanObservableValueDecorator(value, BeanPropertyHelper
499
				value, getPropertyDescriptor(masterType, propertyName));
481
				.getPropertyDescriptor(masterType, propertyName));
500
501
		return decorator;
502
	}
482
	}
503
483
504
	/**
484
	/**
Lines 549-559 Link Here
549
		IObservableList observableList = MasterDetailObservables.detailList(
529
		IObservableList observableList = MasterDetailObservables.detailList(
550
				master, listFactory(realm, propertyName, propertyType),
530
				master, listFactory(realm, propertyName, propertyType),
551
				propertyType);
531
				propertyType);
552
		BeanObservableListDecorator decorator = new BeanObservableListDecorator(
532
		return new BeanObservableListDecorator(observableList,
553
				observableList, getValueTypePropertyDescriptor(master,
533
				BeanPropertyHelper.getValueTypePropertyDescriptor(master,
554
						propertyName));
534
						propertyName));
555
556
		return decorator;
557
	}
535
	}
558
536
559
	/**
537
	/**
Lines 593-607 Link Here
593
	 */
571
	 */
594
	public static IObservableSet observeDetailSet(Realm realm,
572
	public static IObservableSet observeDetailSet(Realm realm,
595
			IObservableValue master, String propertyName, Class propertyType) {
573
			IObservableValue master, String propertyName, Class propertyType) {
596
597
		IObservableSet observableSet = MasterDetailObservables.detailSet(
574
		IObservableSet observableSet = MasterDetailObservables.detailSet(
598
				master, setFactory(realm, propertyName, propertyType),
575
				master, setFactory(realm, propertyName, propertyType),
599
				propertyType);
576
				propertyType);
600
		BeanObservableSetDecorator decorator = new BeanObservableSetDecorator(
577
		return new BeanObservableSetDecorator(observableSet, BeanPropertyHelper
601
				observableSet, getValueTypePropertyDescriptor(master,
578
				.getValueTypePropertyDescriptor(master, propertyName));
602
						propertyName));
603
604
		return decorator;
605
	}
579
	}
606
580
607
	/**
581
	/**
Lines 640-649 Link Here
640
			IObservableValue master, String propertyName) {
614
			IObservableValue master, String propertyName) {
641
		IObservableMap observableMap = MasterDetailObservables.detailMap(
615
		IObservableMap observableMap = MasterDetailObservables.detailMap(
642
				master, mapPropertyFactory(realm, propertyName));
616
				master, mapPropertyFactory(realm, propertyName));
643
		BeanObservableMapDecorator decorator = new BeanObservableMapDecorator(
617
		return new BeanObservableMapDecorator(observableMap, BeanPropertyHelper
644
				observableMap, getValueTypePropertyDescriptor(master,
618
				.getValueTypePropertyDescriptor(master, propertyName));
645
						propertyName));
646
		return decorator;
647
	}
619
	}
648
620
649
	/**
621
	/**
Lines 686-697 Link Here
686
	 */
658
	 */
687
	public static IObservableSet observeSet(Realm realm, Object bean,
659
	public static IObservableSet observeSet(Realm realm, Object bean,
688
			String propertyName, Class elementType) {
660
			String propertyName, Class elementType) {
689
		PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean
661
		ISetProperty property = BeanProperties.setProperty(bean.getClass(),
690
				.getClass(), propertyName);
662
				propertyName, elementType);
691
		elementType = getCollectionElementType(elementType, propertyDescriptor);
663
		PropertyDescriptor propertyDescriptor = ((IBeanProperty) property)
692
664
				.getPropertyDescriptor();
693
		return new JavaBeanObservableSet(realm, bean, propertyDescriptor,
665
		return new BeanObservableSetDecorator(PropertyObservables.observeSet(
694
				elementType);
666
				realm, bean, property), propertyDescriptor);
695
	}
667
	}
696
668
697
	/**
669
	/**
Lines 778-794 Link Here
778
	 * @param propertyName
750
	 * @param propertyName
779
	 *            the name of the property
751
	 *            the name of the property
780
	 * @return a factory for creating {@link IObservableMap} objects
752
	 * @return a factory for creating {@link IObservableMap} objects
781
	 *
753
	 * 
782
	 * @since 1.1
754
	 * @since 1.1
783
	 */
755
	 */
784
	public static IObservableFactory setToMapFactory(final Class beanClass, final String propertyName) {
756
	public static IObservableFactory setToMapFactory(final Class beanClass,
757
			final String propertyName) {
785
		return new IObservableFactory() {
758
		return new IObservableFactory() {
786
			public IObservable createObservable(Object target) {
759
			public IObservable createObservable(Object target) {
787
				return observeMap((IObservableSet) target, beanClass, propertyName);
760
				return observeMap((IObservableSet) target, beanClass,
761
						propertyName);
788
			}
762
			}
789
		};
763
		};
790
	}
764
	}
791
	
765
792
	/**
766
	/**
793
	 * Returns a factory for creating an observable map. The factory, when
767
	 * Returns a factory for creating an observable map. The factory, when
794
	 * provided with a bean object, will create an {@link IObservableMap} in the
768
	 * provided with a bean object, will create an {@link IObservableMap} in the
Lines 826-857 Link Here
826
	public static IObservableFactory mapPropertyFactory(String propertyName) {
800
	public static IObservableFactory mapPropertyFactory(String propertyName) {
827
		return mapPropertyFactory(Realm.getDefault(), propertyName);
801
		return mapPropertyFactory(Realm.getDefault(), propertyName);
828
	}
802
	}
829
830
	/**
831
	 * @param elementType
832
	 *            can be <code>null</code>
833
	 * @param propertyDescriptor
834
	 * @return type of the items in a collection/array property
835
	 */
836
	/*package*/ static Class getCollectionElementType(Class elementType,
837
			PropertyDescriptor propertyDescriptor) {
838
		if (elementType == null) {
839
			Class propertyType = propertyDescriptor.getPropertyType();
840
			elementType = propertyType.isArray() ? propertyType
841
					.getComponentType() : Object.class;
842
		}
843
844
		return elementType;
845
	}
846
847
	/**
848
	 * @param observable
849
	 * @param propertyName
850
	 * @return property descriptor or <code>null</code>
851
	 */
852
	/* package*/ static PropertyDescriptor getValueTypePropertyDescriptor(
853
			IObservableValue observable, String propertyName) {
854
		return (observable.getValueType() != null) ? getPropertyDescriptor(
855
				(Class) observable.getValueType(), propertyName) : null;
856
	}
857
}
803
}
(-)src/org/eclipse/core/databinding/beans/PojoObservables.java (-56 / +51 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 - bugs 221704, 234686, 246625
10
 *     Matthew Hall - bugs 221704, 234686, 246625, 194734
11
 *******************************************************************************/
11
 *******************************************************************************/
12
12
13
package org.eclipse.core.databinding.beans;
13
package org.eclipse.core.databinding.beans;
Lines 23-37 Link Here
23
import org.eclipse.core.databinding.observable.masterdetail.MasterDetailObservables;
23
import org.eclipse.core.databinding.observable.masterdetail.MasterDetailObservables;
24
import org.eclipse.core.databinding.observable.set.IObservableSet;
24
import org.eclipse.core.databinding.observable.set.IObservableSet;
25
import org.eclipse.core.databinding.observable.value.IObservableValue;
25
import org.eclipse.core.databinding.observable.value.IObservableValue;
26
import org.eclipse.core.databinding.property.IListProperty;
27
import org.eclipse.core.databinding.property.IMapProperty;
28
import org.eclipse.core.databinding.property.ISetProperty;
29
import org.eclipse.core.databinding.property.IValueProperty;
30
import org.eclipse.core.databinding.property.PropertyObservables;
26
import org.eclipse.core.internal.databinding.beans.BeanObservableListDecorator;
31
import org.eclipse.core.internal.databinding.beans.BeanObservableListDecorator;
27
import org.eclipse.core.internal.databinding.beans.BeanObservableMapDecorator;
32
import org.eclipse.core.internal.databinding.beans.BeanObservableMapDecorator;
28
import org.eclipse.core.internal.databinding.beans.BeanObservableSetDecorator;
33
import org.eclipse.core.internal.databinding.beans.BeanObservableSetDecorator;
29
import org.eclipse.core.internal.databinding.beans.BeanObservableValueDecorator;
34
import org.eclipse.core.internal.databinding.beans.BeanObservableValueDecorator;
30
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableList;
35
import org.eclipse.core.internal.databinding.beans.BeanPropertyHelper;
31
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableMap;
32
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableSet;
33
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableValue;
34
import org.eclipse.core.internal.databinding.beans.JavaBeanPropertyObservableMap;
35
36
36
/**
37
/**
37
 * A factory for creating observable objects for POJOs (plain old java objects)
38
 * A factory for creating observable objects for POJOs (plain old java objects)
Lines 73-82 Link Here
73
	 */
74
	 */
74
	public static IObservableValue observeValue(Realm realm, Object pojo,
75
	public static IObservableValue observeValue(Realm realm, Object pojo,
75
			String propertyName) {
76
			String propertyName) {
76
77
		IValueProperty property = PojoProperties.valueProperty(pojo.getClass(),
77
		PropertyDescriptor descriptor = BeansObservables.getPropertyDescriptor(
78
				propertyName);
78
				pojo.getClass(), propertyName);
79
		PropertyDescriptor propertyDescriptor = ((IBeanProperty) property)
79
		return new JavaBeanObservableValue(realm, pojo, descriptor, false);
80
				.getPropertyDescriptor();
81
		return new BeanObservableValueDecorator(PropertyObservables
82
				.observeValue(realm, pojo, property), propertyDescriptor);
80
	}
83
	}
81
84
82
	/**
85
	/**
Lines 94-102 Link Here
94
	 */
97
	 */
95
	public static IObservableMap observeMap(IObservableSet domain,
98
	public static IObservableMap observeMap(IObservableSet domain,
96
			Class pojoClass, String propertyName) {
99
			Class pojoClass, String propertyName) {
97
		PropertyDescriptor descriptor = BeansObservables.getPropertyDescriptor(
100
		IValueProperty property = PojoProperties.valueProperty(pojoClass,
98
				pojoClass, propertyName);
101
				propertyName);
99
		return new JavaBeanObservableMap(domain, descriptor, false);
102
		PropertyDescriptor propertyDescriptor = ((IBeanProperty) property)
103
				.getPropertyDescriptor();
104
		return new BeanObservableMapDecorator(PropertyObservables.observeDetailValues(
105
				domain, property), propertyDescriptor);
100
	}
106
	}
101
107
102
	/**
108
	/**
Lines 136-144 Link Here
136
	 */
142
	 */
137
	public static IObservableMap observeMap(Realm realm, Object pojo,
143
	public static IObservableMap observeMap(Realm realm, Object pojo,
138
			String propertyName) {
144
			String propertyName) {
139
		PropertyDescriptor descriptor = BeansObservables.getPropertyDescriptor(
145
		IMapProperty property = PojoProperties.mapProperty(pojo.getClass(),
140
				pojo.getClass(), propertyName);
146
				propertyName);
141
		return new JavaBeanPropertyObservableMap(realm, pojo, descriptor, false);
147
		PropertyDescriptor propertyDescriptor = ((IBeanProperty) property)
148
				.getPropertyDescriptor();
149
		return new BeanObservableMapDecorator(PropertyObservables.observeMap(
150
				pojo, property), propertyDescriptor);
142
	}
151
	}
143
152
144
	/**
153
	/**
Lines 200-208 Link Here
200
	 * collection-typed named property of the given bean object. The returned
209
	 * collection-typed named property of the given bean object. The returned
201
	 * list is mutable. When an item is added or removed the setter is invoked
210
	 * list is mutable. When an item is added or removed the setter is invoked
202
	 * for the list on the parent bean to provide notification to other
211
	 * for the list on the parent bean to provide notification to other
203
	 * listeners via <code>PropertyChangeEvents</code>. This is done to
212
	 * listeners via <code>PropertyChangeEvents</code>. This is done to provide
204
	 * provide the same behavior as is expected from arrays as specified in the
213
	 * the same behavior as is expected from arrays as specified in the bean
205
	 * bean spec in section 7.2.
214
	 * spec in section 7.2.
206
	 * 
215
	 * 
207
	 * @param realm
216
	 * @param realm
208
	 *            the realm
217
	 *            the realm
Lines 211-218 Link Here
211
	 * @param propertyName
220
	 * @param propertyName
212
	 *            the name of the property
221
	 *            the name of the property
213
	 * @param elementType
222
	 * @param elementType
214
	 *            type of the elements in the list. If <code>null</code> and
223
	 *            type of the elements in the list. If <code>null</code> and the
215
	 *            the property is an array the type will be inferred. If
224
	 *            property is an array the type will be inferred. If
216
	 *            <code>null</code> and the property type cannot be inferred
225
	 *            <code>null</code> and the property type cannot be inferred
217
	 *            element type will be <code>null</code>.
226
	 *            element type will be <code>null</code>.
218
	 * @return an observable list tracking the collection-typed named property
227
	 * @return an observable list tracking the collection-typed named property
Lines 220-232 Link Here
220
	 */
229
	 */
221
	public static IObservableList observeList(Realm realm, Object pojo,
230
	public static IObservableList observeList(Realm realm, Object pojo,
222
			String propertyName, Class elementType) {
231
			String propertyName, Class elementType) {
223
		PropertyDescriptor propertyDescriptor = BeansObservables
232
		IListProperty property = PojoProperties.listProperty(pojo.getClass(),
224
				.getPropertyDescriptor(pojo.getClass(), propertyName);
233
				propertyName, elementType);
225
		elementType = BeansObservables.getCollectionElementType(elementType,
234
		PropertyDescriptor propertyDescriptor = ((IBeanProperty) property)
226
				propertyDescriptor);
235
				.getPropertyDescriptor();
227
236
		return new BeanObservableListDecorator(PropertyObservables.observeList(
228
		return new JavaBeanObservableList(realm, pojo, propertyDescriptor,
237
				realm, pojo, property), propertyDescriptor);
229
				elementType, false);
230
	}
238
	}
231
239
232
	/**
240
	/**
Lines 310-322 Link Here
310
	 */
318
	 */
311
	public static IObservableSet observeSet(Realm realm, Object pojo,
319
	public static IObservableSet observeSet(Realm realm, Object pojo,
312
			String propertyName, Class elementType) {
320
			String propertyName, Class elementType) {
313
		PropertyDescriptor propertyDescriptor = BeansObservables
321
		ISetProperty property = PojoProperties.setProperty(pojo.getClass(),
314
				.getPropertyDescriptor(pojo.getClass(), propertyName);
322
				propertyName, elementType);
315
		elementType = BeansObservables.getCollectionElementType(elementType,
323
		PropertyDescriptor propertyDescriptor = ((IBeanProperty) property)
316
				propertyDescriptor);
324
				.getPropertyDescriptor();
317
325
		return new BeanObservableSetDecorator(PropertyObservables.observeSet(
318
		return new JavaBeanObservableSet(realm, pojo, propertyDescriptor,
326
				realm, pojo, property), propertyDescriptor);
319
				elementType, false);
320
	}
327
	}
321
328
322
	/**
329
	/**
Lines 537-550 Link Here
537
	 */
544
	 */
538
	public static IObservableValue observeDetailValue(Realm realm,
545
	public static IObservableValue observeDetailValue(Realm realm,
539
			IObservableValue master, String propertyName, Class propertyType) {
546
			IObservableValue master, String propertyName, Class propertyType) {
540
541
		IObservableValue value = MasterDetailObservables.detailValue(master,
547
		IObservableValue value = MasterDetailObservables.detailValue(master,
542
				valueFactory(realm, propertyName), propertyType);
548
				valueFactory(realm, propertyName), propertyType);
543
		BeanObservableValueDecorator decorator = new BeanObservableValueDecorator(
549
		return new BeanObservableValueDecorator(value, BeanPropertyHelper
544
				value, BeansObservables.getValueTypePropertyDescriptor(master,
550
				.getValueTypePropertyDescriptor(master, propertyName));
545
						propertyName));
546
547
		return decorator;
548
	}
551
	}
549
552
550
	/**
553
	/**
Lines 587-597 Link Here
587
		IObservableList observableList = MasterDetailObservables.detailList(
590
		IObservableList observableList = MasterDetailObservables.detailList(
588
				master, listFactory(realm, propertyName, propertyType),
591
				master, listFactory(realm, propertyName, propertyType),
589
				propertyType);
592
				propertyType);
590
		BeanObservableListDecorator decorator = new BeanObservableListDecorator(
593
		return new BeanObservableListDecorator(observableList,
591
				observableList, BeansObservables
594
				BeanPropertyHelper.getValueTypePropertyDescriptor(master,
592
						.getValueTypePropertyDescriptor(master, propertyName));
595
						propertyName));
593
594
		return decorator;
595
	}
596
	}
596
597
597
	/**
598
	/**
Lines 631-645 Link Here
631
	 */
632
	 */
632
	public static IObservableSet observeDetailSet(Realm realm,
633
	public static IObservableSet observeDetailSet(Realm realm,
633
			IObservableValue master, String propertyName, Class propertyType) {
634
			IObservableValue master, String propertyName, Class propertyType) {
634
635
		IObservableSet observableSet = MasterDetailObservables.detailSet(
635
		IObservableSet observableSet = MasterDetailObservables.detailSet(
636
				master, setFactory(realm, propertyName, propertyType),
636
				master, setFactory(realm, propertyName, propertyType),
637
				propertyType);
637
				propertyType);
638
		BeanObservableSetDecorator decorator = new BeanObservableSetDecorator(
638
		return new BeanObservableSetDecorator(observableSet, BeanPropertyHelper
639
				observableSet, BeansObservables.getValueTypePropertyDescriptor(
639
				.getValueTypePropertyDescriptor(master, propertyName));
640
						master, propertyName));
641
642
		return decorator;
643
	}
640
	}
644
641
645
	/**
642
	/**
Lines 676-685 Link Here
676
			IObservableValue master, String propertyName) {
673
			IObservableValue master, String propertyName) {
677
		IObservableMap observableMap = MasterDetailObservables.detailMap(
674
		IObservableMap observableMap = MasterDetailObservables.detailMap(
678
				master, mapPropertyFactory(realm, propertyName));
675
				master, mapPropertyFactory(realm, propertyName));
679
		BeanObservableMapDecorator decorator = new BeanObservableMapDecorator(
676
		return new BeanObservableMapDecorator(observableMap, BeanPropertyHelper
680
				observableMap, BeansObservables.getValueTypePropertyDescriptor(
677
				.getValueTypePropertyDescriptor(master, propertyName));
681
						master, propertyName));
682
		return decorator;
683
	}
678
	}
684
679
685
	/**
680
	/**
(-)src/org/eclipse/core/databinding/beans/IBeanProperty.java (+30 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.PropertyDescriptor;
15
16
import org.eclipse.core.databinding.property.IProperty;
17
18
/**
19
 * Provides access to details of bean properties.
20
 * 
21
 * @since 1.2
22
 */
23
public interface IBeanProperty extends IProperty {
24
	/**
25
	 * Returns the property descriptor of the bean property being observed
26
	 * 
27
	 * @return the property descriptor of the bean property being observed
28
	 */
29
	public PropertyDescriptor getPropertyDescriptor();
30
}
(-)src/org/eclipse/core/internal/databinding/beans/BeanMapProperty.java (+138 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.BasicMapProperty;
25
26
/**
27
 * @since 3.3
28
 * 
29
 */
30
public class BeanMapProperty extends BasicMapProperty implements IBeanProperty {
31
	private PropertyDescriptor propertyDescriptor;
32
	private boolean attachListener;
33
34
	private ListenerSupport listenerSupport;
35
36
	private Map sourceToCachedMap = Collections.synchronizedMap(new HashMap());
37
38
	/**
39
	 * @param propertyDescriptor
40
	 * @param attachListener
41
	 */
42
	public BeanMapProperty(PropertyDescriptor propertyDescriptor,
43
			boolean attachListener) {
44
		this.propertyDescriptor = propertyDescriptor;
45
		this.attachListener = attachListener;
46
	}
47
48
	private void initListenerSupport() {
49
		if (listenerSupport == null) {
50
			synchronized (this) {
51
				if (listenerSupport == null) {
52
					PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
53
						public void propertyChange(PropertyChangeEvent evt) {
54
							Object source = evt.getSource();
55
							Object oldValue = evt.getOldValue();
56
							Object newValue = evt.getNewValue();
57
							if (oldValue == null && newValue == null) {
58
								// Obscure condition indicating new and old
59
								// values are unknown
60
								oldValue = sourceToCachedMap.get(source);
61
								newValue = BeanPropertyHelper.getProperty(
62
										source, propertyDescriptor);
63
							}
64
							sourceToCachedMap.put(source, newValue);
65
							if (!isUpdating(source)) {
66
								fireMapChange(source, Diffs.computeMapDiff(
67
										asMap(oldValue), asMap(newValue)));
68
							}
69
						}
70
					};
71
					listenerSupport = new ListenerSupport(
72
							propertyChangeListener, propertyDescriptor
73
									.getName());
74
				}
75
			}
76
		}
77
	}
78
79
	protected void addListenerTo(Object source) {
80
		if (source != null && attachListener) {
81
			initListenerSupport();
82
			listenerSupport.hookListener(source);
83
			sourceToCachedMap.put(source, BeanPropertyHelper.getProperty(
84
					source, propertyDescriptor));
85
		}
86
	}
87
88
	protected void removeListenerFrom(Object source) {
89
		if (source != null && attachListener && listenerSupport != null) {
90
			listenerSupport.unhookListener(source);
91
			sourceToCachedMap.remove(source);
92
		}
93
	}
94
95
	public Map getMap(Object source) {
96
		if (source == null)
97
			return Collections.EMPTY_MAP;
98
		Object propertyValue = BeanPropertyHelper.getProperty(source,
99
				propertyDescriptor);
100
		return asMap(propertyValue);
101
	}
102
103
	private Map asMap(Object propertyValue) {
104
		if (propertyValue == null)
105
			return new HashMap();
106
		return (Map) propertyValue;
107
	}
108
109
	protected void setMap(Object source, Map map, MapDiff diff) {
110
		if (source == null)
111
			return;
112
113
		super.setMap(source, map, diff);
114
		sourceToCachedMap.put(source, map);
115
	}
116
117
	protected void doSetMap(Object source, Map map) {
118
		BeanPropertyHelper.setProperty(source, propertyDescriptor, map);
119
	}
120
121
	public synchronized void dispose() {
122
		propertyDescriptor = null;
123
		attachListener = false;
124
		if (listenerSupport != null) {
125
			listenerSupport.dispose();
126
			listenerSupport = null;
127
		}
128
		if (sourceToCachedMap != null) {
129
			sourceToCachedMap.clear();
130
			sourceToCachedMap = null;
131
		}
132
		super.dispose();
133
	}
134
135
	public PropertyDescriptor getPropertyDescriptor() {
136
		return propertyDescriptor;
137
	}
138
}
(-)src/org/eclipse/core/internal/databinding/beans/BeanValueProperty.java (+137 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.property.BasicValueProperty;
24
25
/**
26
 * @since 3.3
27
 * 
28
 */
29
public class BeanValueProperty extends BasicValueProperty implements
30
		IBeanProperty {
31
	private PropertyDescriptor propertyDescriptor;
32
	private Class valueType;
33
	private boolean attachListener;
34
35
	private ListenerSupport listenerSupport;
36
37
	private Map sourceToCachedValue = Collections
38
			.synchronizedMap(new HashMap());
39
40
	/**
41
	 * @param propertyDescriptor
42
	 * @param valueType
43
	 * @param attachListener
44
	 */
45
	public BeanValueProperty(PropertyDescriptor propertyDescriptor,
46
			Class valueType, boolean attachListener) {
47
		this.propertyDescriptor = propertyDescriptor;
48
		this.valueType = valueType == null ? propertyDescriptor
49
				.getPropertyType() : valueType;
50
		this.attachListener = attachListener;
51
	}
52
53
	private void initListenerSupport() {
54
		if (listenerSupport == null) {
55
			synchronized (this) {
56
				if (listenerSupport == null) {
57
					PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
58
						public void propertyChange(PropertyChangeEvent evt) {
59
							Object source = evt.getSource();
60
							Object oldValue = evt.getOldValue();
61
							Object newValue = evt.getNewValue();
62
							if (oldValue == null && newValue == null) {
63
								// Obscure condition indicating new and old
64
								// values are unknown
65
								oldValue = sourceToCachedValue.get(source);
66
								newValue = getValue(source);
67
							}
68
							sourceToCachedValue.put(source, newValue);
69
							if (!isUpdating(source)) {
70
								fireValueChange(source, Diffs.createValueDiff(
71
										oldValue, newValue));
72
							}
73
						}
74
					};
75
					listenerSupport = new ListenerSupport(
76
							propertyChangeListener, propertyDescriptor
77
									.getName());
78
				}
79
			}
80
		}
81
	}
82
83
	protected void addListenerTo(Object source) {
84
		if (source != null && attachListener) {
85
			initListenerSupport();
86
			listenerSupport.hookListener(source);
87
			sourceToCachedValue.put(source, getValue(source));
88
		}
89
	}
90
91
	protected void removeListenerFrom(Object source) {
92
		if (source != null && attachListener && listenerSupport != null) {
93
			listenerSupport.unhookListener(source);
94
			sourceToCachedValue.remove(source);
95
		}
96
	}
97
98
	public Object getValue(Object source) {
99
		if (source == null)
100
			return null;
101
		return BeanPropertyHelper.getProperty(source, propertyDescriptor);
102
	}
103
104
	public void setValue(Object source, Object value) {
105
		if (source == null)
106
			return;
107
		super.setValue(source, value);
108
		sourceToCachedValue.put(source, getValue(source));
109
	}
110
111
	protected void doSetValue(Object source, Object value) {
112
		BeanPropertyHelper.setProperty(source, propertyDescriptor, value);
113
	}
114
115
	public Object getValueType() {
116
		return valueType;
117
	}
118
119
	public PropertyDescriptor getPropertyDescriptor() {
120
		return propertyDescriptor;
121
	}
122
123
	public synchronized void dispose() {
124
		propertyDescriptor = null;
125
		valueType = null;
126
		attachListener = false;
127
		if (listenerSupport != null) {
128
			listenerSupport.dispose();
129
			listenerSupport = null;
130
		}
131
		if (sourceToCachedValue != null) {
132
			sourceToCachedValue.clear();
133
			sourceToCachedValue = null;
134
		}
135
		super.dispose();
136
	}
137
}
(-)src/org/eclipse/core/databinding/beans/PojoProperties.java (+154 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.IListProperty;
17
import org.eclipse.core.databinding.property.IMapProperty;
18
import org.eclipse.core.databinding.property.ISetProperty;
19
import org.eclipse.core.databinding.property.IValueProperty;
20
import org.eclipse.core.internal.databinding.beans.BeanListProperty;
21
import org.eclipse.core.internal.databinding.beans.BeanMapProperty;
22
import org.eclipse.core.internal.databinding.beans.BeanPropertyHelper;
23
import org.eclipse.core.internal.databinding.beans.BeanSetProperty;
24
import org.eclipse.core.internal.databinding.beans.BeanValueProperty;
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 BeanValueProperty(BeanPropertyHelper.getPropertyDescriptor(
67
				beanClass, propertyName), valueType, false);
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 BeanSetProperty(BeanPropertyHelper.getPropertyDescriptor(
101
				beanClass, propertyName), elementType, false);
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 BeanListProperty(BeanPropertyHelper.getPropertyDescriptor(
136
				beanClass, propertyName), elementType, false);
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
	 * @return a map property for the given property name of the given bean
148
	 *         class.
149
	 */
150
	public static IMapProperty mapProperty(Class beanClass, String propertyName) {
151
		return new BeanMapProperty(BeanPropertyHelper.getPropertyDescriptor(
152
				beanClass, propertyName), false);
153
	}
154
}
(-)src/org/eclipse/core/internal/databinding/beans/BeanListProperty.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.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.HashMap;
22
import java.util.List;
23
import java.util.Map;
24
25
import org.eclipse.core.databinding.beans.IBeanProperty;
26
import org.eclipse.core.databinding.observable.Diffs;
27
import org.eclipse.core.databinding.observable.list.ListDiff;
28
import org.eclipse.core.databinding.property.BasicListProperty;
29
30
/**
31
 * @since 3.3
32
 * 
33
 */
34
public class BeanListProperty extends BasicListProperty implements
35
		IBeanProperty {
36
	private PropertyDescriptor propertyDescriptor;
37
	private Class elementType;
38
	private boolean attachListener;
39
40
	private ListenerSupport listenerSupport;
41
42
	private Map sourceToCachedList = Collections.synchronizedMap(new HashMap());
43
44
	/**
45
	 * @param propertyDescriptor
46
	 * @param elementType
47
	 * @param attachListener
48
	 */
49
	public BeanListProperty(PropertyDescriptor propertyDescriptor,
50
			Class elementType, boolean attachListener) {
51
		this.propertyDescriptor = propertyDescriptor;
52
		this.elementType = elementType == null ? BeanPropertyHelper
53
				.getCollectionPropertyElementType(propertyDescriptor)
54
				: elementType;
55
		this.attachListener = attachListener;
56
	}
57
58
	private void initListenerSupport() {
59
		if (listenerSupport == null) {
60
			synchronized (this) {
61
				if (listenerSupport == null) {
62
					PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
63
						public void propertyChange(PropertyChangeEvent evt) {
64
							Object source = evt.getSource();
65
							Object oldValue = evt.getOldValue();
66
							Object newValue = evt.getNewValue();
67
							if (oldValue == null && newValue == null) {
68
								// Obscure condition indicating new and old
69
								// values are unknown
70
								oldValue = sourceToCachedList.get(source);
71
								newValue = BeanPropertyHelper.getProperty(
72
										source, propertyDescriptor);
73
							}
74
							sourceToCachedList.put(source, newValue);
75
							if (!isUpdating(source)) {
76
								fireListChange(source, Diffs.computeListDiff(
77
										asList(oldValue), asList(newValue)));
78
							}
79
						}
80
					};
81
					listenerSupport = new ListenerSupport(
82
							propertyChangeListener, propertyDescriptor
83
									.getName());
84
				}
85
			}
86
		}
87
	}
88
89
	protected void addListenerTo(Object source) {
90
		if (source != null && attachListener) {
91
			initListenerSupport();
92
			listenerSupport.hookListener(source);
93
			sourceToCachedList.put(source, BeanPropertyHelper.getProperty(
94
					source, propertyDescriptor));
95
		}
96
	}
97
98
	protected void removeListenerFrom(Object source) {
99
		if (source != null && attachListener && listenerSupport != null) {
100
			listenerSupport.unhookListener(source);
101
			sourceToCachedList.remove(source);
102
		}
103
	}
104
105
	public List getList(Object source) {
106
		if (source == null)
107
			return Collections.EMPTY_LIST;
108
		Object propertyValue = BeanPropertyHelper.getProperty(source,
109
				propertyDescriptor);
110
		return asList(propertyValue);
111
	}
112
113
	private List asList(Object propertyValue) {
114
		if (propertyValue == null)
115
			return new ArrayList();
116
		if (propertyDescriptor.getPropertyType().isArray())
117
			return new ArrayList(Arrays.asList((Object[]) propertyValue));
118
		return (List) propertyValue;
119
	}
120
121
	protected void setList(Object source, List list, ListDiff diff) {
122
		if (source == null)
123
			return;
124
125
		super.setList(source, list, diff);
126
		sourceToCachedList.put(source, convertListToBeanPropertyType(list));
127
	}
128
129
	protected void doSetList(Object source, List list) {
130
		BeanPropertyHelper.setProperty(source, propertyDescriptor,
131
				convertListToBeanPropertyType(list));
132
	}
133
134
	private Object convertListToBeanPropertyType(List list) {
135
		Object propertyValue = list;
136
		if (propertyDescriptor.getPropertyType().isArray()) {
137
			Class componentType = propertyDescriptor.getPropertyType()
138
					.getComponentType();
139
			Object[] array = (Object[]) Array.newInstance(componentType, list
140
					.size());
141
			list.toArray(array);
142
			propertyValue = array;
143
		}
144
		return propertyValue;
145
	}
146
147
	public Object getElementType() {
148
		return elementType;
149
	}
150
151
	public PropertyDescriptor getPropertyDescriptor() {
152
		return propertyDescriptor;
153
	}
154
155
	public synchronized void dispose() {
156
		propertyDescriptor = null;
157
		elementType = null;
158
		attachListener = false;
159
		if (listenerSupport != null) {
160
			listenerSupport.dispose();
161
			listenerSupport = null;
162
		}
163
		if (sourceToCachedList != null) {
164
			sourceToCachedList.clear();
165
			sourceToCachedList = null;
166
		}
167
		super.dispose();
168
	}
169
}
(-)src/org/eclipse/core/internal/databinding/beans/BeanSetProperty.java (+166 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.HashMap;
21
import java.util.HashSet;
22
import java.util.Map;
23
import java.util.Set;
24
25
import org.eclipse.core.databinding.beans.IBeanProperty;
26
import org.eclipse.core.databinding.observable.Diffs;
27
import org.eclipse.core.databinding.observable.set.SetDiff;
28
import org.eclipse.core.databinding.property.BasicSetProperty;
29
30
/**
31
 * @since 3.3
32
 * 
33
 */
34
public class BeanSetProperty extends BasicSetProperty implements IBeanProperty {
35
	private PropertyDescriptor propertyDescriptor;
36
	private Class elementType;
37
	private boolean attachListener;
38
39
	private ListenerSupport listenerSupport;
40
41
	private Map sourceToCachedSet = Collections.synchronizedMap(new HashMap());
42
43
	/**
44
	 * @param propertyDescriptor
45
	 * @param elementType
46
	 * @param attachListener
47
	 */
48
	public BeanSetProperty(PropertyDescriptor propertyDescriptor,
49
			Class elementType, boolean attachListener) {
50
		this.propertyDescriptor = propertyDescriptor;
51
		this.elementType = elementType == null ? BeanPropertyHelper
52
				.getCollectionPropertyElementType(propertyDescriptor)
53
				: elementType;
54
		this.attachListener = attachListener;
55
	}
56
57
	private void initListenerSupport() {
58
		if (listenerSupport == null) {
59
			synchronized (this) {
60
				if (listenerSupport == null) {
61
					PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
62
						public void propertyChange(PropertyChangeEvent evt) {
63
							Object source = evt.getSource();
64
							Object oldValue = evt.getOldValue();
65
							Object newValue = evt.getNewValue();
66
							if (oldValue == null && newValue == null) {
67
								// Obscure condition indicating new and old
68
								// values are unknown
69
								oldValue = sourceToCachedSet.get(source);
70
								newValue = BeanPropertyHelper.getProperty(
71
										source, propertyDescriptor);
72
							}
73
							sourceToCachedSet.put(source, newValue);
74
							if (!isUpdating(source)) {
75
								fireSetChange(source, Diffs.computeSetDiff(
76
										asSet(oldValue), asSet(newValue)));
77
							}
78
						}
79
					};
80
					listenerSupport = new ListenerSupport(
81
							propertyChangeListener, propertyDescriptor
82
									.getName());
83
				}
84
			}
85
		}
86
	}
87
88
	protected void addListenerTo(Object source) {
89
		if (source != null && attachListener) {
90
			initListenerSupport();
91
			listenerSupport.hookListener(source);
92
			sourceToCachedSet.put(source, BeanPropertyHelper.getProperty(
93
					source, propertyDescriptor));
94
		}
95
	}
96
97
	protected void removeListenerFrom(Object source) {
98
		if (source != null && attachListener && listenerSupport != null) {
99
			listenerSupport.unhookListener(source);
100
			sourceToCachedSet.remove(source);
101
		}
102
	}
103
104
	public Set getSet(Object source) {
105
		if (source == null)
106
			return Collections.EMPTY_SET;
107
		Object propertyValue = BeanPropertyHelper.getProperty(source,
108
				propertyDescriptor);
109
		return asSet(propertyValue);
110
	}
111
112
	private Set asSet(Object propertyValue) {
113
		if (propertyValue == null)
114
			return Collections.EMPTY_SET;
115
		if (propertyDescriptor.getPropertyType().isArray())
116
			return new HashSet(Arrays.asList((Object[]) propertyValue));
117
		return (Set) propertyValue;
118
	}
119
120
	protected void setSet(Object source, Set set, SetDiff diff) {
121
		if (source == null)
122
			return;
123
		super.setSet(source, set, diff);
124
		sourceToCachedSet.put(source, set);
125
	}
126
127
	protected void doSetSet(Object source, Set set) {
128
		BeanPropertyHelper.setProperty(source, propertyDescriptor,
129
				convertSetToBeanPropertyType(set));
130
	}
131
132
	private Object convertSetToBeanPropertyType(Set set) {
133
		Object propertyValue = set;
134
		if (propertyDescriptor.getPropertyType().isArray()) {
135
			Class componentType = propertyDescriptor.getPropertyType()
136
					.getComponentType();
137
			Object[] array = (Object[]) Array.newInstance(componentType, set
138
					.size());
139
			propertyValue = set.toArray(array);
140
		}
141
		return propertyValue;
142
	}
143
144
	public Object getElementType() {
145
		return elementType;
146
	}
147
148
	public PropertyDescriptor getPropertyDescriptor() {
149
		return propertyDescriptor;
150
	}
151
152
	public synchronized void dispose() {
153
		propertyDescriptor = null;
154
		elementType = null;
155
		attachListener = false;
156
		if (listenerSupport != null) {
157
			listenerSupport.dispose();
158
			listenerSupport = null;
159
		}
160
		if (sourceToCachedSet != null) {
161
			sourceToCachedSet.clear();
162
			sourceToCachedSet = null;
163
		}
164
		super.dispose();
165
	}
166
}
(-)src/org/eclipse/core/databinding/beans/BeanProperties.java (+151 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 org.eclipse.core.databinding.property.IListProperty;
15
import org.eclipse.core.databinding.property.IMapProperty;
16
import org.eclipse.core.databinding.property.ISetProperty;
17
import org.eclipse.core.databinding.property.IValueProperty;
18
import org.eclipse.core.internal.databinding.beans.BeanListProperty;
19
import org.eclipse.core.internal.databinding.beans.BeanMapProperty;
20
import org.eclipse.core.internal.databinding.beans.BeanPropertyHelper;
21
import org.eclipse.core.internal.databinding.beans.BeanSetProperty;
22
import org.eclipse.core.internal.databinding.beans.BeanValueProperty;
23
24
/**
25
 * A factory for creating properties for Java objects that conform to the <a
26
 * href="http://java.sun.com/products/javabeans/docs/spec.html">JavaBean
27
 * specification</a> for bound properties.
28
 * 
29
 * @since 1.2
30
 */
31
public class BeanProperties {
32
	/**
33
	 * Returns a value property for the given property name of the given bean
34
	 * class.
35
	 * 
36
	 * @param beanClass
37
	 *            the bean class
38
	 * @param propertyName
39
	 *            the property name
40
	 * @return a value property for the given property name of the given bean
41
	 *         class.
42
	 */
43
	public static IValueProperty valueProperty(Class beanClass,
44
			String propertyName) {
45
		return valueProperty(beanClass, propertyName, null);
46
	}
47
48
	/**
49
	 * Returns a value property for the given property name of the given bean
50
	 * class.
51
	 * 
52
	 * @param beanClass
53
	 *            the bean class
54
	 * @param propertyName
55
	 *            the property name
56
	 * @param valueType
57
	 *            the value type of the returned value property
58
	 * @return a value property for the given property name of the given bean
59
	 *         class.
60
	 */
61
	public static IValueProperty valueProperty(Class beanClass,
62
			String propertyName, Class valueType) {
63
		return new BeanValueProperty(BeanPropertyHelper.getPropertyDescriptor(
64
				beanClass, propertyName), valueType, true);
65
	}
66
67
	/**
68
	 * Returns a set property for the given property name of the given bean
69
	 * class.
70
	 * 
71
	 * @param beanClass
72
	 *            the bean class
73
	 * @param propertyName
74
	 *            the property name
75
	 * @return a set property for the given property name of the given bean
76
	 *         class.
77
	 */
78
	public static ISetProperty setProperty(Class beanClass, String propertyName) {
79
		return setProperty(beanClass, propertyName, null);
80
	}
81
82
	/**
83
	 * Returns a set property for the given property name of the given bean
84
	 * class.
85
	 * 
86
	 * @param beanClass
87
	 *            the bean class
88
	 * @param propertyName
89
	 *            the property name
90
	 * @param elementType
91
	 *            the element type of the returned set property
92
	 * @return a set property for the given property name of the given bean
93
	 *         class.
94
	 */
95
	public static ISetProperty setProperty(Class beanClass,
96
			String propertyName, Class elementType) {
97
		return new BeanSetProperty(BeanPropertyHelper.getPropertyDescriptor(
98
				beanClass, propertyName), elementType, true);
99
	}
100
101
	/**
102
	 * Returns a list property for the given property name of the given bean
103
	 * class.
104
	 * 
105
	 * @param beanClass
106
	 *            the bean class
107
	 * @param propertyName
108
	 *            the property name
109
	 * @return a list property for the given property name of the given bean
110
	 *         class.
111
	 */
112
	public static IListProperty listProperty(Class beanClass,
113
			String propertyName) {
114
		return listProperty(beanClass, propertyName, null);
115
	}
116
117
	/**
118
	 * Returns a list property for the given property name of the given bean
119
	 * class.
120
	 * 
121
	 * @param beanClass
122
	 *            the bean class
123
	 * @param propertyName
124
	 *            the property name
125
	 * @param elementType
126
	 *            the element type of the returned list property
127
	 * @return a list property for the given property name of the given bean
128
	 *         class.
129
	 */
130
	public static IListProperty listProperty(Class beanClass,
131
			String propertyName, Class elementType) {
132
		return new BeanListProperty(BeanPropertyHelper.getPropertyDescriptor(
133
				beanClass, propertyName), elementType, true);
134
	}
135
136
	/**
137
	 * Returns a map property for the given property name of the given bean
138
	 * class.
139
	 * 
140
	 * @param beanClass
141
	 *            the bean class
142
	 * @param propertyName
143
	 *            the property name
144
	 * @return a map property for the given property name of the given bean
145
	 *         class.
146
	 */
147
	public static IMapProperty mapProperty(Class beanClass, String propertyName) {
148
		return new BeanMapProperty(BeanPropertyHelper.getPropertyDescriptor(
149
				beanClass, propertyName), true);
150
	}
151
}
(-)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 setProperty(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 getProperty(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
}

Return to bug 194734