|
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 |
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.observable.Diffs; |
| 23 |
import org.eclipse.core.databinding.property.ValueProperty; |
| 24 |
import org.eclipse.core.databinding.util.Policy; |
| 25 |
import org.eclipse.core.internal.databinding.Util; |
| 26 |
import org.eclipse.core.runtime.IStatus; |
| 27 |
import org.eclipse.core.runtime.Status; |
| 28 |
|
| 29 |
/** |
| 30 |
* @since 3.3 |
| 31 |
* |
| 32 |
*/ |
| 33 |
public class BeanValueProperty extends ValueProperty { |
| 34 |
private PropertyDescriptor propertyDescriptor; |
| 35 |
private final boolean attachListener; |
| 36 |
|
| 37 |
private boolean updating; |
| 38 |
|
| 39 |
private ListenerSupport listenerSupport; |
| 40 |
|
| 41 |
/** |
| 42 |
* @param propertyDescriptor |
| 43 |
*/ |
| 44 |
public BeanValueProperty(PropertyDescriptor propertyDescriptor) { |
| 45 |
this(propertyDescriptor, true); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @param propertyDescriptor |
| 50 |
* @param attachListener |
| 51 |
*/ |
| 52 |
public BeanValueProperty(PropertyDescriptor propertyDescriptor, |
| 53 |
boolean attachListener) { |
| 54 |
this.propertyDescriptor = propertyDescriptor; |
| 55 |
this.attachListener = attachListener; |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
protected void addListenerTo(Object source) { |
| 60 |
if (attachListener) { |
| 61 |
initListenerSupport(); |
| 62 |
listenerSupport.hookListener(source); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
private void initListenerSupport() { |
| 67 |
if (listenerSupport == null) { |
| 68 |
synchronized (this) { |
| 69 |
if (listenerSupport != null) { |
| 70 |
PropertyChangeListener propertyChangeListener = new PropertyChangeListener() { |
| 71 |
public void propertyChange(PropertyChangeEvent evt) { |
| 72 |
if (!updating) { |
| 73 |
fireValueChange(evt.getSource(), Diffs |
| 74 |
.createValueDiff(evt.getOldValue(), evt |
| 75 |
.getNewValue())); |
| 76 |
} |
| 77 |
} |
| 78 |
}; |
| 79 |
listenerSupport = new ListenerSupport( |
| 80 |
propertyChangeListener, propertyDescriptor |
| 81 |
.getName()); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
protected void removeListenerFrom(Object source) { |
| 88 |
if (attachListener && listenerSupport != null) { |
| 89 |
listenerSupport.unhookListener(source); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public Object getValueType(Object source) { |
| 94 |
return propertyDescriptor.getPropertyType(); |
| 95 |
} |
| 96 |
|
| 97 |
public Object getValue(Object source) { |
| 98 |
try { |
| 99 |
Method readMethod = propertyDescriptor.getReadMethod(); |
| 100 |
if (readMethod == null) { |
| 101 |
throw new BindingException(propertyDescriptor.getName() |
| 102 |
+ " property does not have a read method."); //$NON-NLS-1$ |
| 103 |
} |
| 104 |
if (!readMethod.isAccessible()) { |
| 105 |
readMethod.setAccessible(true); |
| 106 |
} |
| 107 |
return readMethod.invoke(source, null); |
| 108 |
} catch (InvocationTargetException e) { |
| 109 |
/* |
| 110 |
* InvocationTargetException wraps any exception thrown by the |
| 111 |
* invoked method. |
| 112 |
*/ |
| 113 |
throw new RuntimeException(e.getCause()); |
| 114 |
} catch (Exception e) { |
| 115 |
if (BeansObservables.DEBUG) { |
| 116 |
Policy |
| 117 |
.getLog() |
| 118 |
.log( |
| 119 |
new Status( |
| 120 |
IStatus.WARNING, |
| 121 |
Policy.JFACE_DATABINDING, |
| 122 |
IStatus.OK, |
| 123 |
"Could not read value of " + source + "." + propertyDescriptor.getName(), e)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 124 |
} |
| 125 |
return null; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
public void setValue(Object source, Object value) { |
| 130 |
Object oldValue = getValue(source); |
| 131 |
|
| 132 |
if (Util.equals(oldValue, value)) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
updating = true; |
| 137 |
try { |
| 138 |
Method writeMethod = propertyDescriptor.getWriteMethod(); |
| 139 |
if (!writeMethod.isAccessible()) { |
| 140 |
writeMethod.setAccessible(true); |
| 141 |
} |
| 142 |
writeMethod.invoke(source, new Object[] { value }); |
| 143 |
} catch (InvocationTargetException e) { |
| 144 |
/* |
| 145 |
* InvocationTargetException wraps any exception thrown by the |
| 146 |
* invoked method. |
| 147 |
*/ |
| 148 |
throw new RuntimeException(e.getCause()); |
| 149 |
} catch (Exception e) { |
| 150 |
if (BeansObservables.DEBUG) { |
| 151 |
Policy |
| 152 |
.getLog() |
| 153 |
.log( |
| 154 |
new Status( |
| 155 |
IStatus.WARNING, |
| 156 |
Policy.JFACE_DATABINDING, |
| 157 |
IStatus.OK, |
| 158 |
"Could not change value of " + source + "." + propertyDescriptor.getName(), e)); //$NON-NLS-1$ //$NON-NLS-2$ |
| 159 |
} |
| 160 |
} finally { |
| 161 |
updating = false; |
| 162 |
} |
| 163 |
|
| 164 |
Object newValue = getValue(source); |
| 165 |
if (!Util.equals(oldValue, newValue)) { |
| 166 |
fireValueChange(source, Diffs.createValueDiff(oldValue, newValue)); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
public synchronized void dispose() { |
| 171 |
if (listenerSupport != null) { |
| 172 |
listenerSupport.dispose(); |
| 173 |
listenerSupport = null; |
| 174 |
} |
| 175 |
propertyDescriptor = null; |
| 176 |
super.dispose(); |
| 177 |
} |
| 178 |
} |