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/beans/PojoProperties.java (-11 / +153 lines)
Lines 13-23 Link Here
13
13
14
import java.beans.PropertyChangeEvent;
14
import java.beans.PropertyChangeEvent;
15
15
16
import org.eclipse.core.databinding.property.list.IListProperty;
16
import org.eclipse.core.databinding.property.list.SimpleListProperty;
17
import org.eclipse.core.databinding.property.map.IMapProperty;
17
import org.eclipse.core.databinding.property.map.SimpleMapProperty;
18
import org.eclipse.core.databinding.property.set.ISetProperty;
18
import org.eclipse.core.databinding.property.set.SimpleSetProperty;
19
import org.eclipse.core.databinding.property.value.IValueProperty;
19
import org.eclipse.core.databinding.property.value.SimpleValueProperty;
20
import org.eclipse.core.internal.databinding.beans.BeanPropertyHelper;
20
import org.eclipse.core.internal.databinding.beans.BeanPropertyHelper;
21
import org.eclipse.core.internal.databinding.beans.AnonymousPojoListProperty;
22
import org.eclipse.core.internal.databinding.beans.AnonymousPojoMapProperty;
23
import org.eclipse.core.internal.databinding.beans.AnonymousPojoSetProperty;
24
import org.eclipse.core.internal.databinding.beans.AnonymousPojoValueProperty;
21
import org.eclipse.core.internal.databinding.beans.PojoListProperty;
25
import org.eclipse.core.internal.databinding.beans.PojoListProperty;
22
import org.eclipse.core.internal.databinding.beans.PojoMapProperty;
26
import org.eclipse.core.internal.databinding.beans.PojoMapProperty;
23
import org.eclipse.core.internal.databinding.beans.PojoSetProperty;
27
import org.eclipse.core.internal.databinding.beans.PojoSetProperty;
Lines 33-38 Link Here
33
 */
37
 */
