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

(-)META-INF/MANIFEST.MF (+1 lines)
Lines 14-19 Link Here
14
 org.eclipse.core.databinding.observable.masterdetail,
14
 org.eclipse.core.databinding.observable.masterdetail,
15
 org.eclipse.core.databinding.observable.set;x-internal:=false,
15
 org.eclipse.core.databinding.observable.set;x-internal:=false,
16
 org.eclipse.core.databinding.observable.value;x-internal:=false,
16
 org.eclipse.core.databinding.observable.value;x-internal:=false,
17
 org.eclipse.core.databinding.property,
17
 org.eclipse.core.databinding.util,
18
 org.eclipse.core.databinding.util,
18
 org.eclipse.core.databinding.validation;x-internal:=false,
19
 org.eclipse.core.databinding.validation;x-internal:=false,
19
 org.eclipse.core.internal.databinding;x-friends:="org.eclipse.core.databinding.beans",
20
 org.eclipse.core.internal.databinding;x-friends:="org.eclipse.core.databinding.beans",
(-)src/org/eclipse/core/databinding/property/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
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().fireChange(
32
				new ValuePropertyChangeEvent(source, this, diff));
33
	}
34
}
(-)src/org/eclipse/core/databinding/property/IValuePropertyChangeListener.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
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property;
13
14
/**
15
 * @since 1.2
16
 * 
17
 */
18
public interface IValuePropertyChangeListener {
19
	/**
20
	 * @param event
21
	 */
22
	public void handleValuePropertyChange(ValuePropertyChangeEvent event);
23
}
(-)src/org/eclipse/core/databinding/property/ValuePropertyChangeEvent.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
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
 */
