JavaBeanPropertyAdapter Reloaded
Michael Heinrichs
michael.heinrichs at oracle.com
Mon Jan 2 06:32:01 PST 2012
Hi everybody,
I took the feedback for the first version of the Java Bean adapter and created a new design with the feedback incorporated. The general idea is to take whatever is available and use it. For example, if the Java Bean property supports change listeners, this will be used to synchronize changes from the Java Bean property to the JavaFX property. If there is no support for change listeners, you can still create an adapter, but you have to do the synchronization yourself or disallow changing the Java Bean property.
I put everything in separated classes now and added a new package that contains the new classes and interfaces: javafx.beans.property.adapter.
There are two new interfaces that define the common functionality of all JavaBeanProperty / ReadOnlyJavaBeanProperty instances:
public interface ReadOnlyJavaBeanProperty<T> extends ReadOnlyProperty<T> {
void fireValueChangedEvent();
void dispose();
}
public interface JavaBeanProperty<T> extends ReadOnlyJavaBeanProperty<T>, Property<T> {}
The method fireValueChangedEvent() can be called to notify the adapter that the Java Bean property changed in case it does not support change listeners. The implementation uses WeakReferences and some automatic cleanup, if there are no references to the property left. But if you want more control, you can call dispose() on an adapter to remove all hooks to the bean.
Then we have a bunch of implementations for all types of Java Bean adapters, e.g.:
public final class ReadOnlyJavaBeanBooleanProperty extends ReadOnlyBooleanProperty implements ReadOnlyJavaBeanProperty<Boolean> {...}
public final class ReadOnlyJavaBeanObjectProperty<T> extends ReadOnlyObjectProperty<T> implements ReadOnlyJavaBeanProperty<T> {...}
public final class JavaBeanBooleanProperty extends BooleanProperty implements JavaBeanProperty<Boolean> {...}
public final class JavaBeanObjectProperty<T> extends ObjectProperty<T> implements JavaBeanProperty<T> {...}
And finally the really interesting part, the builders. There are two possibilities to use them. If you need to create an adapter for the same property of a collection of beans, you should first create the builder and then call createXYZProperty() for each bean. If you need to create just one adapter, there are static methods to do everything with a single call.
public final class ReadOnlyJavaBeanPropertyBuilder {
public static ReadOnlyJavaBeanBooleanProperty createBooleanProperty(Object bean, String propertyName) throws NoSuchMethodException {...}
public static <T> ReadOnlyJavaBeanObjectProperty<T> createObjectProperty(Object bean, String propertyName) throws NoSuchMethodException {...}
// same for all other types
public ReadOnlyJavaBeanPropertyBuilder(String propertyName, Class<?> beanClass) throws NoSuchMethodException {...}
public ReadOnlyJavaBeanPropertyBuilder(String propertyName, Class<?> beanClass, String getterName) throws NoSuchMethodException {...}
public ReadOnlyJavaBeanPropertyBuilder(String propertyName, Class<?> beanClass, Method getter) {...}
public ReadOnlyJavaBeanBooleanProperty createBooleanProperty(Object bean) {...}
public <T> ReadOnlyJavaBeanObjectProperty<T> createObjectProperty(Object bean) {...}
// same for all other types
}
public final class JavaBeanPropertyBuilder {
public static JavaBeanBooleanProperty createBooleanProperty(Object bean, String propertyName) throws NoSuchMethodException {...}
public static <T> JavaBeanObjectProperty<T> createObjectProperty(Object bean, String propertyName) throws NoSuchMethodException {...}
// same for all other types
public JavaBeanPropertyBuilder(String propertyName, Class<?> beanClass) throws NoSuchMethodException {...}
public JavaBeanPropertyBuilder(String propertyName, Class<?> beanClass, String getterName, String setterName) throws NoSuchMethodException {...}
public JavaBeanPropertyBuilder(String propertyName, Class<?> beanClass, Method getter, Method setter) {...}
public JavaBeanBooleanProperty createBooleanProperty(Object bean) {...}
public <T> JavaBeanObjectProperty<T> createObjectProperty(Object bean) {...}
// same for all other types
}
What do you think?
Thanks,
Michael
More information about the openjfx-dev
mailing list