34
public class PojoProperties {
38
public class PojoProperties {
35
	/**
39
	/**
40
	 * Returns a value property for the given property name of an arbitrary bean
41
	 * class. Objects lacking the named property are treated the same as if the
42
	 * property always contains null.
43
	 * 
44
	 * @param propertyName
45
	 *            the property name
46
	 * @return a value property for the given property name of an arbitrary bean
47
	 *         class.
48
	 */
49
	public static SimpleValueProperty value(String propertyName) {
50
		return value(propertyName, null);
51
	}
52
53
	/**
54
	 * Returns a value property for the given property name of an arbitrary bean
55
	 * class. Objects lacking the named property are treated the same as if the
56
	 * property always contains null.
57
	 * 
58
	 * @param propertyName
59
	 *            the property name
60
	 * @param valueType
61
	 *            the value type of the returned value property
62
	 * @return a value property for the given property name of an arbitrary bean
63
	 *         class.
64
	 */
65
	public static SimpleValueProperty value(String propertyName, Class valueType) {
66
		return new AnonymousPojoValueProperty(propertyName, valueType);
67
	}
68
69
	/**
36
	 * Returns a value property for the given property name of the given bean
70
	 * Returns a value property for the given property name of the given bean
37
	 * class.
71
	 * class.
38
	 * 
72
	 * 
Lines 43-49 Link Here
43
	 * @return a value property for the given property name of the given bean
77
	 * @return a value property for the given property name of the given bean
44
	 *         class.
78
	 *         class.
45
	 */
79
	 */
46
	public static IValueProperty value(Class beanClass, String propertyName) {
80
	public static SimpleValueProperty value(Class beanClass, String propertyName) {
47
		return value(beanClass, propertyName, null);
81
		return value(beanClass, propertyName, null);
48
	}
82
	}
49
83
Lines 60-72 Link Here
60
	 * @return a value property for the given property name of the given bean
94
	 * @return a value property for the given property name of the given bean
61
	 *         class.
95
	 *         class.
62
	 */
96
	 */
63
	public static IValueProperty value(Class beanClass, String propertyName,
97
	public static SimpleValueProperty value(Class beanClass, String propertyName,
64
			Class valueType) {
98
			Class valueType) {
65
		return new PojoValueProperty(BeanPropertyHelper.getPropertyDescriptor(
99
		return new PojoValueProperty(BeanPropertyHelper.getPropertyDescriptor(
66
				beanClass, propertyName), valueType);
100
				beanClass, propertyName), valueType);
67
	}
101
	}
68
102
69
	/**
103
	/**
104
	 * Returns a set property for the given property name of an arbitrary bean
105
	 * class. Objects lacking the named property are treated the same as if the
106
	 * property always contains an empty set.
107
	 * 
108
	 * @param propertyName
109
	 *            the property name
110
	 * @return a set property for the given property name of an arbitrary bean
111
	 *         class.
112
	 */
113
	public static SimpleSetProperty set(String propertyName) {
114
		return set(propertyName, null);
115
	}
116
117
	/**
118
	 * Returns a set property for the given property name of an arbitrary bean
119
	 * class. Objects lacking the named property are treated the same as if the
120
	 * property always contains an empty set.
121
	 * 
122
	 * @param propertyName
123
	 *            the property name
124
	 * @param elementType
125
	 *            the element type of the returned set property
126
	 * @return a set property for the given property name of an arbitrary bean
127
	 *         class.
128
	 */
129
	public static SimpleSetProperty set(String propertyName, Class elementType) {
130
		return new AnonymousPojoSetProperty(propertyName, elementType);
131
	}
132
133
	/**
70
	 * Returns a set property for the given property name of the given bean
134
	 * Returns a set property for the given property name of the given bean
71
	 * class.
135
	 * class.
72
	 * 
136
	 * 
Lines 77-83 Link Here
77
	 * @return a set property for the given property name of the given bean
141
	 * @return a set property for the given property name of the given bean
78
	 *         class.
142
	 *         class.
79
	 */
143
	 */
80
	public static ISetProperty set(Class beanClass, String propertyName) {
144
	public static SimpleSetProperty set(Class beanClass, String propertyName) {
81
		return set(beanClass, propertyName, null);
145
		return set(beanClass, propertyName, null);
82
	}
146
	}
83
147
Lines 94-106 Link Here
94
	 * @return a set property for the given property name of the given bean
158
	 * @return a set property for the given property name of the given bean
95
	 *         class.
159
	 *         class.
96
	 */
160
	 */
97
	public static ISetProperty set(Class beanClass, String propertyName,
161
	public static SimpleSetProperty set(Class beanClass, String propertyName,
98
			Class elementType) {
162
			Class elementType) {
99
		return new PojoSetProperty(BeanPropertyHelper.getPropertyDescriptor(
163
		return new PojoSetProperty(BeanPropertyHelper.getPropertyDescriptor(
100
				beanClass, propertyName), elementType);
164
				beanClass, propertyName), elementType);
101
	}
165
	}
102
166
103
	/**
167
	/**
168
	 * Returns a list property for the given property name of an arbitrary bean
169
	 * class. Objects lacking the named property are treated the same as if the
170
	 * property always contains an empty list.
171
	 * 
172
	 * @param propertyName
173
	 *            the property name
174
	 * @return a list property for the given property name of an arbitrary bean
175
	 *         class.
176
	 */
177
	public static SimpleListProperty list(String propertyName) {
178
		return list(propertyName, null);
179
	}
180
181
	/**
182
	 * Returns a list property for the given property name of an arbitrary bean
183
	 * class. Objects lacking the named property are treated the same as if the
184
	 * property always contains an empty list.
185
	 * 
186
	 * @param propertyName
187
	 *            the property name
188
	 * @param elementType
189
	 *            the element type of the returned list property
190
	 * @return a list property for the given property name of the given bean
191
	 *         class.
192
	 */
193
	public static SimpleListProperty list(String propertyName, Class elementType) {
194
		return new AnonymousPojoListProperty(propertyName, elementType);
195
	}
196
197
	/**
104
	 * Returns a list property for the given property name of the given bean
198
	 * Returns a list property for the given property name of the given bean
105
	 * class.
199
	 * class.
106
	 * 
200
	 * 
Lines 111-117 Link Here
111
	 * @return a list property for the given property name of the given bean
205
	 * @return a list property for the given property name of the given bean
112
	 *         class.
206
	 *         class.
113
	 */
207
	 */
114
	public static IListProperty list(Class beanClass, String propertyName) {
208
	public static SimpleListProperty list(Class beanClass, String propertyName) {
115
		return list(beanClass, propertyName, null);
209
		return list(beanClass, propertyName, null);
116
	}
210
	}
117
211
Lines 128-140 Link Here
128
	 * @return a list property for the given property name of the given bean
222
	 * @return a list property for the given property name of the given bean
129
	 *         class.
223
	 *         class.
130
	 */
224
	 */
131
	public static IListProperty list(Class beanClass, String propertyName,
225
	public static SimpleListProperty list(Class beanClass, String propertyName,
132
			Class elementType) {
226
			Class elementType) {
133
		return new PojoListProperty(BeanPropertyHelper.getPropertyDescriptor(
227
		return new PojoListProperty(BeanPropertyHelper.getPropertyDescriptor(
134
				beanClass, propertyName), elementType);
228
				beanClass, propertyName), elementType);
135
	}
229
	}
136
230
137
	/**
231
	/**
232
	 * Returns a map property for the given property name of an arbitrary bean
233
	 * class. Objects lacking the named property are treated the same as if the
234
	 * property always contains an empty map.
235
	 * 
236
	 * @param propertyName
237
	 *            the property name
238
	 * @return a map property for the given property name of an arbitrary bean
239
	 *         class.
240
	 */
241
	public static SimpleMapProperty map(String propertyName) {
242
		return map(propertyName, null, null);
243
	}
244
245
	/**
246
	 * Returns a map property for the given property name of an arbitrary bean
247
	 * class. Objects lacking the named property are treated the same as if the
248
	 * property always contains an empty map.
249
	 * 
250
	 * @param propertyName
251
	 *            the property name
252
	 * @param keyType
253
	 *            the key type for the returned map property
254
	 * @param valueType
255
	 *            the value type for the returned map property
256
	 * @return a map property for the given property name of an arbitrary bean
257
	 *         class.
258
	 */
259
	public static SimpleMapProperty map(String propertyName, Class keyType,
260
			Class valueType) {
261
		return new AnonymousPojoMapProperty(propertyName, keyType, valueType);
262
	}
263
264
	/**
265
	 * Returns a map property for the given property name of the given bean
266
	 * class.
267
	 * 
268
	 * @param beanClass
269
	 *            the bean class
270
	 * @param propertyName
271
	 *            the property name
272
	 * @return a map property for the given property name of the given bean
273
	 *         class.
274
	 */
275
	public static SimpleMapProperty map(Class beanClass, String propertyName) {
276
		return map(beanClass, propertyName, null, null);
277
	}
278
279
	/**
138
	 * Returns a map property for the given property name of the given bean
280
	 * Returns a map property for the given property name of the given bean
139
	 * class.
281
	 * class.
140
	 * 
282
	 * 
Lines 149-155 Link Here
149
	 * @return a map property for the given property name of the given bean
291
	 * @return a map property for the given property name of the given bean
150
	 *         class.
292
	 *         class.
151
	 */
293
	 */
152
	public static IMapProperty map(Class beanClass, String propertyName,
294
	public static SimpleMapProperty map(Class beanClass, String propertyName,
153
			Class keyType, Class valueType) {
295
			Class keyType, Class valueType) {
154
		return new PojoMapProperty(BeanPropertyHelper.getPropertyDescriptor(
296
		return new PojoMapProperty(BeanPropertyHelper.getPropertyDescriptor(
155
				beanClass, propertyName), keyType, valueType);
297
				beanClass, propertyName), keyType, valueType);
(-)src/org/eclipse/core/databinding/beans/BeansObservables.java (-4 / +48 lines)
Lines 92-99 Link Here
92
	}
92
	}
93
93
94
	/**
94
	/**
95
	 * Returns an observable map in the default realm tracking the current
95
	 * Returns an observable map in the given observable set's realm tracking
96
	 * values of the named property for the beans in the given set.
96
	 * the current values of the named property for the beans in the given set.
97
	 * Elements in the set which do not have the named property will have null
98
	 * values, and attempts to {@link IObservableMap#put(Object, Object) put}
99
	 * values to these elements will be ignored.
100
	 * 
101
	 * @param domain
102
	 *            the set of bean objects
103
	 * @param propertyName
104
	 *            the name of the property
105
	 * @return an observable map tracking the current values of the named
106
	 *         property for the beans in the given domain set
107
	 */
108
	public static IObservableMap observeMap(IObservableSet domain,
109
			String propertyName) {
110
		return BeanProperties.value(propertyName).observeDetailValues(domain);
111
	}
112
113
	/**
114
	 * Returns an observable map in the given observable set's realm tracking
115
	 * the current values of the named property for the beans in the given set.
97
	 * 
116
	 * 
98
	 * @param domain
117
	 * @param domain
99
	 *            the set of bean objects
118
	 *            the set of bean objects
Lines 204-211 Link Here
204
	}
223
	}
205
224
206
	/**
225
	/**
207
	 * Returns an array of observable maps in the default realm tracking the
226
	 * Returns an array of observable maps in the given observable set's realm
208
	 * current values of the named propertys for the beans in the given set.
227
	 * tracking the current values of the named properties for the beans in the
228
	 * given set. Elements in the set which do not have the named property will
229
	 * have null values, and attempts to
230
	 * {@link IObservableMap#put(Object, Object) put} values to these elements
231
	 * will be ignored.
232
	 * 
233
	 * @param domain
234
	 *            the set of objects
235
	 * @param propertyNames
236
	 *            the array of property names
237
	 * @return an array of observable maps tracking the current values of the
238
	 *         named propertys for the beans in the given domain set
239
	 */
240
	public static IObservableMap[] observeMaps(IObservableSet domain,
241
			String[] propertyNames) {
242
		IObservableMap[] result = new IObservableMap[propertyNames.length];
243
		for (int i = 0; i < propertyNames.length; i++) {
244
			result[i] = observeMap(domain, propertyNames[i]);
245
		}
246
		return result;
247
	}
248
249
	/**
250
	 * Returns an array of observable maps in the given observable set's realm
251
	 * tracking the current values of the named properties for the beans in the
252
	 * given set.
209
	 * 
253
	 * 
210
	 * @param domain
254
	 * @param domain
211
	 *            the set of objects
255
	 *            the set of objects
(-)src/org/eclipse/core/databinding/beans/BeanProperties.java (-11 / +154 lines)
Lines 7-20 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation (bug 194734)
9
 *     Matthew Hall - initial API and implementation (bug 194734)
10
 *     Matthew Hall - bug 247997
10
 ******************************************************************************/
11
 ******************************************************************************/
11
12
12
package org.eclipse.core.databinding.beans;
13
package org.eclipse.core.databinding.beans;
13
14
14
import org.eclipse.core.databinding.property.list.IListProperty;
15
import org.eclipse.core.databinding.property.list.SimpleListProperty;
15
import org.eclipse.core.databinding.property.map.IMapProperty;
16
import org.eclipse.core.databinding.property.map.SimpleMapProperty;
16
import org.eclipse.core.databinding.property.set.ISetProperty;
17
import org.eclipse.core.databinding.property.set.SimpleSetProperty;
17
import org.eclipse.core.databinding.property.value.IValueProperty;
18
import org.eclipse.core.databinding.property.value.SimpleValueProperty;
19
import org.eclipse.core.internal.databinding.beans.AnonymousBeanListProperty;
20
import org.eclipse.core.internal.databinding.beans.AnonymousBeanMapProperty;
21
import org.eclipse.core.internal.databinding.beans.AnonymousBeanSetProperty;
22
import org.eclipse.core.internal.databinding.beans.AnonymousBeanValueProperty;
18
import org.eclipse.core.internal.databinding.beans.BeanListProperty;
23
import org.eclipse.core.internal.databinding.beans.BeanListProperty;
19
import org.eclipse.core.internal.databinding.beans.BeanMapProperty;
24
import org.eclipse.core.internal.databinding.beans.BeanMapProperty;
20
import org.eclipse.core.internal.databinding.beans.BeanPropertyHelper;
25
import org.eclipse.core.internal.databinding.beans.BeanPropertyHelper;
Lines 30-35 Link Here
30
 */
35
 */
31
public class BeanProperties {
36
public class BeanProperties {
32
	/**
37
	/**
38
	 * Returns a value property for the given property name of an arbitrary bean
39
	 * class. Objects lacking the named property are treated the same as if the
40
	 * property always contains null.
41
	 * 
42
	 * @param propertyName
43
	 *            the property name
44
	 * @return a value property for the given property name of an arbitrary bean
45
	 *         class.
46
	 */
47
	public static SimpleValueProperty value(String propertyName) {
48
		return value(propertyName, null);
49
	}
50
51
	/**
52
	 * Returns a value property for the given property name of an arbitrary bean
53
	 * class. Objects lacking the named property are treated the same as if the
54
	 * property always contains null.
55
	 * 
56
	 * @param propertyName
57
	 *            the property name
58
	 * @param valueType
59
	 *            the value type of the returned value property
60
	 * @return a value property for the given property name of an arbitrary bean
61
	 *         class.
62
	 */
63
	public static SimpleValueProperty value(String propertyName, Class valueType) {
64
		return new AnonymousBeanValueProperty(propertyName, valueType);
65
	}
66
67
	/**
33
	 * Returns a value property for the given property name of the given bean
68
	 * Returns a value property for the given property name of the given bean
34
	 * class.
69
	 * class.
35
	 * 
70
	 * 
Lines 40-46 Link Here
40
	 * @return a value property for the given property name of the given bean
75
	 * @return a value property for the given property name of the given bean
41
	 *         class.
76
	 *         class.
42
	 */
77
	 */
43
	public static IValueProperty value(Class beanClass, String propertyName) {
78
	public static SimpleValueProperty value(Class beanClass, String propertyName) {
44
		return value(beanClass, propertyName, null);
79
		return value(beanClass, propertyName, null);
45
	}
80
	}
46
81
Lines 57-69 Link Here
57
	 * @return a value property for the given property name of the given bean
92
	 * @return a value property for the given property name of the given bean
58
	 *         class.
93
	 *         class.
59
	 */
94
	 */
60
	public static IValueProperty value(Class beanClass, String propertyName,
95
	public static SimpleValueProperty value(Class beanClass, String propertyName,
61
			Class valueType) {
96
			Class valueType) {
62
		return new BeanValueProperty(BeanPropertyHelper.getPropertyDescriptor(
97
		return new BeanValueProperty(BeanPropertyHelper.getPropertyDescriptor(
63
				beanClass, propertyName), valueType);
98
				beanClass, propertyName), valueType);
64
	}
99
	}
65
100
66
	/**
101
	/**
102
	 * Returns a set property for the given property name of an arbitrary bean
103
	 * class.  Objects lacking the named property are treated the same as if the
104
	 * property always contains an empty set.
105
	 * 
106
	 * @param propertyName
107
	 *            the property name
108
	 * @return a set property for the given property name of an arbitrary bean
109
	 *         class.
110
	 */
111
	public static SimpleSetProperty set(String propertyName) {
112
		return set(propertyName, null);
113
	}
114
115
	/**
116
	 * Returns a set property for the given property name of an arbitrary bean
117
	 * class.  Objects lacking the named property are treated the same as if the
118
	 * property always contains an empty set.
119
	 * 
120
	 * @param propertyName
121
	 *            the property name
122
	 * @param elementType
123
	 *            the element type of the returned set property
124
	 * @return a set property for the given property name of an arbitrary bean
125
	 *         class.
126
	 */
127
	public static SimpleSetProperty set(String propertyName, Class elementType) {
128
		return new AnonymousBeanSetProperty(propertyName, elementType);
129
	}
130
131
	/**
67
	 * Returns a set property for the given property name of the given bean
132
	 * Returns a set property for the given property name of the given bean
68
	 * class.
133
	 * class.
69
	 * 
134
	 * 
Lines 74-80 Link Here
74
	 * @return a set property for the given property name of the given bean
139
	 * @return a set property for the given property name of the given bean
75
	 *         class.
140
	 *         class.
76
	 */
141
	 */
77
	public static ISetProperty set(Class beanClass, String propertyName) {
142
	public static SimpleSetProperty set(Class beanClass, String propertyName) {
78
		return set(beanClass, propertyName, null);
143
		return set(beanClass, propertyName, null);
79
	}
144
	}
80
145
Lines 91-103 Link Here
91
	 * @return a set property for the given property name of the given bean
156
	 * @return a set property for the given property name of the given bean
92
	 *         class.
157
	 *         class.
93
	 */
158
	 */
94
	public static ISetProperty set(Class beanClass, String propertyName,
159
	public static SimpleSetProperty set(Class beanClass, String propertyName,
95
			Class elementType) {
160
			Class elementType) {
96
		return new BeanSetProperty(BeanPropertyHelper.getPropertyDescriptor(
161
		return new BeanSetProperty(BeanPropertyHelper.getPropertyDescriptor(
97
				beanClass, propertyName), elementType);
162
				beanClass, propertyName), elementType);
98
	}
163
	}
99
164
100
	/**
165
	/**
166
	 * Returns a list property for the given property name of an arbitrary bean
167
	 * class.  Objects lacking the named property are treated the same as if the
168
	 * property always contains an empty list.
169
	 * 
170
	 * @param propertyName
171
	 *            the property name
172
	 * @return a list property for the given property name of an arbitrary bean
173
	 *         class.
174
	 */
175
	public static SimpleListProperty list(String propertyName) {
176
		return list(propertyName, null);
177
	}
178
179
	/**
180
	 * Returns a list property for the given property name of an arbitrary bean
181
	 * class.  Objects lacking the named property are treated the same as if the
182
	 * property always contains an empty list.
183
	 * 
184
	 * @param propertyName
185
	 *            the property name
186
	 * @param elementType
187
	 *            the element type of the returned list property
188
	 * @return a list property for the given property name of the given bean
189
	 *         class.
190
	 */
191
	public static SimpleListProperty list(String propertyName, Class elementType) {
192
		return new AnonymousBeanListProperty(propertyName, elementType);
193
	}
194
195
	/**
101
	 * Returns a list property for the given property name of the given bean
196
	 * Returns a list property for the given property name of the given bean
102
	 * class.
197
	 * class.
103
	 * 
198
	 * 
Lines 108-114 Link Here
108
	 * @return a list property for the given property name of the given bean
203
	 * @return a list property for the given property name of the given bean
109
	 *         class.
204
	 *         class.
110
	 */
205
	 */
111
	public static IListProperty list(Class beanClass, String propertyName) {
206
	public static SimpleListProperty list(Class beanClass, String propertyName) {
112
		return list(beanClass, propertyName, null);
207
		return list(beanClass, propertyName, null);
113
	}
208
	}
114
209
Lines 125-137 Link Here
125
	 * @return a list property for the given property name of the given bean
220
	 * @return a list property for the given property name of the given bean
126
	 *         class.
221
	 *         class.
127
	 */
222
	 */
128
	public static IListProperty list(Class beanClass, String propertyName,
223
	public static SimpleListProperty list(Class beanClass, String propertyName,
129
			Class elementType) {
224
			Class elementType) {
130
		return new BeanListProperty(BeanPropertyHelper.getPropertyDescriptor(
225
		return new BeanListProperty(BeanPropertyHelper.getPropertyDescriptor(
131
				beanClass, propertyName), elementType);
226
				beanClass, propertyName), elementType);
132
	}
227
	}
133
228
134
	/**
229
	/**
230
	 * Returns a map property for the given property name of an arbitrary bean
231
	 * class.  Objects lacking the named property are treated the same as if the
232
	 * property always contains an empty map.
233
	 * 
234
	 * @param propertyName
235
	 *            the property name
236
	 * @return a map property for the given property name of an arbitrary bean
237
	 *         class.
238
	 */
239
	public static SimpleMapProperty map(String propertyName) {
240
		return map(propertyName, null, null);
241
	}
242
243
	/**
244
	 * Returns a map property for the given property name of an arbitrary bean
245
	 * class.  Objects lacking the named property are treated the same as if the
246
	 * property always contains an empty map.
247
	 * 
248
	 * @param propertyName
249
	 *            the property name
250
	 * @param keyType
251
	 *            the key type for the returned map property
252
	 * @param valueType
253
	 *            the value type for the returned map property
254
	 * @return a map property for the given property name of an arbitrary bean
255
	 *         class.
256
	 */
257
	public static SimpleMapProperty map(String propertyName, Class keyType,
258
			Class valueType) {
259
		return new AnonymousBeanMapProperty(propertyName, keyType, valueType);
260
	}
261
262
	/**
263
	 * Returns a map property for the given property name of the given bean
264
	 * class.
265
	 * 
266
	 * @param beanClass
267
	 *            the bean class
268
	 * @param propertyName
269
	 *            the property name
270
	 * @return a map property for the given property name of the given bean
271
	 *         class.
272
	 */
273
	public static SimpleMapProperty map(Class beanClass, String propertyName) {
274
		return map(beanClass, propertyName, null, null);
275
	}
276
277
	/**
135
	 * Returns a map property for the given property name of the given bean
278
	 * Returns a map property for the given property name of the given bean
136
	 * class.
279
	 * class.
137
	 * 
280
	 * 
Lines 146-152 Link Here
146
	 * @return a map property for the given property name of the given bean
289
	 * @return a map property for the given property name of the given bean
147
	 *         class.
290
	 *         class.
148
	 */
291
	 */
149
	public static IMapProperty map(Class beanClass, String propertyName,
292
	public static SimpleMapProperty map(Class beanClass, String propertyName,
150
			Class keyType, Class valueType) {
293
			Class keyType, Class valueType) {
151
		return new BeanMapProperty(BeanPropertyHelper.getPropertyDescriptor(
294
		return new BeanMapProperty(BeanPropertyHelper.getPropertyDescriptor(
152
				beanClass, propertyName), keyType, valueType);
295
				beanClass, propertyName), keyType, valueType);
(-)src/org/eclipse/core/databinding/beans/PojoObservables.java (-4 / +48 lines)
Lines 82-89 Link Here
82
	}
82
	}
83
83
84
	/**
84
	/**
85
	 * Returns an observable map in the default realm tracking the current
85
	 * Returns an observable map in the given observable set's realm tracking
86
	 * values of the named property for the pojos in the given set.
86
	 * the current values of the named property for the beans in the given set.
87
	 * Elements in the set which do not have the named property will have null
88
	 * values, and attempts to {@link IObservableMap#put(Object, Object) put}
89
	 * values to these elements will be ignored.
90
	 * 
91
	 * @param domain
92
	 *            the set of bean objects
93
	 * @param propertyName
94
	 *            the name of the property
95
	 * @return an observable map tracking the current values of the named
96
	 *         property for the beans in the given domain set
97
	 */
98
	public static IObservableMap observeMap(IObservableSet domain,
99
			String propertyName) {
100
		return PojoProperties.value(propertyName).observeDetailValues(domain);
101
	}
102
103
	/**
104
	 * Returns an observable map in the given observable set's realm tracking
105
	 * the current values of the named property for the pojos in the given set.
87
	 * 
106
	 * 
88
	 * @param domain
107
	 * @param domain
89
	 *            the set of pojo objects
108
	 *            the set of pojo objects
Lines 105-112 Link Here
105
	}
124
	}
106
125
107
	/**
126
	/**
108
	 * Returns an array of observable maps in the default realm tracking the
127
	 * Returns an array of observable maps in the given observable set's realm
109
	 * current values of the named propertys for the pojos in the given set.
128
	 * tracking the current values of the named properties for the beans in the
129
	 * given set. Elements in the set which do not have the named property will
130
	 * have null values, and attempts to
131
	 * {@link IObservableMap#put(Object, Object) put} values to these elements
132
	 * will be ignored.
133
	 * 
134
	 * @param domain
135
	 *            the set of objects
136
	 * @param propertyNames
137
	 *            the array of property names
138
	 * @return an array of observable maps tracking the current values of the
139
	 *         named propertys for the beans in the given domain set
140
	 */
141
	public static IObservableMap[] observeMaps(IObservableSet domain,
142
			String[] propertyNames) {
143
		IObservableMap[] result = new IObservableMap[propertyNames.length];
144
		for (int i = 0; i < propertyNames.length; i++) {
145
			result[i] = observeMap(domain, propertyNames[i]);
146
		}
147
		return result;
148
	}
149
150
	/**
151
	 * Returns an array of observable maps in the given observable set's realm
152
	 * tracking the current values of the named propertys for the pojos in the
153
	 * given set.
110
	 * 
154
	 * 
111
	 * @param domain
155
	 * @param domain
112
	 *            the set of objects
156
	 *            the set of objects
(-)src/org/eclipse/core/internal/databinding/beans/AnonymousBeanListProperty.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 247997)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.beans;
13
14
import java.util.HashMap;
15
import java.util.Map;
16
17
import org.eclipse.core.databinding.BindingException;
18
import org.eclipse.core.databinding.beans.BeanProperties;
19
import org.eclipse.core.databinding.property.list.DelegatingListProperty;
20
import org.eclipse.core.databinding.property.list.SimpleListProperty;
21
22
/**
23
 * @since 3.3
24
 * 
25
 */
26
public class AnonymousBeanListProperty extends DelegatingListProperty {
27
	private final String propertyName;
28
29
	private Map delegates;
30
31
	/**
32
	 * @param propertyName
33
	 * @param elementType
34
	 */
35
	public AnonymousBeanListProperty(String propertyName, Class elementType) {
36
		super(elementType);
37
		this.propertyName = propertyName;
38
		this.delegates = new HashMap();
39
	}
40
41
	protected SimpleListProperty getDelegate(Object source) {
42
		if (source == null)
43
			return null;
44
45
		Class beanClass = source.getClass();
46
		if (delegates.containsKey(beanClass))
47
			return (BeanListProperty) delegates.get(beanClass);
48
49
		BeanListProperty delegate;
50
		try {
51
			delegate = (BeanListProperty) BeanProperties.list(beanClass,
52
					propertyName, (Class) getElementType());
53
		} catch (BindingException noSuchProperty) {
54
			delegate = null;
55
		}
56
		delegates.put(beanClass, delegate);
57
		return delegate;
58
	}
59
60
	public String toString() {
61
		String s = "{{Generic Bean}}." + propertyName + "[]"; //$NON-NLS-1$ //$NON-NLS-2$
62
63
		Class elementType = (Class) getElementType();
64
		if (elementType != null)
65
			s += " <" + elementType.getName() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
66
67
		return s;
68
	}
69
}
(-)src/org/eclipse/core/internal/databinding/beans/AnonymousPojoSetProperty.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 247997)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.beans;
13
14
import java.util.HashMap;
15
import java.util.Map;
16
17
import org.eclipse.core.databinding.BindingException;
18
import org.eclipse.core.databinding.beans.PojoProperties;
19
import org.eclipse.core.databinding.property.set.DelegatingSetProperty;
20
import org.eclipse.core.databinding.property.set.SimpleSetProperty;
21
22
/**
23
 * @since 3.3
24
 * 
25
 */
26
public class AnonymousPojoSetProperty extends DelegatingSetProperty {
27
	private final String propertyName;
28
29
	private Map delegates;
30
31
	/**
32
	 * @param propertyName
33
	 * @param elementType
34
	 */
35
	public AnonymousPojoSetProperty(String propertyName, Class elementType) {
36
		super(elementType);
37
		this.propertyName = propertyName;
38
		this.delegates = new HashMap();
39
	}
40
41
	protected SimpleSetProperty getDelegate(Object source) {
42
		if (source == null)
43
			return null;
44
45
		Class beanClass = source.getClass();
46
		if (delegates.containsKey(beanClass))
47
			return (PojoSetProperty) delegates.get(beanClass);
48
49
		PojoSetProperty delegate;
50
		try {
51
			delegate = (PojoSetProperty) PojoProperties.set(beanClass,
52
					propertyName, (Class) getElementType());
53
		} catch (BindingException noSuchProperty) {
54
			delegate = null;
55
		}
56
		delegates.put(beanClass, delegate);
57
		return delegate;
58
	}
59
60
	public String toString() {
61
		String s = "{{Generic POJO}}." + propertyName + "{}"; //$NON-NLS-1$ //$NON-NLS-2$
62
63
		Class elementType = (Class) getElementType();
64
		if (elementType != null)
65
			s += " <" + elementType.getName() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
66
67
		return s;
68
	}
69
}
(-)src/org/eclipse/core/internal/databinding/beans/AnonymousBeanValueProperty.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 247997)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.beans;
13
14
import java.util.HashMap;
15
import java.util.Map;
16
17
import org.eclipse.core.databinding.BindingException;
18
import org.eclipse.core.databinding.beans.BeanProperties;
19
import org.eclipse.core.databinding.property.value.DelegatingValueProperty;
20
import org.eclipse.core.databinding.property.value.SimpleValueProperty;
21
22
/**
23
 * @since 3.3
24
 * 
25
 */
26
public class AnonymousBeanValueProperty extends DelegatingValueProperty {
27
	private final String propertyName;
28
29
	private Map delegates;
30
31
	/**
32
	 * @param propertyName
33
	 * @param valueType
34
	 */
35
	public AnonymousBeanValueProperty(String propertyName, Class valueType) {
36
		super(valueType);
37
		this.propertyName = propertyName;
38
		this.delegates = new HashMap();
39
	}
40
41
	protected SimpleValueProperty getDelegate(Object source) {
42
		if (source == null)
43
			return null;
44
45
		Class beanClass = source.getClass();
46
		if (delegates.containsKey(beanClass))
47
			return (BeanValueProperty) delegates.get(beanClass);
48
49
		BeanValueProperty delegate;
50
		try {
51
			delegate = (BeanValueProperty) BeanProperties.value(beanClass,
52
					propertyName, (Class) getValueType());
53
		} catch (BindingException noSuchProperty) {
54
			delegate = null;
55
		}
56
		delegates.put(beanClass, delegate);
57
		return delegate;
58
	}
59
60
	public String toString() {
61
		String s = "{{Generic Bean}}." + propertyName; //$NON-NLS-1$
62
63
		Class valueType = (Class) getValueType();
64
		if (valueType != null)
65
			s += " <" + valueType.getName() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
66
67
		return s;
68
	}
69
}
(-)src/org/eclipse/core/internal/databinding/beans/AnonymousBeanMapProperty.java (+73 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 247997)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.beans;
13
14
import java.util.HashMap;
15
import java.util.Map;
16
17
import org.eclipse.core.databinding.BindingException;
18
import org.eclipse.core.databinding.beans.BeanProperties;
19
import org.eclipse.core.databinding.property.map.DelegatingMapProperty;
20
import org.eclipse.core.databinding.property.map.SimpleMapProperty;
21
22
/**
23
 * @since 3.3
24
 * 
25
 */
26
public class AnonymousBeanMapProperty extends DelegatingMapProperty {
27
	private final String propertyName;
28
29
	private Map delegates;
30
31
	/**
32
	 * @param propertyName
33
	 * @param keyType
34
	 * @param valueType
35
	 */
36
	public AnonymousBeanMapProperty(String propertyName, Class keyType,
37
			Class valueType) {
38
		super(keyType, valueType);
39
		this.propertyName = propertyName;
40
		this.delegates = new HashMap();
41
	}
42
43
	protected SimpleMapProperty getDelegate(Object source) {
44
		if (source == null)
45
			return null;
46
47
		Class beanClass = source.getClass();
48
		if (delegates.containsKey(beanClass))
49
			return (BeanMapProperty) delegates.get(beanClass);
50
51
		BeanMapProperty delegate;
52
		try {
53
			delegate = (BeanMapProperty) BeanProperties.map(beanClass,
54
					propertyName, (Class) getKeyType(), (Class) getValueType());
55
		} catch (BindingException noSuchProperty) {
56
			delegate = null;
57
		}
58
		delegates.put(beanClass, delegate);
59
		return delegate;
60
	}
61
62
	public String toString() {
63
		String s = "{{Generic Bean}}." + propertyName + "{:}"; //$NON-NLS-1$ //$NON-NLS-2$
64
65
		Class keyType = (Class) getKeyType();
66
		Class valueType = (Class) getValueType();
67
		if (keyType != null || valueType != null) {
68
			s += " <" + keyType + ", " + valueType + ">"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
69
		}
70
71
		return s;
72
	}
73
}
(-)src/org/eclipse/core/internal/databinding/beans/AnonymousPojoMapProperty.java (+73 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 247997)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.beans;
13
14
import java.util.HashMap;
15
import java.util.Map;
16
17
import org.eclipse.core.databinding.BindingException;
18
import org.eclipse.core.databinding.beans.PojoProperties;
19
import org.eclipse.core.databinding.property.map.DelegatingMapProperty;
20
import org.eclipse.core.databinding.property.map.SimpleMapProperty;
21
22
/**
23
 * @since 3.3
24
 * 
25
 */
26
public class AnonymousPojoMapProperty extends DelegatingMapProperty {
27
	private final String propertyName;
28
29
	private Map delegates;
30
31
	/**
32
	 * @param propertyName
33
	 * @param keyType
34
	 * @param valueType
35
	 */
36
	public AnonymousPojoMapProperty(String propertyName, Class keyType,
37
			Class valueType) {
38
		super(keyType, valueType);
39
		this.propertyName = propertyName;
40
		this.delegates = new HashMap();
41
	}
42
43
	protected SimpleMapProperty getDelegate(Object source) {
44
		if (source == null)
45
			return null;
46
47
		Class beanClass = source.getClass();
48
		if (delegates.containsKey(beanClass))
49
			return (PojoMapProperty) delegates.get(beanClass);
50
51
		PojoMapProperty delegate;
52
		try {
53
			delegate = (PojoMapProperty) PojoProperties.map(beanClass,
54
					propertyName, (Class) getKeyType(), (Class) getValueType());
55
		} catch (BindingException noSuchProperty) {
56
			delegate = null;
57
		}
58
		delegates.put(beanClass, delegate);
59
		return delegate;
60
	}
61
62
	public String toString() {
63
		String s = "{{Generic POJO}}." + propertyName + "{:}"; //$NON-NLS-1$ //$NON-NLS-2$
64
65
		Class keyType = (Class) getKeyType();
66
		Class valueType = (Class) getValueType();
67
		if (keyType != null || valueType != null) {
68
			s += " <" + keyType + ", " + valueType + ">"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
69
		}
70
71
		return s;
72
	}
73
}
(-)src/org/eclipse/core/internal/databinding/beans/AnonymousPojoValueProperty.java (+67 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 247997)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.beans;
13
14
import java.util.HashMap;
15
import java.util.Map;
16
17
import org.eclipse.core.databinding.BindingException;
18
import org.eclipse.core.databinding.beans.PojoProperties;
19
import org.eclipse.core.databinding.property.value.DelegatingValueProperty;
20
import org.eclipse.core.databinding.property.value.SimpleValueProperty;
21
22
/**
23
 * @since 3.3
24
 * 
25
 */
26
public class AnonymousPojoValueProperty extends DelegatingValueProperty {
27
	private final String propertyName;
28
29
	private Map delegates;
30
31
	/**
32
	 * @param propertyName
33
	 * @param valueType
34
	 */
35
	public AnonymousPojoValueProperty(String propertyName, Class valueType) {
36
		super(valueType);
37
		this.propertyName = propertyName;
38
		this.delegates = new HashMap();
39
	}
40
41
	protected SimpleValueProperty getDelegate(Object source) {
42
		if (source == null)
43
			return null;
44
45
		Class pojoClass = source.getClass();
46
		if (delegates.containsKey(pojoClass))
47
			return (PojoValueProperty) delegates.get(pojoClass);
48
49
		PojoValueProperty delegate;
50
		try {
51
			delegate = (PojoValueProperty) PojoProperties.value(pojoClass,
52
					propertyName, (Class) getValueType());
53
		} catch (BindingException noSuchProperty) {
54
			delegate = null;
55
		}
56
		delegates.put(pojoClass, delegate);
57
		return delegate;
58
	}
59
60
	public String toString() {
61
		String s = "{{Generic POJO}}." + propertyName; //$NON-NLS-1$
62
		Class valueType = (Class) getValueType();
63
		if (valueType != null)
64
			s += " <" + valueType.getName() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
65
		return s;
66
	}
67
}
(-)src/org/eclipse/core/internal/databinding/beans/AnonymousPojoListProperty.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 247997)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.beans;
13
14
import java.util.HashMap;
15
import java.util.Map;
16
17
import org.eclipse.core.databinding.BindingException;
18
import org.eclipse.core.databinding.beans.PojoProperties;
19
import org.eclipse.core.databinding.property.list.DelegatingListProperty;
20
import org.eclipse.core.databinding.property.list.SimpleListProperty;
21
22
/**
23
 * @since 3.3
24
 * 
25
 */
26
public class AnonymousPojoListProperty extends DelegatingListProperty {
27
	private final String propertyName;
28
29
	private Map delegates;
30
31
	/**
32
	 * @param propertyName
33
	 * @param elementType
34
	 */
35
	public AnonymousPojoListProperty(String propertyName, Class elementType) {
36
		super(elementType);
37
		this.propertyName = propertyName;
38
		this.delegates = new HashMap();
39
	}
40
41
	protected SimpleListProperty getDelegate(Object source) {
42
		if (source == null)
43
			return null;
44
45
		Class beanClass = source.getClass();
46
		if (delegates.containsKey(beanClass))
47
			return (PojoListProperty) delegates.get(beanClass);
48
49
		PojoListProperty delegate;
50
		try {
51
			delegate = (PojoListProperty) PojoProperties.list(beanClass,
52
					propertyName, (Class) getElementType());
53
		} catch (BindingException noSuchProperty) {
54
			delegate = null;
55
		}
56
		delegates.put(beanClass, delegate);
57
		return delegate;
58
	}
59
60
	public String toString() {
61
		String s = "{{Generic POJO}}." + propertyName + "[]"; //$NON-NLS-1$ //$NON-NLS-2$
62
63
		Class elementType = (Class) getElementType();
64
		if (elementType != null)
65
			s += " <" + elementType.getName() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
66
67
		return s;
68
	}
69
}
(-)src/org/eclipse/core/internal/databinding/beans/AnonymousBeanSetProperty.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 247997)
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.beans;
13
14
import java.util.HashMap;
15
import java.util.Map;
16
17
import org.eclipse.core.databinding.BindingException;
18
import org.eclipse.core.databinding.beans.BeanProperties;
19
import org.eclipse.core.databinding.property.set.DelegatingSetProperty;
20
import org.eclipse.core.databinding.property.set.SimpleSetProperty;
21
22
/**
23
 * @since 3.3
24
 * 
25
 */
