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/jface/internal/databinding/swt/SWTProperties.java (-2 / +3 lines)
Lines 16-22 Link Here
16
 * Constants used to describe properties of SWT controls.
16
 * Constants used to describe properties of SWT controls.
17
 * 
17
 * 
18
 * @since 1.0
18
 * @since 1.0
19
 *
19
 * 
20
 */
20
 */
21
public interface SWTProperties {
21
public interface SWTProperties {
22
22
Lines 24-29 Link Here
24
	 * Applies to Control
24
	 * Applies to Control
25
	 */
25
	 */
26
	public static final String ENABLED = "enabled"; //$NON-NLS-1$
26
	public static final String ENABLED = "enabled"; //$NON-NLS-1$
27
27
	/**
28
	/**
28
	 * Applies to Control
29
	 * Applies to Control
29
	 */
30
	 */
Lines 56-62 Link Here
56
	 * Applies to Text, Label, Combo
57
	 * Applies to Text, Label, Combo
57
	 */
58
	 */
58
	public static final String TEXT = "text"; //$NON-NLS-1$
59
	public static final String TEXT = "text"; //$NON-NLS-1$
59
	
60
60
	/**
61
	/**
61
	 * Applies to Label, CLabel.
62
	 * Applies to Label, CLabel.
62
	 */
63
	 */
(-)src/org/eclipse/jface/internal/databinding/swt/ControlObservableValue.java (-106 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *     Brad Reynolds - bug 164653
11
 *     Matt Carter - bug 170668
12
 *     Brad Reynolds - bug 170848
13
 *******************************************************************************/
14
package org.eclipse.jface.internal.databinding.swt;
15
16
import java.util.HashMap;
17
import java.util.Map;
18
19
import org.eclipse.core.databinding.observable.Diffs;
20
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTObservableValue;
21
import org.eclipse.swt.graphics.Color;
22
import org.eclipse.swt.graphics.Font;
23
import org.eclipse.swt.widgets.Control;
24
25
/**
26
 * @since 1.0
27
 * 
28
 */
29
public class ControlObservableValue extends AbstractSWTObservableValue {
30
31
	private final Control control;
32
33
	private final String attribute;
34
35
	private Object valueType;
36
	
37
	private static final Map SUPPORTED_ATTRIBUTES = new HashMap();
38
	static {
39
		SUPPORTED_ATTRIBUTES.put(SWTProperties.ENABLED, Boolean.TYPE);
40
		SUPPORTED_ATTRIBUTES.put(SWTProperties.VISIBLE, Boolean.TYPE);
41
		SUPPORTED_ATTRIBUTES.put(SWTProperties.TOOLTIP_TEXT, String.class);
42
		SUPPORTED_ATTRIBUTES.put(SWTProperties.FOREGROUND, Color.class);
43
		SUPPORTED_ATTRIBUTES.put(SWTProperties.BACKGROUND, Color.class);
44
		SUPPORTED_ATTRIBUTES.put(SWTProperties.FONT, Font.class);
45
	}
46
	
47
	/**
48
	 * @param control
49
	 * @param attribute
50
	 */
51
	public ControlObservableValue(Control control, String attribute) {
52
		super(control);
53
		this.control = control;
54
		this.attribute = attribute;
55
		if (SUPPORTED_ATTRIBUTES.keySet().contains(attribute)) {
56
			this.valueType = SUPPORTED_ATTRIBUTES.get(attribute); 
57
		} else {
58
			throw new IllegalArgumentException();
59
		}
60
	}
61
62
	public void doSetValue(Object value) {
63
		Object oldValue = doGetValue();
64
		if (attribute.equals(SWTProperties.ENABLED)) {
65
			control.setEnabled(((Boolean) value).booleanValue());
66
		} else if (attribute.equals(SWTProperties.VISIBLE)) {
67
			control.setVisible(((Boolean) value).booleanValue());
68
		} else if (attribute.equals(SWTProperties.TOOLTIP_TEXT)) {
69
			control.setToolTipText((String) value);
70
		} else if (attribute.equals(SWTProperties.FOREGROUND)) {
71
			control.setForeground((Color) value);
72
		} else if (attribute.equals(SWTProperties.BACKGROUND)) {
73
			control.setBackground((Color) value);
74
		} else if (attribute.equals(SWTProperties.FONT)) {
75
			control.setFont((Font) value);
76
		}
77
		fireValueChange(Diffs.createValueDiff(oldValue, value));
78
	}
79
80
	public Object doGetValue() {
81
		if (attribute.equals(SWTProperties.ENABLED)) {
82
			return control.getEnabled() ? Boolean.TRUE : Boolean.FALSE;
83
		}
84
		if (attribute.equals(SWTProperties.VISIBLE)) {
85
			return control.getVisible() ? Boolean.TRUE : Boolean.FALSE;
86
		}
87
		if (attribute.equals(SWTProperties.TOOLTIP_TEXT)) {
88
			return control.getToolTipText();			
89
		}
90
		if (attribute.equals(SWTProperties.FOREGROUND))	 {
91
			return control.getForeground();
92
		}
93
		if (attribute.equals(SWTProperties.BACKGROUND)) {
94
			return control.getBackground();
95
		}
96
		if (attribute.equals(SWTProperties.FONT)) {
97
			return control.getFont();
98
		}
99
		
100
		return null;
101
	}
102
103
	public Object getValueType() {
104
		return valueType;
105
	}
106
}
(-)src/org/eclipse/jface/databinding/swt/SWTObservables.java (-22 / +39 lines)
Lines 19-24 Link Here
19
19
20
import org.eclipse.core.databinding.observable.Realm;
20
import org.eclipse.core.databinding.observable.Realm;
21
import org.eclipse.core.databinding.observable.list.IObservableList;
21
import org.eclipse.core.databinding.observable.list.IObservableList;
22
import org.eclipse.core.databinding.property.IValueProperty;
23
import org.eclipse.core.databinding.property.PropertyObservables;
24
import org.eclipse.jface.internal.databinding.internal.swt.LinkObservableValue;
22
import org.eclipse.jface.internal.databinding.swt.ButtonObservableValue;
25
import org.eclipse.jface.internal.databinding.swt.ButtonObservableValue;
23
import org.eclipse.jface.internal.databinding.swt.CComboObservableList;
26
import org.eclipse.jface.internal.databinding.swt.CComboObservableList;
24
import org.eclipse.jface.internal.databinding.swt.CComboObservableValue;
27
import org.eclipse.jface.internal.databinding.swt.CComboObservableValue;
Lines 26-34 Link Here
26
import org.eclipse.jface.internal.databinding.swt.CLabelObservableValue;
29
import org.eclipse.jface.internal.databinding.swt.CLabelObservableValue;
27
import org.eclipse.jface.internal.databinding.swt.ComboObservableList;
30
import org.eclipse.jface.internal.databinding.swt.ComboObservableList;
28
import org.eclipse.jface.internal.databinding.swt.ComboObservableValue;
31
import org.eclipse.jface.internal.databinding.swt.ComboObservableValue;
29
import org.eclipse.jface.internal.databinding.internal.swt.LinkObservableValue;
30
import org.eclipse.jface.internal.databinding.swt.ComboSingleSelectionObservableValue;
32
import org.eclipse.jface.internal.databinding.swt.ComboSingleSelectionObservableValue;
31
import org.eclipse.jface.internal.databinding.swt.ControlObservableValue;
32
import org.eclipse.jface.internal.databinding.swt.DelayedObservableValue;
33
import org.eclipse.jface.internal.databinding.swt.DelayedObservableValue;
33
import org.eclipse.jface.internal.databinding.swt.LabelObservableValue;
34
import org.eclipse.jface.internal.databinding.swt.LabelObservableValue;
34
import org.eclipse.jface.internal.databinding.swt.ListObservableList;
35
import org.eclipse.jface.internal.databinding.swt.ListObservableList;
Lines 41-46 Link Here
41
import org.eclipse.jface.internal.databinding.swt.TableSingleSelectionObservableValue;
42
import org.eclipse.jface.internal.databinding.swt.TableSingleSelectionObservableValue;
42
import org.eclipse.jface.internal.databinding.swt.TextEditableObservableValue;
43
import org.eclipse.jface.internal.databinding.swt.TextEditableObservableValue;
43
import org.eclipse.jface.internal.databinding.swt.TextObservableValue;
44
import org.eclipse.jface.internal.databinding.swt.TextObservableValue;
45
import org.eclipse.jface.internal.databinding.swt.WidgetObservableValueDecorator;
44
import org.eclipse.swt.custom.CCombo;
46
import org.eclipse.swt.custom.CCombo;
45
import org.eclipse.swt.custom.CLabel;
47
import org.eclipse.swt.custom.CLabel;
46
import org.eclipse.swt.widgets.Button;
48
import org.eclipse.swt.widgets.Button;
Lines 88-100 Link Here
88
90
89
	/**
91
	/**
90
	 * Returns an observable which delays notification of value change events
92
	 * Returns an observable which delays notification of value change events
91
	 * from <code>observable</code> until <code>delay</code> milliseconds
93
	 * from <code>observable</code> until <code>delay</code> milliseconds have
92
	 * have passed since the last change event, or until a FocusOut event is
94
	 * passed since the last change event, or until a FocusOut event is received
93
	 * received from the underlying widget (whichever happens earlier). This
95
	 * from the underlying widget (whichever happens earlier). This class helps
94
	 * class helps to delay validation until the user stops typing. To notify
96
	 * to delay validation until the user stops typing. To notify about pending
95
	 * about pending changes, the returned observable value will fire a stale
97
	 * changes, the returned observable value will fire a stale event when the
96
	 * event when the wrapped observable value fires a change event, but this
98
	 * wrapped observable value fires a change event, but this change is being
97
	 * change is being delayed.
99
	 * delayed.
98
	 * 
100
	 * 
99
	 * @param delay
101
	 * @param delay
100
	 * @param observable
102
	 * @param observable
Lines 104-111 Link Here
104
	 * 
106
	 * 
105
	 * @since 1.2
107
	 * @since 1.2
106
	 */
108
	 */
107
	public static ISWTObservableValue observeDelayedValue(int delay, ISWTObservableValue observable) {
109
	public static ISWTObservableValue observeDelayedValue(int delay,
108
	  return new DelayedObservableValue(delay, observable);
110
			ISWTObservableValue observable) {
111
		return new DelayedObservableValue(delay, observable);
109
	}
112
	}
110
113
111
	/**
114
	/**
Lines 114-120 Link Here
114
	 *         control
117
	 *         control
115
	 */
118
	 */
116
	public static ISWTObservableValue observeEnabled(Control control) {
119
	public static ISWTObservableValue observeEnabled(Control control) {
117
		return new ControlObservableValue(control, SWTProperties.ENABLED);
120
		return observeWidgetPropertyValue(control, ControlProperties
121
				.enabledProperty());
118
	}
122
	}
119
123
120
	/**
124
	/**
Lines 123-129 Link Here
123
	 *         control
127
	 *         control
124
	 */
128
	 */
125
	public static ISWTObservableValue observeVisible(Control control) {
129
	public static ISWTObservableValue observeVisible(Control control) {
126
		return new ControlObservableValue(control, SWTProperties.VISIBLE);
130
		return observeWidgetPropertyValue(control, ControlProperties
131
				.visibleProperty());
132
	}
133
134
	private static ISWTObservableValue observeWidgetPropertyValue(
135
			Control control, IValueProperty property) {
136
		return new WidgetObservableValueDecorator(PropertyObservables
137
				.observeValue(control, property), control);
127
	}
138
	}
128
139
129
	/**
140
	/**
Lines 132-138 Link Here
132
	 *         control
143
	 *         control
133
	 */
144
	 */
134
	public static ISWTObservableValue observeTooltipText(Control control) {
145
	public static ISWTObservableValue observeTooltipText(Control control) {
135
		return new ControlObservableValue(control, SWTProperties.TOOLTIP_TEXT);
146
		return observeWidgetPropertyValue(control, ControlProperties
147
				.toolTipTextProperty());
136
	}
148
	}
137
149
138
	/**
150
	/**
Lines 233-240 Link Here
233
	 * </ul>
245
	 * </ul>
234
	 * 
246
	 * 
235
	 * <li>org.eclipse.swt.widgets.Label</li>
247
	 * <li>org.eclipse.swt.widgets.Label</li>
248
	 * 
236
	 * @param control
249
	 * @param control
237
	 * @param event event type to register for change events
250
	 * @param event
251
	 *            event type to register for change events
238
	 * @return observable value
252
	 * @return observable value
239
	 * @throws IllegalArgumentException
253
	 * @throws IllegalArgumentException
240
	 *             if <code>control</code> type is unsupported
254
	 *             if <code>control</code> type is unsupported
Lines 349-355 Link Here
349
	 *         control
363
	 *         control
350
	 */
364
	 */
351
	public static ISWTObservableValue observeForeground(Control control) {
365
	public static ISWTObservableValue observeForeground(Control control) {
352
		return new ControlObservableValue(control, SWTProperties.FOREGROUND);
366
		return observeWidgetPropertyValue(control, ControlProperties
367
				.foregroundProperty());
353
	}
368
	}
354
369
355
	/**
370
	/**
Lines 358-364 Link Here
358
	 *         control
373
	 *         control
359
	 */
374
	 */
360
	public static ISWTObservableValue observeBackground(Control control) {
375
	public static ISWTObservableValue observeBackground(Control control) {
361
		return new ControlObservableValue(control, SWTProperties.BACKGROUND);
376
		return observeWidgetPropertyValue(control, ControlProperties
377
				.backgroundProperty());
362
	}
378
	}
363
379
364
	/**
380
	/**
Lines 366-377 Link Here
366
	 * @return an observable value tracking the font of the given control
382
	 * @return an observable value tracking the font of the given control
367
	 */
383
	 */
368
	public static ISWTObservableValue observeFont(Control control) {
384
	public static ISWTObservableValue observeFont(Control control) {
369
		return new ControlObservableValue(control, SWTProperties.FONT);
385
		return observeWidgetPropertyValue(control, ControlProperties
386
				.fontProperty());
370
	}
387
	}
371
	
388
372
	/**
389
	/**
373
	 * Returns an observable observing the editable attribute of
390
	 * Returns an observable observing the editable attribute of the provided
374
	 * the provided <code>control</code>. The supported types are:
391
	 * <code>control</code>. The supported types are:
375
	 * <ul>
392
	 * <ul>
376
	 * <li>org.eclipse.swt.widgets.Text</li>
393
	 * <li>org.eclipse.swt.widgets.Text</li>
377
	 * </ul>
394
	 * </ul>
Lines 385-391 Link Here
385
		if (control instanceof Text) {
402
		if (control instanceof Text) {
386
			return new TextEditableObservableValue((Text) control);
403
			return new TextEditableObservableValue((Text) control);
387
		}
404
		}
388
		
405
389
		throw new IllegalArgumentException(
406
		throw new IllegalArgumentException(
390
				"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
407
				"Widget [" + control.getClass().getName() + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$
391
	}
408
	}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlFontProperty.java (+33 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.graphics.Font;
15
import org.eclipse.swt.widgets.Control;
16
17
/**
18
 * @since 3.3
19
 *
20
 */
21
public class ControlFontProperty extends ControlValueProperty {
22
	public Object getValueType() {
23
		return Font.class;
24
	}
25
26
	public Object doGetValue(Control control) {
27
		return control.getFont();
28
	}
29
30
	public void doSetValue(Control control, Object value) {
31
		control.setFont((Font) value);
32
	}
33
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlValueProperty.java (+45 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.core.databinding.observable.Diffs;
15
import org.eclipse.core.databinding.property.ValueProperty;
16
import org.eclipse.jface.util.Util;
17
import org.eclipse.swt.widgets.Control;
18
19
abstract class ControlValueProperty extends ValueProperty {
20
	protected void addListenerTo(Object source) {
21
		// no op
22
	}
23
24
	protected void removeListenerFrom(Object source) {
25
		// no op
26
	}
27
28
	public Object getValue(Object source) {
29
		return doGetValue((Control) source);
30
	}
31
32
	public void setValue(Object source, Object value) {
33
		Object oldValue = getValue(source);
34
35
		doSetValue((Control) source, value);
36
37
		if (hasListeners(source) && !Util.equals(oldValue, value)) {
38
			fireValueChange(source, Diffs.createValueDiff(oldValue, value));
39
		}
40
	}
41
42
	abstract Object doGetValue(Control control);
43
44
	abstract void doSetValue(Control control, Object value);
45
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlForegroundProperty.java (+33 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.graphics.Color;
15
import org.eclipse.swt.widgets.Control;
16
17
/**
18
 * @since 3.3
19
 *
20
 */
21
public class ControlForegroundProperty extends ControlValueProperty {
22
	public Object getValueType() {
23
		return Color.class;
24
	}
25
26
	public Object doGetValue(Control control) {
27
		return control.getForeground();
28
	}
29
30
	public void doSetValue(Control control, Object value) {
31
		control.setForeground((Color) value);
32
	}
33
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlVisibleProperty.java (+32 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
16
/**
17
 * @since 3.3
18
 *
19
 */
20
public class ControlVisibleProperty extends ControlValueProperty {
21
	public Object getValueType() {
22
		return Boolean.TYPE;
23
	}
24
25
	public Object doGetValue(Control control) {
26
		return Boolean.valueOf(control.getVisible());
27
	}
28
29
	public void doSetValue(Control control, Object value) {
30
		control.setVisible(((Boolean) value).booleanValue());
31
	}
32
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlEnabledProperty.java (+32 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
16
/**
17
 * @since 3.3
18
 *
19
 */
20
public class ControlEnabledProperty extends ControlValueProperty {
21
	public Object getValueType() {
22
		return Boolean.TYPE;
23
	}
24
25
	public Object doGetValue(Control control) {
26
		return Boolean.valueOf(control.getEnabled());
27
	}
28
29
	public void doSetValue(Control control, Object value) {
30
		control.setEnabled(((Boolean) value).booleanValue());
31
	}
32
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlToolTipTextProperty.java (+32 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.widgets.Control;
15
16
/**
17
 * @since 3.3
18
 *
19
 */
20
public class ControlToolTipTextProperty extends ControlValueProperty {
21
	public Object getValueType() {
22
		return String.class;
23
	}
24
25
	public Object doGetValue(Control control) {
26
		return control.getToolTipText();
27
	}
28
29
	public void doSetValue(Control control, Object value) {
30
		control.setToolTipText((String) value);
31
	}
32
}
(-)src/org/eclipse/jface/databinding/swt/ControlProperties.java (+68 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.databinding.swt;
13
14
import org.eclipse.core.databinding.property.IValueProperty;
15
import org.eclipse.jface.internal.databinding.swt.ControlBackgroundProperty;
16
import org.eclipse.jface.internal.databinding.swt.ControlEnabledProperty;
17
import org.eclipse.jface.internal.databinding.swt.ControlFontProperty;
18
import org.eclipse.jface.internal.databinding.swt.ControlForegroundProperty;
19
import org.eclipse.jface.internal.databinding.swt.ControlVisibleProperty;
20
import org.eclipse.jface.internal.databinding.swt.ControlToolTipTextProperty;
21
 
22
/**
23
 * @since 1.3
24
 * 
25
 */
26
public class ControlProperties {
27
	/**
28
	 * @return blah
29
	 */
30
	public static IValueProperty enabledProperty() {
31
		return new ControlEnabledProperty();
32
	}
33
34
	/**
35
	 * @return blah
36
	 */
37
	public static IValueProperty visibleProperty() {
38
		return new ControlVisibleProperty();
39
	}
40
41
	/**
42
	 * @return blah
43
	 */
44
	public static IValueProperty toolTipTextProperty() {
45
		return new ControlToolTipTextProperty();
46
	}
47
48
	/**
49
	 * @return blah
50
	 */
51
	public static IValueProperty foregroundProperty() {
52
		return new ControlForegroundProperty();
53
	}
54
55
	/**
56
	 * @return blah
57
	 */
58
	public static IValueProperty backgroundProperty() {
59
		return new ControlBackgroundProperty();
60
	}
61
62
	/**
63
	 * @return blah
64
	 */
65
	public static IValueProperty fontProperty() {
66
		return new ControlFontProperty();
67
	}
68
}
(-)src/org/eclipse/jface/internal/databinding/swt/ControlBackgroundProperty.java (+33 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.swt.graphics.Color;
15
import org.eclipse.swt.widgets.Control;
16
17
/**
18
 * @since 3.3
19
 *
20
 */
21
public class ControlBackgroundProperty extends ControlValueProperty {
22
	public Object getValueType() {
23
		return Color.class;
24
	}
25
26
	public Object doGetValue(Control control) {
27
		return control.getBackground();
28
	}
29
30
	public void doSetValue(Control control, Object value) {
31
		control.setBackground((Color) value);
32
	}
33
}
(-)src/org/eclipse/jface/internal/databinding/swt/WidgetObservableValueDecorator.java (+123 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 The Pampered Chef, Inc. and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     The Pampered Chef, Inc. - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.jface.internal.databinding.swt;
13
14
import org.eclipse.core.databinding.observable.IChangeListener;
15
import org.eclipse.core.databinding.observable.IStaleListener;
16
import org.eclipse.core.databinding.observable.ObservableTracker;
17
import org.eclipse.core.databinding.observable.Realm;
18
import org.eclipse.core.databinding.observable.value.IObservableValue;
19
import org.eclipse.core.databinding.observable.value.IValueChangeListener;
20
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
21
import org.eclipse.jface.util.Util;
22
import org.eclipse.swt.events.DisposeEvent;
23
import org.eclipse.swt.events.DisposeListener;
24
import org.eclipse.swt.widgets.Widget;
25
26
/**
27
 * @since 3.3
28
 *
29
 */
30
public class WidgetObservableValueDecorator implements ISWTObservableValue {
31
	private final IObservableValue delegate;
32
	private final Widget widget;
33
34
	/**
35
	 * @param delegate
36
	 * @param widget
37
	 */
38
	public WidgetObservableValueDecorator(IObservableValue delegate,
39
			Widget widget) {
40
		this.delegate = delegate;
41
		this.widget = widget;
42
		widget.addDisposeListener(disposeListener);
43
	}
44
45
	private DisposeListener disposeListener = new DisposeListener() {
46
		public void widgetDisposed(DisposeEvent e) {
47
			WidgetObservableValueDecorator.this.dispose();
48
		}
49
	};
50
51
	public void addChangeListener(IChangeListener listener) {
52
		delegate.addChangeListener(listener);
53
	}
54
55
	public void addStaleListener(IStaleListener listener) {
56
		delegate.addStaleListener(listener);
57
	}
58
59
	public void addValueChangeListener(IValueChangeListener listener) {
60
		delegate.addValueChangeListener(listener);
61
	}
62
63
	public void dispose() {
64
		delegate.dispose();
65
	}
66
67
	public boolean equals(Object obj) {
68
		if (obj instanceof WidgetObservableValueDecorator) {
69
			WidgetObservableValueDecorator other = (WidgetObservableValueDecorator) obj;
70
			return Util.equals(other.delegate, delegate);
71
		}
72
		return Util.equals(delegate, obj);
73
	}
74
75
	public Realm getRealm() {
76
		return delegate.getRealm();
77
	}
78
79
	public Object getValue() {
80
		getterCalled();
81
		return delegate.getValue();
82
	}
83
84
	private void getterCalled() {
85
		ObservableTracker.getterCalled(this);
86
	}
87
88
	public Object getValueType() {
89
		return delegate.getValueType();
90
	}
91
92
	public int hashCode() {
93
		return delegate.hashCode();
94
	}
95
96
	public boolean isStale() {
97
		getterCalled();
98
		return delegate.isStale();
99
	}
100
101
	public void removeChangeListener(IChangeListener listener) {
102
		delegate.removeChangeListener(listener);
103
	}
104
105
	public void removeStaleListener(IStaleListener listener) {
106
		delegate.removeStaleListener(listener);
107
	}
108
109
	public void removeValueChangeListener(IValueChangeListener listener) {
110
		delegate.removeValueChangeListener(listener);
111
	}
112
113
	public void setValue(Object value) {
114
		delegate.setValue(value);
115
	}
116
117
	/**
118
	 * @return Returns the widget.
119
	 */
120
	public Widget getWidget() {
121
		return widget;
122
	}
123
}
(-)src/org/eclipse/core/tests/internal/databinding/beans/JavaBeanObservableMapTest.java (-9 / +22 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Matthew Hall - bugs 213145, 241585
10
 *     Matthew Hall - bugs 213145, 241585, 194734
11
 *******************************************************************************/
11
 *******************************************************************************/
12
12
13
package org.eclipse.core.tests.internal.databinding.beans;
13
package org.eclipse.core.tests.internal.databinding.beans;
Lines 19-30 Link Here
19
import junit.framework.TestCase;
19
import junit.framework.TestCase;
20
import junit.framework.TestSuite;
20
import junit.framework.TestSuite;
21
21
22
import org.eclipse.core.databinding.beans.BeanProperties;
23
import org.eclipse.core.databinding.beans.BeansObservables;
24
import org.eclipse.core.databinding.beans.IBeanObservable;
25
import org.eclipse.core.databinding.beans.IBeanProperty;
26
import org.eclipse.core.databinding.beans.PojoObservables;
22
import org.eclipse.core.databinding.observable.Realm;
27
import org.eclipse.core.databinding.observable.Realm;
23
import org.eclipse.core.databinding.observable.map.IMapChangeListener;
28
import org.eclipse.core.databinding.observable.map.IMapChangeListener;
29
import org.eclipse.core.databinding.observable.map.IObservableMap;
24
import org.eclipse.core.databinding.observable.map.MapChangeEvent;
30
import org.eclipse.core.databinding.observable.map.MapChangeEvent;
25
import org.eclipse.core.databinding.observable.map.MapDiff;
31
import org.eclipse.core.databinding.observable.map.MapDiff;
26
import org.eclipse.core.databinding.observable.set.WritableSet;
32
import org.eclipse.core.databinding.observable.set.WritableSet;
27
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableMap;
28
import org.eclipse.core.tests.databinding.observable.ThreadRealm;
33
import org.eclipse.core.tests.databinding.observable.ThreadRealm;
29
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
34
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
30
import org.eclipse.jface.databinding.conformance.util.CurrentRealm;
35
import org.eclipse.jface.databinding.conformance.util.CurrentRealm;
Lines 42-48 Link Here
42
47
43
	private PropertyDescriptor propertyDescriptor;
48
	private PropertyDescriptor propertyDescriptor;
44
49
45
	private JavaBeanObservableMap map;
50
	private IObservableMap map;
51
	private IBeanObservable beanObservable;
46
52
47
	protected void setUp() throws Exception {
53
	protected void setUp() throws Exception {
48
		ThreadRealm realm = new ThreadRealm();
54
		ThreadRealm realm = new ThreadRealm();
Lines 54-61 Link Here
54
		set.add(model1);
60
		set.add(model1);
55
		set.add(model2);
61
		set.add(model2);
56
62
57
		propertyDescriptor = new PropertyDescriptor("value", Bean.class);
63
		String propertyName = "value";
58
		map = new JavaBeanObservableMap(set, propertyDescriptor);
64
		propertyDescriptor = ((IBeanProperty) BeanProperties.valueProperty(
65
				Bean.class, propertyName)).getPropertyDescriptor();
66
		map = BeansObservables.observeMap(set, Bean.class, propertyName);
67
		beanObservable = (IBeanObservable) map;
59
	}
68
	}
60
69
61
	public void testGetValue() throws Exception {
70
	public void testGetValue() throws Exception {
Lines 135-145 Link Here
135
	}
144
	}
136
	
145
	
137
	public void testGetObserved() throws Exception {
146
	public void testGetObserved() throws Exception {
138
		assertEquals(set, map.getObserved());
147
		assertEquals(set, beanObservable.getObserved());
139
	}
148
	}
140
	
149
	
141
	public void testGetPropertyDescriptor() throws Exception {
150
	public void testGetPropertyDescriptor() throws Exception {
142
		assertEquals(propertyDescriptor, map.getPropertyDescriptor());
151
		assertEquals(propertyDescriptor, beanObservable.getPropertyDescriptor());
143
	}
152
	}
144
	
153
	
145
	public void testConstructor_SkipRegisterListeners() throws Exception {
154
	public void testConstructor_SkipRegisterListeners() throws Exception {
Lines 148-154 Link Here
148
		Bean bean = new Bean();
157
		Bean bean = new Bean();
149
		set.add(bean);
158
		set.add(bean);
150
		
159
		
151
		JavaBeanObservableMap observable = new JavaBeanObservableMap(set, new PropertyDescriptor("value", Bean.class), false);
160
		IObservableMap observable = PojoObservables.observeMap(set, Bean.class,
161
				"value");
162
		assertFalse(bean.hasListeners("value"));
152
		ChangeEventTracker.observe(observable);
163
		ChangeEventTracker.observe(observable);
153
164
154
		assertFalse(bean.hasListeners("value"));
165
		assertFalse(bean.hasListeners("value"));
Lines 160-166 Link Here
160
		Bean bean = new Bean();
171
		Bean bean = new Bean();
161
		set.add(bean);
172
		set.add(bean);
162
		
173
		
163
		JavaBeanObservableMap observable = new JavaBeanObservableMap(set, new PropertyDescriptor("value", Bean.class));
174
		IObservableMap observable = BeansObservables.observeMap(set,
175
				Bean.class, "value");
176
		assertFalse(bean.hasListeners("value"));
164
		ChangeEventTracker.observe(observable);
177
		ChangeEventTracker.observe(observable);
165
178
166
		assertTrue(bean.hasListeners("value"));
179
		assertTrue(bean.hasListeners("value"));
(-)src/org/eclipse/core/tests/internal/databinding/beans/JavaBeanObservableValueTest.java (-26 / +22 lines)
Lines 9-33 Link Here
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Brad Reynolds - bug 171616
10
 *     Brad Reynolds - bug 171616
11
 *     Katarzyna Marszalek - test case for bug 198519
11
 *     Katarzyna Marszalek - test case for bug 198519
12
 *     Matthew Hall - bug 213145
12
 *     Matthew Hall - bugs 213145, 194734
13
 ******************************************************************************/
13
 ******************************************************************************/
14
14
15
package org.eclipse.core.tests.internal.databinding.beans;
15
package org.eclipse.core.tests.internal.databinding.beans;
16
16
17
import java.beans.IntrospectionException;
18
import java.beans.PropertyDescriptor;
17
import java.beans.PropertyDescriptor;
19
18
20
import junit.framework.Test;
19
import junit.framework.Test;
21
import junit.framework.TestSuite;
20
import junit.framework.TestSuite;
22
21
22
import org.eclipse.core.databinding.beans.BeanProperties;
23
import org.eclipse.core.databinding.beans.BeansObservables;
23
import org.eclipse.core.databinding.beans.BeansObservables;
24
import org.eclipse.core.databinding.beans.IBeanObservable;
25
import org.eclipse.core.databinding.beans.IBeanProperty;
26
import org.eclipse.core.databinding.beans.PojoObservables;
24
import org.eclipse.core.databinding.observable.ChangeEvent;
27
import org.eclipse.core.databinding.observable.ChangeEvent;
25
import org.eclipse.core.databinding.observable.IChangeListener;
28
import org.eclipse.core.databinding.observable.IChangeListener;
26
import org.eclipse.core.databinding.observable.IObservable;
29
import org.eclipse.core.databinding.observable.IObservable;
27
import org.eclipse.core.databinding.observable.Realm;
30
import org.eclipse.core.databinding.observable.Realm;
28
import org.eclipse.core.databinding.observable.value.ComputedValue;
31
import org.eclipse.core.databinding.observable.value.ComputedValue;
29
import org.eclipse.core.databinding.observable.value.IObservableValue;
32
import org.eclipse.core.databinding.observable.value.IObservableValue;
30
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableValue;
31
import org.eclipse.jface.databinding.conformance.MutableObservableValueContractTest;
33
import org.eclipse.jface.databinding.conformance.MutableObservableValueContractTest;
32
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
34
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableValueContractDelegate;
33
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
35
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
Lines 39-73 Link Here
39
 */
41
 */
40
public class JavaBeanObservableValueTest extends AbstractDefaultRealmTestCase {
42
public class JavaBeanObservableValueTest extends AbstractDefaultRealmTestCase {
41
	private Bean bean;
43
	private Bean bean;
42
	private JavaBeanObservableValue observableValue;
44
	private IObservableValue observableValue;
45
	private IBeanObservable beanObservable;
43
	private PropertyDescriptor propertyDescriptor;
46
	private PropertyDescriptor propertyDescriptor;
44
	private String propertyName;
47
	private String propertyName;
45
48
46
	/* (non-Javadoc)
47
	 * @see junit.framework.TestCase#setUp()
48
	 */
49
	protected void setUp() throws Exception {
49
	protected void setUp() throws Exception {
50
		super.setUp();
50
		super.setUp();
51
		
51
		
52
		bean = new Bean();
52
		bean = new Bean();
53
		propertyName = "value";
53
		propertyName = "value";
54
		propertyDescriptor = new PropertyDescriptor(propertyName, Bean.class);
54
		propertyDescriptor = ((IBeanProperty) BeanProperties.valueProperty(
55
		observableValue = new JavaBeanObservableValue(Realm.getDefault(), bean, propertyDescriptor);
55
				Bean.class, propertyName)).getPropertyDescriptor();
56
		observableValue = BeansObservables.observeValue(bean, propertyName);
57
		beanObservable = (IBeanObservable) observableValue;
56
	}
58
	}
57
59
58
	public void testGetObserved() throws Exception {
60
	public void testGetObserved() throws Exception {
59
		assertEquals(bean, observableValue.getObserved());
61
		assertEquals(bean, beanObservable.getObserved());
60
	}
62
	}
61
63
62
	public void testGetPropertyDescriptor() throws Exception {
64
	public void testGetPropertyDescriptor() throws Exception {
63
    	assertEquals(propertyDescriptor, observableValue.getPropertyDescriptor());
65
    	assertEquals(propertyDescriptor, beanObservable.getPropertyDescriptor());
64
	}
66
	}
65
67
66
	public void testSetValueThrowsExceptionThrownByBean() throws Exception {
68
	public void testSetValueThrowsExceptionThrownByBean() throws Exception {
67
		ThrowsSetException temp = new ThrowsSetException();
69
		ThrowsSetException temp = new ThrowsSetException();
68
		JavaBeanObservableValue observable = new JavaBeanObservableValue(Realm
70
		IObservableValue observable = BeansObservables.observeValue(temp,
69
				.getDefault(), temp,
71
				"value");
70
				new PropertyDescriptor("value", ThrowsSetException.class));
71
72
72
		try {
73
		try {
73
			observable.setValue("");
74
			observable.setValue("");
Lines 79-87 Link Here
79
	
80
	
80
	public void testGetValueThrowsExceptionThrownByBean() throws Exception {
81
	public void testGetValueThrowsExceptionThrownByBean() throws Exception {
81
		ThrowsGetException temp = new ThrowsGetException();
82
		ThrowsGetException temp = new ThrowsGetException();
82
		JavaBeanObservableValue observable = new JavaBeanObservableValue(Realm
83
		IObservableValue observable = BeansObservables.observeValue(temp,
83
				.getDefault(), temp,
84
				"value");
84
				new PropertyDescriptor("value", ThrowsGetException.class));
85
85
86
		try {
86
		try {
87
			observable.getValue();
87
			observable.getValue();
Lines 108-121 Link Here
108
	}
108
	}
109
109
110
	public void testConstructor_RegistersListeners() throws Exception {
110
	public void testConstructor_RegistersListeners() throws Exception {
111
		JavaBeanObservableValue observable = new JavaBeanObservableValue(Realm.getDefault(), bean, propertyDescriptor);
111
		IObservableValue observable = BeansObservables.observeValue(bean,
112
				propertyName);
112
		ChangeEventTracker.observe(observable);
113
		ChangeEventTracker.observe(observable);
113
		
114
		
114
		assertTrue(bean.hasListeners(propertyName));
115
		assertTrue(bean.hasListeners(propertyName));
115
	}
116
	}
116
	
117
	
117
	public void testConstructor_SkipRegisterListeners() throws Exception {
118
	public void testConstructor_SkipRegisterListeners() throws Exception {
118
		JavaBeanObservableValue observable = new JavaBeanObservableValue(Realm.getDefault(), bean, propertyDescriptor, false);
119
		IObservableValue observable = PojoObservables.observeValue(bean,
120
				propertyName);
119
		ChangeEventTracker.observe(observable);
121
		ChangeEventTracker.observe(observable);
120
		
122
		
121
		assertFalse(bean.hasListeners(propertyName));
123
		assertFalse(bean.hasListeners(propertyName));
Lines 138-150 Link Here
138
		}
140
		}
139
		
141
		
140
		public IObservableValue createObservableValue(Realm realm) {
142
		public IObservableValue createObservableValue(Realm realm) {
141
			try {
143
			return BeansObservables.observeValue(realm, bean, "value");
142
				PropertyDescriptor propertyDescriptor = new PropertyDescriptor("value", Bean.class);
143
				return new JavaBeanObservableValue(realm, bean,
144
						propertyDescriptor);					
145
			} catch (IntrospectionException e) {
146
				throw new RuntimeException(e);
147
			}
148
		}
144
		}
149
		
145
		
150
		public void change(IObservable observable) {
146
		public void change(IObservable observable) {
(-)src/org/eclipse/core/tests/internal/databinding/beans/JavaBeanObservableArrayBasedListTest.java (-32 / +25 lines)
Lines 7-18 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Matthew Hall - bugs 221351, 213145
10
 *     Matthew Hall - bugs 221351, 213145, 194734
11
 ******************************************************************************/
11
 ******************************************************************************/
12
12
13
package org.eclipse.core.tests.internal.databinding.beans;
13
package org.eclipse.core.tests.internal.databinding.beans;
14
14
15
import java.beans.IntrospectionException;
16
import java.beans.PropertyChangeEvent;
15
import java.beans.PropertyChangeEvent;
17
import java.beans.PropertyChangeListener;
16
import java.beans.PropertyChangeListener;
18
import java.beans.PropertyDescriptor;
17
import java.beans.PropertyDescriptor;
Lines 23-35 Link Here
23
import junit.framework.Test;
22
import junit.framework.Test;
24
import junit.framework.TestSuite;
23
import junit.framework.TestSuite;
25
24
25
import org.eclipse.core.databinding.beans.BeanProperties;
26
import org.eclipse.core.databinding.beans.BeansObservables;
27
import org.eclipse.core.databinding.beans.IBeanObservable;
28
import org.eclipse.core.databinding.beans.IBeanProperty;
26
import org.eclipse.core.databinding.observable.IObservable;
29
import org.eclipse.core.databinding.observable.IObservable;
27
import org.eclipse.core.databinding.observable.IObservableCollection;
30
import org.eclipse.core.databinding.observable.IObservableCollection;
28
import org.eclipse.core.databinding.observable.Realm;
31
import org.eclipse.core.databinding.observable.Realm;
29
import org.eclipse.core.databinding.observable.list.IObservableList;
32
import org.eclipse.core.databinding.observable.list.IObservableList;
30
import org.eclipse.core.databinding.observable.list.ListChangeEvent;
33
import org.eclipse.core.databinding.observable.list.ListChangeEvent;
31
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
34
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
32
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableList;
33
import org.eclipse.jface.databinding.conformance.MutableObservableListContractTest;
35
import org.eclipse.jface.databinding.conformance.MutableObservableListContractTest;
34
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
36
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
35
import org.eclipse.jface.databinding.conformance.util.ListChangeEventTracker;
37
import org.eclipse.jface.databinding.conformance.util.ListChangeEventTracker;
Lines 42-48 Link Here
42
 */
44
 */
43
public class JavaBeanObservableArrayBasedListTest extends
45
public class JavaBeanObservableArrayBasedListTest extends
44
		AbstractDefaultRealmTestCase {
46
		AbstractDefaultRealmTestCase {
45
	private JavaBeanObservableList list;
47
	private IObservableList list;
48
	private IBeanObservable beanObservable;
46
49
47
	private PropertyDescriptor propertyDescriptor;
50
	private PropertyDescriptor propertyDescriptor;
48
51
Lines 59-77 Link Here
59
		super.setUp();
62
		super.setUp();
60
63
61
		propertyName = "array";
64
		propertyName = "array";
62
		propertyDescriptor = new PropertyDescriptor(propertyName, Bean.class);
65
		propertyDescriptor = ((IBeanProperty) BeanProperties.listProperty(
66
				Bean.class, propertyName)).getPropertyDescriptor();
63
		bean = new Bean(new Object[0]);
67
		bean = new Bean(new Object[0]);
64
68
65
		list = new JavaBeanObservableList(SWTObservables.getRealm(Display
69
		list = BeansObservables.observeList(SWTObservables.getRealm(Display
66
				.getDefault()), bean, propertyDescriptor, Bean.class);
70
				.getDefault()), bean, propertyName);
71
		beanObservable = (IBeanObservable) list;
67
	}
72
	}
68
73
69
	public void testGetObserved() throws Exception {
74
	public void testGetObserved() throws Exception {
70
		assertSame(bean, list.getObserved());
75
		assertSame(bean, beanObservable.getObserved());
71
	}
76
	}
72
77
73
	public void testGetPropertyDescriptor() throws Exception {
78
	public void testGetPropertyDescriptor() throws Exception {
74
		assertSame(propertyDescriptor, list.getPropertyDescriptor());
79
		assertEquals(propertyDescriptor, beanObservable.getPropertyDescriptor());
75
	}
80
	}
76
81
77
	public void testRegistersListenerAfterFirstListenerIsAdded()
82
	public void testRegistersListenerAfterFirstListenerIsAdded()
Lines 308-323 Link Here
308
	}
313
	}
309
314
310
	public void testRemoveAll() throws Exception {
315
	public void testRemoveAll() throws Exception {
311
		List elements = Arrays.asList(new String[] { "1", "2" });
316
		list.addAll(Arrays.asList(new String[] { "1", "2", "3", "4" }));
312
		list.addAll(elements);
313
		list.addAll(elements);
314
315
		assertEquals(4, bean.getArray().length);
317
		assertEquals(4, bean.getArray().length);
316
		list.removeAll(elements);
318
319
		list.removeAll(Arrays.asList(new String[] { "2", "4" }));
317
320
318
		assertEquals(2, bean.getArray().length);
321
		assertEquals(2, bean.getArray().length);
319
		assertEquals(elements.get(0), bean.getArray()[0]);
322
		assertEquals("1", bean.getArray()[0]);
320
		assertEquals(elements.get(1), bean.getArray()[1]);
323
		assertEquals("3", bean.getArray()[1]);
321
	}
324
	}
322
325
323
	public void testRemoveAllListChangeEvent() throws Exception {
326
	public void testRemoveAllListChangeEvent() throws Exception {
Lines 471-478 Link Here
471
		PropertyChangeEvent event = listener.evt;
474
		PropertyChangeEvent event = listener.evt;
472
		assertEquals("event did not fire", 1, listener.count);
475
		assertEquals("event did not fire", 1, listener.count);
473
		assertEquals("array", event.getPropertyName());
476
		assertEquals("array", event.getPropertyName());
474
		assertTrue("old value", Arrays.equals(old, (Object[]) event.getOldValue()));
477
		assertTrue("old value", Arrays.equals(old, (Object[]) event
475
		assertTrue("new value", Arrays.equals(bean.getArray(), (Object[]) event.getNewValue()));
478
				.getOldValue()));
479
		assertTrue("new value", Arrays.equals(bean.getArray(), (Object[]) event
480
				.getNewValue()));
476
		assertFalse("lists are equal", Arrays.equals(bean.getArray(), old));
481
		assertFalse("lists are equal", Arrays.equals(bean.getArray(), old));
477
	}
482
	}
478
483
Lines 482-492 Link Here
482
487
483
		PropertyChangeEvent evt;
488
		PropertyChangeEvent evt;
484
489
485
		/*
486
		 * (non-Javadoc)
487
		 * 
488
		 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
489
		 */
490
		public void propertyChange(PropertyChangeEvent evt) {
490
		public void propertyChange(PropertyChangeEvent evt) {
491
			count++;
491
			count++;
492
			this.evt = evt;
492
			this.evt = evt;
Lines 504-520 Link Here
504
		public IObservableCollection createObservableCollection(Realm realm,
504
		public IObservableCollection createObservableCollection(Realm realm,
505
				int elementCount) {
505
				int elementCount) {
506
			String propertyName = "array";
506
			String propertyName = "array";
507
			PropertyDescriptor propertyDescriptor;
508
			try {
509
				propertyDescriptor = new PropertyDescriptor(propertyName,
510
						Bean.class);
511
			} catch (IntrospectionException e) {
512
				throw new RuntimeException(e);
513
			}
514
			Object bean = new Bean(new Object[0]);
507
			Object bean = new Bean(new Object[0]);
515
508
516
			IObservableList list = new JavaBeanObservableList(realm, bean,
509
			IObservableList list = BeansObservables.observeList(realm, bean,
517
					propertyDescriptor, String.class);
510
					propertyName, String.class);
518
			for (int i = 0; i < elementCount; i++)
511
			for (int i = 0; i < elementCount; i++)
519
				list.add(createElement(list));
512
				list.add(createElement(list));
520
			return list;
513
			return list;
(-)src/org/eclipse/core/tests/internal/databinding/beans/JavaBeanObservableSetTest.java (-28 / +22 lines)
Lines 7-18 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Matthew Hall - bugs 221351, 213145
10
 *     Matthew Hall - bugs 221351, 213145, 194734
11
 ******************************************************************************/
11
 ******************************************************************************/
12
12
13
package org.eclipse.core.tests.internal.databinding.beans;
13
package org.eclipse.core.tests.internal.databinding.beans;
14
14
15
import java.beans.IntrospectionException;
16
import java.beans.PropertyDescriptor;
15
import java.beans.PropertyDescriptor;
17
import java.util.Arrays;
16
import java.util.Arrays;
18
import java.util.HashSet;
17
import java.util.HashSet;
Lines 21-33 Link Here
21
import junit.framework.TestCase;
20
import junit.framework.TestCase;
22
import junit.framework.TestSuite;
21
import junit.framework.TestSuite;
23
22
23
import org.eclipse.core.databinding.beans.BeanProperties;
24
import org.eclipse.core.databinding.beans.BeansObservables;
25
import org.eclipse.core.databinding.beans.IBeanObservable;
26
import org.eclipse.core.databinding.beans.IBeanProperty;
27
import org.eclipse.core.databinding.beans.PojoObservables;
24
import org.eclipse.core.databinding.observable.IObservable;
28
import org.eclipse.core.databinding.observable.IObservable;
25
import org.eclipse.core.databinding.observable.IObservableCollection;
29
import org.eclipse.core.databinding.observable.IObservableCollection;
26
import org.eclipse.core.databinding.observable.Realm;
30
import org.eclipse.core.databinding.observable.Realm;
27
import org.eclipse.core.databinding.observable.set.IObservableSet;
31
import org.eclipse.core.databinding.observable.set.IObservableSet;
28
import org.eclipse.core.databinding.observable.set.ISetChangeListener;
32
import org.eclipse.core.databinding.observable.set.ISetChangeListener;
29
import org.eclipse.core.databinding.observable.set.SetChangeEvent;
33
import org.eclipse.core.databinding.observable.set.SetChangeEvent;
30
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableSet;
31
import org.eclipse.jface.databinding.conformance.MutableObservableSetContractTest;
34
import org.eclipse.jface.databinding.conformance.MutableObservableSetContractTest;
32
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
35
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
33
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
36
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
Lines 39-72 Link Here
39
 * @since 3.3
42
 * @since 3.3
40
 */
43
 */
41
public class JavaBeanObservableSetTest extends TestCase {
44
public class JavaBeanObservableSetTest extends TestCase {
42
	private JavaBeanObservableSet observableSet;
45
	private IObservableSet observableSet;
46
	private IBeanObservable beanObservable;
43
	private Bean bean;
47
	private Bean bean;
44
	private PropertyDescriptor propertyDescriptor;
48
	private PropertyDescriptor propertyDescriptor;
45
	private String propertyName;
49
	private String propertyName;
46
	private SetChangeListener listener;
50
	private SetChangeListener listener;
47
51
48
	/*
49
	 * (non-Javadoc)
50
	 * 
51
	 * @see junit.framework.TestCase#setUp()
52
	 */
53
	protected void setUp() throws Exception {
52
	protected void setUp() throws Exception {
54
		bean = new Bean();
53
		bean = new Bean();
55
		propertyName = "set";
54
		propertyName = "set";
56
		propertyDescriptor = new PropertyDescriptor(propertyName, Bean.class);
55
		propertyDescriptor = ((IBeanProperty) BeanProperties.setProperty(
56
				Bean.class, propertyName)).getPropertyDescriptor();
57
57
58
		observableSet = new JavaBeanObservableSet(SWTObservables
58
		observableSet = BeansObservables
59
				.getRealm(Display.getDefault()), bean, propertyDescriptor,
59
				.observeSet(SWTObservables.getRealm(Display.getDefault()),
60
				Bean.class);
60
						bean, propertyName, Bean.class);
61
		beanObservable = (IBeanObservable) observableSet;
61
		listener = new SetChangeListener();
62
		listener = new SetChangeListener();
62
	}
63
	}
63
64
64
	public void testGetObserved() throws Exception {
65
	public void testGetObserved() throws Exception {
65
		assertEquals(bean, observableSet.getObserved());
66
		assertEquals(bean, beanObservable.getObserved());
66
	}
67
	}
67
68
68
	public void testGetPropertyDescriptor() throws Exception {
69
	public void testGetPropertyDescriptor() throws Exception {
69
		assertEquals(propertyDescriptor, observableSet.getPropertyDescriptor());
70
		assertEquals(propertyDescriptor, beanObservable.getPropertyDescriptor());
70
	}
71
	}
71
	
72
	
72
	public void testGetElementType() throws Exception {
73
	public void testGetElementType() throws Exception {
Lines 97-104 Link Here
97
	public void testConstructor_RegisterListeners() throws Exception {
98
	public void testConstructor_RegisterListeners() throws Exception {
98
		bean = new Bean();
99
		bean = new Bean();
99
100
100
		observableSet = new JavaBeanObservableSet(new CurrentRealm(true), bean,
101
		observableSet = BeansObservables.observeSet(new CurrentRealm(true),
101
				propertyDescriptor, Bean.class);
102
				bean, propertyName);
102
		assertFalse(bean.hasListeners(propertyName));
103
		assertFalse(bean.hasListeners(propertyName));
103
		ChangeEventTracker.observe(observableSet);
104
		ChangeEventTracker.observe(observableSet);
104
		assertTrue(bean.hasListeners(propertyName));
105
		assertTrue(bean.hasListeners(propertyName));
Lines 107-114 Link Here
107
	public void testConstructor_SkipsRegisterListeners() throws Exception {
108
	public void testConstructor_SkipsRegisterListeners() throws Exception {
108
		bean = new Bean();
109
		bean = new Bean();
109
110
110
		observableSet = new JavaBeanObservableSet(new CurrentRealm(true), bean,
111
		observableSet = PojoObservables.observeSet(new CurrentRealm(true),
111
				propertyDescriptor, Bean.class, false);
112
				bean, propertyName);
112
		assertFalse(bean.hasListeners(propertyName));
113
		assertFalse(bean.hasListeners(propertyName));
113
		ChangeEventTracker.observe(observableSet);
114
		ChangeEventTracker.observe(observableSet);
114
		assertFalse(bean.hasListeners(propertyName));
115
		assertFalse(bean.hasListeners(propertyName));
Lines 134-149 Link Here
134
				int elementCount) {
135
				int elementCount) {
135
			Bean bean = new Bean();
136
			Bean bean = new Bean();
136
			String propertyName = "set";
137
			String propertyName = "set";
137
			PropertyDescriptor propertyDescriptor;
138
			try {
139
				propertyDescriptor = new PropertyDescriptor(propertyName,
140
						Bean.class);
141
			} catch (IntrospectionException e) {
142
				throw new RuntimeException(e);
143
			}
144
138
145
			IObservableSet set = new JavaBeanObservableSet(realm,
139
			IObservableSet set = BeansObservables.observeSet(realm, bean,
146
					bean, propertyDescriptor, String.class);
140
					propertyName, String.class);
147
			for (int i = 0; i < elementCount; i++)
141
			for (int i = 0; i < elementCount; i++)
148
				set.add(createElement(set));
142
				set.add(createElement(set));
149
			return set;
143
			return set;
(-)src/org/eclipse/core/tests/internal/databinding/beans/BeanObservableValueDecoratorTest.java (-15 / +12 lines)
Lines 7-21 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.databinding.beans.BeanProperties;
18
import org.eclipse.core.databinding.beans.BeansObservables;
19
import org.eclipse.core.databinding.beans.IBeanProperty;
20
import org.eclipse.core.databinding.observable.value.IObservableValue;
17
import org.eclipse.core.internal.databinding.beans.BeanObservableValueDecorator;
21
import org.eclipse.core.internal.databinding.beans.BeanObservableValueDecorator;
18
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableValue;
19
import org.eclipse.jface.databinding.swt.SWTObservables;
22
import org.eclipse.jface.databinding.swt.SWTObservables;
20
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
23
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
21
import org.eclipse.swt.widgets.Display;
24
import org.eclipse.swt.widgets.Display;
Lines 25-51 Link Here
25
 */
28
 */
26
public class BeanObservableValueDecoratorTest extends AbstractDefaultRealmTestCase {
29
public class BeanObservableValueDecoratorTest extends AbstractDefaultRealmTestCase {
27
	private Bean bean;
30
	private Bean bean;
28
	private JavaBeanObservableValue observableValue;
31
	private IObservableValue observableValue;
29
	private BeanObservableValueDecorator decorator;
32
	private BeanObservableValueDecorator decorator;
30
	private PropertyDescriptor propertyDescriptor;
33
	private PropertyDescriptor propertyDescriptor;
31
	
34
	
32
	/*
33
	 * (non-Javadoc)
34
	 * 
35
	 * @see junit.framework.TestCase#setUp()
36
	 */
37
	protected void setUp() throws Exception {
35
	protected void setUp() throws Exception {
38
		super.setUp();
36
		super.setUp();
39
		
37
		
40
		bean = new Bean();
38
		bean = new Bean();
41
		propertyDescriptor = new PropertyDescriptor("value",
39
		String propertyName = "value";
42
				Bean.class);
40
		propertyDescriptor = ((IBeanProperty) BeanProperties.valueProperty(
43
		observableValue = new JavaBeanObservableValue(
41
				Bean.class, propertyName)).getPropertyDescriptor();
44
				SWTObservables.getRealm(Display.getDefault()), bean,
42
		observableValue = BeansObservables.observeValue(SWTObservables
43
				.getRealm(Display.getDefault()), bean, propertyName);
44
		decorator = new BeanObservableValueDecorator(observableValue,
45
				propertyDescriptor);
45
				propertyDescriptor);
46
		decorator = new BeanObservableValueDecorator(
47
				observableValue, new WritableValue(bean, Object.class), observableValue
48
						.getPropertyDescriptor());
49
	}
46
	}
50
47
51
	public void testGetDelegate() throws Exception {
48
	public void testGetDelegate() throws Exception {
(-)src/org/eclipse/core/tests/internal/databinding/beans/JavaBeanObservableArrayBasedSetTest.java (-24 / +18 lines)
Lines 8-19 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation (bug 221351)
9
 *     Matthew Hall - initial API and implementation (bug 221351)
10
 *     Brad Reynolds - through JavaBeanObservableArrayBasedListTest.java
10
 *     Brad Reynolds - through JavaBeanObservableArrayBasedListTest.java
11
 *     Matthew Hall - bug 213145
11
 *     Matthew Hall - bugs 213145, 194734
12
 ******************************************************************************/
12
 ******************************************************************************/
13
13
14
package org.eclipse.core.tests.internal.databinding.beans;
14
package org.eclipse.core.tests.internal.databinding.beans;
15
15
16
import java.beans.IntrospectionException;
17
import java.beans.PropertyChangeEvent;
16
import java.beans.PropertyChangeEvent;
18
import java.beans.PropertyChangeListener;
17
import java.beans.PropertyChangeListener;
19
import java.beans.PropertyDescriptor;
18
import java.beans.PropertyDescriptor;
Lines 25-36 Link Here
25
import junit.framework.Test;
24
import junit.framework.Test;
26
import junit.framework.TestSuite;
25
import junit.framework.TestSuite;
27
26
27
import org.eclipse.core.databinding.beans.BeanProperties;
28
import org.eclipse.core.databinding.beans.BeansObservables;
29
import org.eclipse.core.databinding.beans.IBeanObservable;
30
import org.eclipse.core.databinding.beans.IBeanProperty;
28
import org.eclipse.core.databinding.observable.IObservable;
31
import org.eclipse.core.databinding.observable.IObservable;
29
import org.eclipse.core.databinding.observable.IObservableCollection;
32
import org.eclipse.core.databinding.observable.IObservableCollection;
30
import org.eclipse.core.databinding.observable.Realm;
33
import org.eclipse.core.databinding.observable.Realm;
31
import org.eclipse.core.databinding.observable.set.IObservableSet;
34
import org.eclipse.core.databinding.observable.set.IObservableSet;
32
import org.eclipse.core.databinding.observable.set.SetChangeEvent;
35
import org.eclipse.core.databinding.observable.set.SetChangeEvent;
33
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableSet;
34
import org.eclipse.jface.databinding.conformance.MutableObservableSetContractTest;
36
import org.eclipse.jface.databinding.conformance.MutableObservableSetContractTest;
35
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
37
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
36
import org.eclipse.jface.databinding.conformance.util.SetChangeEventTracker;
38
import org.eclipse.jface.databinding.conformance.util.SetChangeEventTracker;
Lines 43-49 Link Here
43
 */
45
 */
44
public class JavaBeanObservableArrayBasedSetTest extends
46
public class JavaBeanObservableArrayBasedSetTest extends
45
		AbstractDefaultRealmTestCase {
47
		AbstractDefaultRealmTestCase {
46
	private JavaBeanObservableSet set;
48
	private IObservableSet set;
49
	private IBeanObservable beanObservable;
47
50
48
	private PropertyDescriptor propertyDescriptor;
51
	private PropertyDescriptor propertyDescriptor;
49
52
Lines 55-73 Link Here
55
		super.setUp();
58
		super.setUp();
56
59
57
		propertyName = "array";
60
		propertyName = "array";
58
		propertyDescriptor = new PropertyDescriptor(propertyName, Bean.class);
61
		propertyDescriptor = ((IBeanProperty) BeanProperties.setProperty(
62
				Bean.class, propertyName)).getPropertyDescriptor();
59
		bean = new Bean(new HashSet());
63
		bean = new Bean(new HashSet());
60
64
61
		set = new JavaBeanObservableSet(SWTObservables.getRealm(Display
65
		set = BeansObservables.observeSet(SWTObservables.getRealm(Display
62
				.getDefault()), bean, propertyDescriptor, String.class);
66
				.getDefault()), bean, propertyName);
67
		beanObservable = (IBeanObservable) set;
63
	}
68
	}
64
69
65
	public void testGetObserved() throws Exception {
70
	public void testGetObserved() throws Exception {
66
		assertEquals(bean, set.getObserved());
71
		assertEquals(bean, beanObservable.getObserved());
67
	}
72
	}
68
73
69
	public void testGetPropertyDescriptor() throws Exception {
74
	public void testGetPropertyDescriptor() throws Exception {
70
		assertEquals(propertyDescriptor, set.getPropertyDescriptor());
75
		assertEquals(propertyDescriptor, beanObservable.getPropertyDescriptor());
71
	}
76
	}
72
77
73
	public void testRegistersListenerAfterFirstListenerIsAdded()
78
	public void testRegistersListenerAfterFirstListenerIsAdded()
Lines 292-298 Link Here
292
		assertEquals("array", event.getPropertyName());
297
		assertEquals("array", event.getPropertyName());
293
		assertTrue("old value", Arrays.equals(old, (Object[]) event
298
		assertTrue("old value", Arrays.equals(old, (Object[]) event
294
				.getOldValue()));
299
				.getOldValue()));
295
		assertTrue("new value", Arrays.equals(bean.getArray(), (Object[]) event.getNewValue()));
300
		assertTrue("new value", Arrays.equals(bean.getArray(), (Object[]) event
301
				.getNewValue()));
296
		assertFalse("lists are equal", Arrays.equals(bean.getArray(), old));
302
		assertFalse("lists are equal", Arrays.equals(bean.getArray(), old));
297
	}
303
	}
298
304
Lines 302-312 Link Here
302
308
303
		PropertyChangeEvent evt;
309
		PropertyChangeEvent evt;
304
310
305
		/*
306
		 * (non-Javadoc)
307
		 * 
308
		 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
309
		 */
310
		public void propertyChange(PropertyChangeEvent evt) {
311
		public void propertyChange(PropertyChangeEvent evt) {
311
			count++;
312
			count++;
312
			this.evt = evt;
313
			this.evt = evt;
Lines 324-340 Link Here
324
		public IObservableCollection createObservableCollection(Realm realm,
325
		public IObservableCollection createObservableCollection(Realm realm,
325
				int elementCount) {
326
				int elementCount) {
326
			String propertyName = "array";
327
			String propertyName = "array";
327
			PropertyDescriptor propertyDescriptor;
328
			try {
329
				propertyDescriptor = new PropertyDescriptor(propertyName,
330
						Bean.class);
331
			} catch (IntrospectionException e) {
332
				throw new RuntimeException(e);
333
			}
334
			Object bean = new Bean(new Object[0]);
328
			Object bean = new Bean(new Object[0]);
335
329
336
			IObservableSet list = new JavaBeanObservableSet(realm, bean,
330
			IObservableSet list = BeansObservables.observeSet(realm, bean,
337
					propertyDescriptor, String.class);
331
					propertyName, String.class);
338
			for (int i = 0; i < elementCount; i++)
332
			for (int i = 0; i < elementCount; i++)
339
				list.add(createElement(list));
333
				list.add(createElement(list));
340
			return list;
334
			return list;
(-)src/org/eclipse/core/tests/internal/databinding/beans/BeanObservableSetDecoratorTest.java (-10 / +17 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 15-22 Link Here
15
16
16
import junit.framework.TestCase;
17
import junit.framework.TestCase;
17
18
19
import org.eclipse.core.databinding.beans.BeanProperties;
20
import org.eclipse.core.databinding.beans.BeansObservables;
21
import org.eclipse.core.databinding.beans.IBeanObservable;
22
import org.eclipse.core.databinding.beans.IBeanProperty;
23
import org.eclipse.core.databinding.observable.set.IObservableSet;
18
import org.eclipse.core.internal.databinding.beans.BeanObservableSetDecorator;
24
import org.eclipse.core.internal.databinding.beans.BeanObservableSetDecorator;
19
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableSet;
20
import org.eclipse.jface.databinding.swt.SWTObservables;
25
import org.eclipse.jface.databinding.swt.SWTObservables;
21
import org.eclipse.swt.widgets.Display;
26
import org.eclipse.swt.widgets.Display;
22
27
Lines 25-31 Link Here
25
 */
30
 */
26
public class BeanObservableSetDecoratorTest extends TestCase {
31
public class BeanObservableSetDecoratorTest extends TestCase {
27
	private PropertyDescriptor propertyDescriptor;
32
	private PropertyDescriptor propertyDescriptor;
28
	private JavaBeanObservableSet observableSet;
33
	private IObservableSet observableSet;
34
	private IBeanObservable beanObservable;
29
	private BeanObservableSetDecorator decorator;
35
	private BeanObservableSetDecorator decorator;
30
36
31
	/*
37
	/*
Lines 37-49 Link Here
37
		super.setUp();
43
		super.setUp();
38
44
39
		Bean bean = new Bean();
45
		Bean bean = new Bean();
40
		propertyDescriptor = new PropertyDescriptor("set",
46
		String propertyName = "set";
41
				Bean.class);
47
		propertyDescriptor = ((IBeanProperty) BeanProperties.setProperty(
42
		observableSet = new JavaBeanObservableSet(
48
				Bean.class, propertyName)).getPropertyDescriptor();
43
				SWTObservables.getRealm(Display.getDefault()), bean,
49
		observableSet = BeansObservables.observeSet(SWTObservables
44
				propertyDescriptor, String.class);
50
				.getRealm(Display.getDefault()), bean, propertyName);
45
		decorator = new BeanObservableSetDecorator(
51
		beanObservable = (IBeanObservable) observableSet;
46
				observableSet, observableSet, propertyDescriptor);
52
		decorator = new BeanObservableSetDecorator(observableSet,
53
				propertyDescriptor);
47
	}
54
	}
48
55
49
	public void testGetDelegate() throws Exception {
56
	public void testGetDelegate() throws Exception {
Lines 51-57 Link Here
51
	}
58
	}
52
59
53
	public void testGetObserved() throws Exception {
60
	public void testGetObserved() throws Exception {
54
		assertEquals(observableSet, decorator.getObserved());
61
		assertEquals(beanObservable.getObserved(), decorator.getObserved());
55
	}
62
	}
56
63
57
	public void testGetPropertyDescriptor() throws Exception {
64
	public void testGetPropertyDescriptor() throws Exception {
(-)src/org/eclipse/core/tests/internal/databinding/beans/BeanObservableListDecoratorTest.java (-9 / +11 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 18-30 Link Here
18
import junit.framework.TestCase;
18
import junit.framework.TestCase;
19
import junit.framework.TestSuite;
19
import junit.framework.TestSuite;
20
20
21
import org.eclipse.core.databinding.beans.BeansObservables;
22
import org.eclipse.core.databinding.beans.IBeanObservable;
21
import org.eclipse.core.databinding.observable.IObservable;
23
import org.eclipse.core.databinding.observable.IObservable;
22
import org.eclipse.core.databinding.observable.IObservableCollection;
24
import org.eclipse.core.databinding.observable.IObservableCollection;
23
import org.eclipse.core.databinding.observable.Realm;
25
import org.eclipse.core.databinding.observable.Realm;
24
import org.eclipse.core.databinding.observable.list.IObservableList;
26
import org.eclipse.core.databinding.observable.list.IObservableList;
25
import org.eclipse.core.databinding.observable.list.WritableList;
27
import org.eclipse.core.databinding.observable.list.WritableList;
26
import org.eclipse.core.internal.databinding.beans.BeanObservableListDecorator;
28
import org.eclipse.core.internal.databinding.beans.BeanObservableListDecorator;
27
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableList;
28
import org.eclipse.jface.databinding.conformance.MutableObservableListContractTest;
29
import org.eclipse.jface.databinding.conformance.MutableObservableListContractTest;
29
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
30
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
30
import org.eclipse.jface.databinding.swt.SWTObservables;
31
import org.eclipse.jface.databinding.swt.SWTObservables;
Lines 35-41 Link Here
35
 */
36
 */
36
public class BeanObservableListDecoratorTest extends TestCase {
37
public class BeanObservableListDecoratorTest extends TestCase {
37
	private PropertyDescriptor propertyDescriptor;
38
	private PropertyDescriptor propertyDescriptor;
38
	private JavaBeanObservableList observableList;
39
	private IObservableList observableList;
40
	private IBeanObservable beanObservable;
39
	private BeanObservableListDecorator decorator;
41
	private BeanObservableListDecorator decorator;
40
42
41
	/*
43
	/*
Lines 49-58 Link Here
49
		Bean bean = new Bean();
51
		Bean bean = new Bean();
50
		propertyDescriptor = new PropertyDescriptor(
52
		propertyDescriptor = new PropertyDescriptor(
51
				"list", Bean.class,"getList","setList");
53
				"list", Bean.class,"getList","setList");
52
		observableList = new JavaBeanObservableList(
54
		observableList = BeansObservables.observeList(SWTObservables
53
				SWTObservables.getRealm(Display.getDefault()), bean,
55
				.getRealm(Display.getDefault()), bean, "list");
54
				propertyDescriptor, Bean.class);
56
		beanObservable = (IBeanObservable) observableList;
55
		decorator = new BeanObservableListDecorator(observableList, observableList, propertyDescriptor);
57
		decorator = new BeanObservableListDecorator(observableList, propertyDescriptor);
56
	}
58
	}
57
59
58
	public void testGetDelegate() throws Exception {
60
	public void testGetDelegate() throws Exception {
Lines 60-66 Link Here
60
	}
62
	}
61
63
62
	public void testGetObserved() throws Exception {
64
	public void testGetObserved() throws Exception {
63
		assertEquals(observableList, decorator.getObserved());
65
		assertEquals(beanObservable.getObserved(), decorator.getObserved());
64
	}
66
	}
65
67
66
	public void testGetPropertyDescriptor() throws Exception {
68
	public void testGetPropertyDescriptor() throws Exception {
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/internal/databinding/beans/JavaBeanObservableListTest.java (-36 / +26 lines)
Lines 7-18 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     Brad Reynolds - initial API and implementation
9
 *     Brad Reynolds - initial API and implementation
10
 *     Matthew Hall - bugs 221351, 213145
10
 *     Matthew Hall - bugs 221351, 213145, 194734
11
 ******************************************************************************/
11
 ******************************************************************************/
12
12
13
package org.eclipse.core.tests.internal.databinding.beans;
13
package org.eclipse.core.tests.internal.databinding.beans;
14
14
15
import java.beans.IntrospectionException;
16
import java.beans.PropertyChangeEvent;
15
import java.beans.PropertyChangeEvent;
17
import java.beans.PropertyChangeListener;
16
import java.beans.PropertyChangeListener;
18
import java.beans.PropertyDescriptor;
17
import java.beans.PropertyDescriptor;
Lines 24-36 Link Here
24
import junit.framework.Test;
23
import junit.framework.Test;
25
import junit.framework.TestSuite;
24
import junit.framework.TestSuite;
26
25
26
import org.eclipse.core.databinding.beans.BeanProperties;
27
import org.eclipse.core.databinding.beans.BeansObservables;
28
import org.eclipse.core.databinding.beans.IBeanObservable;
29
import org.eclipse.core.databinding.beans.IBeanProperty;
30
import org.eclipse.core.databinding.beans.PojoObservables;
27
import org.eclipse.core.databinding.observable.IObservable;
31
import org.eclipse.core.databinding.observable.IObservable;
28
import org.eclipse.core.databinding.observable.IObservableCollection;
32
import org.eclipse.core.databinding.observable.IObservableCollection;
29
import org.eclipse.core.databinding.observable.Realm;
33
import org.eclipse.core.databinding.observable.Realm;
30
import org.eclipse.core.databinding.observable.list.IObservableList;
34
import org.eclipse.core.databinding.observable.list.IObservableList;
31
import org.eclipse.core.databinding.observable.list.ListChangeEvent;
35
import org.eclipse.core.databinding.observable.list.ListChangeEvent;
32
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
36
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
33
import org.eclipse.core.internal.databinding.beans.JavaBeanObservableList;
34
import org.eclipse.jface.databinding.conformance.MutableObservableListContractTest;
37
import org.eclipse.jface.databinding.conformance.MutableObservableListContractTest;
35
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
38
import org.eclipse.jface.databinding.conformance.delegate.AbstractObservableCollectionContractDelegate;
36
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
39
import org.eclipse.jface.databinding.conformance.util.ChangeEventTracker;
Lines 43-49 Link Here
43
 * @since 1.1
46
 * @since 1.1
44
 */
47
 */
45
public class JavaBeanObservableListTest extends AbstractDefaultRealmTestCase {
48
public class JavaBeanObservableListTest extends AbstractDefaultRealmTestCase {
46
	private JavaBeanObservableList list;
49
	private IObservableList list;
50
	private IBeanObservable beanObservable;
47
51
48
	private PropertyDescriptor propertyDescriptor;
52
	private PropertyDescriptor propertyDescriptor;
49
53
Lines 60-78 Link Here
60
		super.setUp();
64
		super.setUp();
61
65
62
		propertyName = "list";
66
		propertyName = "list";
63
		propertyDescriptor = new PropertyDescriptor(propertyName, Bean.class);
67
		propertyDescriptor = ((IBeanProperty) BeanProperties.listProperty(
68
				Bean.class, propertyName)).getPropertyDescriptor();
64
		bean = new Bean(new ArrayList());
69
		bean = new Bean(new ArrayList());
65
70
66
		list = new JavaBeanObservableList(SWTObservables.getRealm(Display
71
		list = BeansObservables.observeList(SWTObservables.getRealm(Display
67
				.getDefault()), bean, propertyDescriptor, String.class);
72
				.getDefault()), bean, propertyName);
73
		beanObservable = (IBeanObservable) list;
68
	}
74
	}
69
75
70
	public void testGetObserved() throws Exception {
76
	public void testGetObserved() throws Exception {
71
		assertEquals(bean, list.getObserved());
77
		assertEquals(bean, beanObservable.getObserved());
72
	}
78
	}
73
79
74
	public void testGetPropertyDescriptor() throws Exception {
80
	public void testGetPropertyDescriptor() throws Exception {
75
		assertEquals(propertyDescriptor, list.getPropertyDescriptor());
81
		assertEquals(propertyDescriptor, beanObservable.getPropertyDescriptor());
76
	}
82
	}
77
83
78
	public void testRegistersListenerAfterFirstListenerIsAdded()
84
	public void testRegistersListenerAfterFirstListenerIsAdded()
Lines 322-337 Link Here
322
	}
328
	}
323
329
324
	public void testRemoveAll() throws Exception {
330
	public void testRemoveAll() throws Exception {
325
		List elements = Arrays.asList(new String[] { "1", "2" });
331
		list.addAll(Arrays.asList(new String[] { "1", "2", "3", "4" }));
326
		list.addAll(elements);
327
		list.addAll(elements);
328
329
		assertEquals(4, bean.getList().size());
332
		assertEquals(4, bean.getList().size());
330
		list.removeAll(elements);
333
334
		list.removeAll(Arrays.asList(new String[] { "2", "4" }));
331
335
332
		assertEquals(2, bean.getList().size());
336
		assertEquals(2, bean.getList().size());
333
		assertEquals(elements.get(0), bean.getList().get(0));
337
		assertEquals("1", bean.getList().get(0));
334
		assertEquals(elements.get(1), bean.getList().get(1));
338
		assertEquals("3", bean.getList().get(1));
335
	}
339
	}
336
340
337
	public void testRemoveAllListChangeEvent() throws Exception {
341
	public void testRemoveAllListChangeEvent() throws Exception {
Lines 467-475 Link Here
467
471
468
	public void testConstructor_RegistersListener() throws Exception {
472
	public void testConstructor_RegistersListener() throws Exception {
469
		Bean bean = new Bean();
473
		Bean bean = new Bean();
470
		JavaBeanObservableList observable = new JavaBeanObservableList(Realm
474
		IObservableList observable = BeansObservables.observeList(Realm
471
				.getDefault(), bean,
475
				.getDefault(), bean, "list");
472
				new PropertyDescriptor("list", Bean.class), Bean.class);
473
476
474
		assertFalse(bean.hasListeners("list"));
477
		assertFalse(bean.hasListeners("list"));
475
		ChangeEventTracker.observe(observable);
478
		ChangeEventTracker.observe(observable);
Lines 478-486 Link Here
478
481
479
	public void testConstructor_SkipsRegisterListener() throws Exception {
482
	public void testConstructor_SkipsRegisterListener() throws Exception {
480
		Bean bean = new Bean();
483
		Bean bean = new Bean();
481
		JavaBeanObservableList observable = new JavaBeanObservableList(Realm
484
		IObservableList observable = PojoObservables.observeList(Realm
482
				.getDefault(), bean,
485
				.getDefault(), bean, "list");
483
				new PropertyDescriptor("list", Bean.class), Bean.class, false);
484
486
485
		assertFalse(bean.hasListeners("list"));
487
		assertFalse(bean.hasListeners("list"));
486
		ChangeEventTracker.observe(observable);
488
		ChangeEventTracker.observe(observable);
Lines 517-527 Link Here
517
519
518
		PropertyChangeEvent evt;
520
		PropertyChangeEvent evt;
519
521
520
		/*
521
		 * (non-Javadoc)
522
		 * 
523
		 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
524
		 */
525
		public void propertyChange(PropertyChangeEvent evt) {
522
		public void propertyChange(PropertyChangeEvent evt) {
526
			count++;
523
			count++;
527
			this.evt = evt;
524
			this.evt = evt;
Lines 539-555 Link Here
539
		public IObservableCollection createObservableCollection(Realm realm,
536
		public IObservableCollection createObservableCollection(Realm realm,
540
				int elementCount) {
537
				int elementCount) {
541
			String propertyName = "list";
538
			String propertyName = "list";
542
			PropertyDescriptor propertyDescriptor;
543
			try {
544
				propertyDescriptor = new PropertyDescriptor(propertyName,
545
						Bean.class);
546
			} catch (IntrospectionException e) {
547
				throw new RuntimeException(e);
548
			}
549
			Object bean = new Bean(new ArrayList());
539
			Object bean = new Bean(new ArrayList());
550
540
551
			IObservableList list = new JavaBeanObservableList(realm, bean,
541
			IObservableList list = BeansObservables.observeList(realm, bean,
552
					propertyDescriptor, String.class);
542
					propertyName, String.class);
553
			for (int i = 0; i < elementCount; i++)
543
			for (int i = 0; i < elementCount; i++)
554
				list.add(createElement(list));
544
				list.add(createElement(list));
555
			return list;
545
			return list;
(-)src/org/eclipse/core/tests/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
(-)src/org/eclipse/jface/tests/internal/databinding/swt/ControlObservableValueTest.java (-41 / +23 lines)
Lines 12-19 Link Here
12
12
13
package org.eclipse.jface.tests.internal.databinding.swt;
13
package org.eclipse.jface.tests.internal.databinding.swt;
14
14
15
import org.eclipse.jface.internal.databinding.swt.ControlObservableValue;
15
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
16
import org.eclipse.jface.internal.databinding.swt.SWTProperties;
16
import org.eclipse.jface.databinding.swt.SWTObservables;
17
import org.eclipse.jface.resource.JFaceResources;
17
import org.eclipse.jface.resource.JFaceResources;
18
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
18
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;
19
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.SWT;
Lines 30-36 Link Here
30
30
31
	protected void setUp() throws Exception {
31
	protected void setUp() throws Exception {
32
		super.setUp();
32
		super.setUp();
33
		
33
34
		shell = new Shell();
34
		shell = new Shell();
35
	}
35
	}
36
36
Lines 47-95 Link Here
47
	}
47
	}
48
48
49
	public void testSetValueEnabled() throws Exception {
49
	public void testSetValueEnabled() throws Exception {
50
		ControlObservableValue observableValue = new ControlObservableValue(
50
		ISWTObservableValue observableValue = SWTObservables
51
				shell, SWTProperties.ENABLED);
51
				.observeEnabled(shell);
52
		Boolean value = Boolean.FALSE;
52
		Boolean value = Boolean.FALSE;
53
		observableValue.setValue(value);
53
		observableValue.setValue(value);
54
		assertFalse(shell.isEnabled());
54
		assertFalse(shell.isEnabled());
55
	}
55
	}
56
56
57
	public void testGetValueEnabled() throws Exception {
57
	public void testGetValueEnabled() throws Exception {
58
		ControlObservableValue value = new ControlObservableValue(shell,
58
		ISWTObservableValue value = SWTObservables.observeEnabled(shell);
59
				SWTProperties.ENABLED);
60
		shell.setEnabled(false);
59
		shell.setEnabled(false);
61
		assertEquals(Boolean.FALSE, value.getValue());
60
		assertEquals(Boolean.FALSE, value.getValue());
62
	}
61
	}
63
62
64
	public void testGetValueTypeEnabled() throws Exception {
63
	public void testGetValueTypeEnabled() throws Exception {
65
		ControlObservableValue value = new ControlObservableValue(shell,
64
		ISWTObservableValue value = SWTObservables.observeEnabled(shell);
66
				SWTProperties.ENABLED);
67
		assertEquals(boolean.class, value.getValueType());
65
		assertEquals(boolean.class, value.getValueType());
68
	}
66
	}
69
67
70
	public void testSetValueVisible() throws Exception {
68
	public void testSetValueVisible() throws Exception {
71
		ControlObservableValue value = new ControlObservableValue(shell,
69
		ISWTObservableValue value = SWTObservables.observeVisible(shell);
72
				SWTProperties.VISIBLE);
73
		value.setValue(Boolean.FALSE);
70
		value.setValue(Boolean.FALSE);
74
		assertFalse(shell.isVisible());
71
		assertFalse(shell.isVisible());
75
	}
72
	}
76
73
77
	public void testGetValueVisible() throws Exception {
74
	public void testGetValueVisible() throws Exception {
78
		ControlObservableValue value = new ControlObservableValue(shell,
75
		ISWTObservableValue value = SWTObservables.observeVisible(shell);
79
				SWTProperties.VISIBLE);
80
		shell.setVisible(false);
76
		shell.setVisible(false);
81
		assertEquals(Boolean.FALSE, value.getValue());
77
		assertEquals(Boolean.FALSE, value.getValue());
82
	}
78
	}
83
79
84
	public void testGetValueTypeVisible() throws Exception {
80
	public void testGetValueTypeVisible() throws Exception {
85
		ControlObservableValue value = new ControlObservableValue(shell,
81
		ISWTObservableValue value = SWTObservables.observeVisible(shell);
86
				SWTProperties.VISIBLE);
87
		assertEquals(Boolean.TYPE, value.getValueType());
82
		assertEquals(Boolean.TYPE, value.getValueType());
88
	}
83
	}
89
84
90
	public void testSetValueForeground() throws Exception {
85
	public void testSetValueForeground() throws Exception {
91
		ControlObservableValue value = new ControlObservableValue(shell,
86
		ISWTObservableValue value = SWTObservables.observeForeground(shell);
92
				SWTProperties.FOREGROUND);
93
87
94
		Color color = shell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
88
		Color color = shell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
95
89
Lines 98-105 Link Here
98
	}
92
	}
99
93
100
	public void testGetValueForeground() throws Exception {
94
	public void testGetValueForeground() throws Exception {
101
		ControlObservableValue value = new ControlObservableValue(shell,
95
		ISWTObservableValue value = SWTObservables.observeForeground(shell);
102
				SWTProperties.FOREGROUND);
103
96
104
		Color color = shell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
97
		Color color = shell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
105
		shell.setForeground(color);
98
		shell.setForeground(color);
Lines 107-120 Link Here
107
	}
100
	}
108
101
109
	public void testGetValueTypeForgroundColor() throws Exception {
102
	public void testGetValueTypeForgroundColor() throws Exception {
110
		ControlObservableValue value = new ControlObservableValue(shell,
103
		ISWTObservableValue value = SWTObservables.observeForeground(shell);
111
				SWTProperties.FOREGROUND);
112
		assertEquals(Color.class, value.getValueType());
104
		assertEquals(Color.class, value.getValueType());
113
	}
105
	}
114
106
115
	public void testGetValueBackground() throws Exception {
107
	public void testGetValueBackground() throws Exception {
116
		ControlObservableValue value = new ControlObservableValue(shell,
108
		ISWTObservableValue value = SWTObservables.observeBackground(shell);
117
				SWTProperties.BACKGROUND);
118
109
119
		Color color = shell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
110
		Color color = shell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
120
		shell.setBackground(color);
111
		shell.setBackground(color);
Lines 122-129 Link Here
122
	}
113
	}
123
114
124
	public void testSetValueBackground() throws Exception {
115
	public void testSetValueBackground() throws Exception {
125
		ControlObservableValue value = new ControlObservableValue(shell,
116
		ISWTObservableValue value = SWTObservables.observeBackground(shell);
126
				SWTProperties.BACKGROUND);
127
117
128
		Color color = shell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
118
		Color color = shell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
129
119
Lines 132-151 Link Here
132
	}
122
	}
133
123
134
	public void testGetValueTypeBackgroundColor() throws Exception {
124
	public void testGetValueTypeBackgroundColor() throws Exception {
135
		ControlObservableValue value = new ControlObservableValue(shell,
125
		ISWTObservableValue value = SWTObservables.observeBackground(shell);
136
				SWTProperties.BACKGROUND);
137
		assertEquals(Color.class, value.getValueType());
126
		assertEquals(Color.class, value.getValueType());
138
	}
127
	}
139
128
140
	public void testGetValueTypeTooltip() throws Exception {
129
	public void testGetValueTypeTooltip() throws Exception {
141
		ControlObservableValue value = new ControlObservableValue(shell,
130
		ISWTObservableValue value = SWTObservables.observeTooltipText(shell);
142
				SWTProperties.TOOLTIP_TEXT);
143
		assertEquals(String.class, value.getValueType());
131
		assertEquals(String.class, value.getValueType());
144
	}
132
	}
145
133
146
	public void testSetValueFont() throws Exception {
134
	public void testSetValueFont() throws Exception {
147
		ControlObservableValue value = new ControlObservableValue(shell,
135
		ISWTObservableValue value = SWTObservables.observeFont(shell);
148
				SWTProperties.FONT);
149
136
150
		Font font = JFaceResources.getDialogFont();
137
		Font font = JFaceResources.getDialogFont();
151
138
Lines 154-161 Link Here
154
	}
141
	}
155
142
156
	public void testGetValueFont() throws Exception {
143
	public void testGetValueFont() throws Exception {
157
		ControlObservableValue value = new ControlObservableValue(shell,
144
		ISWTObservableValue value = SWTObservables.observeFont(shell);
158
				SWTProperties.FONT);
159
145
160
		Font font = JFaceResources.getDialogFont();
146
		Font font = JFaceResources.getDialogFont();
161
		shell.setFont(font);
147
		shell.setFont(font);
Lines 163-192 Link Here
163
	}
149
	}
164
150
165
	public void testGetValueTypeFont() throws Exception {
151
	public void testGetValueTypeFont() throws Exception {
166
		ControlObservableValue value = new ControlObservableValue(shell,
152
		ISWTObservableValue value = SWTObservables.observeFont(shell);
167
				SWTProperties.FONT);
168
		assertEquals(Font.class, value.getValueType());
153
		assertEquals(Font.class, value.getValueType());
169
	}
154
	}
170
155
171
	public void testSetValueTooltipText() throws Exception {
156
	public void testSetValueTooltipText() throws Exception {
172
		ControlObservableValue value = new ControlObservableValue(shell,
157
		ISWTObservableValue value = SWTObservables.observeTooltipText(shell);
173
				SWTProperties.TOOLTIP_TEXT);
174
		String text = "text";
158
		String text = "text";
175
		value.setValue(text);
159
		value.setValue(text);
176
		assertEquals(text, shell.getToolTipText());
160
		assertEquals(text, shell.getToolTipText());
177
	}
161
	}
178
162
179
	public void testGetValueTooltipText() throws Exception {
163
	public void testGetValueTooltipText() throws Exception {
180
		ControlObservableValue value = new ControlObservableValue(shell,
164
		ISWTObservableValue value = SWTObservables.observeTooltipText(shell);
181
				SWTProperties.TOOLTIP_TEXT);
182
		String text = "text";
165
		String text = "text";
183
		shell.setToolTipText(text);
166
		shell.setToolTipText(text);
184
		assertEquals(text, value.getValue());
167
		assertEquals(text, value.getValue());
185
	}
168
	}
186
169
187
	public void testGetValueTypeTooltipText() throws Exception {
170
	public void testGetValueTypeTooltipText() throws Exception {
188
		ControlObservableValue value = new ControlObservableValue(shell,
171
		ISWTObservableValue value = SWTObservables.observeTooltipText(shell);
189
				SWTProperties.TOOLTIP_TEXT);
190
		assertEquals(String.class, value.getValueType());
172
		assertEquals(String.class, value.getValueType());
191
	}
173
	}
192
}
174
}
(-)src/org/eclipse/core/internal/databinding/beans/BeanObservableSetDecorator.java (-11 / +26 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-23 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;
23
import org.eclipse.core.databinding.observable.ObservableTracker;
21
import org.eclipse.core.databinding.observable.Realm;
24
import org.eclipse.core.databinding.observable.Realm;
22
import org.eclipse.core.databinding.observable.set.IObservableSet;
25
import org.eclipse.core.databinding.observable.set.IObservableSet;
23
import org.eclipse.core.databinding.observable.set.ISetChangeListener;
26
import org.eclipse.core.databinding.observable.set.ISetChangeListener;
Lines 30-57 Link Here
30
 */
33
 */
31
public class BeanObservableSetDecorator implements IObservableSet, IBeanObservable {
34
public class BeanObservableSetDecorator implements IObservableSet, 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
50
	public boolean add(Object o) {
49
	public boolean add(Object o) {
50
		getterCalled();
51
		return delegate.add(o);
51
		return delegate.add(o);
52
	}
52
	}
53
53
54
	private void getterCalled() {
55
		ObservableTracker.getterCalled(this);
56
	}
57
54
	public boolean addAll(Collection c) {
58
	public boolean addAll(Collection c) {
59
		getterCalled();
55
		return delegate.addAll(c);
60
		return delegate.addAll(c);
56
	}
61
	}
57
62
Lines 68-81 Link Here
68
	}
73
	}
69
74
70
	public void clear() {
75
	public void clear() {
76
		getterCalled();
71
		delegate.clear();
77
		delegate.clear();
72
	}
78
	}
73
79
74
	public boolean contains(Object o) {
80
	public boolean contains(Object o) {
81
		getterCalled();
75
		return delegate.contains(o);
82
		return delegate.contains(o);
76
	}
83
	}
77
84
78
	public boolean containsAll(Collection c) {
85
	public boolean containsAll(Collection c) {
86
		getterCalled();
79
		return delegate.containsAll(c);
87
		return delegate.containsAll(c);
80
	}
88
	}
81
89
Lines 84-89 Link Here
84
	}
92
	}
85
93
86
	public boolean equals(Object obj) {
94
	public boolean equals(Object obj) {
95
		getterCalled();
87
		if (obj instanceof BeanObservableSetDecorator) {
96
		if (obj instanceof BeanObservableSetDecorator) {
88
			BeanObservableSetDecorator other = (BeanObservableSetDecorator) obj;
97
			BeanObservableSetDecorator other = (BeanObservableSetDecorator) obj;
89
			return Util.equals(other.delegate, delegate);
98
			return Util.equals(other.delegate, delegate);
Lines 100-125 Link Here
100
	}
109
	}
101
110
102
	public int hashCode() {
111
	public int hashCode() {
112
		getterCalled();
103
		return delegate.hashCode();
113
		return delegate.hashCode();
104
	}
114
	}
105
115
106
	public boolean isEmpty() {
116
	public boolean isEmpty() {
117
		getterCalled();
107
		return delegate.isEmpty();
118
		return delegate.isEmpty();
108
	}
119
	}
109
120
110
	public boolean isStale() {
121
	public boolean isStale() {
122
		getterCalled();
111
		return delegate.isStale();
123
		return delegate.isStale();
112
	}
124
	}
113
125
114
	public Iterator iterator() {
126
	public Iterator iterator() {
127
		getterCalled();
115
		return delegate.iterator();
128
		return delegate.iterator();
116
	}
129
	}
117
130
118
	public boolean remove(Object o) {
131
	public boolean remove(Object o) {
132
		getterCalled();
119
		return delegate.remove(o);
133
		return delegate.remove(o);
120
	}
134
	}
121
135
122
	public boolean removeAll(Collection c) {
136
	public boolean removeAll(Collection c) {
137
		getterCalled();
123
		return delegate.removeAll(c);
138
		return delegate.removeAll(c);
124
	}
139
	}
125
140
Lines 136-166 Link Here
136
	}
151
	}
137
152
138
	public boolean retainAll(Collection c) {
153
	public boolean retainAll(Collection c) {
154
		getterCalled();
139
		return delegate.retainAll(c);
155
		return delegate.retainAll(c);
140
	}
156
	}
141
157
142
	public int size() {
158
	public int size() {
159
		getterCalled();
143
		return delegate.size();
160
		return delegate.size();
144
	}
161
	}
145
162
146
	public Object[] toArray() {
163
	public Object[] toArray() {
164
		getterCalled();
147
		return delegate.toArray();
165
		return delegate.toArray();
148
	}
166
	}
149
167
150
	public Object[] toArray(Object[] a) {
168
	public Object[] toArray(Object[] a) {
169
		getterCalled();
151
		return delegate.toArray(a);
170
		return delegate.toArray(a);
152
	}
171
	}
153
172
154
	/* (non-Javadoc)
155
	 * @see org.eclipse.core.databinding.beans.IBeanObservable#getObserved()
156
	 */
157
	public Object getObserved() {
173
	public Object getObserved() {
158
		return observed;
174
		if (delegate instanceof IObserving)
175
			return ((IObserving) delegate).getObserved();
176
		return null;
159
	}
177
	}
160
178
161
	/* (non-Javadoc)
162
	 * @see org.eclipse.core.databinding.beans.IBeanObservable#getPropertyDescriptor()
163
	 */
164
	public PropertyDescriptor getPropertyDescriptor() {
179
	public PropertyDescriptor getPropertyDescriptor() {
165
		return propertyDescriptor;
180
		return propertyDescriptor;
166
	}
181
	}
(-)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 (-15 / +13 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 73-81 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
	}
Lines 85-90 Link Here
85
	}
90
	}
86
91
87
	public boolean isStale() {
92
	public boolean isStale() {
93
		getterCalled();
88
		return delegate.isStale();
94
		return delegate.isStale();
89
	}
95
	}
90
96
Lines 104-123 Link Here
104
		delegate.setValue(value);
110
		delegate.setValue(value);
105
	}
111
	}
106
112
107
	/*
108
	 * (non-Javadoc)
109
	 * 
110
	 * @see org.eclipse.core.databinding.beans.IBeanObservable#getObserved()
111
	 */
112
	public Object getObserved() {
113
	public Object getObserved() {
113
		return observed.getValue();
114
		if (delegate instanceof IObserving)
115
			return ((IObserving) delegate).getObserved();
116
		return null;
114
	}
117
	}
115
118
116
	/*
117
	 * (non-Javadoc)
118
	 * 
119
	 * @see org.eclipse.core.databinding.beans.IBeanObservable#getPropertyDescriptor()
120
	 */
121
	public PropertyDescriptor getPropertyDescriptor() {
119
	public PropertyDescriptor getPropertyDescriptor() {
122
		return descriptor;
120
		return descriptor;
123
	}
121
	}
(-)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 (-99 / +46 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 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)
242
				.getPropertyDescriptor();
257
243
258
		return new JavaBeanObservableList(realm, bean, propertyDescriptor,
244
		return new BeanObservableListDecorator(PropertyObservables.observeList(
259
				elementType);
245
				realm, bean, property), propertyDescriptor);
260
	}
246
	}
261
247
262
	/**
248
	/**
Lines 439-449 Link Here
439
425
440
		IObservableValue value = MasterDetailObservables.detailValue(master,
426
		IObservableValue value = MasterDetailObservables.detailValue(master,
441
				valueFactory(realm, propertyName), propertyType);
427
				valueFactory(realm, propertyName), propertyType);
442
		BeanObservableValueDecorator decorator = new BeanObservableValueDecorator(
428
		return new BeanObservableValueDecorator(value, BeanPropertyHelper
443
				value, master, getValueTypePropertyDescriptor(master,
429
				.getValueTypePropertyDescriptor(master, propertyName));
444
						propertyName));
445
446
		return decorator;
447
	}
430
	}
448
431
449
	/**
432
	/**
Lines 492-506 Link Here
492
	 * @since 1.1
475
	 * @since 1.1
493
	 */
476
	 */
494
	public static IObservableValue observeDetailValue(Realm realm,
477
	public static IObservableValue observeDetailValue(Realm realm,
495
			IObservableValue master, Class masterType, String propertyName, Class propertyType) {
478
			IObservableValue master, Class masterType, String propertyName,
479
			Class propertyType) {
496
		Assert.isNotNull(masterType, "masterType cannot be null"); //$NON-NLS-1$
480
		Assert.isNotNull(masterType, "masterType cannot be null"); //$NON-NLS-1$
497
		IObservableValue value = MasterDetailObservables.detailValue(master,
481
		IObservableValue value = MasterDetailObservables.detailValue(master,
498
				valueFactory(realm, propertyName), propertyType);
482
				valueFactory(realm, propertyName), propertyType);
499
		BeanObservableValueDecorator decorator = new BeanObservableValueDecorator(
483
		return new BeanObservableValueDecorator(value, BeanPropertyHelper
500
				value, master, getPropertyDescriptor(masterType,
484
				.getPropertyDescriptor(masterType, propertyName));
501
						propertyName));
502
503
		return decorator;
504
	}
485
	}
505
486
506
	/**
487
	/**
Lines 551-561 Link Here
551
		IObservableList observableList = MasterDetailObservables.detailList(
532
		IObservableList observableList = MasterDetailObservables.detailList(
552
				master, listFactory(realm, propertyName, propertyType),
533
				master, listFactory(realm, propertyName, propertyType),
553
				propertyType);
534
				propertyType);
554
		BeanObservableListDecorator decorator = new BeanObservableListDecorator(
535
		return new BeanObservableListDecorator(observableList,
555
				observableList, master, getValueTypePropertyDescriptor(master,
536
				BeanPropertyHelper.getValueTypePropertyDescriptor(master,
556
						propertyName));
537
						propertyName));
557
558
		return decorator;
559
	}
538
	}
560
539
561
	/**
540
	/**
Lines 599-609 Link Here
599
		IObservableSet observableSet = MasterDetailObservables.detailSet(
578
		IObservableSet observableSet = MasterDetailObservables.detailSet(
600
				master, setFactory(realm, propertyName, propertyType),
579
				master, setFactory(realm, propertyName, propertyType),
601
				propertyType);
580
				propertyType);
602
		BeanObservableSetDecorator decorator = new BeanObservableSetDecorator(
581
		return new BeanObservableSetDecorator(observableSet, BeanPropertyHelper
603
				observableSet, master, getValueTypePropertyDescriptor(master,
582
				.getValueTypePropertyDescriptor(master, propertyName));
604
						propertyName));
605
606
		return decorator;
607
	}
583
	}
608
584
609
	/**
585
	/**
Lines 642-651 Link Here
642
			IObservableValue master, String propertyName) {
618
			IObservableValue master, String propertyName) {
643
		IObservableMap observableMap = MasterDetailObservables.detailMap(
619
		IObservableMap observableMap = MasterDetailObservables.detailMap(
644
				master, mapPropertyFactory(realm, propertyName));
620
				master, mapPropertyFactory(realm, propertyName));
645
		BeanObservableMapDecorator decorator = new BeanObservableMapDecorator(
621
		return new BeanObservableMapDecorator(observableMap, BeanPropertyHelper
646
				observableMap, master, getValueTypePropertyDescriptor(master,
622
				.getValueTypePropertyDescriptor(master, propertyName));
647
						propertyName));
648
		return decorator;
649
	}
623
	}
650
624
651
	/**
625
	/**
Lines 688-699 Link Here
688
	 */
662
	 */
689
	public static IObservableSet observeSet(Realm realm, Object bean,
663
	public static IObservableSet observeSet(Realm realm, Object bean,
690
			String propertyName, Class elementType) {
664
			String propertyName, Class elementType) {
691
		PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean
665
		ISetProperty property = BeanProperties.setProperty(bean.getClass(),
692
				.getClass(), propertyName);
666
				propertyName, elementType);
693
		elementType = getCollectionElementType(elementType, propertyDescriptor);
667
		PropertyDescriptor propertyDescriptor = ((IBeanProperty) property)
668
				.getPropertyDescriptor();
694
669
695
		return new JavaBeanObservableSet(realm, bean, propertyDescriptor,
670
		return new BeanObservableSetDecorator(PropertyObservables.observeSet(
696
				elementType);
671
				realm, bean, property), propertyDescriptor);
697
	}
672
	}
698
673
699
	/**
674
	/**
Lines 828-859 Link Here
828
	public static IObservableFactory mapPropertyFactory(String propertyName) {
803
	public static IObservableFactory mapPropertyFactory(String propertyName) {
829
		return mapPropertyFactory(Realm.getDefault(), propertyName);
804
		return mapPropertyFactory(Realm.getDefault(), propertyName);
830
	}
805
	}
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
}
806
}
(-)src/org/eclipse/core/databinding/beans/PojoObservables.java (-49 / +46 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 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 (+214 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.HashSet;
20
import java.util.Iterator;
21
import java.util.Map;
22
import java.util.Set;
23
24
import org.eclipse.core.databinding.beans.IBeanProperty;
25
import org.eclipse.core.databinding.observable.Diffs;
26
import org.eclipse.core.databinding.observable.map.MapDiff;
27
import org.eclipse.core.databinding.property.MapProperty;
28
29
/**
30
 * @since 3.3
31
 * 
32
 */
33
public class BeanMapProperty extends MapProperty implements IBeanProperty {
34
	private PropertyDescriptor propertyDescriptor;
35
	private boolean attachListener;
36
37
	private ListenerSupport listenerSupport;
38
39
	private PropertyUpdateHelper updateHelper = new PropertyUpdateHelper();
40
41
	private Map sourceToCachedMap = Collections.synchronizedMap(new HashMap());
42
43
	/**
44
	 * @param propertyDescriptor
45
	 * @param attachListener
46
	 */
47
	public BeanMapProperty(PropertyDescriptor propertyDescriptor,
48
			boolean attachListener) {
49
		this.propertyDescriptor = propertyDescriptor;
50
		this.attachListener = attachListener;
51
	}
52
53
	private void initListenerSupport() {
54
		if (listenerSupport == null) {
55
			synchronized (this) {
56
				if (listenerSupport == null) {
57
					PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
58
						public void propertyChange(PropertyChangeEvent evt) {
59
							Object source = evt.getSource();
60
							if (!updateHelper.isUpdating(source)) {
61
								Object oldValue = evt.getOldValue();
62
								Object newValue = evt.getNewValue();
63
								if (oldValue == null && newValue == null) {
64
									// Obscure condition indicating new and old
65
									// values are unknown
66
									oldValue = sourceToCachedMap.get(source);
67
									newValue = BeanPropertyHelper.getProperty(
68
											source, propertyDescriptor);
69
								}
70
								sourceToCachedMap.put(source, newValue);
71
								fireMapChange(source, Diffs.computeMapDiff(
72
										asMap(oldValue), asMap(newValue)));
73
							}
74
						}
75
					};
76
					listenerSupport = new ListenerSupport(
77
							propertyChangeListener, propertyDescriptor
78
									.getName());
79
				}
80
			}
81
		}
82
	}
83
84
	protected void addListenerTo(Object source) {
85
		if (source != null && attachListener) {
86
			initListenerSupport();
87
			listenerSupport.hookListener(source);
88
			sourceToCachedMap.put(source, BeanPropertyHelper.getProperty(
89
					source, propertyDescriptor));
90
		}
91
	}
92
93
	protected void removeListenerFrom(Object source) {
94
		if (source != null && attachListener && listenerSupport != null) {
95
			listenerSupport.unhookListener(source);
96
			sourceToCachedMap.remove(source);
97
		}
98
	}
99
100
	public Map getMap(Object source) {
101
		if (source == null)
102
			return Collections.EMPTY_MAP;
103
		Object propertyValue = BeanPropertyHelper.getProperty(source,
104
				propertyDescriptor);
105
		return asMap(propertyValue);
106
	}
107
108
	private Map asMap(Object propertyValue) {
109
		if (propertyValue == null)
110
			return new HashMap();
111
		return (Map) propertyValue;
112
	}
113
114
	private void setMap(Object source, Map map, MapDiff diff) {
115
		if (source == null)
116
			return;
117
118
		if (diff == null) {
119
			diff = Diffs.computeMapDiff(getMap(source), map);
120
		}
121
122
		updateHelper.setUpdating(source, true);
123
		try {
124
			BeanPropertyHelper.setProperty(source, propertyDescriptor, map);
125
		} finally {
126
			updateHelper.setUpdating(source, false);
127
		}
128
129
		if (hasListeners(source)) {
130
			sourceToCachedMap.put(source, map);
131
			fireMapChange(source, diff);
132
		}
133
	}
134
135
	public void clear(Object source) {
136
		setMap(source, new HashMap(), Diffs.createMapDiffRemoveAll(new HashMap(
137
				getMap(source))));
138
	}
139
140
	public Object put(Object source, Object key, Object value) {
141
		Map map = new HashMap(getMap(source));
142
		boolean addition = !map.containsKey(key);
143
		Object result = map.put(key, value);
144
		MapDiff diff;
145
		if (addition) {
146
			diff = Diffs.createMapDiffSingleAdd(key, value);
147
		} else {
148
			diff = Diffs.createMapDiffSingleChange(key, result, value);
149
		}
150
		setMap(source, map, diff);
151
		return result;
152
	}
153
154
	public void putAll(Object source, Map t) {
155
		Map map = new HashMap(getMap(source));
156
		Set addedKeys = new HashSet();
157
		Set changedKeys = new HashSet();
158
		Map oldValues = new HashMap();
159
		Map newValues = new HashMap();
160
		for (Iterator it = t.entrySet().iterator(); it.hasNext();) {
161
			Map.Entry entry = (Map.Entry) it.next();
162
			Object key = entry.getKey();
163
			Object newValue = entry.getValue();
164
			boolean addition = !map.containsKey(key);
165
			Object oldValue = map.put(key, newValue);
166
			if (addition) {
167
				addedKeys.add(key);
168
			} else {
169
				changedKeys.add(key);
170
				oldValues.put(key, oldValue);
171
			}
172
			newValues.put(key, newValue);
173
		}
174
		setMap(source, map, Diffs.createMapDiff(addedKeys,
175
				Collections.EMPTY_SET, changedKeys, oldValues, newValues));
176
	}
177
178
	public Object remove(Object source, Object key) {
179
		Map map = getMap(source);
180
		if (map.containsKey(key)) {
181
			map = new HashMap(map);
182
			Object result = map.remove(key);
183
			setMap(source, map, Diffs.createMapDiffSingleRemove(key, result));
184
			return result;
185
		}
186
		return null;
187
	}
188
189
	public int size(Object source) {
190
		return getMap(source).size();
191
	}
192
193
	public synchronized void dispose() {
194
		propertyDescriptor = null;
195
		attachListener = false;
196
		if (listenerSupport != null) {
197
			listenerSupport.dispose();
198
			listenerSupport = null;
199
		}
200
		if (updateHelper != null) {
201
			updateHelper.dispose();
202
			updateHelper = null;
203
		}
204
		if (sourceToCachedMap != null) {
205
			sourceToCachedMap.clear();
206
			sourceToCachedMap = null;
207
		}
208
		super.dispose();
209
	}
210
211
	public PropertyDescriptor getPropertyDescriptor() {
212
		return propertyDescriptor;
213
	}
214
}
(-)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 (+330 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.Iterator;
24
import java.util.List;
25
import java.util.ListIterator;
26
import java.util.Map;
27
28
import org.eclipse.core.databinding.beans.IBeanProperty;
29
import org.eclipse.core.databinding.observable.Diffs;
30
import org.eclipse.core.databinding.observable.list.ListDiff;
31
import org.eclipse.core.databinding.observable.list.ListDiffEntry;
32
import org.eclipse.core.databinding.property.ListProperty;
33
34
/**
35
 * @since 3.3
36
 * 
37
 */
38
public class BeanListProperty extends ListProperty implements IBeanProperty {
39
	private PropertyDescriptor propertyDescriptor;
40
	private Class elementType;
41
	private boolean attachListener;
42
43
	private ListenerSupport listenerSupport;
44
45
	private PropertyUpdateHelper updateHelper = new PropertyUpdateHelper();
46
47
	private Map sourceToCachedList = Collections.synchronizedMap(new HashMap());
48
49
	/**
50
	 * @param propertyDescriptor
51
	 * @param elementType
52
	 * @param attachListener
53
	 */
54
	public BeanListProperty(PropertyDescriptor propertyDescriptor,
55
			Class elementType, boolean attachListener) {
56
		this.propertyDescriptor = propertyDescriptor;
57
		this.elementType = elementType == null ? BeanPropertyHelper
58
				.getCollectionPropertyElementType(propertyDescriptor)
59
				: elementType;
60
		this.attachListener = attachListener;
61
	}
62
63
	private void initListenerSupport() {
64
		if (listenerSupport == null) {
65
			synchronized (this) {
66
				if (listenerSupport == null) {
67
					PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
68
						public void propertyChange(PropertyChangeEvent evt) {
69
							Object source = evt.getSource();
70
							if (!updateHelper.isUpdating(source)) {
71
								Object oldValue = evt.getOldValue();
72
								Object newValue = evt.getNewValue();
73
								if (oldValue == null && newValue == null) {
74
									// Obscure condition indicating new and old
75
									// values are unknown
76
									oldValue = sourceToCachedList.get(source);
77
									newValue = BeanPropertyHelper.getProperty(
78
											source, propertyDescriptor);
79
								}
80
								sourceToCachedList.put(source, newValue);
81
								fireListChange(source, Diffs.computeListDiff(
82
										asList(oldValue), asList(newValue)));
83
							}
84
						}
85
					};
86
					listenerSupport = new ListenerSupport(
87
							propertyChangeListener, propertyDescriptor
88
									.getName());
89
				}
90
			}
91
		}
92
	}
93
94
	protected void addListenerTo(Object source) {
95
		if (source != null && attachListener) {
96
			initListenerSupport();
97
			listenerSupport.hookListener(source);
98
			sourceToCachedList.put(source, BeanPropertyHelper.getProperty(
99
					source, propertyDescriptor));
100
		}
101
	}
102
103
	protected void removeListenerFrom(Object source) {
104
		if (source != null && attachListener && listenerSupport != null) {
105
			listenerSupport.unhookListener(source);
106
			sourceToCachedList.remove(source);
107
		}
108
	}
109
110
	public List getList(Object source) {
111
		if (source == null)
112
			return Collections.EMPTY_LIST;
113
		Object propertyValue = BeanPropertyHelper.getProperty(source,
114
				propertyDescriptor);
115
		return asList(propertyValue);
116
	}
117
118
	private List asList(Object propertyValue) {
119
		if (propertyValue == null)
120
			return new ArrayList();
121
		if (propertyDescriptor.getPropertyType().isArray())
122
			return new ArrayList(Arrays.asList((Object[]) propertyValue));
123
		return (List) propertyValue;
124
	}
125
126
	private void setList(Object source, List list, ListDiff diff) {
127
		if (source == null)
128
			return;
129
130
		if (diff == null) {
131
			diff = Diffs.computeListDiff(getList(source), list);
132
		}
133
134
		updateHelper.setUpdating(source, true);
135
		try {
136
			BeanPropertyHelper.setProperty(source, propertyDescriptor,
137
					convertListToBeanPropertyType(list));
138
		} finally {
139
			updateHelper.setUpdating(source, false);
140
		}
141
142
		if (hasListeners(source)) {
143
			sourceToCachedList.put(source, list);
144
			fireListChange(source, diff);
145
		}
146
	}
147
148
	private Object convertListToBeanPropertyType(List list) {
149
		Object propertyValue = list;
150
		if (propertyDescriptor.getPropertyType().isArray()) {
151
			Class componentType = propertyDescriptor.getPropertyType()
152
					.getComponentType();
153
			Object[] array = (Object[]) Array.newInstance(componentType, list
154
					.size());
155
			list.toArray(array);
156
			propertyValue = array;
157
		}
158
		return propertyValue;
159
	}
160
161
	public boolean add(Object source, Object o) {
162
		add(source, size(source), o);
163
		return true;
164
	}
165
166
	public boolean addAll(Object source, Collection c) {
167
		if (c.isEmpty())
168
			return false;
169
		addAll(source, size(source), c);
170
		return true;
171
	}
172
173
	public void clear(Object source) {
174
		if (isEmpty(source))
175
			return;
176
		List list = getList(source);
177
		ListDiffEntry[] entries = new ListDiffEntry[list.size()];
178
		int i = 0;
179
		for (Iterator it = getList(source).iterator(); it.hasNext(); i++) {
180
			entries[i] = Diffs.createListDiffEntry(0, false, it.next());
181
		}
182
		setList(source, new ArrayList(), Diffs.createListDiff(entries));
183
	}
184
185
	public boolean remove(Object source, Object o) {
186
		int i = indexOf(source, o);
187
		if (i == -1)
188
			return false;
189
		remove(source, i);
190
		return true;
191
	}
192
193
	public boolean removeAll(Object source, Collection c) {
194
		if (isEmpty(source)) {
195
			return false;
196
		}
197
		if (c.isEmpty()) {
198
			return false;
199
		}
200
		List list = new ArrayList(getList(source));
201
		List entries = new ArrayList();
202
		for (ListIterator it = list.listIterator(); it.hasNext();) {
203
			Object o = it.next();
204
			if (c.contains(o)) {
205
				entries.add(Diffs.createListDiffEntry(it.previousIndex(),
206
						false, o));
207
				it.remove();
208
			}
209
		}
210
		boolean changed = !entries.isEmpty();
211
		if (changed) {
212
			ListDiffEntry[] ea = (ListDiffEntry[]) entries
213
					.toArray(new ListDiffEntry[entries.size()]);
214
			setList(source, list, Diffs.createListDiff(ea));
215
		}
216
		return changed;
217
	}
218
219
	public boolean retainAll(Object source, Collection c) {
220
		if (isEmpty(source)) {
221
			return false;
222
		}
223
		if (c.isEmpty()) {
224
			clear(source);
225
			return true;
226
		}
227
		List list = new ArrayList(getList(source));
228
		List entries = new ArrayList();
229
		for (ListIterator it = list.listIterator(); it.hasNext();) {
230
			Object o = it.next();
231
			if (!c.contains(o)) {
232
				entries.add(Diffs.createListDiffEntry(it.previousIndex(),
233
						false, o));
234
				it.remove();
235
			}
236
		}
237
		boolean changed = !entries.isEmpty();
238
		if (changed) {
239
			ListDiffEntry[] ea = (ListDiffEntry[]) entries
240
					.toArray(new ListDiffEntry[entries.size()]);
241
			setList(source, list, Diffs.createListDiff(ea));
242
		}
243
		return changed;
244
	}
245
246
	public void add(Object source, int index, Object element) {
247
		List list = new ArrayList(getList(source));
248
		list.add(index, element);
249
		setList(source, list, Diffs.createListDiff(Diffs.createListDiffEntry(
250
				index, true, element)));
251
	}
252
253
	public boolean addAll(Object source, int index, Collection c) {
254
		if (c.isEmpty()) {
255
			return false;
256
		}
257
258
		List list = new ArrayList(getList(source));
259
		List entries = new ArrayList();
260
		int i = index;
261
		for (Iterator it = c.iterator(); it.hasNext(); i++) {
262
			Object o = it.next();
263
			list.add(i, o);
264
			entries.add(Diffs.createListDiffEntry(i, true, o));
265
		}
266
		boolean changed = !entries.isEmpty();
267
		if (changed) {
268
			ListDiffEntry[] ea = (ListDiffEntry[]) entries
269
					.toArray(new ListDiffEntry[entries.size()]);
270
			setList(source, list, Diffs.createListDiff(ea));
271
		}
272
		return changed;
273
	}
274
275
	public Object remove(Object source, int index) {
276
		List list = new ArrayList(getList(source));
277
		Object result = list.remove(index);
278
		setList(source, list, Diffs.createListDiff(Diffs.createListDiffEntry(
279
				index, false, result)));
280
		return result;
281
	}
282
283
	public Object set(Object source, int index, Object element) {
284
		List list = new ArrayList(getList(source));
285
		Object result = list.set(index, element);
286
		setList(source, list, Diffs.createListDiff(Diffs.createListDiffEntry(
287
				index, false, result), Diffs.createListDiffEntry(index, true,
288
				element)));
289
		return result;
290
	}
291
292
	public Object move(Object source, int oldIndex, int newIndex) {
293
		if (oldIndex == newIndex)
294
			return get(source, oldIndex);
295
		List list = new ArrayList(getList(source));
296
		Object result = list.remove(oldIndex);
297
		list.add(newIndex, result);
298
		setList(source, list, Diffs.createListDiff(Diffs.createListDiffEntry(
299
				oldIndex, false, result), Diffs.createListDiffEntry(newIndex,
300
				true, result)));
301
		return result;
302
	}
303
304
	public Object getElementType() {
305
		return elementType;
306
	}
307
308
	public PropertyDescriptor getPropertyDescriptor() {
309
		return propertyDescriptor;
310
	}
311
312
	public synchronized void dispose() {
313
		propertyDescriptor = null;
314
		elementType = null;
315
		attachListener = false;
316
		if (listenerSupport != null) {
317
			listenerSupport.dispose();
318
			listenerSupport = null;
319
		}
320
		if (updateHelper != null) {
321
			updateHelper.dispose();
322
			updateHelper = null;
323
		}
324
		if (sourceToCachedList != null) {
325
			sourceToCachedList.clear();
326
			sourceToCachedList = null;
327
		}
328
		super.dispose();
329
	}
330
}
(-)src/org/eclipse/core/internal/databinding/beans/BeanSetProperty.java (+271 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.Iterator;
24
import java.util.Map;
25
import java.util.Set;
26
27
import org.eclipse.core.databinding.beans.IBeanProperty;
28
import org.eclipse.core.databinding.observable.Diffs;
29
import org.eclipse.core.databinding.observable.set.SetDiff;
30
import org.eclipse.core.databinding.property.SetProperty;
31
32
/**
33
 * @since 3.3
34
 * 
35
 */
36
public class BeanSetProperty extends SetProperty implements IBeanProperty {
37
	private PropertyDescriptor propertyDescriptor;
38
	private Class elementType;
39
	private boolean attachListener;
40
41
	private ListenerSupport listenerSupport;
42
43
	private PropertyUpdateHelper updateHelper = new PropertyUpdateHelper();
44
45
	private Map sourceToCachedSet = Collections.synchronizedMap(new HashMap());
46
47
	/**
48
	 * @param propertyDescriptor
49
	 * @param elementType
50
	 * @param attachListener
51
	 */
52
	public BeanSetProperty(PropertyDescriptor propertyDescriptor,
53
			Class elementType, boolean attachListener) {
54
		this.propertyDescriptor = propertyDescriptor;
55
		this.elementType = elementType == null ? BeanPropertyHelper
56
				.getCollectionPropertyElementType(propertyDescriptor)
57
				: elementType;
58
		this.attachListener = attachListener;
59
	}
60
61
	private void initListenerSupport() {
62
		if (listenerSupport == null) {
63
			synchronized (this) {
64
				if (listenerSupport == null) {
65
					PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
66
						public void propertyChange(PropertyChangeEvent evt) {
67
							Object source = evt.getSource();
68
							if (!updateHelper.isUpdating(source)) {
69
								Object oldValue = evt.getOldValue();
70
								Object newValue = evt.getNewValue();
71
								if (oldValue == null && newValue == null) {
72
									// Obscure condition indicating new and old
73
									// values are unknown
74
									oldValue = sourceToCachedSet.get(source);
75
									newValue = BeanPropertyHelper.getProperty(
76
											source, propertyDescriptor);
77
								}
78
								sourceToCachedSet.put(source, newValue);
79
								fireSetChange(source, Diffs.computeSetDiff(
80
										asSet(oldValue), asSet(newValue)));
81
							}
82
						}
83
					};
84
					listenerSupport = new ListenerSupport(
85
							propertyChangeListener, propertyDescriptor
86
									.getName());
87
				}
88
			}
89
		}
90
	}
91
92
	protected void addListenerTo(Object source) {
93
		if (source != null && attachListener) {
94
			initListenerSupport();
95
			listenerSupport.hookListener(source);
96
			sourceToCachedSet.put(source, BeanPropertyHelper.getProperty(
97
					source, propertyDescriptor));
98
		}
99
	}
100
101
	protected void removeListenerFrom(Object source) {
102
		if (source != null && attachListener && listenerSupport != null) {
103
			listenerSupport.unhookListener(source);
104
			sourceToCachedSet.remove(source);
105
		}
106
	}
107
108
	public Set getSet(Object source) {
109
		if (source == null)
110
			return Collections.EMPTY_SET;
111
		Object propertyValue = BeanPropertyHelper.getProperty(source,
112
				propertyDescriptor);
113
		return asSet(propertyValue);
114
	}
115
116
	private Set asSet(Object propertyValue) {
117
		if (propertyValue == null)
118
			return Collections.EMPTY_SET;
119
		if (propertyDescriptor.getPropertyType().isArray())
120
			return new HashSet(Arrays.asList((Object[]) propertyValue));
121
		return (Set) propertyValue;
122
	}
123
124
	private void setSet(Object source, Set set, SetDiff diff) {
125
		if (source == null)
126
			return;
127
128
		if (diff == null) {
129
			diff = Diffs.computeSetDiff(getSet(source), set);
130
		}
131
132
		updateHelper.setUpdating(source, true);
133
		try {
134
			BeanPropertyHelper.setProperty(source, propertyDescriptor,
135
					convertSetToBeanPropertyType(set));
136
		} finally {
137
			updateHelper.setUpdating(source, false);
138
		}
139
140
		if (hasListeners(source)) {
141
			sourceToCachedSet.put(source, set);
142
			fireSetChange(source, diff);
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 = getSet(source);
161
		if (!set.contains(o)) {
162
			set = new HashSet(set);
163
			boolean added = set.add(o);
164
			if (added)
165
				setSet(source, set, Diffs.createSetDiff(Collections
166
						.singleton(o), Collections.EMPTY_SET));
167
			return added;
168
		}
169
		return false;
170
	}
171
172
	public boolean addAll(Object source, Collection c) {
173
		Set set = getSet(source);
174
		Set additions = new HashSet();
175
		for (Iterator it = c.iterator(); it.hasNext();) {
176
			Object o = it.next();
177
			if (!set.contains(o)) {
178
				additions.add(o);
179
			}
180
		}
181
		boolean changed = !additions.isEmpty();
182
		if (changed) {
183
			set = new HashSet(set);
184
			set.addAll(additions);
185
			setSet(source, set, Diffs.createSetDiff(additions,
186
					Collections.EMPTY_SET));
187
		}
188
		return changed;
189
	}
190
191
	public void clear(Object source) {
192
		setSet(source, new HashSet(), Diffs.createSetDiff(
193
				Collections.EMPTY_SET, getSet(source)));
194
	}
195
196
	public boolean remove(Object source, Object o) {
197
		Set set = getSet(source);
198
		if (set.contains(o)) {
199
			set = new HashSet(set);
200
			boolean removed = set.remove(o);
201
			if (removed)
202
				setSet(source, set, Diffs.createSetDiff(Collections.EMPTY_SET,
203
						Collections.singleton(o)));
204
			return removed;
205
		}
206
		return false;
207
	}
208
209
	public boolean removeAll(Object source, Collection c) {
210
		Set set = new HashSet(getSet(source));
211
		Set removals = new HashSet();
212
		for (Iterator it = set.iterator(); it.hasNext();) {
213
			Object o = it.next();
214
			if (c.contains(o)) {
215
				removals.add(o);
216
				it.remove();
217
			}
218
		}
219
		boolean changed = !removals.isEmpty();
220
		if (changed) {
221
			setSet(source, set, Diffs.createSetDiff(Collections.EMPTY_SET,
222
					removals));
223
		}
224
		return changed;
225
	}
226
227
	public boolean retainAll(Object source, Collection c) {
228
		Set set = new HashSet(getSet(source));
229
		Set removals = new HashSet();
230
		for (Iterator it = set.iterator(); it.hasNext();) {
231
			Object o = it.next();
232
			if (!c.contains(o)) {
233
				removals.add(o);
234
				it.remove();
235
			}
236
		}
237
		boolean changed = !removals.isEmpty();
238
		if (changed) {
239
			setSet(source, set, Diffs.createSetDiff(Collections.EMPTY_SET,
240
					removals));
241
		}
242
		return changed;
243
	}
244
245
	public Object getElementType() {
246
		return elementType;
247
	}
248
249
	public PropertyDescriptor getPropertyDescriptor() {
250
		return propertyDescriptor;
251
	}
252
253
	public synchronized void dispose() {
254
		propertyDescriptor = null;
255
		elementType = null;
256
		attachListener = false;
257
		if (listenerSupport != null) {
258
			listenerSupport.dispose();
259
			listenerSupport = null;
260
		}
261
		if (updateHelper != null) {
262
			updateHelper.dispose();
263
			updateHelper = null;
264
		}
265
		if (sourceToCachedSet != null) {
266
			sourceToCachedSet.clear();
267
			sourceToCachedSet = null;
268
		}
269
		super.dispose();
270
	}
271
}
(-)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/databinding/observable/map/ComputedObservableMap.java (-5 / +37 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Matthew Hall - bug 241585
10
 *     Matthew Hall - bugs 241585, 194734
11
 *******************************************************************************/
11
 *******************************************************************************/
12
12
13
package org.eclipse.core.databinding.observable.map;
13
package org.eclipse.core.databinding.observable.map;
Lines 31-37 Link Here
31
 */
31
 */
32
public abstract class ComputedObservableMap extends AbstractObservableMap {
32
public abstract class ComputedObservableMap extends AbstractObservableMap {
33
33
34
	private final IObservableSet keySet;
34
	private IObservableSet keySet;
35
35
36
	private ISetChangeListener setChangeListener = new ISetChangeListener() {
36
	private ISetChangeListener setChangeListener = new ISetChangeListener() {
37
		public void handleSetChange(SetChangeEvent event) {
37
		public void handleSetChange(SetChangeEvent event) {
Lines 109-118 Link Here
109
		this.keySet.addSetChangeListener(setChangeListener);
109
		this.keySet.addSetChangeListener(setChangeListener);
110
	}
110
	}
111
111
112
	/**
113
     * @deprecated Subclasses are no longer required to call this method.
114
     */
112
	protected void init() {
115
	protected void init() {
113
		for (Iterator it = this.keySet.iterator(); it.hasNext();) {
116
	}
114
			Object key = it.next();
117
115
			hookListener(key);
118
	protected void firstListenerAdded() {
119
		hookListeners();
120
	}
121
122
	protected void lastListenerRemoved() {
123
		unhookListeners();
124
	}
125
126
	private void hookListeners() {
127
		if (keySet != null) {
128
			for (Iterator it = this.keySet.iterator(); it.hasNext();) {
129
				Object key = it.next();
130
				hookListener(key);
131
			}
132
		}
133
	}
134
135
	private void unhookListeners() {
136
		if (keySet != null) {
137
			Object[] keys = keySet.toArray();
138
			for (int i = 0; i < keys.length; i++) {
139
				unhookListener(keys[i]);
140
			}
141
			keySet = null;
116
		}
142
		}
117
	}
143
	}
118
144
Lines 163-166 Link Here
163
	 * @return the old value for the given key
189
	 * @return the old value for the given key
164
	 */
190
	 */
165
	protected abstract Object doPut(Object key, Object value);
191
	protected abstract Object doPut(Object key, Object value);
192
193
	public void dispose() {
194
		unhookListeners();
195
		entrySet = null;
196
		super.dispose();
197
	}
166
}
198
}
(-)src/org/eclipse/core/databinding/observable/list/AbstractObservableList.java (-11 / +20 lines)
Lines 7-17 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Brad Reynolds - bug 164653
10
 *     Brad Reynolds - bugs 164653, 167204
11
 *     Brad Reynolds - bug 167204
11
 *     Matthew Hall - bugs 118516, 208858, 208332, 194734
12
 *     Matthew Hall - bug 118516
13
 *     Matthew Hall - bug 208858
14
 *     Matthew Hall - bug 208332
15
 *******************************************************************************/
12
 *******************************************************************************/
16
13
17
package org.eclipse.core.databinding.observable.list;
14
package org.eclipse.core.databinding.observable.list;
Lines 76-86 Link Here
76
	}
73
	}
77
74
78
	public synchronized void addListChangeListener(IListChangeListener listener) {
75
	public synchronized void addListChangeListener(IListChangeListener listener) {
79
		changeSupport.addListener(ListChangeEvent.TYPE, listener);
76
		if (changeSupport != null) {
77
			changeSupport.addListener(ListChangeEvent.TYPE, listener);
78
		}
80
	}
79
	}
81
80
82
	public synchronized void removeListChangeListener(IListChangeListener listener) {
81
	public synchronized void removeListChangeListener(IListChangeListener listener) {
83
		changeSupport.removeListener(ListChangeEvent.TYPE, listener);
82
		if (changeSupport != null) {
83
			changeSupport.removeListener(ListChangeEvent.TYPE, listener);
84
		}
84
	}
85
	}
85
86
86
	protected void fireListChange(ListDiff diff) {
87
	protected void fireListChange(ListDiff diff) {
Lines 90-108 Link Here
90
	}
91
	}
91
92
92
	public synchronized void addChangeListener(IChangeListener listener) {
93
	public synchronized void addChangeListener(IChangeListener listener) {
93
		changeSupport.addChangeListener(listener);
94
		if (changeSupport != null) {
95
			changeSupport.addChangeListener(listener);
96
		}
94
	}
97
	}
95
98
96
	public synchronized void removeChangeListener(IChangeListener listener) {
99
	public synchronized void removeChangeListener(IChangeListener listener) {
97
		changeSupport.removeChangeListener(listener);
100
		if (changeSupport != null) {
101
			changeSupport.removeChangeListener(listener);
102
		}
98
	}
103
	}
99
104
100
	public synchronized void addStaleListener(IStaleListener listener) {
105
	public synchronized void addStaleListener(IStaleListener listener) {
101
		changeSupport.addStaleListener(listener);
106
		if (changeSupport != null) {
107
			changeSupport.addStaleListener(listener);
108
		}
102
	}
109
	}
103
110
104
	public synchronized void removeStaleListener(IStaleListener listener) {
111
	public synchronized void removeStaleListener(IStaleListener listener) {
105
		changeSupport.removeStaleListener(listener);
112
		if (changeSupport != null) {
113
			changeSupport.removeStaleListener(listener);
114
		}
106
	}
115
	}
107
116
108
	/**
117
	/**
(-)META-INF/MANIFEST.MF (+4 lines)
Lines 14-19 Link Here
14
 org.eclipse.core.databinding.observable.masterdetail,
14
 org.eclipse.core.databinding.observable.masterdetail,
15
 org.eclipse.core.databinding.observable.set;x-internal:=false,
15
 org.eclipse.core.databinding.observable.set;x-internal:=false,
16
 org.eclipse.core.databinding.observable.value;x-internal:=false,
16
 org.eclipse.core.databinding.observable.value;x-internal:=false,
17
 org.eclipse.core.databinding.property,
18
 org.eclipse.core.databinding.property.masterdetail,
17
 org.eclipse.core.databinding.util,
19
 org.eclipse.core.databinding.util,
18
 org.eclipse.core.databinding.validation;x-internal:=false,
20
 org.eclipse.core.databinding.validation;x-internal:=false,
19
 org.eclipse.core.internal.databinding;x-friends:="org.eclipse.core.databinding.beans",
21
 org.eclipse.core.internal.databinding;x-friends:="org.eclipse.core.databinding.beans",
Lines 21-26 Link Here
21
 org.eclipse.core.internal.databinding.observable;x-internal:=true,
23
 org.eclipse.core.internal.databinding.observable;x-internal:=true,
22
 org.eclipse.core.internal.databinding.observable.masterdetail;x-friends:="org.eclipse.jface.tests.databinding",
24
 org.eclipse.core.internal.databinding.observable.masterdetail;x-friends:="org.eclipse.jface.tests.databinding",
23
 org.eclipse.core.internal.databinding.observable.tree;x-friends:="org.eclipse.jface.databinding,org.eclipse.jface.tests.databinding",
25
 org.eclipse.core.internal.databinding.observable.tree;x-friends:="org.eclipse.jface.databinding,org.eclipse.jface.tests.databinding",
26
 org.eclipse.core.internal.databinding.property;x-friends:="org.eclipse.jface.databinding,org.eclipse.jface.tests.databinding",
27
 org.eclipse.core.internal.databinding.property.masterdetail;x-friends:="org.eclipse.jface.databinding,org.eclipse.jface.tests.databinding",
24
 org.eclipse.core.internal.databinding.validation;x-friends:="org.eclipse.jface.tests.databinding"
28
 org.eclipse.core.internal.databinding.validation;x-friends:="org.eclipse.jface.tests.databinding"
25
Require-Bundle: org.eclipse.equinox.common;bundle-version="[3.2.0,4.0.0)"
29
Require-Bundle: org.eclipse.equinox.common;bundle-version="[3.2.0,4.0.0)"
26
Import-Package-Comment: see http://wiki.eclipse.org/
30
Import-Package-Comment: see http://wiki.eclipse.org/
(-)src/org/eclipse/core/databinding/property/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 (+53 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation (bug 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.core.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
22
		IListProperty {
23
	Collection getCollection(Object source) {
24
		return getList(source);
25
	}
26
27
	public Object get(Object source, int index) {
28
		return getList(source).get(index);
29
	}
30
31
	public int indexOf(Object source, Object o) {
32
		return getList(source).indexOf(o);
33
	}
34
35
	public int lastIndexOf(Object source, Object o) {
36
		return getList(source).lastIndexOf(o);
37
	}
38
39
	public final void addListChangeListener(Object source,
40
			IListPropertyChangeListener listener) {
41
		getChangeSupport().addListener(source, listener);
42
	}
43
44
	public final void removeListChangeListener(Object source,
45
			IListPropertyChangeListener listener) {
46
		getChangeSupport().removeListener(source, listener);
47
	}
48
49
	protected final void fireListChange(Object source, ListDiff diff) {
50
		getChangeSupport().firePropertyChange(
51
				new ListPropertyChangeEvent(source, this, diff));
52
	}
53
}
(-)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 (+121 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation (bug 194734)
10
 ******************************************************************************/
11
12
package org.eclipse.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 hadListeners = 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 (!hadListeners)
60
			property.addListenerTo(source);
61
	}
62
63
	/**
64
	 * Returns whether the change support has listeners registered for the given
65
	 * property source
66
	 * 
67
	 * @param source
68
	 *            the property source
69
	 * @return whether the change support has listeners registered for the given
70
	 *         property source
71
	 */
72
	public boolean hasListeners(Object source) {
73
		return changeListeners != null && changeListeners.containsKey(source);
74
	}
75
76
	protected final void removeListener(Object source,
77
			IPropertyChangeListener listener) {
78
		if (changeListeners != null) {
79
			ListenerList listeners = (ListenerList) changeListeners.get(source);
80
			if (listeners != null) {
81
				listeners.remove(listener);
82
				if (listeners.isEmpty()) {
83
					synchronized (listeners) {
84
						if (listeners.isEmpty()) {
85
							changeListeners.remove(source);
86
							property.removeListenerFrom(source);
87
						}
88
					}
89
				}
90
			}
91
		}
92
	}
93
94
	protected final void firePropertyChange(PropertyChangeEvent event) {
95
		ListenerList listenerList;
96
		synchronized (this) {
97
			if (changeListeners == null)
98
				return;
99
			listenerList = (ListenerList) changeListeners
100
					.get(event.getSource());
101
		}
102
		if (listenerList != null) {
103
			Object[] listeners = listenerList.getListeners();
104
			for (int i = 0; i < listeners.length; i++) {
105
				event.dispatch((IPropertyChangeListener) listeners[i]);
106
			}
107
		}
108
	}
109
110
	void dispose() {
111
		if (changeListeners != null) {
112
			for (Iterator iterator = changeListeners.keySet().iterator(); iterator
113
					.hasNext();) {
114
				Object source = iterator.next();
115
				property.removeListenerFrom(source);
116
				iterator.remove();
117
			}
118
			changeListeners = null;
119
		}
120
	}
121
}
(-)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 (+369 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
35
		IObserving {
36
	private Object source;
37
	private IListProperty property;
38
39
	private volatile boolean updating = false;
40
41
	private boolean disposed = false;
42
43
	private transient volatile int modCount = 0;
44
45
	private IListPropertyChangeListener listener = new IListPropertyChangeListener() {
46
		public void handleListPropertyChange(final ListPropertyChangeEvent event) {
47
			if (!disposed && !updating) {
48
				getRealm().exec(new Runnable() {
49
					public void run() {
50
						getRealm().exec(new Runnable() {
51
							public void run() {
52
								modCount++;
53
								fireListChange(event.diff);
54
							}
55
						});
56
					}
57
				});
58
			}
59
		}
60
	};
61
62
	/**
63
	 * @param realm
64
	 * @param source
65
	 * @param property
66
	 */
67
	public PropertyObservableList(Realm realm, Object source,
68
			IListProperty property) {
69
		super(realm);
70
		this.source = source;
71
		this.property = property;
72
	}
73
74
	protected void firstListenerAdded() {
75
		if (!disposed) {
76
			property.addListChangeListener(source, listener);
77
		}
78
	}
79
80
	protected void lastListenerRemoved() {
81
		if (!disposed) {
82
			property.removeListChangeListener(source, listener);
83
		}
84
	}
85
86
	private void getterCalled() {
87
		ObservableTracker.getterCalled(this);
88
	}
89
90
	public Object getElementType() {
91
		return property.getElementType();
92
	}
93
94
	// Queries
95
96
	protected int doGetSize() {
97
		return property.size(source);
98
	}
99
100
	public boolean contains(Object o) {
101
		getterCalled();
102
		return property.contains(source, o);
103
	}
104
105
	public boolean containsAll(Collection c) {
106
		getterCalled();
107
		return property.containsAll(source, c);
108
	}
109
110
	public Object get(int index) {
111
		getterCalled();
112
		return property.get(source, index);
113
	}
114
115
	public int indexOf(Object o) {
116
		getterCalled();
117
		return property.indexOf(source, o);
118
	}
119
120
	public boolean isEmpty() {
121
		getterCalled();
122
		return property.isEmpty(source);
123
	}
124
125
	public int lastIndexOf(Object o) {
126
		getterCalled();
127
		return property.lastIndexOf(source, o);
128
	}
129
130
	public Object[] toArray() {
131
		getterCalled();
132
		return property.toArray(source);
133
	}
134
135
	public Object[] toArray(Object[] a) {
136
		getterCalled();
137
		return property.toArray(source, a);
138
	}
139
140
	// Single change operations
141
142
	public boolean add(Object o) {
143
		checkRealm();
144
		return property.add(source, o);
145
	}
146
147
	public Iterator iterator() {
148
		getterCalled();
149
		return new Iterator() {
150
			int lastReturned = -1;
151
			int expectedModCount = modCount;
152
			ListIterator delegate = new ArrayList(property.getList(source))
153
					.listIterator();
154
155
			public boolean hasNext() {
156
				getterCalled();
157
				checkForComodification();
158
				return delegate.hasNext();
159
			}
160
161
			public Object next() {
162
				getterCalled();
163
				checkForComodification();
164
				Object next = delegate.next();
165
				lastReturned = delegate.previousIndex();
166
				return next;
167
			}
168
169
			public void remove() {
170
				checkRealm();
171
				checkForComodification();
172
				if (lastReturned == -1)
173
					throw new IllegalStateException();
174
175
				delegate.remove(); // stay in sync
176
177
				property.remove(source, lastReturned);
178
179
				lastReturned = -1;
180
				expectedModCount = modCount;
181
			}
182
183
			private void checkForComodification() {
184
				if (expectedModCount != modCount)
185
					throw new ConcurrentModificationException();
186
			}
187
		};
188
	}
189
190
	public Object move(int oldIndex, int newIndex) {
191
		getterCalled();
192
		return property.move(source, oldIndex, newIndex);
193
	}
194
195
	public boolean remove(Object o) {
196
		getterCalled();
197
		return property.remove(source, o);
198
	}
199
200
	public void add(int index, Object o) {
201
		getterCalled();
202
		property.add(source, index, o);
203
	}
204
205
	public ListIterator listIterator() {
206
		return listIterator(0);
207
	}
208
209
	public ListIterator listIterator(final int index) {
210
		getterCalled();
211
		return new ListIterator() {
212
			int lastReturned = -1;
213
			int expectedModCount = modCount;
214
			ListIterator delegate = new ArrayList(property.getList(source))
215
					.listIterator(index);
216
217
			public boolean hasNext() {
218
				getterCalled();
219
				checkForComodification();
220
				return delegate.hasNext();
221
			}
222
223
			public int nextIndex() {
224
				getterCalled();
225
				checkForComodification();
226
				return delegate.nextIndex();
227
			}
228
229
			public Object next() {
230
				getterCalled();
231
				checkForComodification();
232
				Object next = delegate.next();
233
				lastReturned = delegate.previousIndex();
234
				return next;
235
			}
236
237
			public boolean hasPrevious() {
238
				getterCalled();
239
				checkForComodification();
240
				return delegate.hasPrevious();
241
			}
242
243
			public int previousIndex() {
244
				getterCalled();
245
				checkForComodification();
246
				return delegate.previousIndex();
247
			}
248
249
			public Object previous() {
250
				getterCalled();
251
				checkForComodification();
252
				Object previous = delegate.previous();
253
				lastReturned = delegate.nextIndex();
254
				return previous;
255
			}
256
257
			public void add(Object o) {
258
				checkRealm();
259
				checkForComodification();
260
				int index = delegate.nextIndex();
261
262
				delegate.add(o); // keep in sync
263
264
				property.add(source, index, o);
265
266
				lastReturned = -1;
267
				expectedModCount = modCount;
268
			}
269
270
			public void set(Object o) {
271
				checkRealm();
272
				checkForComodification();
273
274
				delegate.set(o);
275
276
				property.set(source, lastReturned, o);
277
278
				expectedModCount = modCount;
279
			}
280
281
			public void remove() {
282
				checkRealm();
283
				checkForComodification();
284
				if (lastReturned == -1)
285
					throw new IllegalStateException();
286
287
				delegate.remove(); // keep in sync
288
289
				property.remove(source, lastReturned);
290
291
				lastReturned = -1;
292
				expectedModCount = modCount;
293
			}
294
295
			private void checkForComodification() {
296
				if (expectedModCount != modCount)
297
					throw new ConcurrentModificationException();
298
			}
299
300
		};
301
	}
302
303
	public Object remove(int index) {
304
		getterCalled();
305
		return property.remove(source, index);
306
	}
307
308
	public Object set(int index, Object o) {
309
		getterCalled();
310
		return property.set(source, index, o);
311
	}
312
313
	public List subList(int fromIndex, int toIndex) {
314
		getterCalled();
315
		return Collections.unmodifiableList(property.getList(source).subList(
316
				fromIndex, toIndex));
317
	}
318
319
	// Bulk change operations
320
321
	public boolean addAll(Collection c) {
322
		getterCalled();
323
		return property.addAll(source, c);
324
	}
325
326
	public boolean addAll(int index, Collection c) {
327
		getterCalled();
328
		return property.addAll(source, index, c);
329
	}
330
331
	public boolean removeAll(Collection c) {
332
		getterCalled();
333
		return property.removeAll(source, c);
334
	}
335
336
	public boolean retainAll(Collection c) {
337
		getterCalled();
338
		return property.retainAll(source, c);
339
	}
340
341
	public void clear() {
342
		getterCalled();
343
		property.clear(source);
344
	}
345
346
	public boolean equals(Object o) {
347
		getterCalled();
348
		return property.equals(source, o);
349
	}
350
351
	public int hashCode() {
352
		getterCalled();
353
		return property.hashCode(source);
354
	}
355
356
	public Object getObserved() {
357
		return source;
358
	}
359
360
	public synchronized void dispose() {
361
		if (!disposed) {
362
			disposed = true;
363
			property.removeListChangeListener(source, listener);
364
			property = null;
365
			source = null;
366
		}
367
		super.dispose();
368
	}
369
}
(-)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 disposed = false;
31
32
	private IValuePropertyChangeListener listener = new IValuePropertyChangeListener() {
33
		public void handleValuePropertyChange(
34
				final ValuePropertyChangeEvent event) {
35
			if (!disposed) {
36
				getRealm().exec(new Runnable() {
37
					public void run() {
38
						Object key = event.getSource();
39
						Object oldValue = event.diff.getOldValue();
40
						Object newValue = event.diff.getNewValue();
41
						fireMapChange(Diffs.createMapDiffSingleChange(key,
42
								oldValue, newValue));
43
					}
44
				});
45
			}
46
		}
47
	};
48
49
	/**
50
	 * @param keySet
51
	 * @param valueProperty
52
	 */
53
	public SetValuePropertyObservableMap(IObservableSet keySet,
54
			IValueProperty valueProperty) {
55
		super(keySet);
56
		this.property = valueProperty;
57
	}
58
59
	protected Object doGet(Object key) {
60
		return property.getValue(key);
61
	}
62
63
	protected Object doPut(Object key, Object value) {
64
		Object result = property.getValue(key);
65
		property.setValue(key, value);
66
		return result;
67
	}
68
69
	protected void hookListener(Object addedKey) {
70
		if (!disposed)
71
			property.addValueChangeListener(addedKey, listener);
72
	}
73
74
	protected void unhookListener(Object removedKey) {
75
		if (!disposed)
76
			property.removeValueChangeListener(removedKey, listener);
77
	}
78
79
	public Object getObserved() {
80
		return keySet();
81
	}
82
83
	public synchronized void dispose() {
84
		super.dispose();
85
86
		if (!disposed) {
87
			disposed = true;
88
			property = null;
89
			listener = null;
90
		}
91
	}
92
}
(-)src/org/eclipse/core/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
}

Return to bug 194734