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 (-1 / +13 lines)
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 163-166 Link Here
163
	 * @return the old value for the given key
163
	 * @return the old value for the given key
164
	 */
164
	 */
165
	protected abstract Object doPut(Object key, Object value);
165
	protected abstract Object doPut(Object key, Object value);
166
167
	public void dispose() {
168
		if (keySet != null) {
169
			Object[] keys = keySet.toArray();
170
			for (int i = 0; i < keys.length; i++) {
171
				unhookListener(keys[i]);
172
			}
173
			keySet = null;
174
		}
175
		entrySet = null;
176
		super.dispose();
177
	}
166
}
178
}
(-)src/org/eclipse/core/databinding/observable/list/AbstractObservableList.java (-6 / +18 lines)
Lines 76-86 Link Here
76
	}
76
	}
77
77
78
	public synchronized void addListChangeListener(IListChangeListener listener) {
78
	public synchronized void addListChangeListener(IListChangeListener listener) {
79
		changeSupport.addListener(ListChangeEvent.TYPE, listener);
79
		if (changeSupport != null) {
80
			changeSupport.addListener(ListChangeEvent.TYPE, listener);
81
		}
80
	}
82
	}
81
83
82
	public synchronized void removeListChangeListener(IListChangeListener listener) {
84
	public synchronized void removeListChangeListener(IListChangeListener listener) {
83
		changeSupport.removeListener(ListChangeEvent.TYPE, listener);
85
		if (changeSupport != null) {
86
			changeSupport.removeListener(ListChangeEvent.TYPE, listener);
87
		}
84
	}
88
	}
85
89
86
	protected void fireListChange(ListDiff diff) {
90
	protected void fireListChange(ListDiff diff) {
Lines 90-108 Link Here
90
	}
94
	}
91
95
92
	public synchronized void addChangeListener(IChangeListener listener) {
96
	public synchronized void addChangeListener(IChangeListener listener) {
93
		changeSupport.addChangeListener(listener);
97
		if (changeSupport != null) {
98
			changeSupport.addChangeListener(listener);
99
		}
94
	}
100
	}
95
101
96
	public synchronized void removeChangeListener(IChangeListener listener) {
102
	public synchronized void removeChangeListener(IChangeListener listener) {
97
		changeSupport.removeChangeListener(listener);
103
		if (changeSupport != null) {
104
			changeSupport.removeChangeListener(listener);
105
		}
98
	}
106
	}
99
107
100
	public synchronized void addStaleListener(IStaleListener listener) {
108
	public synchronized void addStaleListener(IStaleListener listener) {
101
		changeSupport.addStaleListener(listener);
109
		if (changeSupport != null) {
110
			changeSupport.addStaleListener(listener);
111
		}
102
	}
112
	}
103
113
104
	public synchronized void removeStaleListener(IStaleListener listener) {
114
	public synchronized void removeStaleListener(IStaleListener listener) {
105
		changeSupport.removeStaleListener(listener);
115
		if (changeSupport != null) {
116
			changeSupport.removeStaleListener(listener);
117
		}
106
	}
118
	}
107
119
108
	/**
120
	/**
(-)META-INF/MANIFEST.MF (+1 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,
17
 org.eclipse.core.databinding.util,
18
 org.eclipse.core.databinding.util,
18
 org.eclipse.core.databinding.validation;x-internal:=false,
19
 org.eclipse.core.databinding.validation;x-internal:=false,
19
 org.eclipse.core.internal.databinding;x-friends:="org.eclipse.core.databinding.beans",
20
 org.eclipse.core.internal.databinding;x-friends:="org.eclipse.core.databinding.beans",
(-)src/org/eclipse/core/databinding/property/IListProperty.java (+105 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
 * @since 1.2
19
 * 
20
 */