26
public class AnonymousBeanSetProperty extends DelegatingSetProperty {
27
	private final String propertyName;
28
29
	private Map delegates;
30
31
	/**
32
	 * @param propertyName
33
	 * @param elementType
34
	 */
35
	public AnonymousBeanSetProperty(String propertyName, Class elementType) {
36
		super(elementType);
37
		this.propertyName = propertyName;
38
		this.delegates = new HashMap();
39
	}
40
41
	protected SimpleSetProperty getDelegate(Object source) {
42
		if (source == null)
43
			return null;
44
45
		Class beanClass = source.getClass();
46
		if (delegates.containsKey(beanClass))
47
			return (BeanSetProperty) delegates.get(beanClass);
48
49
		BeanSetProperty delegate;
50
		try {
51
			delegate = (BeanSetProperty) BeanProperties.set(beanClass,
52
					propertyName, (Class) getElementType());
53
		} catch (BindingException noSuchProperty) {
54
			delegate = null;
55
		}
56
		delegates.put(beanClass, delegate);
57
		return delegate;
58
	}
59
60
	public String toString() {
61
		String s = "{{Generic Bean}}." + propertyName + "{}"; //$NON-NLS-1$ //$NON-NLS-2$
62
63
		Class elementType = (Class) getElementType();
64
		if (elementType != null)
65
			s += " <" + elementType.getName() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
66
67
		return s;
68
	}
69
}
(-)src/org/eclipse/jface/examples/databinding/snippets/Snippet026AnonymousBeanProperties.java (+373 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.examples.databinding.snippets;
13
14
import java.beans.PropertyChangeEvent;
15
import java.beans.PropertyChangeListener;
16
import java.beans.PropertyChangeSupport;
17
import java.util.Collections;
18
import java.util.Iterator;
19
import java.util.Set;
20
import java.util.TreeSet;
21
22
import org.eclipse.core.databinding.beans.BeanProperties;
23
import org.eclipse.core.databinding.observable.Diffs;
24
import org.eclipse.core.databinding.observable.Realm;
25
import org.eclipse.core.databinding.observable.map.IObservableMap;
26
import org.eclipse.core.databinding.observable.masterdetail.IObservableFactory;
27
import org.eclipse.core.databinding.observable.set.IObservableSet;
28
import org.eclipse.core.databinding.observable.set.SetDiff;
29
import org.eclipse.core.databinding.property.INativePropertyListener;
30
import org.eclipse.core.databinding.property.set.DelegatingSetProperty;
31
import org.eclipse.core.databinding.property.set.ISetPropertyChangeListener;
32
import org.eclipse.core.databinding.property.set.SetPropertyChangeEvent;
33
import org.eclipse.core.databinding.property.set.SimpleSetProperty;
34
import org.eclipse.jface.databinding.swt.SWTObservables;
35
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
36
import org.eclipse.jface.databinding.viewers.ObservableSetTreeContentProvider;
37
import org.eclipse.jface.viewers.TreeViewer;
38
import org.eclipse.swt.SWT;
39
import org.eclipse.swt.layout.GridData;
40
import org.eclipse.swt.layout.GridLayout;
41
import org.eclipse.swt.widgets.Display;
42
import org.eclipse.swt.widgets.Shell;
43
import org.eclipse.swt.widgets.Tree;
44
import org.eclipse.swt.widgets.TreeColumn;
45
46
/**
47
 * @since 3.2
48
 * 
49
 */
50
public class Snippet026AnonymousBeanProperties {
51
	private TreeViewer viewer;
52
53
	public static void main(String[] args) {
54
		Display display = new Display();
55
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
56
			public void run() {
57
				try {
58
					Snippet026AnonymousBeanProperties window = new Snippet026AnonymousBeanProperties();
59
					window.open();
60
				} catch (Exception e) {
61
					e.printStackTrace();
62
				}
63
			}
64
		});
65
	}
