|
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 |
} |