21
public interface IListProperty extends ICollectionProperty {
22
	/**
23
	 * @param source
24
	 * @return a List with the current contents of the source's list property
25
	 */
26
	List getList(Object source);
27
28
	/**
29
	 * @param source
30
	 * @param index
31
	 * @param c
32
	 * @return whether the source's list property was changed
33
	 */
34
	boolean addAll(Object source, int index, Collection c);
35
36
	/**
37
	 * @param source
38
	 * @param index
39
	 * @return the element at the given location in the source's list property
40
	 */
41
	Object get(Object source, int index);
42
43
	/**
44
	 * @param source
45
	 * @param index
46
	 * @param element
47
	 * @return the element previous at the given location in the source's list
48
	 *         property
49
	 */
50
	Object set(Object source, int index, Object element);
51
52
	/**
53
	 * @param source
54
	 * @param oldIndex
55
	 * @param newIndex
56
	 * @return the element that was moved
57
	 */
58
	Object move(Object source, int oldIndex, int newIndex);
59
60
	/**
61
	 * @param source
62
	 * @param index
63
	 * @param element
64
	 */
65
	void add(Object source, int index, Object element);
66
67
	/**
68
	 * @param source
69
	 * @param index
70
	 * @return the element that was removed from the source's list property
71
	 */
72
	Object remove(Object source, int index);
73
74
	/**
75
	 * @param source
76
	 * @param o
77
	 * @return the index of the first location of the given element in the
78
	 *         source's list property, or -1 if the list does not contain the
79
	 *         element
80
	 */
81
	int indexOf(Object source, Object o);
82
83
	/**
84
	 * @param source
85
	 * @param o
86
	 * @return the index of the last location of the given element in the
87
	 *         source's list property, or -1 if the list does not contain the
88
	 *         element
89
	 */
90
	int lastIndexOf(Object source, Object o);
91
92
	/**
93
	 * @param source
94
	 * @param listener
95
	 */
96
	public void addListChangeListener(Object source,
97
			IListPropertyChangeListener listener);
98
99
	/**
100
	 * @param source
101
	 * @param listener
102
	 */
103
	public void removeListChangeListener(Object source,
104
			IListPropertyChangeListener listener);
105
}
(-)src/org/eclipse/core/databinding/property/ISetProperty.java (+39 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
 * @since 1.2
18
 */
19
public interface ISetProperty extends ICollectionProperty {
20
	/**
21
	 * @param source
22
	 * @return a Set with the current contents of the source's set property
23
	 */
24
	public Set getSet(Object source);
25
26
	/**
27
	 * @param source
28
	 * @param listener
29
	 */
30
	public void addSetChangeListener(Object source,
31
			ISetPropertyChangeListener listener);
32
33
	/**
34
	 * @param source
35
	 * @param listener
36
	 */
37
	public void removeSetChangeListener(Object source,
38
			ISetPropertyChangeListener listener);
39
}
(-)src/org/eclipse/core/databinding/property/ObservableProperties.java (+55 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation (bug 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property;
13
14
import org.eclipse.core.databinding.observable.list.IObservableList;
15
import org.eclipse.core.databinding.observable.map.IObservableMap;
16
import org.eclipse.core.databinding.observable.set.IObservableSet;
17
import org.eclipse.core.databinding.observable.value.IObservableValue;
18
19
/**
20
 * @since 1.2
21
 * 
22
 */
23
public class ObservableProperties {
24
	/**
25
	 * @param observable
26
	 * @return blah
27
	 */
28
	public static IValueProperty detailValue(IObservableValue observable) {
29
		return null;
30
	}
31
32
	/**
33
	 * @param observable
34
	 * @return blah
35
	 */
36
	public static ISetProperty detailSet(IObservableSet observable) {
37
		return null;
38
	}
39
40
	/**
41
	 * @param observable
42
	 * @return blah
43
	 */
44
	public static IListProperty detailList(IObservableList observable) {
45
		return null;
46
	}
47
48
	/**
49
	 * @param observable
50
	 * @return blah
51
	 */
52
	public static IMapProperty detailMap(IObservableMap observable) {
53
		return null;
54
	}
55
}
(-)src/org/eclipse/core/databinding/property/ValuePropertyChangeEvent.java (+52 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation (bug 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property;
13
14
import org.eclipse.core.databinding.observable.value.ValueDiff;
15
import org.eclipse.core.runtime.Assert;
16
17
/**
18
 * @since 1.2
19
 * 
20
 */
21
public class ValuePropertyChangeEvent extends PropertyChangeEvent {
22
	private static final long serialVersionUID = 1L;
23
24
	/**
25
	 * 
26
	 */
27
	public final IValueProperty property;
28
29
	/**
30
	 * ValueDiff with the old and new values of the property. May be null to
31
	 * indicate that the details of the value change are unknown.
32
	 */
33
	public final ValueDiff diff;
34
	
35
	/**
36
	 * @param source
37
	 * @param property
38
	 * @param diff
39
	 */
40
	public ValuePropertyChangeEvent(Object source, IValueProperty property,
41
			ValueDiff diff) {
42
		super(source);
43
		this.property = property;
44
		Assert.isNotNull(diff, "diff cannot be null"); //$NON-NLS-1$
45
		this.diff = diff;
46
	}
47
48
	void dispatch(IPropertyChangeListener listener) {
49
		((IValuePropertyChangeListener) listener)
50
				.handleValuePropertyChange(this);
51
	}
52
}
(-)src/org/eclipse/core/databinding/property/MapProperty.java (+62 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation (bug 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property;
13
14
import org.eclipse.core.databinding.observable.map.MapDiff;
15
16
/**
17
 * @since 1.2
18
 */
19
public abstract class MapProperty extends Property implements IMapProperty {
20
	public boolean containsKey(Object source, Object key) {
21
		return getMap(source).containsKey(key);
22
	}
23
24
	public boolean containsValue(Object source, Object value) {
25
		return getMap(source).containsValue(value);
26
	}
27
28
	public boolean equals(Object source, Object o) {
29
		return getMap(source).equals(o);
30
	}
31
32
	public Object get(Object source, Object key) {
33
		return getMap(source).get(key);
34
	}
35
36
	public int hashCode(Object source) {
37
		return getMap(source).hashCode();
38
	}
39
40
	public boolean isEmpty(Object source) {
41
		return getMap(source).isEmpty();
42
	}
43
44
	public int size(Object source) {
45
		return getMap(source).size();
46
	}
47
48
	public final void addMapChangeListener(Object source,
49
			IMapPropertyChangeListener listener) {
50
		getChangeSupport().addListener(source, listener);
51
	}
52
53
	public final void removeMapChangeListener(Object source,
54
			IMapPropertyChangeListener listener) {
55
		getChangeSupport().removeListener(source, listener);
56
	}
57
58
	protected final void fireMapChange(Object source, MapDiff diff) {
59
		getChangeSupport().firePropertyChange(
60
				new MapPropertyChangeEvent(source, this, diff));
61
	}
62
}
(-)src/org/eclipse/core/databinding/property/ListProperty.java (+52 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation (bug 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property;
13
14
import java.util.Collection;
15
16
import org.eclipse.core.databinding.observable.list.ListDiff;
17
18
/**
19
 * @since 1.2
20
 */
21
public abstract class ListProperty extends CollectionProperty implements IListProperty {
22
	Collection getCollection(Object source) {
23
		return getList(source);
24
	}
25
26
	public Object get(Object source, int index) {
27
		return getList(source).get(index);
28
	}
29
30
	public int indexOf(Object source, Object o) {
31
		return getList(source).indexOf(o);
32
	}
33
34
	public int lastIndexOf(Object source, Object o) {
35
		return getList(source).lastIndexOf(o);
36
	}
37
38
	public final void addListChangeListener(Object source,
39
			IListPropertyChangeListener listener) {
40
		getChangeSupport().addListener(source, listener);
41
	}
42
43
	public final void removeListChangeListener(Object source,
44
			IListPropertyChangeListener listener) {
45
		getChangeSupport().removeListener(source, listener);
46
	}
47
48
	protected final void fireListChange(Object source, ListDiff diff) {
49
		getChangeSupport().firePropertyChange(
50
				new ListPropertyChangeEvent(source, this, diff));
51
	}
52
}
(-)src/org/eclipse/core/internal/databinding/property/PropertyObservableMap.java (+172 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.IObserving;
20
import org.eclipse.core.databinding.observable.ObservableTracker;
21
import org.eclipse.core.databinding.observable.Realm;
22
import org.eclipse.core.databinding.observable.map.AbstractObservableMap;
23
import org.eclipse.core.databinding.property.IMapProperty;
24
import org.eclipse.core.databinding.property.IMapPropertyChangeListener;
25
import org.eclipse.core.databinding.property.MapPropertyChangeEvent;
26
27
/**
28
 * @since 3.3
29
 * 
30
 */
31
public class PropertyObservableMap extends AbstractObservableMap implements
32
		IObserving {
33
	private Object source;
34
	private IMapProperty property;
35
36
	private volatile boolean updating = false;
37
38
	private boolean disposed = false;
39
40
	private transient volatile int modCount = 0;
41
42
	private IMapPropertyChangeListener listener = new IMapPropertyChangeListener() {
43
		public void handleMapPropertyChange(final MapPropertyChangeEvent event) {
44
			if (!disposed && !updating) {
45
				getRealm().exec(new Runnable() {
46
					public void run() {
47
						getRealm().exec(new Runnable() {
48
							public void run() {
49
								modCount++;
50
								fireMapChange(event.diff);
51
							}
52
						});
53
					}
54
				});
55
			}
56
		}
57
	};
58
59
	/**
60
	 * @param realm
61
	 * @param source
62
	 * @param property
63
	 */
64
	public PropertyObservableMap(Realm realm, Object source,
65
			IMapProperty property) {
66
		super(realm);
67
		this.source = source;
68
		this.property = property;
69
	}
70
71
	private void getterCalled() {
72
		ObservableTracker.getterCalled(this);
73
	}
74
75
	protected void firstListenerAdded() {
76
		if (!disposed) {
77
			property.addMapChangeListener(source, listener);
78
		}
79
	}
80
81
	protected void lastListenerRemoved() {
82
		if (!disposed) {
83
			property.removeMapChangeListener(source, listener);
84
		}
85
	}
86
87
	public boolean containsKey(Object key) {
88
		getterCalled();
89
		return property.containsKey(source, key);
90
	}
91
92
	public boolean containsValue(Object value) {
93
		getterCalled();
94
		return property.containsValue(source, value);
95
	}
96
97
	public Set entrySet() {
98
		getterCalled();
99
		// unmodifiable for now
100
		return Collections.unmodifiableSet(property.getMap(source).entrySet());
101
	}
102
103
	public Object get(Object key) {
104
		getterCalled();
105
		return property.get(source, key);
106
	}
107
108
	public boolean isEmpty() {
109
		getterCalled();
110
		return property.isEmpty(source);
111
	}
112
113
	public Set keySet() {
114
		getterCalled();
115
		return Collections.unmodifiableSet(property.getMap(source).keySet());
116
	}
117
118
	public Object put(Object key, Object value) {
119
		checkRealm();
120
		return property.put(source, key, value);
121
	}
122
123
	public void putAll(Map m) {
124
		checkRealm();
125
		property.putAll(source, m);
126
	}
127
128
	public Object remove(Object key) {
129
		checkRealm();
130
		return property.remove(source, key);
131
	}
132
133
	public int size() {
134
		getterCalled();
135
		return property.size(source);
136
	}
137
138
	public Collection values() {
139
		getterCalled();
140
		return Collections.unmodifiableCollection(property.getMap(source)
141
				.values());
142
	}
143
144
	public void clear() {
145
		getterCalled();
146
		property.clear(source);
147
	}
148
149
	public boolean equals(Object o) {
150
		getterCalled();
151
		return property.equals(source, o);
152
	}
153
154
	public int hashCode() {
155
		getterCalled();
156
		return property.hashCode(source);
157
	}
158
159
	public Object getObserved() {
160
		return source;
161
	}
162
163
	public synchronized void dispose() {
164
		if (!disposed) {
165
			disposed = true;
166
			property.removeMapChangeListener(source, listener);
167
			property = null;
168
			source = null;
169
		}
170
		super.dispose();
171
	}
172
}
(-)src/org/eclipse/core/databinding/property/SetPropertyChangeEvent.java (+50 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
 * @since 1.2
19
 */
20
public class SetPropertyChangeEvent extends PropertyChangeEvent {
21
	private static final long serialVersionUID = 1L;
22
23
	/**
24
	 * 
25
	 */
26
	public final ISetProperty property;
27
28
	/**
29
	 * SetDiff enumerating the added and removed elements in the set. May be
30
	 * null to indicate that the details of the set change are unknown.
31
	 */
32
	public final SetDiff diff;
33
34
	/**
35
	 * @param source
36
	 * @param property
37
	 * @param diff
38
	 */
39
	public SetPropertyChangeEvent(Object source, ISetProperty property,
40
			SetDiff diff) {
41
		super(source);
42
		this.property = property;
43
		Assert.isNotNull(diff, "diff cannot be null"); //$NON-NLS-1$
44
		this.diff = diff;
45
	}
46
47
	void dispatch(IPropertyChangeListener listener) {
48
		((ISetPropertyChangeListener) listener).handleSetPropertyChange(this);
49
	}
50
}
(-)src/org/eclipse/core/databinding/property/IValueProperty.java (+49 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
 * @since 1.2
16
 * 
17
 */
18
public interface IValueProperty extends IProperty {
19
	/**
20
	 * @param source
21
	 * @return the property value
22
	 */
23
	public Object getValue(Object source);
24
25
	/**
26
	 * @param source
27
	 * @param value
28
	 */
29
	public void setValue(Object source, Object value);
30
31
	/**
32
	 * @return the value type of the property, or <code>null</code> if untyped.
33
	 */
34
	public Object getValueType();
35
36
	/**
37
	 * @param source
38
	 * @param listener
39
	 */
40
	public void addValueChangeListener(Object source,
41
			IValuePropertyChangeListener listener);
42
43
	/**
44
	 * @param source
45
	 * @param listener
46
	 */
47
	public void removeValueChangeListener(Object source,
48
			IValuePropertyChangeListener listener);
49
}
(-)src/org/eclipse/core/databinding/property/IMapProperty.java (+116 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
 * @since 1.2
18
 */
19
public interface IMapProperty extends IProperty {
20
	/**
21
	 * @param source
22
	 * @return a Map with the current contents of the source's map property
23
	 */
24
	Map getMap(Object source);
25
26
	/**
27
	 * @param source
28
	 * @return the size of the source's map property
29
	 */
30
	int size(Object source);
31
32
	/**
33
	 * @param source
34
	 * @return whether the source's map property is empty
35
	 */
36
	boolean isEmpty(Object source);
37
38
	/**
39
	 * @param source
40
	 * @param key
41
	 * @return whether the given key is contained in the key set of the source's
42
	 *         map property
43
	 */
44
	boolean containsKey(Object source, Object key);
45
46
	/**
47
	 * @param source
48
	 * @param value
49
	 * @return whether the given value is contains in the values collection of
50
	 *         the source's map property
51
	 */
52
	boolean containsValue(Object source, Object value);
53
54
	/**
55
	 * @param source
56
	 * @param key
57
	 * @return the value associated with the given key in the source's map
58
	 *         property
59
	 */
60
	Object get(Object source, Object key);
61
62
	/**
63
	 * @param source
64
	 * @param key
65
	 * @param value
66
	 * @return the value that was previously associated with the given key in
67
	 *         the source's map property
68
	 */
69
	Object put(Object source, Object key, Object value);
70
71
	/**
72
	 * @param source
73
	 * @param key
74
	 * @return the value that was previously associated with the given key in
75
	 *         the source's map property
76
	 */
77
	Object remove(Object source, Object key);
78
79
	/**
80
	 * @param source
81
	 * @param t
82
	 */
83
	void putAll(Object source, Map t);
84
85
	/**
86
	 * @param source
87
	 */
88
	void clear(Object source);
89
90
	/**
91
	 * @param source
92
	 * @param o
93
	 * @return whether the source's map property is equal to the argument
94
	 */
95
	boolean equals(Object source, Object o);
96
97
	/**
98
	 * @param source
99
	 * @return the hash code of the source's map property
100
	 */
101
	int hashCode(Object source);
102
103
	/**
104
	 * @param source
105
	 * @param listener
106
	 */
107
	public void addMapChangeListener(Object source,
108
			IMapPropertyChangeListener listener);
109
110
	/**
111
	 * @param source
112
	 * @param listener
113
	 */
114
	public void removeMapChangeListener(Object source,
115
			IMapPropertyChangeListener listener);
116
}
(-)src/org/eclipse/core/databinding/property/IMapPropertyChangeListener.java (+22 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
 * @since 1.2
16
 */
17
public interface IMapPropertyChangeListener extends IPropertyChangeListener {
18
	/**
19
	 * @param event
20
	 */
21
	public void handleMapPropertyChange(MapPropertyChangeEvent event);
22
}
(-)src/org/eclipse/core/databinding/property/IValuePropertyChangeListener.java (+22 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
 * @since 1.2
16
 */
17
public interface IValuePropertyChangeListener extends IPropertyChangeListener {
18
	/**
19
	 * @param event
20
	 */
21
	public void handleValuePropertyChange(ValuePropertyChangeEvent event);
22
}
(-)src/org/eclipse/core/databinding/property/ValueProperty.java (+34 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation (bug 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property;
13
14
import org.eclipse.core.databinding.observable.value.ValueDiff;
15
16
/**
17
 * @since 1.2
18
 */
19
public abstract class ValueProperty extends Property implements IValueProperty {
20
	public final void addValueChangeListener(Object source,
21
			IValuePropertyChangeListener listener) {
22
		getChangeSupport().addListener(source, listener);
23
	}
24
25
	public final void removeValueChangeListener(Object source,
26
			IValuePropertyChangeListener listener) {
27
		getChangeSupport().removeListener(source, listener);
28
	}
29
30
	protected final void fireValueChange(Object source, ValueDiff diff) {
31
		getChangeSupport().firePropertyChange(
32
				new ValuePropertyChangeEvent(source, this, diff));
33
	}
34
}
(-)src/org/eclipse/core/databinding/property/masterdetail/MasterDetailProperties.java (+107 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
 * @since 1.2
28
 */
29
public class MasterDetailProperties {
30
	// Properties of IValueProperty master properties
31
32
	/**
33
	 * @param masterValue
34
	 * @param detailValue
35
	 * @return blah
36
	 */
37
	public static IValueProperty detailValue(IValueProperty masterValue,
38
			IValueProperty detailValue) {
39
		return new ValuePropertyDetailValue(masterValue, detailValue);
40
	}
41
42
	/**
43
	 * @param masterValue
44
	 * @param detailList
45
	 * @return blah
46
	 */
47
	public static IListProperty detailList(IValueProperty masterValue,
48
			IListProperty detailList) {
49
		return new ValuePropertyDetailList(masterValue, detailList);
50
	}
51
52
	/**
53
	 * @param masterValue
54
	 * @param detailSet
55
	 * @return blah
56
	 */
57
	public static ISetProperty detailSet(IValueProperty masterValue,
58
			ISetProperty detailSet) {
59
		return new ValuePropertyDetailSet(masterValue, detailSet);
60
	}
61
62
	/**
63
	 * @param masterValue
64
	 * @param detailMap
65
	 * @return blah
66
	 */
67
	public static IMapProperty detailMap(IValueProperty masterValue,
68
			IMapProperty detailMap) {
69
		return new ValuePropertyDetailMap(masterValue, detailMap);
70
	}
71
72
	// Properties of IListProperty master properties
73
74
	/**
75
	 * @param masterList
76
	 * @param detailValue
77
	 * @return blah
78
	 */
79
	public static IListProperty detailList(IListProperty masterList,
80
			IValueProperty detailValue) {
81
		return new ListPropertyDetailValueList(masterList, detailValue);
82
	}
83
84
	// Properties of ISetProperty master properties
85
86
	/**
87
	 * @param masterKeySet
88
	 * @param detailValues
89
	 * @return blah
90
	 */
91
	public static IMapProperty detailMap(ISetProperty masterKeySet,
92
			IValueProperty detailValues) {
93
		return new SetPropertyDetailValueMap(masterKeySet, detailValues);
94
	}
95
96
	// Properties of IMapProperty master properties
97
98
	/**
99
	 * @param masterMap
100
	 * @param detailValues
101
	 * @return blah
102
	 */
103
	public static IMapProperty detailMap(IMapProperty masterMap,
104
			IValueProperty detailValues) {
105
		return new MapPropertyDetailValueMap(masterMap, detailValues);
106
	}
107
}
(-)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/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/databinding/property/SetProperty.java (+41 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation (bug 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property;
13
14
import java.util.Collection;
15
16
import org.eclipse.core.databinding.observable.set.SetDiff;
17
18
/**
19
 * @since 1.2
20
 */
21
public abstract class SetProperty extends CollectionProperty implements
22
		ISetProperty {
23
	Collection getCollection(Object source) {
24
		return getSet(source);
25
	}
26
27
	public final void addSetChangeListener(Object source,
28
			ISetPropertyChangeListener listener) {
29
		getChangeSupport().addListener(source, listener);
30
	}
31
32
	public final void removeSetChangeListener(Object source,
33
			ISetPropertyChangeListener listener) {
34
		getChangeSupport().removeListener(source, listener);
35
	}
36
37
	protected final void fireSetChange(Object source, SetDiff diff) {
38
		getChangeSupport().firePropertyChange(
39
				new SetPropertyChangeEvent(source, this, diff));
40
	}
41
}
(-)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/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/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/ISetPropertyChangeListener.java (+22 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
 * @since 1.2
16
 */
17
public interface ISetPropertyChangeListener extends IPropertyChangeListener {
18
	/**
19
	 * @param event
20
	 */
21
	public void handleSetPropertyChange(SetPropertyChangeEvent event);
22
}
(-)src/org/eclipse/core/databinding/property/MapPropertyChangeEvent.java (+50 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
 * @since 1.2
19
 */
20
public class MapPropertyChangeEvent extends PropertyChangeEvent {
21
	private static final long serialVersionUID = 1L;
22
23
	/**
24
	 * 
25
	 */
26
	public final IMapProperty property;
27
28
	/**
29
	 * MapDiff enumerating the added, changed, and removed entries in the map.
30
	 * May be null to indicate that the details of the map change are unknown.
31
	 */
32
	public final MapDiff diff;
33
34
	/**
35
	 * @param source
36
	 * @param property
37
	 * @param diff
38
	 */
39
	public MapPropertyChangeEvent(Object source, IMapProperty property,
40
			MapDiff diff) {
41
		super(source);
42
		this.property = property;
43
		Assert.isNotNull(diff, "diff cannot be null"); //$NON-NLS-1$
44
		this.diff = diff;
45
	}
46
47
	void dispatch(IPropertyChangeListener listener) {
48
		((IMapPropertyChangeListener) listener).handleMapPropertyChange(this);
49
	}
50
}
(-)src/org/eclipse/core/databinding/property/ListPropertyChangeEvent.java (+50 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
 * @since 1.2
19
 */
20
public class ListPropertyChangeEvent extends PropertyChangeEvent {
21
	private static final long serialVersionUID = 1L;
22
23
	/**
24
	 * 
25
	 */
26
	public final IListProperty property;
27
28
	/**
29
	 * ListDiff enumerating the added and removed elements in the list. May be
30
	 * null to indicate that the details of the list change are unknown.
31
	 */
32
	public final ListDiff diff;
33
34
	/**
35
	 * @param source
36
	 * @param property
37
	 * @param diff
38
	 */
39
	public ListPropertyChangeEvent(Object source, IListProperty property,
40
			ListDiff diff) {
41
		super(source);
42
		this.property = property;
43
		Assert.isNotNull(diff, "diff cannot be null"); //$NON-NLS-1$
44
		this.diff = diff;
45
	}
46
47
	void dispatch(IPropertyChangeListener listener) {
48
		((IListPropertyChangeListener) listener).handleListPropertyChange(this);
49
	}
50
}
(-)src/org/eclipse/core/databinding/property/ICollectionProperty.java (+120 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
 * @since 1.2
18
 * 
19
 */
20
public interface ICollectionProperty extends IProperty {
21
	/**
22
	 * @param source
23
	 * @return the size of the source's collection property
24
	 */
25
	int size(Object source);
26
27
	/**
28
	 * @param source
29
	 * @return whether the source's collection property is empty
30
	 */
31
	boolean isEmpty(Object source);
32
33
	/**
34
	 * @param source
35
	 * @param o
36
	 * @return whether the source's collection property contains the given
37
	 *         element
38
	 */
39
	boolean contains(Object source, Object o);
40
41
	/**
42
	 * @param source
43
	 * @return an array of all elements in the source's collection property
44
	 */
45
	Object[] toArray(Object source);
46
47
	/**
48
	 * @param source
49
	 * @param array
50
	 * @return an array of all elements in the source's collection property
51
	 */
52
	Object[] toArray(Object source, Object[] array);
53
54
	/**
55
	 * @param source
56
	 * @param o
57
	 * @return whether the element was added to the source's collection property
58
	 */
59
	boolean add(Object source, Object o);
60
61
	/**
62
	 * @param source
63
	 * @param o
64
	 * @return whether the element was removed from the source's collection
65
	 *         property
66
	 */
67
	boolean remove(Object source, Object o);
68
69
	/**
70
	 * @param source
71
	 * @param c
72
	 * @return whether the source's collection property contains all elements in
73
	 *         the given collection
74
	 */
75
	boolean containsAll(Object source, Collection c);
76
77
	/**
78
	 * @param source
79
	 * @param c
80
	 * @return whether the source's collection property was changed
81
	 */
82
	boolean addAll(Object source, Collection c);
83
84
	/**
85
	 * @param source
86
	 * @param c
87
	 * @return whether the source's collection property was changed
88
	 */
89
	boolean removeAll(Object source, Collection c);
90
91
	/**
92
	 * @param source
93
	 * @param c
94
	 * @return whether the source's collection property was changed
95
	 */
96
	boolean retainAll(Object source, Collection c);
97
98
	/**
99
	 * @param source
100
	 */
101
	void clear(Object source);
102
103
	/**
104
	 * @param source
105
	 * @param o
106
	 * @return whether the source's collection property is equal to the argument
107
	 */
108
	boolean equals(Object source, Object o);
109
110
	/**
111
	 * @param source
112
	 * @return the hash code of the source's collection property
113
	 */
114
	int hashCode(Object source);
115
116
	/**
117
	 * @return the type of the elements or <code>null</code> if untyped
118
	 */
119
	Object getElementType();
120
}
(-)src/org/eclipse/core/databinding/property/PropertyChangeEvent.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
import java.util.EventObject;
15
16
/**
17
 * @since 1.2
18
 */
19
public abstract class PropertyChangeEvent extends EventObject {
20
	private static final long serialVersionUID = 1L;
21
22
	PropertyChangeEvent(Object source) {
23
		super(source);
24
	}
25
26
	abstract void dispatch(IPropertyChangeListener listener);
27
}
(-)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/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
/**
22
 * @since 1.2
23
 */
24
public class PropertyChangeSupport {
25
	private Property property;
26
	private Map changeListeners;
27
28
	PropertyChangeSupport(Property property) {
29
		this.property = property;
30
		this.changeListeners = null;
31
	}
32
33
	protected final void addListener(Object source,
34
			IPropertyChangeListener listener) {
35
		boolean wasListening = hasListeners(source);
36
37
		if (changeListeners == null) {
38
			synchronized (this) {
39
				if (changeListeners == null) {
40
					changeListeners = Collections
41
							.synchronizedMap(new IdentityHashMap());
42
				}
43
			}
44
		}
45
46
		ListenerList listeners;
47
		synchronized (changeListeners) {
48
			if (changeListeners.containsKey(source)) {
49
				listeners = (ListenerList) changeListeners.get(source);
50
			} else {
51
				changeListeners.put(source, listeners = new ListenerList());
52
			}
53
		}
54
55
		synchronized (listeners) {
56
			listeners.add(listener);
57
		}
58
59
		if (!wasListening)
60
			property.addListenerTo(source);
61
	}
62
63
	/**
64
	 * Returns whether the change support has listeners registered for the given property source
65
	 * @param source the property source
66
	 * @return whether the change support has listeners registered for the given property source
67
	 */
68
	public boolean hasListeners(Object source) {
69
		return changeListeners != null
70
				&& 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 (+229 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.set.IObservableSet;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.internal.databinding.property.PropertyObservableList;
22
import org.eclipse.core.internal.databinding.property.PropertyObservableMap;
23
import org.eclipse.core.internal.databinding.property.PropertyObservableSet;
24
import org.eclipse.core.internal.databinding.property.PropertyObservableValue;
25
import org.eclipse.core.internal.databinding.property.SetValuePropertyObservableMap;
26
27
/**
28
 * @since 1.2
29
 * 
30
 */
31
public class PropertyObservables {
32
	/**
33
	 * @param source
34
	 * @param property
35
	 * @return an observable value that tracks the given property of the source
36
	 *         object.
37
	 * @since 1.2
38
	 */
39
	public static IObservableValue observeValue(Object source,
40
			IValueProperty property) {
41
		return observeValue(Realm.getDefault(), source, property);
42
	}
43
44
	/**
45
	 * @param realm
46
	 * @param source
47
	 * @param property
48
	 * @return an observable value that tracks the given property of the source
49
	 *         object
50
	 * @since 1.2
51
	 */
52
	public static IObservableValue observeValue(Realm realm, Object source,
53
			IValueProperty property) {
54
		return new PropertyObservableValue(realm, source, property);
55
	}
56
57
	/**
58
	 * @param source
59
	 * @param property
60
	 * @return an observable set that tracks the given property of the source
61
	 *         object
62
	 */
63
	public static IObservableSet observeSet(Object source, ISetProperty property) {
64
		return observeSet(Realm.getDefault(), source, property);
65
	}
66
67
	/**
68
	 * @param realm
69
	 * @param source
70
	 * @param property
71
	 * @return an observable set that tracks the given property of the source
72
	 *         object
73
	 */
74
	public static IObservableSet observeSet(Realm realm, Object source,
75
			ISetProperty property) {
76
		return new PropertyObservableSet(realm, source, property);
77
	}
78
79
	/**
80
	 * @param source
81
	 * @param property
82
	 * @return an observable list that tracks the given property of the source
83
	 *         object
84
	 */
85
	public static IObservableList observeList(Object source,
86
			IListProperty property) {
87
		return observeList(Realm.getDefault(), source, property);
88
	}
89
90
	/**
91
	 * @param realm
92
	 * @param source
93
	 * @param property
94
	 * @return an observable set that tracks the given property of the source
95
	 *         object
96
	 */
97
	public static IObservableList observeList(Realm realm, Object source,
98
			IListProperty property) {
99
		return new PropertyObservableList(realm, source, property);
100
	}
101
102
	/**
103
	 * @param source
104
	 * @param property
105
	 * @return an observable map that tracks the given property of the source
106
	 *         object
107
	 */
108
	public static IObservableMap observeMap(Object source, IMapProperty property) {
109
		return observeMap(Realm.getDefault(), source, property);
110
	}
111
112
	/**
113
	 * @param realm
114
	 * @param source
115
	 * @param property
116
	 * @return an observable set that tracks the given property of the source
117
	 *         object
118
	 */
119
	public static IObservableMap observeMap(Realm realm, Object source,
120
			IMapProperty property) {
121
		return new PropertyObservableMap(realm, source, property);
122
	}
123
124
	/**
125
	 * Returns an observable map in the default realm tracking the current
126
	 * values of the named property for the beans in the given set.
127
	 * 
128
	 * @param keySet
129
	 * @param valueProperty
130
	 * @return an observable map 
131
	 */
132
	public static IObservableMap observeMap(IObservableSet keySet,
133
			IValueProperty valueProperty) {
134
		return new SetValuePropertyObservableMap(keySet, valueProperty);
135
	}
136
137
	/**
138
	 * Returns a factory for creating observable values tracking the given
139
	 * property of a particular source object
140
	 * 
141
	 * @param property
142
	 *            the property to be observed
143
	 * @return an observable value factory on
144
	 */
145
	public static IObservableFactory valueFactory(IValueProperty property) {
146
		return valueFactory(Realm.getDefault(), property);
147
	}
148
149
	/**
150
	 * Returns a factory for creating observable values tracking the given
151
	 * property of a particular source object
152
	 * 
153
	 * @param realm
154
	 *            the realm to use
155
	 * @param property
156
	 *            the property to be observed
157
	 * @return an observable value factory on
158
	 */
159
	public static IObservableFactory valueFactory(final Realm realm,
160
			final IValueProperty property) {
161
		return new IObservableFactory() {
162
			public IObservable createObservable(Object target) {
163
				return observeValue(realm, target, property);
164
			}
165
		};
166
	}
167
168
	/**
169
	 * Returns a factory for creating observable sets tracking the given
170
	 * property of a particular source object
171
	 * 
172
	 * @param property
173
	 *            the property to be observed
174
	 * @return an observable value factory on
175
	 */
176
	public static IObservableFactory setFactory(ISetProperty property) {
177
		return setFactory(Realm.getDefault(), property);
178
	}
179
180
	/**
181
	 * Returns a factory for creating obervable sets tracking the given property
182
	 * of a particular source object
183
	 * 
184
	 * @param realm
185
	 *            the realm to use
186
	 * @param property
187
	 *            the property to be observed
188
	 * @return an observable value factory on
189
	 */
190
	public static IObservableFactory setFactory(final Realm realm,
191
			final ISetProperty property) {
192
		return new IObservableFactory() {
193
			public IObservable createObservable(Object target) {
194
				return observeSet(realm, target, property);
195
			}
196
		};
197
	}
198
199
	/**
200
	 * Returns a factory for creating observable lists tracking the given
201
	 * property of a particular source object
202
	 * 
203
	 * @param property
204
	 *            the property to be observed
205
	 * @return an observable value factory on
206
	 */
207
	public static IObservableFactory listFactory(IListProperty property) {
208
		return listFactory(Realm.getDefault(), property);
209
	}
210
211
	/**
212
	 * Returns a factory for creating obervable lists tracking the given
213
	 * property of a particular source object
214
	 * 
215
	 * @param realm
216
	 *            the realm to use
217
	 * @param property
218
	 *            the property to be observed
219
	 * @return an observable value factory on
220
	 */
221
	public static IObservableFactory listFactory(final Realm realm,
222
			final IListProperty property) {
223
		return new IObservableFactory() {
224
			public IObservable createObservable(Object target) {
225
				return observeList(realm, target, property);
226
			}
227
		};
228
	}
229
}
(-)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 (+368 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.IObserving;
23
import org.eclipse.core.databinding.observable.ObservableTracker;
24
import org.eclipse.core.databinding.observable.Realm;
25
import org.eclipse.core.databinding.observable.list.AbstractObservableList;
26
import org.eclipse.core.databinding.property.IListProperty;
27
import org.eclipse.core.databinding.property.IListPropertyChangeListener;
28
import org.eclipse.core.databinding.property.ListPropertyChangeEvent;
29
30
/**
31
 * @since 3.3
32
 * 
33
 */
34
public class PropertyObservableList extends AbstractObservableList implements IObserving {
35
	private Object source;
36
	private IListProperty property;
37
38
	private volatile boolean updating = false;
39
40
	private boolean disposed = false;
41
42
	private transient volatile int modCount = 0;
43
44
	private IListPropertyChangeListener listener = new IListPropertyChangeListener() {
45
		public void handleListPropertyChange(final ListPropertyChangeEvent event) {
46
			if (!disposed && !updating) {
47
				getRealm().exec(new Runnable() {
48
					public void run() {
49
						getRealm().exec(new Runnable() {
50
							public void run() {
51
								modCount++;
52
								fireListChange(event.diff);
53
							}
54
						});
55
					}
56
				});
57
			}
58
		}
59
	};
60
61
	/**
62
	 * @param realm
63
	 * @param source
64
	 * @param property
65
	 */
66
	public PropertyObservableList(Realm realm, Object source,
67
			IListProperty property) {
68
		super(realm);
69
		this.source = source;
70
		this.property = property;
71
	}
72
73
	protected void firstListenerAdded() {
74
		if (!disposed) {
75
			property.addListChangeListener(source, listener);
76
		}
77
	}
78
79
	protected void lastListenerRemoved() {
80
		if (!disposed) {
81
			property.removeListChangeListener(source, listener);
82
		}
83
	}
84
85
	private void getterCalled() {
86
		ObservableTracker.getterCalled(this);
87
	}
88
89
	public Object getElementType() {
90
		return property.getElementType();
91
	}
92
93
	// Queries
94
95
	protected int doGetSize() {
96
		return property.size(source);
97
	}
98
99
	public boolean contains(Object o) {
100
		getterCalled();
101
		return property.contains(source, o);
102
	}
103
104
	public boolean containsAll(Collection c) {
105
		getterCalled();
106
		return property.containsAll(source, c);
107
	}
108
109
	public Object get(int index) {
110
		getterCalled();
111
		return property.get(source, index);
112
	}
113
114
	public int indexOf(Object o) {
115
		getterCalled();
116
		return property.indexOf(source, o);
117
	}
118
119
	public boolean isEmpty() {
120
		getterCalled();
121
		return property.isEmpty(source);
122
	}
123
124
	public int lastIndexOf(Object o) {
125
		getterCalled();
126
		return property.lastIndexOf(source, o);
127
	}
128
129
	public Object[] toArray() {
130
		getterCalled();
131
		return property.toArray(source);
132
	}
133
134
	public Object[] toArray(Object[] a) {
135
		getterCalled();
136
		return property.toArray(source, a);
137
	}
138
139
	// Single change operations
140
141
	public boolean add(Object o) {
142
		checkRealm();
143
		return property.add(source, o);
144
	}
145
146
	public Iterator iterator() {
147
		getterCalled();
148
		return new Iterator() {
149
			int lastReturned = -1;
150
			int expectedModCount = modCount;
151
			ListIterator delegate = new ArrayList(property.getList(source))
152
					.listIterator();
153
154
			public boolean hasNext() {
155
				getterCalled();
156
				checkForComodification();
157
				return delegate.hasNext();
158
			}
159
160
			public Object next() {
161
				getterCalled();
162
				checkForComodification();
163
				Object next = delegate.next();
164
				lastReturned = delegate.previousIndex();
165
				return next;
166
			}
167
168
			public void remove() {
169
				checkRealm();
170
				checkForComodification();
171
				if (lastReturned == -1)
172
					throw new IllegalStateException();
173
174
				delegate.remove(); // stay in sync
175
176
				property.remove(source, lastReturned);
177
178
				lastReturned = -1;
179
				expectedModCount = modCount;
180
			}
181
182
			private void checkForComodification() {
183
				if (expectedModCount != modCount)
184
					throw new ConcurrentModificationException();
185
			}
186
		};
187
	}
188
189
	public Object move(int oldIndex, int newIndex) {
190
		getterCalled();
191
		return property.move(source, oldIndex, newIndex);
192
	}
193
194
	public boolean remove(Object o) {
195
		getterCalled();
196
		return property.remove(source, o);
197
	}
198
199
	public void add(int index, Object o) {
200
		getterCalled();
201
		property.add(source, index, o);
202
	}
203
204
	public ListIterator listIterator() {
205
		return listIterator(0);
206
	}
207
208
	public ListIterator listIterator(final int index) {
209
		getterCalled();
210
		return new ListIterator() {
211
			int lastReturned = -1;
212
			int expectedModCount = modCount;
213
			ListIterator delegate = new ArrayList(property.getList(source))
214
					.listIterator(index);
215
216
			public boolean hasNext() {
217
				getterCalled();
218
				checkForComodification();
219
				return delegate.hasNext();
220
			}
221
222
			public int nextIndex() {
223
				getterCalled();
224
				checkForComodification();
225
				return delegate.nextIndex();
226
			}
227
228
			public Object next() {
229
				getterCalled();
230
				checkForComodification();
231
				Object next = delegate.next();
232
				lastReturned = delegate.previousIndex();
233
				return next;
234
			}
235
236
			public boolean hasPrevious() {
237
				getterCalled();
238
				checkForComodification();
239
				return delegate.hasPrevious();
240
			}
241
242
			public int previousIndex() {
243
				getterCalled();
244
				checkForComodification();
245
				return delegate.previousIndex();
246
			}
247
248
			public Object previous() {
249
				getterCalled();
250
				checkForComodification();
251
				Object previous = delegate.previous();
252
				lastReturned = delegate.nextIndex();
253
				return previous;
254
			}
255
256
			public void add(Object o) {
257
				checkRealm();
258
				checkForComodification();
259
				int index = delegate.nextIndex();
260
261
				delegate.add(o); // keep in sync
262
263
				property.add(source, index, o);
264
265
				lastReturned = -1;
266
				expectedModCount = modCount;
267
			}
268
269
			public void set(Object o) {
270
				checkRealm();
271
				checkForComodification();
272
273
				delegate.set(o);
274
275
				property.set(source, lastReturned, o);
276
277
				expectedModCount = modCount;
278
			}
279
280
			public void remove() {
281
				checkRealm();
282
				checkForComodification();
283
				if (lastReturned == -1)
284
					throw new IllegalStateException();
285
286
				delegate.remove(); // keep in sync
287
288
				property.remove(source, lastReturned);
289
290
				lastReturned = -1;
291
				expectedModCount = modCount;
292
			}
293
294
			private void checkForComodification() {
295
				if (expectedModCount != modCount)
296
					throw new ConcurrentModificationException();
297
			}
298
299
		};
300
	}
301
302
	public Object remove(int index) {
303
		getterCalled();
304
		return property.remove(source, index);
305
	}
306
307
	public Object set(int index, Object o) {
308
		getterCalled();
309
		return property.set(source, index, o);
310
	}
311
312
	public List subList(int fromIndex, int toIndex) {
313
		getterCalled();
314
		return Collections.unmodifiableList(property.getList(source).subList(
315
				fromIndex, toIndex));
316
	}
317
318
	// Bulk change operations
319
320
	public boolean addAll(Collection c) {
321
		getterCalled();
322
		return property.addAll(source, c);
323
	}
324
325
	public boolean addAll(int index, Collection c) {
326
		getterCalled();
327
		return property.addAll(source, index, c);
328
	}
329
330
	public boolean removeAll(Collection c) {
331
		getterCalled();
332
		return property.removeAll(source, c);
333
	}
334
335
	public boolean retainAll(Collection c) {
336
		getterCalled();
337
		return property.retainAll(source, c);
338
	}
339
340
	public void clear() {
341
		getterCalled();
342
		property.clear(source);
343
	}
344
345
	public boolean equals(Object o) {
346
		getterCalled();
347
		return property.equals(source, o);
348
	}
349
350
	public int hashCode() {
351
		getterCalled();
352
		return property.hashCode(source);
353
	}
354
355
	public Object getObserved() {
356
		return source;
357
	}
358
359
	public synchronized void dispose() {
360
		if (!disposed) {
361
			disposed = true;
362
			property.removeListChangeListener(source, listener);
363
			property = null;
364
			source = null;
365
		}
366
		super.dispose();
367
	}
368
}
(-)src/org/eclipse/core/internal/databinding/property/PropertyObservableSet.java (+192 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.IObserving;
21
import org.eclipse.core.databinding.observable.Realm;
22
import org.eclipse.core.databinding.observable.set.AbstractObservableSet;
23
import org.eclipse.core.databinding.property.ISetProperty;
24
import org.eclipse.core.databinding.property.ISetPropertyChangeListener;
25
import org.eclipse.core.databinding.property.SetPropertyChangeEvent;
26
27
/**
28
 * @since 3.3
29
 * 
30
 */
31
public class PropertyObservableSet extends AbstractObservableSet implements
32
		IObserving {
33
	private Object source;
34
	private ISetProperty property;
35
36
	private boolean updating = false;
37
38
	private boolean disposed = false;
39
40
	private transient volatile int modCount = 0;
41
42
	private ISetPropertyChangeListener listener = new ISetPropertyChangeListener() {
43
		public void handleSetPropertyChange(final SetPropertyChangeEvent event) {
44
			if (!disposed && !updating) {
45
				getRealm().exec(new Runnable() {
46
					public void run() {
47
						getRealm().exec(new Runnable() {
48
							public void run() {
49
								modCount++;
50
								fireSetChange(event.diff);
51
							}
52
						});
53
					}
54
				});
55
			}
56
		}
57
	};
58
59
	/**
60
	 * @param realm
61
	 * @param source
62
	 * @param property
63
	 */
64
	public PropertyObservableSet(Realm realm, Object source,
65
			ISetProperty property) {
66
		super(realm);
67
		this.source = source;
68
		this.property = property;
69
	}
70
71
	protected void firstListenerAdded() {
72
		if (!disposed) {
73
			property.addSetChangeListener(source, listener);
74
		}
75
	}
76
77
	protected void lastListenerRemoved() {
78
		if (!disposed) {
79
			property.removeSetChangeListener(source, listener);
80
		}
81
	}
82
83
	protected Set getWrappedSet() {
84
		return property.getSet(source);
85
	}
86
87
	public Object getElementType() {
88
		return property.getElementType();
89
	}
90
91
	// Queries
92
93
	protected int doGetSize() {
94
		return property.size(source);
95
	}
96
97
	// Single change operations
98
99
	public boolean add(Object o) {
100
		checkRealm();
101
		return property.add(source, o);
102
	}
103
104
	public Iterator iterator() {
105
		getterCalled();
106
		return new Iterator() {
107
			int expectedModCount = modCount;
108
			Iterator delegate = new HashSet(property.getSet(source)).iterator();
109
			Object last = null;
110
111
			public boolean hasNext() {
112
				getterCalled();
113
				checkForComodification();
114
				return delegate.hasNext();
115
			}
116
117
			public Object next() {
118
				getterCalled();
119
				checkForComodification();
120
				Object next = delegate.next();
121
				last = next;
122
				return next;
123
			}
124
125
			public void remove() {
126
				checkRealm();
127
				checkForComodification();
128
129
				delegate.remove(); // stay in sync
130
131
				property.remove(source, last);
132
133
				last = null;
134
				expectedModCount = modCount;
135
			}
136
137
			private void checkForComodification() {
138
				if (expectedModCount != modCount)
139
					throw new ConcurrentModificationException();
140
			}
141
		};
142
	}
143
144
	public boolean remove(Object o) {
145
		getterCalled();
146
		return property.remove(source, o);
147
	}
148
149
	// Bulk change operations
150
151
	public boolean addAll(Collection c) {
152
		getterCalled();
153
		return property.addAll(source, c);
154
	}
155
156
	public boolean removeAll(Collection c) {
157
		getterCalled();
158
		return property.removeAll(source, c);
159
	}
160
161
	public boolean retainAll(Collection c) {
162
		getterCalled();
163
		return property.retainAll(source, c);
164
	}
165
166
	public void clear() {
167
		getterCalled();
168
		property.clear(source);
169
	}
170
171
	public boolean equals(Object o) {
172
		return property.equals(source, o);
173
	}
174
175
	public int hashCode() {
176
		return property.hashCode(source);
177
	}
178
179
	public Object getObserved() {
180
		return source;
181
	}
182
183
	public synchronized void dispose() {
184
		if (!disposed) {
185
			disposed = true;
186
			property.removeSetChangeListener(source, listener);
187
			property = null;
188
			source = null;
189
		}
190
		super.dispose();
191
	}
192
}
(-)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 (+55 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation (bug 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property;
13
14
import java.util.Collection;
15
16
/**
17
 * @since 1.2
18
 * 
19
 */
20
public abstract class CollectionProperty extends Property implements
21
		ICollectionProperty {
22
	abstract Collection getCollection(Object source);
23
24
	public boolean contains(Object source, Object o) {
25
		return getCollection(source).contains(o);
26
	}
27
28
	public boolean containsAll(Object source, Collection c) {
29
		return getCollection(source).containsAll(c);
30
	}
31
32
	public boolean equals(Object source, Object o) {
33
		return getCollection(source).equals(o);
34
	}
35
36
	public int hashCode(Object source) {
37
		return getCollection(source).hashCode();
38
	}
39
40
	public boolean isEmpty(Object source) {
41
		return getCollection(source).isEmpty();
42
	}
43
44
	public int size(Object source) {
45
		return getCollection(source).size();
46
	}
47
48
	public Object[] toArray(Object source, Object[] array) {
49
		return getCollection(source).toArray(array);
50
	}
51
52
	public Object[] toArray(Object source) {
53
		return getCollection(source).toArray();
54
	}
55
}
(-)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 updating = false;
31
	private boolean disposed = false;
32
33
	private IValuePropertyChangeListener listener = new IValuePropertyChangeListener() {
34
		public void handleValuePropertyChange(
35
				final ValuePropertyChangeEvent event) {
36
			if (!disposed && !updating) {
37
				getRealm().exec(new Runnable() {
38
					public void run() {
39
						fireMapChange(Diffs.createMapDiffSingleChange(event
40
								.getSource(), event.diff.getOldValue(),
41
								event.diff.getNewValue()));
42
					}
43
				});
44
			}
45
		}
46
	};
47
48
	/**
49
	 * @param keySet
50
	 * @param valueProperty
51
	 */
52
	public SetValuePropertyObservableMap(IObservableSet keySet,
53
			IValueProperty valueProperty) {
54
		super(keySet);
55
		this.property = valueProperty;
56
	}
57
58
	protected Object doGet(Object key) {
59
		return property.getValue(key);
60
	}
61
62
	protected Object doPut(Object key, Object value) {
63
		Object result = property.getValue(key);
64
		property.setValue(key, value);
65
		keySet().add(key);
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/internal/databinding/property/PropertyObservableValue.java (+110 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.Realm;
17
import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
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
import org.eclipse.core.internal.databinding.Util;
22
23
/**
24
 * @since 1.2
25
 * 
26
 */
27
public class PropertyObservableValue extends AbstractObservableValue implements
28
		IObserving {
29
	private Object source;
30
	private IValueProperty property;
31
32
	private boolean updating = false;
33
34
	private boolean disposed = false;
35
36
	private IValuePropertyChangeListener listener = new IValuePropertyChangeListener() {
37
		public void handleValuePropertyChange(
38
				final ValuePropertyChangeEvent event) {
39
			if (!disposed && !updating) {
40
				getRealm().exec(new Runnable() {
41
					public void run() {
42
						fireValueChange(event.diff);
43
					}
44
				});
45
			}
46
		}
47
	};
48
49
	/**
50
	 * @param realm
51
	 * @param source
52
	 * @param property
53
	 */
54
	public PropertyObservableValue(Realm realm, Object source,
55
			IValueProperty property) {
56
		super(realm);
57
		this.source = source;
58
		this.property = property;
59
	}
60
61
	protected void firstListenerAdded() {
62
		if (!disposed) {
63
			property.addValueChangeListener(source, listener);
64
		}
65
	}
66
67
	protected void lastListenerRemoved() {
68
		if (!disposed) {
69
			property.removeValueChangeListener(source, listener);
70
		}
71
	}
72
73
	protected Object doGetValue() {
74
		return property.getValue(source);
75
	}
76
77
	protected void doSetValue(Object value) {
78
		Object oldValue = doGetValue();
79
80
		updating = true;
81
		try {
82
			property.setValue(source, value);
83
		} finally {
84
			updating = false;
85
		}
86
87
		Object newValue = doGetValue();
88
		if (!Util.equals(oldValue, newValue)) {
89
			fireValueChange(Diffs.createValueDiff(oldValue, newValue));
90
		}
91
	}
92
93
	public Object getValueType() {
94
		return property.getValueType();
95
	}
96
97
	public Object getObserved() {
98
		return source;
99
	}
100
101
	public synchronized void dispose() {
102
		if (!disposed) {
103
			disposed = true;
104
			property.removeValueChangeListener(source, listener);
105
			property = null;
106
			source = null;
107
		}
108
		super.dispose();
109
	}
110
}
(-)src/org/eclipse/core/databinding/property/IListPropertyChangeListener.java (+22 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
 * @since 1.2
16
 */
17
public interface IListPropertyChangeListener extends IPropertyChangeListener {
18
	/**
19
	 * @param event
20
	 */
21
	public void handleListPropertyChange(ListPropertyChangeEvent event);
22
}
(-)src/org/eclipse/core/databinding/property/Property.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
 * @since 1.2
16
 * 
17
 */
18
public abstract class Property implements IProperty {
19
	private PropertyChangeSupport changeSupport;
20
21
	synchronized PropertyChangeSupport getChangeSupport() {
22
		if (changeSupport == null)
23
			changeSupport = new PropertyChangeSupport(this);
24
		return changeSupport;
25
	}
26
27
	/**
28
	 * Notifies the property that the first listener has been added for the
29
	 * given source object. Implementers should register a listener on the
30
	 * source object which fires an appropriate change event when a property
31
	 * change is observed on the source.
32
	 * 
33
	 * @param source
34
	 *            the source object to observe for property changes.
35
	 * @see #removeListenerFrom(Object)
36
	 */
37
	protected abstract void addListenerTo(Object source);
38
39
	/**
40
	 * Notifies the property that the last listener has been removed for the
41
	 * given source object. Implementers should unregister any previously
42
	 * registered listeners from the source.
43
	 * 
44
	 * @param source
45
	 *            the source object to stop observing for property changes.
46
	 * @see #addListenerTo(Object)
47
	 */
48
	protected abstract void removeListenerFrom(Object source);
49
50
	/**
51
	 * Returns whether this property has listeners registered for the given
52
	 * property source
53
	 * 
54
	 * @param source
55
	 *            the property source
56
	 * @return whether this property has listeners registered for the given
57
	 *         property source
58
	 */
59
	protected boolean hasListeners(Object source) {
60
		return changeSupport != null && changeSupport.hasListeners(source);
61
	}
62
63
	public synchronized void dispose() {
64
		if (changeSupport != null) {
65
			changeSupport.dispose();
66
			changeSupport = null;
67
		}
68
	}
69
}
(-)src/org/eclipse/core/internal/databinding/beans/BeanObservableSetDecorator.java (-15 / +10 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Matthew Hall - bug 194734
10
 ******************************************************************************/
11
 ******************************************************************************/
11
12
12
package org.eclipse.core.internal.databinding.beans;
13
package org.eclipse.core.internal.databinding.beans;
Lines 17-22 Link Here
17
18
18
import org.eclipse.core.databinding.beans.IBeanObservable;
19
import org.eclipse.core.databinding.beans.IBeanObservable;
19
import org.eclipse.core.databinding.observable.IChangeListener;
20
import org.eclipse.core.databinding.observable.IChangeListener;
21
import org.eclipse.core.databinding.observable.IObserving;
20
import org.eclipse.core.databinding.observable.IStaleListener;
22
import org.eclipse.core.databinding.observable.IStaleListener;
21
import org.eclipse.core.databinding.observable.Realm;
23
import org.eclipse.core.databinding.observable.Realm;
22
import org.eclipse.core.databinding.observable.set.IObservableSet;
24
import org.eclipse.core.databinding.observable.set.IObservableSet;
Lines 28-49 Link Here
28
 * 
30
 * 
29
 * @since 3.3
31
 * @since 3.3
30
 */
32
 */
31
public class BeanObservableSetDecorator implements IObservableSet, IBeanObservable {
33
public class BeanObservableSetDecorator implements IObservableSet,
34
		IBeanObservable {
32
	private IObservableSet delegate;
35
	private IObservableSet delegate;
33
	private Object observed;
34
	private PropertyDescriptor propertyDescriptor;
36
	private PropertyDescriptor propertyDescriptor;
35
37
36
	/**
38
	/**
37
	 * @param delegate 
39
	 * @param delegate
38
	 * @param observed 
39
	 * @param propertyDescriptor
40
	 * @param propertyDescriptor
40
	 */
41
	 */
41
	public BeanObservableSetDecorator(IObservableSet delegate,
42
	public BeanObservableSetDecorator(IObservableSet delegate,
42
			Object observed,
43
			PropertyDescriptor propertyDescriptor) {
43
			PropertyDescriptor propertyDescriptor) {
44
		
44
45
		this.delegate = delegate;
45
		this.delegate = delegate;
46
		this.observed = observed;
47
		this.propertyDescriptor = propertyDescriptor;
46
		this.propertyDescriptor = propertyDescriptor;
48
	}
47
	}
49
48
Lines 151-166 Link Here
151
		return delegate.toArray(a);
150
		return delegate.toArray(a);
152
	}
151
	}
153
152
154
	/* (non-Javadoc)
155
	 * @see org.eclipse.core.databinding.beans.IBeanObservable#getObserved()
156
	 */
157
	public Object getObserved() {
153
	public Object getObserved() {
158
		return observed;
154
		if (delegate instanceof IObserving)
155
			return ((IObserving) delegate).getObserved();
156
		return null;
159
	}
157
	}
160
158
161
	/* (non-Javadoc)
162
	 * @see org.eclipse.core.databinding.beans.IBeanObservable#getPropertyDescriptor()
163
	 */
164
	public PropertyDescriptor getPropertyDescriptor() {
159
	public PropertyDescriptor getPropertyDescriptor() {
165
		return propertyDescriptor;
160
		return propertyDescriptor;
166
	}
161
	}
Lines 170-174 Link Here
170
	 */
165
	 */
171
	public IObservableSet getDelegate() {
166
	public IObservableSet getDelegate() {
172
		return delegate;
167
		return delegate;
173
	}	
168
	}
174
}
169
}
(-)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/BeanObservableMapDecorator.java (-5 / +5 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation (bug 221704)
9
 *     Matthew Hall - initial API and implementation (bug 221704)
10
 *     Matthew Hall - bug 194734
10
 ******************************************************************************/
11
 ******************************************************************************/
11
12
12
package org.eclipse.core.internal.databinding.beans;
13
package org.eclipse.core.internal.databinding.beans;
Lines 18-23 Link Here
18
19
19
import org.eclipse.core.databinding.beans.IBeanObservable;
20
import org.eclipse.core.databinding.beans.IBeanObservable;
20
import org.eclipse.core.databinding.observable.IChangeListener;
21
import org.eclipse.core.databinding.observable.IChangeListener;
22
import org.eclipse.core.databinding.observable.IObserving;
21
import org.eclipse.core.databinding.observable.IStaleListener;
23
import org.eclipse.core.databinding.observable.IStaleListener;
22
import org.eclipse.core.databinding.observable.Realm;
24
import org.eclipse.core.databinding.observable.Realm;
23
import org.eclipse.core.databinding.observable.map.IMapChangeListener;
25
import org.eclipse.core.databinding.observable.map.IMapChangeListener;
Lines 31-50 Link Here
31
 */
33
 */
32
public class BeanObservableMapDecorator implements IObservableMap, IBeanObservable {
34
public class BeanObservableMapDecorator implements IObservableMap, IBeanObservable {
33
	private IObservableMap delegate;
35
	private IObservableMap delegate;
34
	private Object observed;
35
	private PropertyDescriptor propertyDescriptor;
36
	private PropertyDescriptor propertyDescriptor;
36
37
37
	/**
38
	/**
38
	 * @param delegate 
39
	 * @param delegate 
39
	 * @param observed 
40
	 * @param propertyDescriptor
40
	 * @param propertyDescriptor
41
	 */
41
	 */
42
	public BeanObservableMapDecorator(IObservableMap delegate,
42
	public BeanObservableMapDecorator(IObservableMap delegate,
43
			Object observed,
44
			PropertyDescriptor propertyDescriptor) {
43
			PropertyDescriptor propertyDescriptor) {
45
		
44
		
46
		this.delegate = delegate;
45
		this.delegate = delegate;
47
		this.observed = observed;
48
		this.propertyDescriptor = propertyDescriptor;
46
		this.propertyDescriptor = propertyDescriptor;
49
	}
47
	}
50
48
Lines 105-111 Link Here
105
	}
103
	}
106
104
107
	public Object getObserved() {
105
	public Object getObserved() {
108
		return observed;
106
		if (delegate instanceof IObserving)
107
			return ((IObserving) delegate).getObserved();
108
		return null;
109
	}
109
	}
110
110
111
	public PropertyDescriptor getPropertyDescriptor() {
111
	public PropertyDescriptor getPropertyDescriptor() {
(-)src/org/eclipse/core/internal/databinding/beans/BeanObservableValueDecorator.java (-18 / +15 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Matthew Hall - bug 194734
10
 ******************************************************************************/
11
 ******************************************************************************/
11
12
12
package org.eclipse.core.internal.databinding.beans;
13
package org.eclipse.core.internal.databinding.beans;
Lines 15-21 Link Here
15
16
16
import org.eclipse.core.databinding.beans.IBeanObservable;
17
import org.eclipse.core.databinding.beans.IBeanObservable;
17
import org.eclipse.core.databinding.observable.IChangeListener;
18
import org.eclipse.core.databinding.observable.IChangeListener;
19
import org.eclipse.core.databinding.observable.IObserving;
18
import org.eclipse.core.databinding.observable.IStaleListener;
20
import org.eclipse.core.databinding.observable.IStaleListener;
21
import org.eclipse.core.databinding.observable.ObservableTracker;
19
import org.eclipse.core.databinding.observable.Realm;
22
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
23
import org.eclipse.core.databinding.observable.value.IObservableValue;
21
import org.eclipse.core.databinding.observable.value.IValueChangeListener;
24
import org.eclipse.core.databinding.observable.value.IValueChangeListener;
Lines 30-46 Link Here
30
		IBeanObservable {
33
		IBeanObservable {
31
	private final IObservableValue delegate;
34
	private final IObservableValue delegate;
32
	private final PropertyDescriptor descriptor;
35
	private final PropertyDescriptor descriptor;
33
	private final IObservableValue observed;
34
36
35
	/**
37
	/**
36
	 * @param delegate
38
	 * @param delegate
37
	 * @param observed 
38
	 * @param descriptor
39
	 * @param descriptor
39
	 */
40
	 */
40
	public BeanObservableValueDecorator(IObservableValue delegate, IObservableValue observed,
41
	public BeanObservableValueDecorator(IObservableValue delegate,
41
			PropertyDescriptor descriptor) {
42
			PropertyDescriptor descriptor) {
42
		this.delegate = delegate;
43
		this.delegate = delegate;
43
		this.observed = observed;
44
		this.descriptor = descriptor;
44
		this.descriptor = descriptor;
45
	}
45
	}
46
46
Lines 59-65 Link Here
59
	public void dispose() {
59
	public void dispose() {
60
		delegate.dispose();
60
		delegate.dispose();
61
	}
61
	}
62
	
62
63
	public boolean equals(Object obj) {
63
	public boolean equals(Object obj) {
64
		if (obj instanceof BeanObservableValueDecorator) {
64
		if (obj instanceof BeanObservableValueDecorator) {
65
			BeanObservableValueDecorator other = (BeanObservableValueDecorator) obj;
65
			BeanObservableValueDecorator other = (BeanObservableValueDecorator) obj;
Lines 73-85 Link Here
73
	}
73
	}
74
74
75
	public Object getValue() {
75
	public Object getValue() {
76
		getterCalled();
76
		return delegate.getValue();
77
		return delegate.getValue();
77
	}
78
	}
78
79
80
	private void getterCalled() {
81
		ObservableTracker.getterCalled(this);
82
	}
83
79
	public Object getValueType() {
84
	public Object getValueType() {
80
		return delegate.getValueType();
85
		return delegate.getValueType();
81
	}
86
	}
82
	
87
83
	public int hashCode() {
88
	public int hashCode() {
84
		return delegate.hashCode();
89
		return delegate.hashCode();
85
	}
90
	}
Lines 104-127 Link Here
104
		delegate.setValue(value);
109
		delegate.setValue(value);
105
	}
110
	}
106
111
107
	/*
108
	 * (non-Javadoc)
109
	 * 
110
	 * @see org.eclipse.core.databinding.beans.IBeanObservable#getObserved()
111
	 */
112
	public Object getObserved() {
112
	public Object getObserved() {
113
		return observed.getValue();
113
		if (delegate instanceof IObserving)
114
			return ((IObserving) delegate).getObserved();
115
		return null;
114
	}
116
	}
115
117
116
	/*
117
	 * (non-Javadoc)
118
	 * 
119
	 * @see org.eclipse.core.databinding.beans.IBeanObservable#getPropertyDescriptor()
120
	 */
121
	public PropertyDescriptor getPropertyDescriptor() {
118
	public PropertyDescriptor getPropertyDescriptor() {
122
		return descriptor;
119
		return descriptor;
123
	}
120
	}
124
	
121
125
	/**
122
	/**
126
	 * @return observable value delegate
123
	 * @return observable value delegate
127
	 */
124
	 */
(-)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/internal/databinding/beans/BeanObservableListDecorator.java (-16 / +6 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Matthew Hall - bug 208858
10
 *     Matthew Hall - bugs 208858, 194734
11
 ******************************************************************************/
11
 ******************************************************************************/
12
12
13
package org.eclipse.core.internal.databinding.beans;
13
package org.eclipse.core.internal.databinding.beans;
Lines 19-24 Link Here
19
import java.util.ListIterator;
19
import java.util.ListIterator;
20
20
21
import org.eclipse.core.databinding.beans.IBeanObservable;
21
import org.eclipse.core.databinding.beans.IBeanObservable;
22
import org.eclipse.core.databinding.observable.IObserving;
22
import org.eclipse.core.databinding.observable.IStaleListener;
23
import org.eclipse.core.databinding.observable.IStaleListener;
23
import org.eclipse.core.databinding.observable.ObservableTracker;
24
import org.eclipse.core.databinding.observable.ObservableTracker;
24
import org.eclipse.core.databinding.observable.StaleEvent;
25
import org.eclipse.core.databinding.observable.StaleEvent;
Lines 39-57 Link Here
39
	private IStaleListener delegateStaleListener;
40
	private IStaleListener delegateStaleListener;
40
	private IListChangeListener delegateListChangeListener;
41
	private IListChangeListener delegateListChangeListener;
41
42
42
	private Object observed;
43
	private PropertyDescriptor propertyDescriptor;
43
	private PropertyDescriptor propertyDescriptor;
44
44
45
	/**
45
	/**
46
	 * @param delegate
46
	 * @param delegate
47
	 * @param observed
48
	 * @param propertyDescriptor
47
	 * @param propertyDescriptor
49
	 */
48
	 */
50
	public BeanObservableListDecorator(IObservableList delegate,
49
	public BeanObservableListDecorator(IObservableList delegate,
51
			Object observed, PropertyDescriptor propertyDescriptor) {
50
			PropertyDescriptor propertyDescriptor) {
52
		super(delegate.getRealm());
51
		super(delegate.getRealm());
53
		this.delegate = delegate;
52
		this.delegate = delegate;
54
		this.observed = observed;
55
		this.propertyDescriptor = propertyDescriptor;
53
		this.propertyDescriptor = propertyDescriptor;
56
	}
54
	}
57
55
Lines 205-224 Link Here
205
		return delegate;
203
		return delegate;
206
	}
204
	}
207
205
208
	/*
209
	 * (non-Javadoc)
210
	 * 
211
	 * @see org.eclipse.core.databinding.beans.IBeanObservable#getObserved()
212
	 */
213
	public Object getObserved() {
206
	public Object getObserved() {
214
		return observed;
207
		if (delegate instanceof IObserving)
208
			return ((IObserving) delegate).getObserved();
209
		return null;
215
	}
210
	}
216
211
217
	/*
218
	 * (non-Javadoc)
219
	 * 
220
	 * @see org.eclipse.core.databinding.beans.IBeanObservable#getPropertyDescriptor()
221
	 */
222
	public PropertyDescriptor getPropertyDescriptor() {
212
	public PropertyDescriptor getPropertyDescriptor() {
223
		return propertyDescriptor;
213
		return propertyDescriptor;
224
	}
214
	}
(-)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
11
 *     Matthew Hall - bugs 221704, 234686, 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.observeMap(
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
				realm, 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 439-449 Link Here
439
424
440
		IObservableValue value = MasterDetailObservables.detailValue(master,
425
		IObservableValue value = MasterDetailObservables.detailValue(master,
441
				valueFactory(realm, propertyName), propertyType);
426
				valueFactory(realm, propertyName), propertyType);
442
		BeanObservableValueDecorator decorator = new BeanObservableValueDecorator(
427
		return new BeanObservableValueDecorator(value, BeanPropertyHelper
443
				value, master, getValueTypePropertyDescriptor(master,
428
				.getValueTypePropertyDescriptor(master, propertyName));
444
						propertyName));
445
446
		return decorator;
447
	}
429
	}
448
430
449
	/**
431
	/**
Lines 469-478 Link Here
469
	/**
451
	/**
470
	 * Helper method for
452
	 * Helper method for
471
	 * <code>MasterDetailObservables.detailValue(master, valueFactory(realm,
453
	 * <code>MasterDetailObservables.detailValue(master, valueFactory(realm,
472
	 * propertyName), propertyType)</code>.
454
	 * propertyName), propertyType)</code>. This method returns an
473
	 * This method returns an {@link IBeanObservable} with a
455
	 * {@link IBeanObservable} with a {@link PropertyDescriptor} based on the
474
	 * {@link PropertyDescriptor} based on the given master type and property
456
	 * given master type and property name.
475
	 * name.
476
	 * 
457
	 * 
477
	 * @param realm
458
	 * @param realm
478
	 *            the realm
459
	 *            the realm
Lines 492-506 Link Here
492
	 * @since 1.1
473
	 * @since 1.1
493
	 */
474
	 */
494
	public static IObservableValue observeDetailValue(Realm realm,
475
	public static IObservableValue observeDetailValue(Realm realm,
495
			IObservableValue master, Class masterType, String propertyName, Class propertyType) {
476
			IObservableValue master, Class masterType, String propertyName,
477
			Class propertyType) {
496
		Assert.isNotNull(masterType, "masterType cannot be null"); //$NON-NLS-1$
478
		Assert.isNotNull(masterType, "masterType cannot be null"); //$NON-NLS-1$
497
		IObservableValue value = MasterDetailObservables.detailValue(master,
479
		IObservableValue value = MasterDetailObservables.detailValue(master,
498
				valueFactory(realm, propertyName), propertyType);
480
				valueFactory(realm, propertyName), propertyType);
499
		BeanObservableValueDecorator decorator = new BeanObservableValueDecorator(
481
		return new BeanObservableValueDecorator(value, BeanPropertyHelper
500
				value, master, getPropertyDescriptor(masterType,
482
				.getPropertyDescriptor(masterType, propertyName));
501
						propertyName));
502
503
		return decorator;
504
	}
483
	}
505
484
506
	/**
485
	/**
Lines 551-561 Link Here
551
		IObservableList observableList = MasterDetailObservables.detailList(
530
		IObservableList observableList = MasterDetailObservables.detailList(
552
				master, listFactory(realm, propertyName, propertyType),
531
				master, listFactory(realm, propertyName, propertyType),
553
				propertyType);
532
				propertyType);
554
		BeanObservableListDecorator decorator = new BeanObservableListDecorator(
533
		return new BeanObservableListDecorator(observableList,
555
				observableList, master, getValueTypePropertyDescriptor(master,
534
				BeanPropertyHelper.getValueTypePropertyDescriptor(master,
556
						propertyName));
535
						propertyName));
557
558
		return decorator;
559
	}
536
	}
560
537
561
	/**
538
	/**
Lines 599-609 Link Here
599
		IObservableSet observableSet = MasterDetailObservables.detailSet(
576
		IObservableSet observableSet = MasterDetailObservables.detailSet(
600
				master, setFactory(realm, propertyName, propertyType),
577
				master, setFactory(realm, propertyName, propertyType),
601
				propertyType);
578
				propertyType);
602
		BeanObservableSetDecorator decorator = new BeanObservableSetDecorator(
579
		return new BeanObservableSetDecorator(observableSet, BeanPropertyHelper
603
				observableSet, master, getValueTypePropertyDescriptor(master,
580
				.getValueTypePropertyDescriptor(master, propertyName));
604
						propertyName));
605
606
		return decorator;
607
	}
581
	}
608
582
609
	/**
583
	/**
Lines 642-651 Link Here
642
			IObservableValue master, String propertyName) {
616
			IObservableValue master, String propertyName) {
643
		IObservableMap observableMap = MasterDetailObservables.detailMap(
617
		IObservableMap observableMap = MasterDetailObservables.detailMap(
644
				master, mapPropertyFactory(realm, propertyName));
618
				master, mapPropertyFactory(realm, propertyName));
645
		BeanObservableMapDecorator decorator = new BeanObservableMapDecorator(
619
		return new BeanObservableMapDecorator(observableMap, BeanPropertyHelper
646
				observableMap, master, getValueTypePropertyDescriptor(master,
620
				.getValueTypePropertyDescriptor(master, propertyName));
647
						propertyName));
648
		return decorator;
649
	}
621
	}
650
622
651
	/**
623
	/**
Lines 688-699 Link Here
688
	 */
660
	 */
689
	public static IObservableSet observeSet(Realm realm, Object bean,
661
	public static IObservableSet observeSet(Realm realm, Object bean,
690
			String propertyName, Class elementType) {
662
			String propertyName, Class elementType) {
691
		PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean
663
		ISetProperty property = BeanProperties.setProperty(bean.getClass(),
692
				.getClass(), propertyName);
664
				propertyName, elementType);
693
		elementType = getCollectionElementType(elementType, propertyDescriptor);
665
		PropertyDescriptor propertyDescriptor = ((IBeanProperty) property)
694
666
				.getPropertyDescriptor();
695
		return new JavaBeanObservableSet(realm, bean, propertyDescriptor,
667
		return new BeanObservableSetDecorator(PropertyObservables.observeSet(
696
				elementType);
668
				realm, bean, property), propertyDescriptor);
697
	}
669
	}
698
670
699
	/**
671
	/**
Lines 780-796 Link Here
780
	 * @param propertyName
752
	 * @param propertyName
781
	 *            the name of the property
753
	 *            the name of the property
782
	 * @return a factory for creating {@link IObservableMap} objects
754
	 * @return a factory for creating {@link IObservableMap} objects
783
	 *
755
	 * 
784
	 * @since 1.1
756
	 * @since 1.1
785
	 */
757
	 */
786
	public static IObservableFactory setToMapFactory(final Class beanClass, final String propertyName) {
758
	public static IObservableFactory setToMapFactory(final Class beanClass,
759
			final String propertyName) {
787
		return new IObservableFactory() {
760
		return new IObservableFactory() {
788
			public IObservable createObservable(Object target) {
761
			public IObservable createObservable(Object target) {
789
				return observeMap((IObservableSet) target, beanClass, propertyName);
762
				return observeMap((IObservableSet) target, beanClass,
763
						propertyName);
790
			}
764
			}
791
		};
765
		};
792
	}
766
	}
793
	
767
794
	/**
768
	/**
795
	 * Returns a factory for creating an observable map. The factory, when
769
	 * Returns a factory for creating an observable map. The factory, when
796
	 * provided with a bean object, will create an {@link IObservableMap} in the
770
	 * provided with a bean object, will create an {@link IObservableMap} in the
Lines 828-859 Link Here
828
	public static IObservableFactory mapPropertyFactory(String propertyName) {
802
	public static IObservableFactory mapPropertyFactory(String propertyName) {
829
		return mapPropertyFactory(Realm.getDefault(), propertyName);
803
		return mapPropertyFactory(Realm.getDefault(), propertyName);
830
	}
804
	}
831
832
	/**
833
	 * @param elementType
834
	 *            can be <code>null</code>
835
	 * @param propertyDescriptor
836
	 * @return type of the items in a collection/array property
837
	 */
838
	/*package*/ static Class getCollectionElementType(Class elementType,
839
			PropertyDescriptor propertyDescriptor) {
840
		if (elementType == null) {
841
			Class propertyType = propertyDescriptor.getPropertyType();
842
			elementType = propertyType.isArray() ? propertyType
843
					.getComponentType() : Object.class;
844
		}
845
846
		return elementType;
847
	}
848
849
	/**
850
	 * @param observable
851
	 * @param propertyName
852
	 * @return property descriptor or <code>null</code>
853
	 */
854
	/* package*/ static PropertyDescriptor getValueTypePropertyDescriptor(
855
			IObservableValue observable, String propertyName) {
856
		return (observable.getValueType() != null) ? getPropertyDescriptor(
857
				(Class) observable.getValueType(), propertyName) : null;
858
	}
859
}
805
}
(-)src/org/eclipse/core/databinding/beans/PojoObservables.java (-54 / +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
10
 *     Matthew Hall - bugs 221704, 234686, 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.observeMap(
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
				realm, 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 540-550 Link Here
540
547
541
		IObservableValue value = MasterDetailObservables.detailValue(master,
548
		IObservableValue value = MasterDetailObservables.detailValue(master,
542
				valueFactory(realm, propertyName), propertyType);
549
				valueFactory(realm, propertyName), propertyType);
543
		BeanObservableValueDecorator decorator = new BeanObservableValueDecorator(
550
		return new BeanObservableValueDecorator(value, BeanPropertyHelper
544
				value, master, BeansObservables.getValueTypePropertyDescriptor(
551
				.getValueTypePropertyDescriptor(master, propertyName));
545
						master, propertyName));
546
547
		return decorator;
548
	}
552
	}
549
553
550
	/**
554
	/**
Lines 587-597 Link Here
587
		IObservableList observableList = MasterDetailObservables.detailList(
591
		IObservableList observableList = MasterDetailObservables.detailList(
588
				master, listFactory(realm, propertyName, propertyType),
592
				master, listFactory(realm, propertyName, propertyType),
589
				propertyType);
593
				propertyType);
590
		BeanObservableListDecorator decorator = new BeanObservableListDecorator(
594
		return new BeanObservableListDecorator(observableList,
591
				observableList, master, BeansObservables
595
				BeanPropertyHelper.getValueTypePropertyDescriptor(master,
592
						.getValueTypePropertyDescriptor(master, propertyName));
596
						propertyName));
593
594
		return decorator;
595
	}
597
	}
596
598
597
	/**
599
	/**
Lines 635-645 Link Here
635
		IObservableSet observableSet = MasterDetailObservables.detailSet(
637
		IObservableSet observableSet = MasterDetailObservables.detailSet(
636
				master, setFactory(realm, propertyName, propertyType),
638
				master, setFactory(realm, propertyName, propertyType),
637
				propertyType);
639
				propertyType);
638
		BeanObservableSetDecorator decorator = new BeanObservableSetDecorator(
640
		return new BeanObservableSetDecorator(observableSet, BeanPropertyHelper
639
				observableSet, master, BeansObservables
641
				.getValueTypePropertyDescriptor(master, propertyName));
640
						.getValueTypePropertyDescriptor(master, propertyName));
641
642
		return decorator;
643
	}
642
	}
644
643
645
	/**
644
	/**
Lines 676-685 Link Here
676
			IObservableValue master, String propertyName) {
675
			IObservableValue master, String propertyName) {
677
		IObservableMap observableMap = MasterDetailObservables.detailMap(
676
		IObservableMap observableMap = MasterDetailObservables.detailMap(
678
				master, mapPropertyFactory(realm, propertyName));
677
				master, mapPropertyFactory(realm, propertyName));
679
		BeanObservableMapDecorator decorator = new BeanObservableMapDecorator(
678
		return new BeanObservableMapDecorator(observableMap, BeanPropertyHelper
680
				observableMap, master, BeansObservables
679
				.getValueTypePropertyDescriptor(master, propertyName));
681
						.getValueTypePropertyDescriptor(master, propertyName));
682
		return decorator;
683
	}
680
	}
684
681
685
	/**
682
	/**
(-)src/org/eclipse/core/databinding/beans/IBeanProperty.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.beans;
13
14
import java.beans.PropertyDescriptor;
15
16
import org.eclipse.core.databinding.property.IProperty;
17
18
/**
19
 * @since 1.2
20
 * 
21
 */
22
public interface IBeanProperty extends IProperty {
23
	/**
24
	 * @return property descriptor for the bean property
25
	 */
26
	public PropertyDescriptor getPropertyDescriptor();
27
}
(-)src/org/eclipse/core/internal/databinding/beans/PropertyUpdateHelper.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.internal.databinding.beans;
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
	boolean isUpdating(Object source) {
29
		Map map = (Map) localMap.get();
30
		return map.containsKey(source);
31
	}
32
33
	void setUpdating(Object source, boolean state) {
34
		Map map = (Map) localMap.get();
35
		Integer count = (Integer) map.get(source);
36
		int newCount = (count == null ? 0 : count.intValue())
37
				+ (state ? 1 : -1);
38
		if (newCount > 0)
39
			map.put(source, new Integer(newCount));
40
		else
41
			map.remove(source);
42
	}
43
44
	void dispose() {
45
		if (localMap != null) {
46
			Map map = (Map) localMap.get();
47
			if (map != null) {
48
				map.clear();
49
				localMap.set(null);
50
			}
51
			localMap = null;
52
		}
53
	}
54
}
(-)src/org/eclipse/core/internal/databinding/beans/BeanMapProperty.java (+206 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.MapProperty;
24
import org.eclipse.core.internal.databinding.Util;
25
26
/**
27
 * @since 3.3
28
 * 
29
 */
30
public class BeanMapProperty extends MapProperty implements IBeanProperty {
31
	private PropertyDescriptor propertyDescriptor;
32
	private boolean attachListener;
33
34
	private ListenerSupport listenerSupport;
35
36
	private PropertyUpdateHelper updateHelper = new PropertyUpdateHelper();
37
38
	private Map sourceToCachedMap = Collections.synchronizedMap(new HashMap());
39
40
	/**
41
	 * @param propertyDescriptor
42
	 * @param attachListener
43
	 */
44
	public BeanMapProperty(PropertyDescriptor propertyDescriptor,
45
			boolean attachListener) {
46
		this.propertyDescriptor = propertyDescriptor;
47
		this.attachListener = attachListener;
48
	}
49
50
	private void initListenerSupport() {
51
		if (listenerSupport == null) {
52
			synchronized (this) {
53
				if (listenerSupport == null) {
54
					PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
55
						public void propertyChange(PropertyChangeEvent evt) {
56
							Object source = evt.getSource();
57
							if (!updateHelper.isUpdating(source)) {
58
								Object oldValue = evt.getOldValue();
59
								Object newValue = evt.getNewValue();
60
								if (oldValue == null && newValue == null) {
61
									// Obscure condition indicating new and old
62
									// values are unknown
63
									oldValue = sourceToCachedMap.get(source);
64
									newValue = BeanPropertyHelper.getProperty(
65
											source, propertyDescriptor);
66
								}
67
								sourceToCachedMap.put(source, newValue);
68
								fireMapChange(source, Diffs.computeMapDiff(
69
										asMap(oldValue), asMap(newValue)));
70
							}
71
						}
72
					};
73
					listenerSupport = new ListenerSupport(
74
							propertyChangeListener, propertyDescriptor
75
									.getName());
76
				}
77
			}
78
		}
79
	}
80
81
	protected void addListenerTo(Object source) {
82
		if (source != null && attachListener) {
83
			initListenerSupport();
84
			listenerSupport.hookListener(source);
85
			sourceToCachedMap.put(source, BeanPropertyHelper.getProperty(
86
					source, propertyDescriptor));
87
		}
88
	}
89
90
	protected void removeListenerFrom(Object source) {
91
		if (source != null && attachListener && listenerSupport != null) {
92
			listenerSupport.unhookListener(source);
93
			sourceToCachedMap.remove(source);
94
		}
95
	}
96
97
	public Map getMap(Object source) {
98
		if (source == null)
99
			return Collections.EMPTY_MAP;
100
		Object propertyValue = BeanPropertyHelper.getProperty(source,
101
				propertyDescriptor);
102
		return asMap(propertyValue);
103
	}
104
105
	private Map asMap(Object propertyValue) {
106
		if (propertyValue == null)
107
			return new HashMap();
108
		return (Map) propertyValue;
109
	}
110
111
	private void setMap(Object source, Map map) {
112
		if (source == null)
113
			return;
114
115
		Map oldMap = getMap(source);
116
117
		updateHelper.setUpdating(source, true);
118
		try {
119
			BeanPropertyHelper.setProperty(source, propertyDescriptor, map);
120
		} finally {
121
			updateHelper.setUpdating(source, false);
122
		}
123
124
		if (hasListeners(source)) {
125
			Map newMap = getMap(source);
126
			if (!Util.equals(oldMap, newMap)) {
127
				sourceToCachedMap.put(source, newMap);
128
				fireMapChange(source, Diffs.computeMapDiff(oldMap, newMap));
129
			}
130
		}
131
	}
132
133
	public void clear(Object source) {
134
		setMap(source, new HashMap());
135
	}
136
137
	public boolean containsKey(Object source, Object key) {
138
		return getMap(source).containsKey(key);
139
	}
140
141
	public boolean containsValue(Object source, Object value) {
142
		return getMap(source).containsValue(value);
143
	}
144
145
	public boolean equals(Object source, Object o) {
146
		return getMap(source).equals(o);
147
	}
148
149
	public Object get(Object source, Object key) {
150
		return getMap(source).get(key);
151
	}
152
153
	public int hashCode(Object source) {
154
		return getMap(source).hashCode();
155
	}
156
157
	public boolean isEmpty(Object source) {
158
		return getMap(source).isEmpty();
159
	}
160
161
	public Object put(Object source, Object key, Object value) {
162
		Map map = new HashMap(getMap(source));
163
		Object result = map.put(key, value);
164
		setMap(source, map);
165
		return result;
166
	}
167
168
	public void putAll(Object source, Map t) {
169
		Map map = new HashMap(getMap(source));
170
		map.putAll(t);
171
		setMap(source, map);
172
	}
173
174
	public Object remove(Object source, Object key) {
175
		Map map = new HashMap(getMap(source));
176
		Object result = map.remove(key);
177
		setMap(source, map);
178
		return result;
179
	}
180
181
	public int size(Object source) {
182
		return getMap(source).size();
183
	}
184
185
	public synchronized void dispose() {
186
		propertyDescriptor = null;
187
		attachListener = false;
188
		if (listenerSupport != null) {
189
			listenerSupport.dispose();
190
			listenerSupport = null;
191
		}
192
		if (updateHelper != null) {
193
			updateHelper.dispose();
194
			updateHelper = null;
195
		}
196
		if (sourceToCachedMap != null) {
197
			sourceToCachedMap.clear();
198
			sourceToCachedMap = null;
199
		}
200
		super.dispose();
201
	}
202
203
	public PropertyDescriptor getPropertyDescriptor() {
204
		return propertyDescriptor;
205
	}
206
}
(-)src/org/eclipse/core/internal/databinding/beans/BeanValueProperty.java (+155 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.ValueProperty;
24
import org.eclipse.core.internal.databinding.Util;
25
26
/**
27
 * @since 3.3
28
 * 
29
 */
30
public class BeanValueProperty extends ValueProperty implements IBeanProperty {
31
	private PropertyDescriptor propertyDescriptor;
32
	private Class valueType;
33
	private boolean attachListener;
34
35
	private ListenerSupport listenerSupport;
36
37
	private PropertyUpdateHelper updateHelper = new PropertyUpdateHelper();
38
39
	private Map sourceToCachedValue = Collections
40
			.synchronizedMap(new HashMap());
41
42
	/**
43
	 * @param propertyDescriptor
44
	 * @param valueType
45
	 * @param attachListener
46
	 */
47
	public BeanValueProperty(PropertyDescriptor propertyDescriptor,
48
			Class valueType, boolean attachListener) {
49
		this.propertyDescriptor = propertyDescriptor;
50
		this.valueType = valueType == null ? propertyDescriptor
51
				.getPropertyType() : valueType;
52
		this.attachListener = attachListener;
53
	}
54
55
	private void initListenerSupport() {
56
		if (listenerSupport == null) {
57
			synchronized (this) {
58
				if (listenerSupport == null) {
59
					PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
60
						public void propertyChange(PropertyChangeEvent evt) {
61
							Object source = evt.getSource();
62
							if (!updateHelper.isUpdating(source)) {
63
								Object oldValue = evt.getOldValue();
64
								Object newValue = evt.getNewValue();
65
								if (oldValue == null && newValue == null) {
66
									// Obscure condition indicating new and old
67
									// values are unknown
68
									oldValue = sourceToCachedValue.get(source);
69
									newValue = getValue(source);
70
								}
71
								sourceToCachedValue.put(source, newValue);
72
								fireValueChange(source, Diffs.createValueDiff(
73
										oldValue, newValue));
74
							}
75
						}
76
					};
77
					listenerSupport = new ListenerSupport(
78
							propertyChangeListener, propertyDescriptor
79
									.getName());
80
				}
81
			}
82
		}
83
	}
84
85
	protected void addListenerTo(Object source) {
86
		if (source != null && attachListener) {
87
			initListenerSupport();
88
			listenerSupport.hookListener(source);
89
			sourceToCachedValue.put(source, getValue(source));
90
		}
91
	}
92
93
	protected void removeListenerFrom(Object source) {
94
		if (source != null && attachListener && listenerSupport != null) {
95
			listenerSupport.unhookListener(source);
96
			sourceToCachedValue.remove(source);
97
		}
98
	}
99
100
	public Object getValue(Object source) {
101
		if (source == null)
102
			return null;
103
		return BeanPropertyHelper.getProperty(source, propertyDescriptor);
104
	}
105
106
	public void setValue(Object source, Object value) {
107
		if (source == null)
108
			return;
109
110
		Object oldValue = getValue(source);
111
112
		updateHelper.setUpdating(source, true);
113
		try {
114
			BeanPropertyHelper.setProperty(source, propertyDescriptor, value);
115
		} finally {
116
			updateHelper.setUpdating(source, false);
117
		}
118
119
		if (hasListeners(source)) {
120
			Object newValue = getValue(source);
121
			if (!Util.equals(oldValue, newValue)) {
122
				sourceToCachedValue.put(source, newValue);
123
				fireValueChange(source, Diffs.createValueDiff(oldValue,
124
						newValue));
125
			}
126
		}
127
	}
128
129
	public Object getValueType() {
130
		return valueType;
131
	}
132
133
	public PropertyDescriptor getPropertyDescriptor() {
134
		return propertyDescriptor;
135
	}
136
137
	public synchronized void dispose() {
138
		propertyDescriptor = null;
139
		valueType = null;
140
		attachListener = false;
141
		if (listenerSupport != null) {
142
			listenerSupport.dispose();
143
			listenerSupport = null;
144
		}
145
		if (updateHelper != null) {
146
			updateHelper.dispose();
147
			updateHelper = null;
148
		}
149
		if (sourceToCachedValue != null) {
150
			sourceToCachedValue.clear();
151
			sourceToCachedValue = null;
152
		}
153
		super.dispose();
154
	}
155
}
(-)src/org/eclipse/core/databinding/beans/PojoProperties.java (+109 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
 * @since 1.2
26
 */
27
public class PojoProperties {
28
	/**
29
	 * @param beanClass
30
	 * @param propertyName
31
	 * @return a value property for the given property name of the given bean
32
	 *         class.
33
	 */
34
	public static IValueProperty valueProperty(Class beanClass,
35
			String propertyName) {
36
		return valueProperty(beanClass, propertyName, null);
37
	}
38
39
	/**
40
	 * @param beanClass
41
	 * @param propertyName
42
	 * @param valueType
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, Class valueType) {
48
		return new BeanValueProperty(BeanPropertyHelper.getPropertyDescriptor(
49
				beanClass, propertyName), valueType, false);
50
	}
51
52
	/**
53
	 * @param beanClass
54
	 * @param propertyName
55
	 * @return a list property for the given property name of the given bean
56
	 *         class.
57
	 */
58
	public static ISetProperty setProperty(Class beanClass, String propertyName) {
59
		return setProperty(beanClass, propertyName, null);
60
	}
61
62
	/**
63
	 * @param beanClass
64
	 * @param propertyName
65
	 * @param elementType
66
	 * @return a list property for the given property name of the given bean
67
	 *         class.
68
	 */
69
	public static ISetProperty setProperty(Class beanClass,
70
			String propertyName, Class elementType) {
71
		return new BeanSetProperty(BeanPropertyHelper.getPropertyDescriptor(
72
				beanClass, propertyName), elementType, false);
73
	}
74
75
	/**
76
	 * @param beanClass
77
	 * @param propertyName
78
	 * @return a list property for the given property name of the given bean
79
	 *         class.
80
	 */
81
	public static IListProperty listProperty(Class beanClass,
82
			String propertyName) {
83
		return listProperty(beanClass, propertyName, null);
84
	}
85
86
	/**
87
	 * @param beanClass
88
	 * @param propertyName
89
	 * @param elementType
90
	 * @return a list property for the given property name of the given bean
91
	 *         class.
92
	 */
93
	public static IListProperty listProperty(Class beanClass,
94
			String propertyName, Class elementType) {
95
		return new BeanListProperty(BeanPropertyHelper.getPropertyDescriptor(
96
				beanClass, propertyName), elementType, false);
97
	}
98
99
	/**
100
	 * @param beanClass
101
	 * @param propertyName
102
	 * @return a map property for the given property name of the given bean
103
	 *         class.
104
	 */
105
	public static IMapProperty mapProperty(Class beanClass, String propertyName) {
106
		return new BeanMapProperty(BeanPropertyHelper.getPropertyDescriptor(
107
				beanClass, propertyName), false);
108
	}
109
}
(-)src/org/eclipse/core/internal/databinding/beans/BeanListProperty.java (+265 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.Collection;
21
import java.util.Collections;
22
import java.util.HashMap;
23
import java.util.List;
24
import java.util.Map;
25
26
import org.eclipse.core.databinding.beans.IBeanProperty;
27
import org.eclipse.core.databinding.observable.Diffs;
28
import org.eclipse.core.databinding.property.ListProperty;
29
import org.eclipse.core.internal.databinding.Util;
30
31
/**
32
 * @since 3.3
33
 * 
34
 */
35
public class BeanListProperty extends ListProperty implements IBeanProperty {
36
	private PropertyDescriptor propertyDescriptor;
37
	private Class elementType;
38
	private boolean attachListener;
39
40
	private ListenerSupport listenerSupport;
41
42
	private PropertyUpdateHelper updateHelper = new PropertyUpdateHelper();
43
44
	private Map sourceToCachedList = Collections.synchronizedMap(new HashMap());
45
46
	/**
47
	 * @param propertyDescriptor
48
	 * @param elementType
49
	 * @param attachListener
50
	 */
51
	public BeanListProperty(PropertyDescriptor propertyDescriptor,
52
			Class elementType, boolean attachListener) {
53
		this.propertyDescriptor = propertyDescriptor;
54
		this.elementType = elementType == null ? BeanPropertyHelper
55
				.getCollectionPropertyElementType(propertyDescriptor)
56
				: elementType;
57
		this.attachListener = attachListener;
58
	}
59
60
	private void initListenerSupport() {
61
		if (listenerSupport == null) {
62
			synchronized (this) {
63
				if (listenerSupport == null) {
64
					PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
65
						public void propertyChange(PropertyChangeEvent evt) {
66
							Object source = evt.getSource();
67
							if (!updateHelper.isUpdating(source)) {
68
								Object oldValue = evt.getOldValue();
69
								Object newValue = evt.getNewValue();
70
								if (oldValue == null && newValue == null) {
71
									// Obscure condition indicating new and old
72
									// values are unknown
73
									oldValue = sourceToCachedList.get(source);
74
									newValue = BeanPropertyHelper.getProperty(
75
											source, propertyDescriptor);
76
								}
77
								sourceToCachedList.put(source, newValue);
78
								fireListChange(source, Diffs.computeListDiff(
79
										asList(oldValue), asList(newValue)));
80
							}
81
						}
82
					};
83
					listenerSupport = new ListenerSupport(
84
							propertyChangeListener, propertyDescriptor
85
									.getName());
86
				}
87
			}
88
		}
89
	}
90
91
	protected void addListenerTo(Object source) {
92
		if (source != null && attachListener) {
93
			initListenerSupport();
94
			listenerSupport.hookListener(source);
95
			sourceToCachedList.put(source, BeanPropertyHelper.getProperty(
96
					source, propertyDescriptor));
97
		}
98
	}
99
100
	protected void removeListenerFrom(Object source) {
101
		if (source != null && attachListener && listenerSupport != null) {
102
			listenerSupport.unhookListener(source);
103
			sourceToCachedList.remove(source);
104
		}
105
	}
106
107
	public List getList(Object source) {
108
		if (source == null)
109
			return Collections.EMPTY_LIST;
110
		Object propertyValue = BeanPropertyHelper.getProperty(source,
111
				propertyDescriptor);
112
		return asList(propertyValue);
113
	}
114
115
	private List asList(Object propertyValue) {
116
		if (propertyValue == null)
117
			return new ArrayList();
118
		if (propertyDescriptor.getPropertyType().isArray())
119
			return new ArrayList(Arrays.asList((Object[]) propertyValue));
120
		return (List) propertyValue;
121
	}
122
123
	private void setList(Object source, List list) {
124
		if (source == null)
125
			return;
126
127
		List oldList = getList(source);
128
129
		updateHelper.setUpdating(source, true);
130
		try {
131
			BeanPropertyHelper.setProperty(source, propertyDescriptor,
132
					convertListToBeanPropertyType(list));
133
		} finally {
134
			updateHelper.setUpdating(source, false);
135
		}
136
137
		if (hasListeners(source)) {
138
			List newList = getList(source);
139
			if (!Util.equals(oldList, newList)) {
140
				sourceToCachedList.put(source, newList);
141
				fireListChange(source, Diffs.computeListDiff(oldList, newList));
142
			}
143
		}
144
	}
145
146
	private Object convertListToBeanPropertyType(List list) {
147
		Object propertyValue = list;
148
		if (propertyDescriptor.getPropertyType().isArray()) {
149
			Class componentType = propertyDescriptor.getPropertyType()
150
					.getComponentType();
151
			Object[] array = (Object[]) Array.newInstance(componentType, list
152
					.size());
153
			list.toArray(array);
154
			propertyValue = array;
155
		}
156
		return propertyValue;
157
	}
158
159
	public boolean add(Object source, Object o) {
160
		List list = new ArrayList(getList(source));
161
		boolean result = list.add(o);
162
		setList(source, list);
163
		return result;
164
	}
165
166
	public boolean addAll(Object source, Collection c) {
167
		List list = new ArrayList(getList(source));
168
		list.addAll(c);
169
		setList(source, list);
170
		return true;
171
	}
172
173
	public void clear(Object source) {
174
		setList(source, new ArrayList());
175
	}
176
177
	public boolean remove(Object source, Object o) {
178
		List list = new ArrayList(getList(source));
179
		boolean removed = list.remove(o);
180
		if (removed) {
181
			setList(source, list);
182
		}
183
		return removed;
184
	}
185
186
	public boolean removeAll(Object source, Collection c) {
187
		List list = new ArrayList(getList(source));
188
		boolean changed = list.removeAll(c);
189
		if (changed) {
190
			setList(source, list);
191
		}
192
		return changed;
193
	}
194
195
	public boolean retainAll(Object source, Collection c) {
196
		List list = new ArrayList(getList(source));
197
		boolean changed = list.retainAll(c);
198
		if (changed) {
199
			setList(source, list);
200
		}
201
		return changed;
202
	}
203
204
	public void add(Object source, int index, Object element) {
205
		List list = new ArrayList(getList(source));
206
		list.add(index, element);
207
		setList(source, list);
208
	}
209
210
	public boolean addAll(Object source, int index, Collection c) {
211
		List list = new ArrayList(getList(source));
212
		boolean changed = list.addAll(index, c);
213
		setList(source, list);
214
		return changed;
215
	}
216
217
	public Object remove(Object source, int index) {
218
		List list = new ArrayList(getList(source));
219
		Object result = list.remove(index);
220
		setList(source, list);
221
		return result;
222
	}
223
224
	public Object set(Object source, int index, Object element) {
225
		List list = new ArrayList(getList(source));
226
		Object result = list.set(index, element);
227
		setList(source, list);
228
		return result;
229
	}
230
231
	public Object move(Object source, int oldIndex, int newIndex) {
232
		List list = new ArrayList(getList(source));
233
		Object result = list.remove(oldIndex);
234
		list.add(newIndex, result);
235
		setList(source, list);
236
		return result;
237
	}
238
239
	public Object getElementType() {
240
		return elementType;
241
	}
242
243
	public PropertyDescriptor getPropertyDescriptor() {
244
		return propertyDescriptor;
245
	}
246
247
	public synchronized void dispose() {
248
		propertyDescriptor = null;
249
		elementType = null;
250
		attachListener = false;
251
		if (listenerSupport != null) {
252
			listenerSupport.dispose();
253
			listenerSupport = null;
254
		}
255
		if (updateHelper != null) {
256
			updateHelper.dispose();
257
			updateHelper = null;
258
		}
259
		if (sourceToCachedList != null) {
260
			sourceToCachedList.clear();
261
			sourceToCachedList = null;
262
		}
263
		super.dispose();
264
	}
265
}
(-)src/org/eclipse/core/internal/databinding/beans/BeanSetProperty.java (+232 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.Collection;
20
import java.util.Collections;
21
import java.util.HashMap;
22
import java.util.HashSet;
23
import java.util.Map;
24
import java.util.Set;
25
26
import org.eclipse.core.databinding.beans.IBeanProperty;
27
import org.eclipse.core.databinding.observable.Diffs;
28
import org.eclipse.core.databinding.property.SetProperty;
29
import org.eclipse.core.internal.databinding.Util;
30
31
/**
32
 * @since 3.3
33
 * 
34
 */
35
public class BeanSetProperty extends SetProperty implements IBeanProperty {
36
	private PropertyDescriptor propertyDescriptor;
37
	private Class elementType;
38
	private boolean attachListener;
39
40
	private ListenerSupport listenerSupport;
41
42
	private PropertyUpdateHelper updateHelper = new PropertyUpdateHelper();
43
44
	private Map sourceToCachedSet = Collections.synchronizedMap(new HashMap());
45
46
	/**
47
	 * @param propertyDescriptor
48
	 * @param elementType
49
	 * @param attachListener
50
	 */
51
	public BeanSetProperty(PropertyDescriptor propertyDescriptor,
52
			Class elementType, boolean attachListener) {
53
		this.propertyDescriptor = propertyDescriptor;
54
		this.elementType = elementType == null ? BeanPropertyHelper
55
				.getCollectionPropertyElementType(propertyDescriptor)
56
				: elementType;
57
		this.attachListener = attachListener;
58
	}
59
60
	private void initListenerSupport() {
61
		if (listenerSupport == null) {
62
			synchronized (this) {
63
				if (listenerSupport == null) {
64
					PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
65
						public void propertyChange(PropertyChangeEvent evt) {
66
							Object source = evt.getSource();
67
							if (!updateHelper.isUpdating(source)) {
68
								Object oldValue = evt.getOldValue();
69
								Object newValue = evt.getNewValue();
70
								if (oldValue == null && newValue == null) {
71
									// Obscure condition indicating new and old
72
									// values are unknown
73
									oldValue = sourceToCachedSet.get(source);
74
									newValue = BeanPropertyHelper.getProperty(
75
											source, propertyDescriptor);
76
								}
77
								sourceToCachedSet.put(source, newValue);
78
								fireSetChange(source, Diffs.computeSetDiff(
79
										asSet(oldValue), asSet(newValue)));
80
							}
81
						}
82
					};
83
					listenerSupport = new ListenerSupport(
84
							propertyChangeListener, propertyDescriptor
85
									.getName());
86
				}
87
			}
88
		}
89
	}
90
91
	protected void addListenerTo(Object source) {
92
		if (source != null && attachListener) {
93
			initListenerSupport();
94
			listenerSupport.hookListener(source);
95
			sourceToCachedSet.put(source, BeanPropertyHelper.getProperty(
96
					source, propertyDescriptor));
97
		}
98
	}
99
100
	protected void removeListenerFrom(Object source) {
101
		if (source != null && attachListener && listenerSupport != null) {
102
			listenerSupport.unhookListener(source);
103
			sourceToCachedSet.remove(source);
104
		}
105
	}
106
107
	public Set getSet(Object source) {
108
		if (source == null)
109
			return Collections.EMPTY_SET;
110
		Object propertyValue = BeanPropertyHelper.getProperty(source,
111
				propertyDescriptor);
112
		return asSet(propertyValue);
113
	}
114
115
	private Set asSet(Object propertyValue) {
116
		if (propertyValue == null)
117
			return Collections.EMPTY_SET;
118
		if (propertyDescriptor.getPropertyType().isArray())
119
			return new HashSet(Arrays.asList((Object[]) propertyValue));
120
		return (Set) propertyValue;
121
	}
122
123
	private void setSet(Object source, Set set) {
124
		if (source == null)
125
			return;
126
127
		Set oldSet = getSet(source);
128
129
		updateHelper.setUpdating(source, true);
130
		try {
131
			BeanPropertyHelper.setProperty(source, propertyDescriptor,
132
					convertSetToBeanPropertyType(set));
133
		} finally {
134
			updateHelper.setUpdating(source, false);
135
		}
136
137
		if (hasListeners(source)) {
138
			Set newSet = new HashSet(getSet(source));
139
			if (!Util.equals(oldSet, newSet)) {
140
				sourceToCachedSet.put(source, newSet);
141
				fireSetChange(source, Diffs.computeSetDiff(oldSet, newSet));
142
			}
143
		}
144
	}
145
146
	private Object convertSetToBeanPropertyType(Set set) {
147
		Object propertyValue = set;
148
		if (propertyDescriptor.getPropertyType().isArray()) {
149
			Class componentType = propertyDescriptor.getPropertyType()
150
					.getComponentType();
151
			Object[] array = (Object[]) Array.newInstance(componentType, set
152
					.size());
153
			set.toArray(array);
154
			propertyValue = array;
155
		}
156
		return propertyValue;
157
	}
158
159
	public boolean add(Object source, Object o) {
160
		Set set = new HashSet(getSet(source));
161
		boolean result = set.add(o);
162
		setSet(source, set);
163
		return result;
164
	}
165
166
	public boolean addAll(Object source, Collection c) {
167
		Set set = new HashSet(getSet(source));
168
		boolean changed = set.addAll(c);
169
		if (changed) {
170
			setSet(source, set);
171
		}
172
		return changed;
173
	}
174
175
	public void clear(Object source) {
176
		setSet(source, new HashSet());
177
	}
178
179
	public boolean remove(Object source, Object o) {
180
		Set set = new HashSet(getSet(source));
181
		boolean removed = set.remove(o);
182
		if (removed) {
183
			setSet(source, set);
184
		}
185
		return removed;
186
	}
187
188
	public boolean removeAll(Object source, Collection c) {
189
		Set set = new HashSet(getSet(source));
190
		boolean changed = set.removeAll(c);
191
		if (changed) {
192
			setSet(source, set);
193
		}
194
		return changed;
195
	}
196
197
	public boolean retainAll(Object source, Collection c) {
198
		Set set = new HashSet(getSet(source));
199
		boolean changed = set.retainAll(c);
200
		if (changed) {
201
			setSet(source, set);
202
		}
203
		return changed;
204
	}
205
206
	public Object getElementType() {
207
		return elementType;
208
	}
209
210
	public PropertyDescriptor getPropertyDescriptor() {
211
		return propertyDescriptor;
212
	}
213
214
	public synchronized void dispose() {
215
		propertyDescriptor = null;
216
		elementType = null;
217
		attachListener = false;
218
		if (listenerSupport != null) {
219
			listenerSupport.dispose();
220
			listenerSupport = null;
221
		}
222
		if (updateHelper != null) {
223
			updateHelper.dispose();
224
			updateHelper = null;
225
		}
226
		if (sourceToCachedSet != null) {
227
			sourceToCachedSet.clear();
228
			sourceToCachedSet = null;
229
		}
230
		super.dispose();
231
	}
232
}
(-)src/org/eclipse/core/databinding/beans/BeanProperties.java (+110 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
 * @since 1.2
26
 * 
27
 */
28
public class BeanProperties {
29
	/**
30
	 * @param beanClass
31
	 * @param propertyName
32
	 * @return a value property for the given property name of the given bean
33
	 *         class.
34
	 */
35
	public static IValueProperty valueProperty(Class beanClass,
36
			String propertyName) {
37
		return valueProperty(beanClass, propertyName, null);
38
	}
39
40
	/**
41
	 * @param beanClass
42
	 * @param propertyName
43
	 * @param valueType
44
	 * @return a value property for the given property name of the given bean
45
	 *         class.
46
	 */
47
	public static IValueProperty valueProperty(Class beanClass,
48
			String propertyName, Class valueType) {
49
		return new BeanValueProperty(BeanPropertyHelper.getPropertyDescriptor(
50
				beanClass, propertyName), valueType, true);
51
	}
52
53
	/**
54
	 * @param beanClass
55
	 * @param propertyName
56
	 * @return a list property for the given property name of the given bean
57
	 *         class.
58
	 */
59
	public static ISetProperty setProperty(Class beanClass, String propertyName) {
60
		return setProperty(beanClass, propertyName, null);
61
	}
62
63
	/**
64
	 * @param beanClass
65
	 * @param propertyName
66
	 * @param elementType
67
	 * @return a list property for the given property name of the given bean
68
	 *         class.
69
	 */
70
	public static ISetProperty setProperty(Class beanClass,
71
			String propertyName, Class elementType) {
72
		return new BeanSetProperty(BeanPropertyHelper.getPropertyDescriptor(
73
				beanClass, propertyName), elementType, true);
74
	}
75
76
	/**
77
	 * @param beanClass
78
	 * @param propertyName
79
	 * @return a list property for the given property name of the given bean
80
	 *         class.
81
	 */
82
	public static IListProperty listProperty(Class beanClass,
83
			String propertyName) {
84
		return listProperty(beanClass, propertyName, null);
85
	}
86
87
	/**
88
	 * @param beanClass
89
	 * @param propertyName
90
	 * @param elementType
91
	 * @return a list property for the given property name of the given bean
92
	 *         class.
93
	 */
94
	public static IListProperty listProperty(Class beanClass,
95
			String propertyName, Class elementType) {
96
		return new BeanListProperty(BeanPropertyHelper.getPropertyDescriptor(
97
				beanClass, propertyName), elementType, true);
98
	}
99
100
	/**
101
	 * @param beanClass
102
	 * @param propertyName
103
	 * @return a map property for the given property name of the given bean
104
	 *         class.
105
	 */
106
	public static IMapProperty mapProperty(Class beanClass, String propertyName) {
107
		return new BeanMapProperty(BeanPropertyHelper.getPropertyDescriptor(
108
				beanClass, propertyName), true);
109
	}
110
}
(-)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
}
(-)src/org/eclipse/core/tests/internal/databinding/beans/JavaBeanObservableValueTest.java (-60 / +66 lines)
Lines 14-33 Link Here
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;
33
import org.eclipse.core.databinding.property.IValueProperty;
31
import org.eclipse.jface.databinding.conformance.MutableObservableValueContractTest;
34
import org.eclipse.jface.databinding.conformance.MutableObservableValueContractTest;
32
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
35
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
33
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
36
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
Lines 39-166 Link Here
39
 */
42
 */
40
public class JavaBeanObservableValueTest extends AbstractDefaultRealmTestCase {
43
public class JavaBeanObservableValueTest extends AbstractDefaultRealmTestCase {
41
	private Bean bean;
44
	private Bean bean;
42
	private JavaBeanObservableValue observableValue;
45
	private IValueProperty property;
46
	private IBeanProperty beanProperty;
47
48
	private IObservableValue observableValue;
49
	private IBeanObservable beanObservable;
50
43
	private PropertyDescriptor propertyDescriptor;
51
	private PropertyDescriptor propertyDescriptor;
44
	private String propertyName;
52
	private String propertyName;
45
53
46
	/* (non-Javadoc)
47
	 * @see junit.framework.TestCase#setUp()
48
	 */
49
	protected void setUp() throws Exception {
54
	protected void setUp() throws Exception {
50
		super.setUp();
55
		super.setUp();
51
		
56
52
		bean = new Bean();
57
		bean = new Bean();
58
53
		propertyName = "value";
59
		propertyName = "value";
54
		propertyDescriptor = new PropertyDescriptor(propertyName, Bean.class);
60
		property = BeanProperties.valueProperty(Bean.class, propertyName);
55
		observableValue = new JavaBeanObservableValue(Realm.getDefault(), bean, propertyDescriptor);
61
		beanProperty = (IBeanProperty) property;
62
		propertyDescriptor = beanProperty.getPropertyDescriptor();
63
64
		observableValue = BeansObservables.observeValue(bean, propertyName);
65
		beanObservable = (IBeanObservable) observableValue;
56
	}
66
	}
57
67
58
	public void testGetObserved() throws Exception {
68
	public void testGetObserved() throws Exception {
59
		assertEquals(bean, observableValue.getObserved());
69
		assertEquals(bean, beanObservable.getObserved());
60
	}
70
	}
61
71
62
	public void testGetPropertyDescriptor() throws Exception {
72
	public void testGetPropertyDescriptor() throws Exception {
63
    	assertEquals(propertyDescriptor, observableValue.getPropertyDescriptor());
73
		assertEquals(propertyDescriptor, beanObservable.getPropertyDescriptor());
64
	}
74
	}
65
75
66
	public void testSetValueThrowsExceptionThrownByBean() throws Exception {
76
	public void testSetValueThrowsExceptionThrownByBean() throws Exception {
67
		ThrowsSetException temp = new ThrowsSetException();
77
		ThrowsSetException errorThrowingBean = new ThrowsSetException();
68
		JavaBeanObservableValue observable = new JavaBeanObservableValue(Realm
78
		IObservableValue observable = BeansObservables.observeValue(
69
				.getDefault(), temp,
79
				errorThrowingBean, "value");
70
				new PropertyDescriptor("value", ThrowsSetException.class));
71
72
		try {
80
		try {
73
			observable.setValue("");
81
			observable.setValue("");
74
			fail("exception should have been thrown");
82
			fail("exception should have been thrown");
75
		} catch (RuntimeException e) {	
83
		} catch (RuntimeException e) {
76
			assertEquals(temp.thrownException, e.getCause());
84
			assertEquals(errorThrowingBean.thrownException, e.getCause());
77
		}
85
		}
78
	}
86
	}
79
	
80
	public void testGetValueThrowsExceptionThrownByBean() throws Exception {
81
		ThrowsGetException temp = new ThrowsGetException();
82
		JavaBeanObservableValue observable = new JavaBeanObservableValue(Realm
83
				.getDefault(), temp,
84
				new PropertyDescriptor("value", ThrowsGetException.class));
85
87
88
	public void testGetValueThrowsExceptionThrownByBean() throws Exception {
89
		ThrowsGetException errorThrowingBean = new ThrowsGetException();
90
		IObservableValue observable = BeansObservables.observeValue(
91
				errorThrowingBean, "value");
86
		try {
92
		try {
87
			observable.getValue();
93
			observable.getValue();
88
			fail("exception should have been thrown");
94
			fail("exception should have been thrown");
89
		} catch (RuntimeException e) {	
95
		} catch (RuntimeException e) {
90
			assertEquals(temp.thrownException, e.getCause());
96
			assertEquals(errorThrowingBean.thrownException, e.getCause());
91
		}
97
		}
92
	}
98
	}
93
	
99
94
	public void testBug198519() {
100
	public void testBug198519() {
95
		final SimplePerson person = new SimplePerson();
101
		final SimplePerson person = new SimplePerson();
96
		final ComputedValue cv = new ComputedValue() {
102
		final ComputedValue cv = new ComputedValue() {
97
            final IObservableValue name = BeansObservables.observeValue(person, "name"); //$NON-NLS-1$
103
			final IObservableValue name = BeansObservables.observeValue(person,
98
            protected Object calculate() {
104
					"name"); //$NON-NLS-1$
99
                return Boolean.valueOf(name.getValue() != null);
105
100
            }
106
			protected Object calculate() {
101
        };
107
				return Boolean.valueOf(name.getValue() != null);
102
        cv.addChangeListener(new IChangeListener() {
108
			}
103
        	public void handleChange(ChangeEvent event) {
109
		};
104
        		cv.getValue();
110
		cv.addChangeListener(new IChangeListener() {
105
        	}
111
			public void handleChange(ChangeEvent event) {
106
        });
112
				cv.getValue();
107
        person.setName("foo");
113
			}
114
		});
115
		person.setName("foo");
108
	}
116
	}
109
117
110
	public void testConstructor_RegistersListeners() throws Exception {
118
	public void testConstructor_RegistersListeners() throws Exception {
111
		JavaBeanObservableValue observable = new JavaBeanObservableValue(Realm.getDefault(), bean, propertyDescriptor);
119
		IObservableValue observable = BeansObservables.observeValue(bean,
120
				propertyName);
112
		ChangeEventTracker.observe(observable);
121
		ChangeEventTracker.observe(observable);
113
		
122
114
		assertTrue(bean.hasListeners(propertyName));
123
		assertTrue(bean.hasListeners(propertyName));
115
	}
124
	}
116
	
125
117
	public void testConstructor_SkipRegisterListeners() throws Exception {
126
	public void testConstructor_SkipRegisterListeners() throws Exception {
118
		JavaBeanObservableValue observable = new JavaBeanObservableValue(Realm.getDefault(), bean, propertyDescriptor, false);
127
		IObservableValue observable = PojoObservables.observeValue(bean,
128
				propertyName);
119
		ChangeEventTracker.observe(observable);
129
		ChangeEventTracker.observe(observable);
120
		
130
121
		assertFalse(bean.hasListeners(propertyName));
131
		assertFalse(bean.hasListeners(propertyName));
122
	}
132
	}
123
	
133
124
	public static Test suite() {
134
	public static Test suite() {
125
		TestSuite suite = new TestSuite(JavaBeanObservableValueTest.class.getName());
135
		TestSuite suite = new TestSuite(JavaBeanObservableValueTest.class
136
				.getName());
126
		suite.addTestSuite(JavaBeanObservableValueTest.class);
137
		suite.addTestSuite(JavaBeanObservableValueTest.class);
127
		suite.addTest(MutableObservableValueContractTest.suite(new Delegate()));
138
		suite.addTest(MutableObservableValueContractTest.suite(new Delegate()));
128
		return suite;
139
		return suite;
129
	}
140
	}
130
141
131
	/* package */ static class Delegate extends AbstractObservableValueContractDelegate {
142
	/* package */static class Delegate extends
143
			AbstractObservableValueContractDelegate {
132
		private Bean bean;
144
		private Bean bean;
133
		
145
134
		public void setUp() {
146
		public void setUp() {
135
			super.setUp();
147
			super.setUp();
136
			
148
137
			bean = new Bean("");
149
			bean = new Bean("");
138
		}
150
		}
139
		
151
140
		public IObservableValue createObservableValue(Realm realm) {
152
		public IObservableValue createObservableValue(Realm realm) {
141
			try {
153
			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
		}
154
		}
149
		
155
150
		public void change(IObservable observable) {
156
		public void change(IObservable observable) {
151
			IObservableValue observableValue = (IObservableValue) observable;
157
			IObservableValue observableValue = (IObservableValue) observable;
152
			observableValue.setValue(createValue(observableValue));
158
			observableValue.setValue(createValue(observableValue));
153
		}
159
		}
154
		
160
155
		public Object getValueType(IObservableValue observable) {
161
		public Object getValueType(IObservableValue observable) {
156
			return String.class;
162
			return String.class;
157
		}
163
		}
158
		
164
159
		public Object createValue(IObservableValue observable) {
165
		public Object createValue(IObservableValue observable) {
160
			return observable.getValue() + "a";
166
			return observable.getValue() + "a";
161
		}
167
		}
162
	}
168
	}
163
	
169
164
	/**
170
	/**
165
	 * Throws an exception when the value is set.
171
	 * Throws an exception when the value is set.
166
	 * 
172
	 * 
(-)src/org/eclipse/core/tests/internal/databinding/beans/BeanObservableValueDecoratorTest.java (-18 / +11 lines)
Lines 7-19 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Matthew Hall - bug 194734
10
 ******************************************************************************/
11
 ******************************************************************************/
11
12
12
package org.eclipse.core.tests.internal.databinding.beans;
13
package org.eclipse.core.tests.internal.databinding.beans;
13
14
14
import java.beans.PropertyDescriptor;
15
import java.beans.PropertyDescriptor;
15
16
16
import org.eclipse.core.databinding.observable.value.WritableValue;
17
import org.eclipse.core.internal.databinding.beans.BeanObservableValueDecorator;
17
import org.eclipse.core.internal.databinding.beans.BeanObservableValueDecorator;
18
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableValue;
18
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableValue;
19
import org.eclipse.jface.databinding.swt.SWTObservables;
19
import org.eclipse.jface.databinding.swt.SWTObservables;
Lines 23-57 Link Here
23
/**
23
/**
24
 * @since 3.3
24
 * @since 3.3
25
 */
25
 */
26
public class BeanObservableValueDecoratorTest extends AbstractDefaultRealmTestCase {
26
public class BeanObservableValueDecoratorTest extends
27
		AbstractDefaultRealmTestCase {
27
	private Bean bean;
28
	private Bean bean;
28
	private JavaBeanObservableValue observableValue;
29
	private JavaBeanObservableValue observableValue;
29
	private BeanObservableValueDecorator decorator;
30
	private BeanObservableValueDecorator decorator;
30
	private PropertyDescriptor propertyDescriptor;
31
	private PropertyDescriptor propertyDescriptor;
31
	
32
32
	/*
33
	 * (non-Javadoc)
34
	 * 
35
	 * @see junit.framework.TestCase#setUp()
36
	 */
37
	protected void setUp() throws Exception {
33
	protected void setUp() throws Exception {
38
		super.setUp();
34
		super.setUp();
39
		
35
40
		bean = new Bean();
36
		bean = new Bean();
41
		propertyDescriptor = new PropertyDescriptor("value",
37
		propertyDescriptor = new PropertyDescriptor("value", Bean.class);
42
				Bean.class);
38
		observableValue = new JavaBeanObservableValue(SWTObservables
43
		observableValue = new JavaBeanObservableValue(
39
				.getRealm(Display.getDefault()), bean, propertyDescriptor);
44
				SWTObservables.getRealm(Display.getDefault()), bean,
40
		decorator = new BeanObservableValueDecorator(observableValue,
45
				propertyDescriptor);
41
				observableValue.getPropertyDescriptor());
46
		decorator = new BeanObservableValueDecorator(
47
				observableValue, new WritableValue(bean, Object.class), observableValue
48
						.getPropertyDescriptor());
49
	}
42
	}
50
43
51
	public void testGetDelegate() throws Exception {
44
	public void testGetDelegate() throws Exception {
52
		assertEquals(observableValue, decorator.getDelegate());
45
		assertEquals(observableValue, decorator.getDelegate());
53
	}
46
	}
54
	
47
55
	public void testGetObserved() throws Exception {
48
	public void testGetObserved() throws Exception {
56
		assertEquals(bean, decorator.getObserved());
49
		assertEquals(bean, decorator.getObserved());
57
	}
50
	}
(-)src/org/eclipse/core/tests/internal/databinding/beans/BeanObservableSetDecoratorTest.java (-8 / +8 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Matthew Hall - bug 194734
10
 ******************************************************************************/
11
 ******************************************************************************/
11
12
12
package org.eclipse.core.tests.internal.databinding.beans;
13
package org.eclipse.core.tests.internal.databinding.beans;
Lines 37-49 Link Here
37
		super.setUp();
38
		super.setUp();
38
39
39
		Bean bean = new Bean();
40
		Bean bean = new Bean();
40
		propertyDescriptor = new PropertyDescriptor("set",
41
		propertyDescriptor = new PropertyDescriptor("set", Bean.class);
41
				Bean.class);
42
		observableSet = new JavaBeanObservableSet(SWTObservables
42
		observableSet = new JavaBeanObservableSet(
43
				.getRealm(Display.getDefault()), bean, propertyDescriptor,
43
				SWTObservables.getRealm(Display.getDefault()), bean,
44
				String.class);
44
				propertyDescriptor, String.class);
45
		decorator = new BeanObservableSetDecorator(observableSet,
45
		decorator = new BeanObservableSetDecorator(
46
				propertyDescriptor);
46
				observableSet, observableSet, propertyDescriptor);
47
	}
47
	}
48
48
49
	public void testGetDelegate() throws Exception {
49
	public void testGetDelegate() throws Exception {
Lines 51-57 Link Here
51
	}
51
	}
52
52
53
	public void testGetObserved() throws Exception {
53
	public void testGetObserved() throws Exception {
54
		assertEquals(observableSet, decorator.getObserved());
54
		assertEquals(observableSet.getObserved(), decorator.getObserved());
55
	}
55
	}
56
56
57
	public void testGetPropertyDescriptor() throws Exception {
57
	public void testGetPropertyDescriptor() throws Exception {
(-)src/org/eclipse/core/tests/internal/databinding/beans/BeanObservableListDecoratorTest.java (-11 / +13 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Matthew Hall - bugs 208858, 213145
10
 *     Matthew Hall - bugs 208858, 213145, 194734
11
 ******************************************************************************/
11
 ******************************************************************************/
12
12
13
package org.eclipse.core.tests.internal.databinding.beans;
13
package org.eclipse.core.tests.internal.databinding.beans;
Lines 45-58 Link Here
45
	 */
45
	 */
46
	protected void setUp() throws Exception {
46
	protected void setUp() throws Exception {
47
		super.setUp();
47
		super.setUp();
48
		
48
49
		Bean bean = new Bean();
49
		Bean bean = new Bean();
50
		propertyDescriptor = new PropertyDescriptor(
50
		propertyDescriptor = new PropertyDescriptor("list", Bean.class,
51
				"list", Bean.class,"getList","setList");
51
				"getList", "setList");
52
		observableList = new JavaBeanObservableList(
52
		observableList = new JavaBeanObservableList(SWTObservables
53
				SWTObservables.getRealm(Display.getDefault()), bean,
53
				.getRealm(Display.getDefault()), bean, propertyDescriptor,
54
				propertyDescriptor, Bean.class);
54
				Bean.class);
55
		decorator = new BeanObservableListDecorator(observableList, observableList, propertyDescriptor);
55
		decorator = new BeanObservableListDecorator(observableList,
56
				propertyDescriptor);
56
	}
57
	}
57
58
58
	public void testGetDelegate() throws Exception {
59
	public void testGetDelegate() throws Exception {
Lines 60-66 Link Here
60
	}
61
	}
61
62
62
	public void testGetObserved() throws Exception {
63
	public void testGetObserved() throws Exception {
63
		assertEquals(observableList, decorator.getObserved());
64
		assertEquals(observableList.getObserved(), decorator.getObserved());
64
	}
65
	}
65
66
66
	public void testGetPropertyDescriptor() throws Exception {
67
	public void testGetPropertyDescriptor() throws Exception {
Lines 68-74 Link Here
68
	}
69
	}
69
70
70
	public static Test suite() {
71
	public static Test suite() {
71
		TestSuite suite = new TestSuite(BeanObservableListDecoratorTest.class.getName());
72
		TestSuite suite = new TestSuite(BeanObservableListDecoratorTest.class
73
				.getName());
72
		suite.addTestSuite(BeanObservableListDecoratorTest.class);
74
		suite.addTestSuite(BeanObservableListDecoratorTest.class);
73
		suite.addTest(MutableObservableListContractTest.suite(new Delegate()));
75
		suite.addTest(MutableObservableListContractTest.suite(new Delegate()));
74
		return suite;
76
		return suite;
Lines 80-86 Link Here
80
			final WritableList delegate = new WritableList(realm);
82
			final WritableList delegate = new WritableList(realm);
81
			for (int i = 0; i < elementCount; i++)
83
			for (int i = 0; i < elementCount; i++)
82
				delegate.add(createElement(delegate));
84
				delegate.add(createElement(delegate));
83
			return new BeanObservableListDecorator(delegate, null, null);
85
			return new BeanObservableListDecorator(delegate, null);
84
		}
86
		}
85
87
86
		private int counter;
88
		private int counter;
(-)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/core/tests/databinding/beans/BeansObservablesTest.java (-3 / +3 lines)
Lines 9-15 Link Here
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Brad Reynolds - bug 164268, 171616
10
 *     Brad Reynolds - bug 164268, 171616
11
 *     Mike Evans - bug 217558
11
 *     Mike Evans - bug 217558
12
 *     Matthew Hall - bug 221351
12
 *     Matthew Hall - bugs 221351, 194734
13
 *******************************************************************************/
13
 *******************************************************************************/
14
14
15
package org.eclipse.core.tests.databinding.beans;
15
package org.eclipse.core.tests.databinding.beans;
Lines 121-127 Link Here
121
		BeanObservableListDecorator beanObservable = (BeanObservableListDecorator) detailList;
121
		BeanObservableListDecorator beanObservable = (BeanObservableListDecorator) detailList;
122
		assertEquals("property descriptor", Bean.class.getMethod("getList",
122
		assertEquals("property descriptor", Bean.class.getMethod("getList",
123
				null), beanObservable.getPropertyDescriptor().getReadMethod());
123
				null), beanObservable.getPropertyDescriptor().getReadMethod());
124
		assertEquals("observed", parent, beanObservable.getObserved());
124
		assertEquals("observed", parent.getValue(), beanObservable.getObserved());
125
125
126
		// DetailObservableList is package level we can do a straight instanceof
126
		// DetailObservableList is package level we can do a straight instanceof
127
		// check
127
		// check
Lines 152-158 Link Here
152
		assertEquals("property descriptor", Bean.class
152
		assertEquals("property descriptor", Bean.class
153
				.getMethod("getSet", null), beanObservable
153
				.getMethod("getSet", null), beanObservable
154
				.getPropertyDescriptor().getReadMethod());
154
				.getPropertyDescriptor().getReadMethod());
155
		assertEquals("observed", parent, beanObservable.getObserved());
155
		assertEquals("observed", parent.getValue(), beanObservable.getObserved());
156
156
157
		// DetailObservableSet is package level we can't do a straight
157
		// DetailObservableSet is package level we can't do a straight
158
		// instanceof check
158
		// instanceof check

Return to bug 194734