66
67
	private ApplicationModel model;
68
	private Shell shell;
69
	private Tree tree;
70
71
	// Minimal JavaBeans support
72
	public static abstract class AbstractModelObject {
73
		private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
74
				this);
75
76
		public void addPropertyChangeListener(PropertyChangeListener listener) {
77
			propertyChangeSupport.addPropertyChangeListener(listener);
78
		}
79
80
		public void addPropertyChangeListener(String propertyName,
81
				PropertyChangeListener listener) {
82
			propertyChangeSupport.addPropertyChangeListener(propertyName,
83
					listener);
84
		}
85
86
		public void removePropertyChangeListener(PropertyChangeListener listener) {
87
			propertyChangeSupport.removePropertyChangeListener(listener);
88
		}
89
90
		public void removePropertyChangeListener(String propertyName,
91
				PropertyChangeListener listener) {
92
			propertyChangeSupport.removePropertyChangeListener(propertyName,
93
					listener);
94
		}
95
96
		protected void firePropertyChange(String propertyName, Object oldValue,
97
				Object newValue) {
98
			propertyChangeSupport.firePropertyChange(propertyName, oldValue,
99
					newValue);
100
		}
101
	}
102
103
	public static class ContactGroup extends AbstractModelObject implements
104
			Comparable {
105
		private String name;
106
		private Set contacts = new TreeSet();
107
108
		ContactGroup(String name) {
109
			this.name = checkNull(name);
110
		}
111
112
		private String checkNull(String string) {
113
			if (string == null)
114
				throw new NullPointerException();
115
			return string;
116
		}
117
118
		public String getName() {
119
			return name;
120
		}
121
122
		public void setName(String name) {
123
			firePropertyChange("name", this.name, this.name = checkNull(name));
124
		}
125
126
		public Set getContacts() {
127
			return new TreeSet(contacts);
128
		}
129
130
		public void addContact(Contact contact) {
131
			Set oldValue = getContacts();
132
			contacts.add(contact);
133
			Set newValue = getContacts();
134
			firePropertyChange("contacts", oldValue, newValue);
135
		}
136
137
		public void removeContact(Contact contact) {
138
			Set oldValue = getContacts();
139
			contacts.remove(contact);
140
			Set newValue = getContacts();
141
			firePropertyChange("contacts", oldValue, newValue);
142
		}
143
144
		public int compareTo(Object o) {
145
			ContactGroup that = (ContactGroup) o;
146
			return this.name.compareTo(that.name);
147
		}
148
	}