20
public class ValuePropertyChangeEvent extends PropertyChangeEvent {
21
	private final IValueProperty property;
22
	private final ValueDiff diff;
23
24
	/**
25
	 * @param source
26
	 * @param property
27
	 * @param diff
28
	 */
29
	public ValuePropertyChangeEvent(Object source, IValueProperty property,
30
			ValueDiff diff) {
31
		super(source);
32
		this.property = property;
33
		this.diff = diff;
34
	}
35
36
	/**
37
	 * @return Returns the property.
38
	 */
39
	public IValueProperty getProperty() {
40
		return property;
41
	}
42
43
	/**
44
	 * @return Returns the diff.
45
	 */
46
	public ValueDiff getDiff() {
47
		return diff;
48
	}
49
50
	protected void dispatch(Object listener) {
51
		((IValuePropertyChangeListener)listener).handleValuePropertyChange(this);
52
	}
53
}
(-)src/org/eclipse/core/databinding/property/PropertyChangeEvent.java (+36 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property;
13
14
/**
15
 * @since 1.2
16
 *
17
 */
18
public abstract class PropertyChangeEvent {
19
	private final Object source;
20
	
21
	/**
22
	 * @param source
23
	 */
24
	public PropertyChangeEvent(Object source) {
25
		this.source = source;
26
	}
27
28
	protected abstract void dispatch(Object listener);
29
30
	/**
31
	 * @return Returns the source.
32
	 */
33
	public final Object getSource() {
34
		return source;
35
	}
36
}
(-)src/org/eclipse/core/internal/databinding/property/PropertyObservableValue.java (+104 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.property;
13
14
import org.eclipse.core.databinding.observable.Diffs;
15
import org.eclipse.core.databinding.observable.Realm;
16
import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
17
import org.eclipse.core.databinding.property.IValueProperty;
18
import org.eclipse.core.databinding.property.IValuePropertyChangeListener;
19
import org.eclipse.core.databinding.property.ValuePropertyChangeEvent;
20
import org.eclipse.core.internal.databinding.Util;
21
22
/**
23
 * @since 1.2
24
 * 
25
 */
26
public class PropertyObservableValue extends AbstractObservableValue {
27
	private Object source;
28
	private IValueProperty property;
29
30
	private volatile boolean updating = false;
31
32
	private boolean disposed = false;
33
34
	private IValuePropertyChangeListener listener = new IValuePropertyChangeListener() {
35
		public void handleValuePropertyChange(
36
				final ValuePropertyChangeEvent event) {
37
			if (!disposed && !updating) {
38
				getRealm().exec(new Runnable() {
39
					public void run() {
40
						fireValueChange(event.getDiff());
41
					}
42
				});
43
			}
44
		}
45
	};
46
47
	/**
48
	 * @param realm
49
	 * @param source
50
	 * @param property
51
	 */
52
	public PropertyObservableValue(Realm realm, Object source,
53
			IValueProperty property) {
54
		super(realm);
55
		this.source = source;
56
		this.property = property;
57
	}
58
59
	protected void firstListenerAdded() {
60
		if (!disposed) {
61
			property.addValueChangeListener(source, listener);
62
		}
63
	}
64
65
	protected void lastListenerRemoved() {
66
		if (!disposed) {
67
			property.removeValueChangeListener(source, listener);
68
		}
69
	}
70
71
	protected Object doGetValue() {
72
		return property.getValue(source);
73
	}
74
75
	protected void doSetValue(Object value) {
76
		Object oldValue = doGetValue();
77
78
		updating = true;
79
		try {
80
			property.setValue(source, value);
81
		} finally {
82
			updating = false;
83
		}
84
85
		Object newValue = doGetValue();
86
		if (!Util.equals(oldValue, newValue)) {
87
			fireValueChange(Diffs.createValueDiff(oldValue, newValue));
88
		}
89
	}
90
91
	public Object getValueType() {
92
		return property.getValueType(source);
93
	}
94
95
	public synchronized void dispose() {
96
		if (!disposed) {
97
			disposed = true;
98
			property.removeValueChangeListener(source, listener);
99
			property = null;
100
			source = null;
101
		}
102
		super.dispose();
103
	}
104
}
(-)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
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/PropertyChangeSupport.java (+106 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property;
13
14
import java.util.Collections;
15
import java.util.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, Object listener) {
34
		boolean wasListening = changeListeners.containsKey(source);
35
36
		if (changeListeners == null) {
37
			synchronized (this) {
38
				if (changeListeners == null) {
39
					changeListeners = Collections
40
							.synchronizedMap(new IdentityHashMap());
41
				}
42
			}
43
		}
44
45
		ListenerList listeners;
46
		synchronized (changeListeners) {
47
			if (changeListeners.containsKey(source)) {
48
				listeners = (ListenerList) changeListeners.get(source);
49
			} else {
50
				changeListeners.put(source, listeners = new ListenerList());
51
			}
52
		}
53
54
		synchronized (listeners) {
55
			listeners.add(listener);
56
		}
57
58
		if (!wasListening)
59
			property.addListenerTo(source);
60
	}
61
62
	protected final void removeListener(Object source, Object listener) {
63
		if (changeListeners != null) {
64
			ListenerList listeners = (ListenerList) changeListeners.get(source);
65
			if (listeners != null) {
66
				listeners.remove(listener);
67
				if (listeners.isEmpty()) {
68
					synchronized (listeners) {
69
						if (listeners.isEmpty()) {
70
							changeListeners.remove(source);
71
							property.removeListenerFrom(source);
72
						}
73
					}
74
				}
75
			}
76
		}
77
	}
78
79
	protected final void fireChange(PropertyChangeEvent event) {
80
		ListenerList listenerList;
81
		synchronized (this) {
82
			if (changeListeners == null)
83
				return;
84
			listenerList = (ListenerList) changeListeners
85
					.get(event.getSource());
86
		}
87
		if (listenerList != null) {
88
			Object[] listeners = listenerList.getListeners();
89
			for (int i = 0; i < listeners.length; i++) {
90
				event.dispatch(listeners[i]);
91
			}
92
		}
93
	}
94
95
	void dispose() {
96
		if (changeListeners != null) {
97
			for (Iterator iterator = changeListeners.keySet().iterator(); iterator
98
					.hasNext();) {
99
				Object source = iterator.next();
100
				property.removeListenerFrom(source);
101
				iterator.remove();
102
			}
103
			changeListeners = null;
104
		}
105
	}
106
}
(-)src/org/eclipse/core/databinding/property/PropertyObservables.java (+80 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property;
13
14
import org.eclipse.core.databinding.observable.IObservable;
15
import org.eclipse.core.databinding.observable.Realm;
16
import org.eclipse.core.databinding.observable.masterdetail.IObservableFactory;
17
import org.eclipse.core.databinding.observable.value.IObservableValue;
18
import org.eclipse.core.internal.databinding.property.PropertyObservableValue;
19
20
/**
21
 * @since 1.2
22
 * 
23
 */
24
public class PropertyObservables {
25
	/**
26
	 * @param source
27
	 * @param property
28
	 * @return an observable value that tracks the given property of the source
29
	 *         object.
30
	 * @since 1.2
31
	 */
32
	public static IObservableValue observeValue(Object source,
33
			IValueProperty property) {
34
		return observeValue(Realm.getDefault(), source, property);
35
	}
36
37
	/**
38
	 * @param realm
39
	 * @param source
40
	 * @param property
41
	 * @return an observable value that tracks the given property of the source
42
	 *         object
43
	 * @since 1.2
44
	 */
45
	public static IObservableValue observeValue(Realm realm, Object source,
46
			IValueProperty property) {
47
		return new PropertyObservableValue(realm, source, property);
48
	}
49
50
	/**
51
	 * Returns a factory for creating obervable values tracking the given
52
	 * property of a particular source object
53
	 * 
54
	 * @param property
55
	 *            the property to be observed
56
	 * @return an observable value factory on
57
	 */
58
	public static IObservableFactory valueFactory(IValueProperty property) {
59
		return valueFactory(Realm.getDefault(), property);
60
	}
61
62
	/**
63
	 * Returns a factory for creating obervable values tracking the given
64
	 * property of a particular source object
65
	 * 
66
	 * @param realm
67
	 *            the realm to use
68
	 * @param property
69
	 *            the property to be observed
70
	 * @return an observable value factory
71
	 */
72
	public static IObservableFactory valueFactory(final Realm realm,
73
			final IValueProperty property) {
74
		return new IObservableFactory() {
75
			public IObservable createObservable(Object target) {
76
				return observeValue(realm, target, property);
77
			}
78
		};
79
	}
80
}
(-)src/org/eclipse/core/databinding/property/Property.java (+56 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation
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
	public synchronized void dispose() {
51
		if (changeSupport != null) {
52
			changeSupport.dispose();
53
			changeSupport = null;
54
		}
55
	}
56
}
(-)src/org/eclipse/core/databinding/property/IValueProperty.java (+51 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.property;
13
14
/**
15
 * Interface for observing a value property of a source object.
16
 * 
17
 * @since 1.2
18
 */
19
public interface IValueProperty extends IProperty {
20
	/**
21
	 * @param source
22
	 * @return the value type
23
	 */
24
	public Object getValueType(Object source);
25
26
	/**
27
	 * @param source
28
	 * @return the property value
29
	 */
30
	public Object getValue(Object source);
31
32
	/**
33
	 * @param source
34
	 * @param value
35
	 */
36
	public void setValue(Object source, Object value);
37
38
	/**
39
	 * @param source
40
	 * @param listener
41
	 */
42
	public void addValueChangeListener(Object source,
43
			IValuePropertyChangeListener listener);
44
45
	/**
46
	 * @param source
47
	 * @param listener
48
	 */
49
	public void removeValueChangeListener(Object source,
50
			IValuePropertyChangeListener listener);
51
}
(-)src/org/eclipse/core/internal/databinding/beans/BeanValueProperty.java (+122 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.beans;
13
14
import java.beans.PropertyChangeEvent;
15
import java.beans.PropertyChangeListener;
16
import java.beans.PropertyDescriptor;
17
18
import org.eclipse.core.databinding.observable.Diffs;
19
import org.eclipse.core.databinding.property.ValueProperty;
20
import org.eclipse.core.internal.databinding.Util;
21
22
/**
23
 * @since 3.3
24
 * 
25
 */
26
public class BeanValueProperty extends ValueProperty {
27
	private PropertyDescriptor propertyDescriptor;
28
	private final boolean attachListener;
29
30
	private boolean updating;
31
32
	private ListenerSupport listenerSupport;
33
34
	/**
35
	 * @param propertyDescriptor
36
	 */
37
	public BeanValueProperty(PropertyDescriptor propertyDescriptor) {
38
		this(propertyDescriptor, true);
39
	}
40
41
	/**
42
	 * @param propertyDescriptor
43
	 * @param attachListener
44
	 */
45
	public BeanValueProperty(PropertyDescriptor propertyDescriptor,
46
			boolean attachListener) {
47
		this.propertyDescriptor = propertyDescriptor;
48
		this.attachListener = attachListener;
49
50
	}
51
52
	private void initListenerSupport() {
53
		if (listenerSupport == null) {
54
			synchronized (this) {
55
				if (listenerSupport != null) {
56
					PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
57
						public void propertyChange(PropertyChangeEvent evt) {
58
							if (!updating) {
59
								fireValueChange(evt.getSource(), Diffs
60
										.createValueDiff(evt.getOldValue(), evt
61
												.getNewValue()));
62
							}
63
						}
64
					};
65
					listenerSupport = new ListenerSupport(
66
							propertyChangeListener, propertyDescriptor
67
									.getName());
68
				}
69
			}
70
		}
71
	}
72
73
	protected void addListenerTo(Object source) {
74
		if (attachListener) {
75
			initListenerSupport();
76
			listenerSupport.hookListener(source);
77
		}
78
	}
79
80
	protected void removeListenerFrom(Object source) {
81
		if (attachListener && listenerSupport != null) {
82
			listenerSupport.unhookListener(source);
83
		}
84
	}
85
86
	public Object getValueType(Object source) {
87
		return propertyDescriptor.getPropertyType();
88
	}
89
90
	public Object getValue(Object source) {
91
		return BeanPropertyHelper.getProperty(source, propertyDescriptor);
92
	}
93
94
	public void setValue(Object source, Object value) {
95
		Object oldValue = getValue(source);
96
97
		if (Util.equals(oldValue, value)) {
98
			return;
99
		}
100
101
		updating = true;
102
		try {
103
			BeanPropertyHelper.setProperty(source, propertyDescriptor, value);
104
		} finally {
105
			updating = false;
106
		}
107
108
		Object newValue = getValue(source);
109
		if (!Util.equals(oldValue, newValue)) {
110
			fireValueChange(source, Diffs.createValueDiff(oldValue, newValue));
111
		}
112
	}
113
114
	public synchronized void dispose() {
115
		if (listenerSupport != null) {
116
			listenerSupport.dispose();
117
			listenerSupport = null;
118
		}
119
		propertyDescriptor = null;
120
		super.dispose();
121
	}
122
}
(-)src/org/eclipse/core/databinding/beans/PojoProperties.java (+32 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.beans;
13
14
import org.eclipse.core.databinding.property.IValueProperty;
15
import org.eclipse.core.internal.databinding.beans.BeanValueProperty;
16
17
/**
18
 * @since 1.2
19
 * 
20
 */
21
public class PojoProperties {
22
	/**
23
	 * @param beanClass
24
	 * @param propertyName
25
	 * @return a bean value property for the given property name.
26
	 */
27
	public static IValueProperty valueProperty(Class beanClass,
28
			String propertyName) {
29
		return new BeanValueProperty(BeansObservables.getPropertyDescriptor(
30
				beanClass, propertyName), false);
31
	}
32
}
(-)src/org/eclipse/core/databinding/beans/BeanProperties.java (+33 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 Matthew Hall and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Matthew Hall - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.core.databinding.beans;
13
14
import org.eclipse.core.databinding.property.IValueProperty;
15
import org.eclipse.core.internal.databinding.beans.BeanValueProperty;
16
17
/**
18
 * @since 1.2
19
 * 
20
 */
21
public class BeanProperties {
22
	/**
23
	 * @param beanClass
24
	 * @param propertyName
25
	 * @return a value property for the given property name of the given bean
26
	 *         class.
27
	 */
28
	public static IValueProperty valueProperty(Class beanClass,
29
			String propertyName) {
30
		return new BeanValueProperty(BeansObservables.getPropertyDescriptor(
31
				beanClass, propertyName));
32
	}
33
}
(-)src/org/eclipse/core/internal/databinding/beans/BeanPropertyHelper.java (+109 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2008 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 ******************************************************************************/
11
12
package org.eclipse.core.internal.databinding.beans;
13
14
import java.beans.PropertyDescriptor;
15
import java.lang.reflect.InvocationTargetException;
16
import java.lang.reflect.Method;
17
18
import org.eclipse.core.databinding.BindingException;
19
import org.eclipse.core.databinding.beans.BeansObservables;
20
import org.eclipse.core.databinding.util.Policy;
21
import org.eclipse.core.runtime.IStatus;
22
import org.eclipse.core.runtime.Status;
23
24
/**
25
 * @since 1.2
26
 * 
27
 */
28
public class BeanPropertyHelper {
29
	/**
30
	 * Sets the contents of the given property on the given source object to the
31
	 * given value.
32
	 * 
33
	 * @param source
34
	 *            the source object which has the property being updated
35
	 * @param propertyDescriptor
36
	 *            the property being changed
37
	 * @param value
38
	 *            the new value of the property
39
	 */
40
	public static void setProperty(Object source,
41
			PropertyDescriptor propertyDescriptor, Object value) {
42
		try {
43
			Method writeMethod = propertyDescriptor.getWriteMethod();
44
			if (!writeMethod.isAccessible()) {
45
				writeMethod.setAccessible(true);
46
			}
47
			writeMethod.invoke(source, new Object[] { value });
48
		} catch (InvocationTargetException e) {
49
			/*
50
			 * InvocationTargetException wraps any exception thrown by the
51
			 * invoked method.
52
			 */
53
			throw new RuntimeException(e.getCause());
54
		} catch (Exception e) {
55
			if (BeansObservables.DEBUG) {
56
				Policy
57
						.getLog()
58
						.log(
59
								new Status(
60
										IStatus.WARNING,
61
										Policy.JFACE_DATABINDING,
62
										IStatus.OK,
63
										"Could not change value of " + source + "." + propertyDescriptor.getName(), e)); //$NON-NLS-1$ //$NON-NLS-2$
64
			}
65
		}
66
	}
67
68
	/**
69
	 * Returns the contents of the given property for the given bean.
70
	 * 
71
	 * @param source
72
	 *            the source bean
73
	 * @param propertyDescriptor
74
	 *            the property to retrieve
75
	 * @return the contents of the given property for the given bean.
76
	 */
77
	public static Object getProperty(Object source,
78
			PropertyDescriptor propertyDescriptor) {
79
		try {
80
			Method readMethod = propertyDescriptor.getReadMethod();
81
			if (readMethod == null) {
82
				throw new BindingException(propertyDescriptor.getName()
83
						+ " property does not have a read method."); //$NON-NLS-1$
84
			} 
85
			if (!readMethod.isAccessible()) {
86
				readMethod.setAccessible(true);
87
			}
88
			return readMethod.invoke(source, null);
89
		} catch (InvocationTargetException e) {
90
			/*
91
			 * InvocationTargetException wraps any exception thrown by the
92
			 * invoked method.
93
			 */
94
			throw new RuntimeException(e.getCause());
95
		} catch (Exception e) {
96
			if (BeansObservables.DEBUG) {
97
				Policy
98
						.getLog()
99
						.log(
100
								new Status(
101
										IStatus.WARNING,
102
										Policy.JFACE_DATABINDING,
103
										IStatus.OK,
104
										"Could not read value of " + source + "." + propertyDescriptor.getName(), e)); //$NON-NLS-1$ //$NON-NLS-2$
105
			}
106
			return null;
107
		}
108
	}
109
}

Return to bug 194734