/
BeanUtils.java
53 lines (47 loc) · 2.01 KB
/
BeanUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package org.openpnp.util;
import java.beans.PropertyChangeListener;
import java.lang.reflect.Method;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.Converter;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
public class BeanUtils {
public static boolean addPropertyChangeListener(Object obj, String property, PropertyChangeListener listener) {
try {
Method method = obj.getClass().getMethod("addPropertyChangeListener", String.class, PropertyChangeListener.class);
method.invoke(obj, property, listener);
return true;
}
catch (Exception e) {
return false;
}
}
public static boolean addPropertyChangeListener(Object obj, PropertyChangeListener listener) {
try {
Method method = obj.getClass().getMethod("addPropertyChangeListener", PropertyChangeListener.class);
method.invoke(obj, listener);
return true;
}
catch (Exception e) {
return false;
}
}
public static AutoBinding bind(UpdateStrategy updateStrategy, Object source, String sourceProperty,
Object target, String targetProperty) {
AutoBinding binding = Bindings.createAutoBinding(updateStrategy, source,
BeanProperty.create(sourceProperty), target, BeanProperty.create(targetProperty));
binding.bind();
return binding;
}
public static AutoBinding bind(UpdateStrategy updateStrategy, Object source, String sourceProperty,
Object target, String targetProperty, Converter converter) {
AutoBinding binding = Bindings.createAutoBinding(updateStrategy, source,
BeanProperty.create(sourceProperty), target, BeanProperty.create(targetProperty));
if (converter != null) {
binding.setConverter(converter);
}
binding.bind();
return binding;
}
}