149
150
	public static class Contact extends AbstractModelObject implements
151
			Comparable {
152
		private String name;
153
		private String status;
154
155
		private String checkNull(String string) {
156
			if (string == null)
157
				throw new NullPointerException();
158
			return string;
159
		}
160
161
		public Contact(String name, String status) {
162
			this.name = checkNull(name);
163
			this.status = checkNull(status);
164
		}
165
166
		public String getName() {
167
			return name;
168
		}
169
170
		public void setName(String name) {
171
			firePropertyChange("name", this.name, this.name = checkNull(name));
172
		}
173
174
		public String getStatus() {
175
			return status;
176
		}
177
178
		public void setStatus(String status) {
179
			firePropertyChange("status", this.status,
180
					this.status = checkNull(status));
181
		}
182
183
		public int compareTo(Object o) {
184
			Contact that = (Contact) o;
185
			int result = this.name.compareTo(that.name);
186
			if (result == 0)
187
				result = this.status.compareTo(that.status);
188
			return result;
189
		}
190
	}
191
192
	public static class ApplicationModel extends AbstractModelObject {
193
		private Set groups = new TreeSet();
194
195
		public Set getGroups() {
196
			return new TreeSet(groups);
197
		}
198
199
		public void setGroups(Set groups) {
200
			Set oldValue = getGroups();
201
			this.groups = new TreeSet(groups);
202
			Set newValue = getGroups();
203
			firePropertyChange("groups", oldValue, newValue);
204
		}
205
	}
206
207
	/**
208
	 * List property for the "contacts" property of a ContactGroup. Since
209
	 * ContactGroup does not have a setContacts() method we have to write our
210
	 * own property to apply list changes incrementally through the addContact
211
	 * and removeContact methods.
212
	 */
213
	public static class ContactGroupContactsProperty extends SimpleSetProperty {
214
		protected Object getElementType() {
215
			return Contact.class;
216
		}
217
218
		protected Set doGetSet(Object source) {
219
			if (source == null)
220
				return Collections.EMPTY_SET;
221
			return ((ContactGroup) source).getContacts();
222
		}
223
224
		protected boolean setSet(Object source, Set set, SetDiff diff) {
225
			if (source == null)
226
				return false;
227
			ContactGroup group = (ContactGroup) source;
228
			for (Iterator it = diff.getRemovals().iterator(); it.hasNext();) {
229
				Contact contact = (Contact) it.next();
230
				group.removeContact(contact);
231
			}
232
			for (Iterator it = diff.getAdditions().iterator(); it.hasNext();) {
233
				Contact contact = (Contact) it.next();
234
				group.addContact(contact);
235
			}
236
			return true;
237
		}
238
239
		protected INativePropertyListener adaptListener(
240
				final ISetPropertyChangeListener listener) {
241
			return new Listener(listener);
242
		}
243
244
		private class Listener implements INativePropertyListener,
245
				PropertyChangeListener {
246
			private final ISetPropertyChangeListener listener;
247
248
			Listener(ISetPropertyChangeListener listener) {
249
				this.listener = listener;
250
			}
251
252
			public void propertyChange(PropertyChangeEvent evt) {
253
				Object source = evt.getSource();
254
				SimpleSetProperty property = ContactGroupContactsProperty.this;
255
				SetDiff diff = Diffs.computeSetDiff((Set) evt.getOldValue(),
256
						(Set) evt.getNewValue());
257
				listener.handleSetPropertyChange(new SetPropertyChangeEvent(
258
						source, property, diff));
259
			}
260
		}
261
262
		protected void addListener(Object source,
263
				INativePropertyListener listener) {
264
			if (source != null) {
265
				((ContactGroup) source).addPropertyChangeListener("contacts",
266
						(Listener) listener);
267
			}
268
		}
269
270
		protected void removeListener(Object source,
271
				INativePropertyListener listener) {
272
			if (source != null) {
273
				((ContactGroup) source).removePropertyChangeListener(
274
						"contacts", (Listener) listener);
275
			}
276
		}
277
	}
278
279
	public void open() {
280
		model = createDefaultModel();
281
282
		final Display display = Display.getDefault();
283
		createContents();
284
		bindUI();
285
		shell.open();
286
		shell.layout();
287
		while (!shell.isDisposed()) {
288
			if (!display.readAndDispatch())
289
				display.sleep();
290
		}
291
	}
292
293
	/**
294
	 * @return
295
	 */
296
	private ApplicationModel createDefaultModel() {
297
		ContactGroup swtGroup = new ContactGroup("SWT");
298
		swtGroup.addContact(new Contact("Steve Northover", "Busy"));
299
		swtGroup.addContact(new Contact("Grant Gayed", "Online"));
300
		swtGroup.addContact(new Contact("Veronika Irvine", "Offline"));
301
		swtGroup.addContact(new Contact("Mike Wilson", "Online"));
302
		swtGroup.addContact(new Contact("Christophe Cornu", "Idle"));
303
		swtGroup.addContact(new Contact("Lynne Kues", "Online"));
304
		swtGroup.addContact(new Contact("Silenio Quarti", "Idle"));
305
306
		ContactGroup jdbGroup = new ContactGroup("JFace Data Binding");
307
		jdbGroup.addContact(new Contact("Boris Bokowski", "Online"));
308
		jdbGroup.addContact(new Contact("Matthew Hall", "Idle"));
309
310
		Set groups = new TreeSet();
311
		groups.add(swtGroup);
312
		groups.add(jdbGroup);
313
		ApplicationModel model = new ApplicationModel();
314
		model.setGroups(groups);
315
316
		return model;
317
	}
318
319
	/**
320
	 * Create contents of the window
321
	 */
322
	protected void createContents() {
323
		shell = new Shell();
324
		shell.setSize(379, 393);
325
		shell.setText("Snippet026AnonymousBeanProperties");
326
		shell.setLayout(new GridLayout());
327
328
		viewer = new TreeViewer(shell, SWT.BORDER);
329
		tree = viewer.getTree();
330
		tree.setHeaderVisible(true);
331
		tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
332
333
		final TreeColumn nameColumn = new TreeColumn(tree, SWT.NONE);
334
		nameColumn.setWidth(163);
335
		nameColumn.setText("Name");
336
337
		final TreeColumn newColumnTreeColumn = new TreeColumn(tree, SWT.NONE);
338
		newColumnTreeColumn.setWidth(100);
339
		newColumnTreeColumn.setText("Status");
340
	}
341
342
	private void bindUI() {
343
		IObservableFactory treeElementFactory = new DelegatingSetProperty() {
344
			SimpleSetProperty modelGroups = BeanProperties.set(
345
					ApplicationModel.class, "groups");
346
			SimpleSetProperty groupContacts = BeanProperties.set(
347
					ContactGroup.class, "contacts");
348
349
			protected SimpleSetProperty getDelegate(Object source) {
350
				if (source instanceof ApplicationModel)
351
					return modelGroups;
352
				if (source instanceof ContactGroup)
353
					return groupContacts;
354
				return null;
355
			}
356
		}.setFactory();
357
358
		ObservableSetTreeContentProvider cp = new ObservableSetTreeContentProvider(
359
				treeElementFactory, null);
360
		viewer.setContentProvider(cp);
361
362
		IObservableSet knownElements = cp.getKnownElements();
363
		IObservableMap[] labelMaps = new IObservableMap[] {
364
				BeanProperties.value("name").observeDetailValues(knownElements),
365
				BeanProperties.value("status").observeDetailValues(
366
						knownElements) };
367
		viewer.setLabelProvider(new ObservableMapLabelProvider(labelMaps));
368
369
		viewer.setInput(model);
370
371
		viewer.expandAll();
372
	}
373
}
(-)src/org/eclipse/core/databinding/property/map/DelegatingMapProperty.java (+127 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.map;
13
14
import java.util.Collections;
15
import java.util.HashMap;
16
import java.util.Map;
17
18
import org.eclipse.core.databinding.observable.Realm;
19
import org.eclipse.core.databinding.observable.map.IObservableMap;
20
import org.eclipse.core.databinding.observable.map.MapDiff;
21
import org.eclipse.core.databinding.property.INativePropertyListener;
22
23
/**
24
 * @since 3.3
25
 * 
26
 */
27
public abstract class DelegatingMapProperty extends SimpleMapProperty {
28
	private final Object keyType;
29
	private final Object valueType;
30
31
	protected DelegatingMapProperty() {
32
		this(null, null);
33
	}
34
35
	protected DelegatingMapProperty(Object keyType, Object valueType) {
36
		this.keyType = keyType;
37
		this.valueType = valueType;
38
	}
39
40
	protected abstract SimpleMapProperty getDelegate(Object source);
41
42
	protected Object getKeyType() {
43
		return keyType;
44
	}
45
46
	protected Object getValueType() {
47
		return valueType;
48
	}
49
50
	protected Map doGetMap(Object source) {
51
		SimpleMapProperty delegate = getDelegate(source);
52
		if (delegate == null)
53
			return Collections.EMPTY_MAP;
54
		return delegate.doGetMap(source);
55
	}
56
57
	protected boolean setMap(Object source, Map map, MapDiff diff) {
58
		SimpleMapProperty delegate = getDelegate(source);
59
		if (delegate == null)
60
			return false;
61
		return delegate.setMap(source, map, diff);
62
	}
63
64
	protected INativePropertyListener adaptListener(
65
			IMapPropertyChangeListener listener) {
66
		return new Listener(listener);
67
	}
68
69
	private class Listener implements INativePropertyListener,
70
			IMapPropertyChangeListener {
71
		private final IMapPropertyChangeListener listener;
72
73
		private Map nativeListeners;
74
75
		private Listener(IMapPropertyChangeListener listener) {
76
			this.listener = listener;
77
			this.nativeListeners = new HashMap();
78
		}
79
80
		public void handleMapPropertyChange(MapPropertyChangeEvent event) {
81
			listener.handleMapPropertyChange(event);
82
		}
83
84
		private INativePropertyListener getNativeListener(
85
				SimpleMapProperty delegate) {
86
			if (nativeListeners.containsKey(delegate))
87
				return (INativePropertyListener) nativeListeners.get(delegate);
88
89
			INativePropertyListener nativeListener = delegate
90
					.adaptListener(this);
91
			nativeListeners.put(delegate, nativeListener);
92
			return nativeListener;
93
		}
94
95
		private void addListener(Object source) {
96
			SimpleMapProperty delegate = getDelegate(source);
97
			if (delegate != null) {
98
				delegate.addListener(source, getNativeListener(delegate));
99
			}
100
		}
101
102
		private void removeListener(Object source) {
103
			SimpleMapProperty delegate = getDelegate(source);
104
			if (delegate != null) {
105
				delegate.addListener(source, getNativeListener(delegate));
106
			}
107
		}
108
	}
109
110
	protected void addListener(Object source, INativePropertyListener listener) {
111
		if (listener != null)
112
			((Listener) listener).addListener(source);
113
	}
114
115
	protected void removeListener(Object source,
116
			INativePropertyListener listener) {
117
		if (listener != null)
118
			((Listener) listener).removeListener(source);
119
	}
120
121
	public IObservableMap observeMap(Realm realm, Object source) {
122
		SimpleMapProperty delegate = getDelegate(source);
123
		if (delegate != null)
124
			return delegate.observeMap(realm, source);
125
		return super.observeMap(realm, source);
126
	}
127
}
(-)src/org/eclipse/core/databinding/property/list/DelegatingListProperty.java (+122 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.list;
13
14
import java.util.Collections;
15
import java.util.HashMap;
16
import java.util.List;
17
import java.util.Map;
18
19
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.list.IObservableList;
21
import org.eclipse.core.databinding.observable.list.ListDiff;
22
import org.eclipse.core.databinding.property.INativePropertyListener;
23
24
/**
25
 * @since 3.3
26
 * 
27
 */
28
public abstract class DelegatingListProperty extends SimpleListProperty {
29
	private final Object elementType;
30
31
	protected DelegatingListProperty() {
32
		this(null);
33
	}
34
35
	protected DelegatingListProperty(Object elementType) {
36
		this.elementType = elementType;
37
	}
38
39
	protected abstract SimpleListProperty getDelegate(Object source);
40
41
	protected Object getElementType() {
42
		return elementType;
43
	}
44
45
	protected List doGetList(Object source) {
46
		SimpleListProperty delegate = getDelegate(source);
47
		if (delegate == null)
48
			return Collections.EMPTY_LIST;
49
		return delegate.doGetList(source);
50
	}
51
52
	protected boolean setList(Object source, List list, ListDiff diff) {
53
		SimpleListProperty delegate = getDelegate(source);
54
		if (delegate == null)
55
			return false;
56
		return delegate.setList(source, list, diff);
57
	}
58
59
	protected INativePropertyListener adaptListener(
60
			IListPropertyChangeListener listener) {
61
		return new Listener(listener);
62
	}
63
64
	private class Listener implements INativePropertyListener,
65
			IListPropertyChangeListener {
66
		private final IListPropertyChangeListener listener;
67
68
		private Map nativeListeners;
69
70
		private Listener(IListPropertyChangeListener listener) {
71
			this.listener = listener;
72
			this.nativeListeners = new HashMap();
73
		}
74
75
		public void handleListPropertyChange(ListPropertyChangeEvent event) {
76
			listener.handleListPropertyChange(event);
77
		}
78
79
		private INativePropertyListener getNativeListener(
80
				SimpleListProperty delegate) {
81
			if (nativeListeners.containsKey(delegate))
82
				return (INativePropertyListener) nativeListeners.get(delegate);
83
84
			INativePropertyListener nativeListener = delegate
85
					.adaptListener(this);
86
			nativeListeners.put(delegate, nativeListener);
87
			return nativeListener;
88
		}
89
90
		private void addListener(Object source) {
91
			SimpleListProperty delegate = getDelegate(source);
92
			if (delegate != null) {
93
				delegate.addListener(source, getNativeListener(delegate));
94
			}
95
		}
96
97
		private void removeListener(Object source) {
98
			SimpleListProperty delegate = getDelegate(source);
99
			if (delegate != null) {
100
				delegate.addListener(source, getNativeListener(delegate));
101
			}
102
		}
103
	}
104
105
	protected void addListener(Object source, INativePropertyListener listener) {
106
		if (listener != null)
107
			((Listener) listener).addListener(source);
108
	}
109
110
	protected void removeListener(Object source,
111
			INativePropertyListener listener) {
112
		if (listener != null)
113
			((Listener) listener).removeListener(source);
114
	}
115
116
	public IObservableList observeList(Realm realm, Object source) {
117
		SimpleListProperty delegate = getDelegate(source);
118
		if (delegate != null)
119
			return delegate.observeList(realm, source);
120
		return super.observeList(realm, source);
121
	}
122
}
(-)src/org/eclipse/core/databinding/property/value/DelegatingValueProperty.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
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.value;
13
14
import java.util.HashMap;
15
import java.util.Map;
16
17
import org.eclipse.core.databinding.observable.Realm;
18
import org.eclipse.core.databinding.observable.value.IObservableValue;
19
import org.eclipse.core.databinding.property.INativePropertyListener;
20
21
/**
22
 * @since 3.3
23
 * 
24
 */
25
public abstract class DelegatingValueProperty extends SimpleValueProperty {
26
	private final Object valueType;
27
28
	protected DelegatingValueProperty() {
29
		this(null);
30
	}
31
32
	protected DelegatingValueProperty(Object valueType) {
33
		this.valueType = valueType;
34
	}
35
36
	protected abstract SimpleValueProperty getDelegate(Object source);
37
38
	protected Object getValueType() {
39
		return valueType;
40
	}
41
42
	protected Object getValue(Object source) {
43
		SimpleValueProperty delegate = getDelegate(source);
44
		if (delegate == null)
45
			return null;
46
		return delegate.getValue(source);
47
	}
48
49
	protected boolean setValue(Object source, Object value) {
50
		SimpleValueProperty delegate = getDelegate(source);
51
		if (delegate == null)
52
			return false;
53
		return delegate.setValue(source, value);
54
	}
55
56
	public INativePropertyListener adaptListener(
57
			final IValuePropertyChangeListener listener) {
58
		return new Listener(listener);
59
	}
60
61
	private class Listener implements INativePropertyListener,
62
			IValuePropertyChangeListener {
63
		private final IValuePropertyChangeListener listener;
64
65
		private Map nativeListeners;
66
67
		private Listener(IValuePropertyChangeListener listener) {
68
			this.listener = listener;
69
			this.nativeListeners = new HashMap();
70
		}
71
72
		public void handleValuePropertyChange(ValuePropertyChangeEvent event) {
73
			listener.handleValuePropertyChange(event);
74
		}
75
76
		private INativePropertyListener getNativeListener(
77
				SimpleValueProperty delegate) {
78
			if (nativeListeners.containsKey(delegate))
79
				return (INativePropertyListener) nativeListeners.get(delegate);
80
81
			INativePropertyListener nativeListener = delegate
82
					.adaptListener(this);
83
			nativeListeners.put(delegate, nativeListener);
84
			return nativeListener;
85
		}
86
87
		private void addListener(Object source) {
88
			SimpleValueProperty delegate = getDelegate(source);
89
			if (delegate != null) {
90
				delegate.addListener(source, getNativeListener(delegate));
91
			}
92
		}
93
94
		private void removeListener(Object source) {
95
			SimpleValueProperty delegate = getDelegate(source);
96
			if (delegate != null) {
97
				delegate.addListener(source, getNativeListener(delegate));
98
			}
99
		}
100
	}
101
102
	public void addListener(Object source, INativePropertyListener listener) {
103
		if (listener != null)
104
			((Listener) listener).addListener(source);
105
	}
106
107
	public void removeListener(Object source, INativePropertyListener listener) {
108
		if (listener != null)
109
			((Listener) listener).removeListener(source);
110
	}
111
112
	public IObservableValue observeValue(Realm realm, Object source) {
113
		SimpleValueProperty delegate = getDelegate(source);
114
		if (delegate != null)
115
			return delegate.observeValue(realm, source);
116
		return super.observeValue(realm, source);
117
	}
118
}
(-)src/org/eclipse/core/databinding/property/set/DelegatingSetProperty.java (+122 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property.set;
13
14
import java.util.Collections;
15
import java.util.HashMap;
16
import java.util.Map;
17
import java.util.Set;
18
19
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.set.IObservableSet;
21
import org.eclipse.core.databinding.observable.set.SetDiff;
22
import org.eclipse.core.databinding.property.INativePropertyListener;
23
24
/**
25
 * @since 3.3
26
 * 
27
 */
28
public abstract class DelegatingSetProperty extends SimpleSetProperty {
29
	private final Object elementType;
30
31
	protected DelegatingSetProperty() {
32
		this(null);
33
	}
34
35
	protected DelegatingSetProperty(Object elementType) {
36
		this.elementType = elementType;
37
	}
38
39
	protected abstract SimpleSetProperty getDelegate(Object source);
40
41
	protected Object getElementType() {
42
		return elementType;
43
	}
44
45
	protected Set doGetSet(Object source) {
46
		SimpleSetProperty delegate = getDelegate(source);
47
		if (delegate == null)
48
			return Collections.EMPTY_SET;
49
		return delegate.doGetSet(source);
50
	}
51
52
	protected boolean setSet(Object source, Set set, SetDiff diff) {
53
		SimpleSetProperty delegate = getDelegate(source);
54
		if (delegate == null)
55
			return false;
56
		return delegate.setSet(source, set, diff);
57
	}
58
59
	protected INativePropertyListener adaptListener(
60
			ISetPropertyChangeListener listener) {
61
		return new Listener(listener);
62
	}
63
64
	private class Listener implements INativePropertyListener,
65
			ISetPropertyChangeListener {
66
		private final ISetPropertyChangeListener listener;
67
68
		private Map nativeListeners;
69
70
		private Listener(ISetPropertyChangeListener listener) {
71
			this.listener = listener;
72
			this.nativeListeners = new HashMap();
73
		}
74
75
		public void handleSetPropertyChange(SetPropertyChangeEvent event) {
76
			listener.handleSetPropertyChange(event);
77
		}
78
79
		private INativePropertyListener getNativeListener(
80
				SimpleSetProperty delegate) {
81
			if (nativeListeners.containsKey(delegate))
82
				return (INativePropertyListener) nativeListeners.get(delegate);
83
84
			INativePropertyListener nativeListener = delegate
85
					.adaptListener(this);
86
			nativeListeners.put(delegate, nativeListener);
87
			return nativeListener;
88
		}
89
90
		private void addListener(Object source) {
91
			SimpleSetProperty delegate = getDelegate(source);
92
			if (delegate != null) {
93
				delegate.addListener(source, getNativeListener(delegate));
94
			}
95
		}
96
97
		private void removeListener(Object source) {
98
			SimpleSetProperty delegate = getDelegate(source);
99
			if (delegate != null) {
100
				delegate.addListener(source, getNativeListener(delegate));
101
			}
102
		}
103
	}
104
105
	protected void addListener(Object source, INativePropertyListener listener) {
106
		if (listener != null)
107
			((Listener) listener).addListener(source);
108
	}
109
110
	protected void removeListener(Object source,
111
			INativePropertyListener listener) {
112
		if (listener != null)
113
			((Listener) listener).removeListener(source);
114
	}
115
116
	public IObservableSet observeSet(Realm realm, Object source) {
117
		SimpleSetProperty delegate = getDelegate(source);
118
		if (delegate != null)
119
			return delegate.observeSet(realm, source);
120
		return super.observeSet(realm, source);
121
	}
122
}

Return to bug 194734