Created
July 10, 2013 11:47
-
-
Save stijnvanbael/5965627 to your computer and use it in GitHub Desktop.
Convenience builder for creating bindings with Java Beansbinding.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jdesktop.beansbinding.AutoBinding; | |
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy; | |
import org.jdesktop.beansbinding.BeanProperty; | |
import org.jdesktop.beansbinding.Bindings; | |
import org.jdesktop.beansbinding.Converter; | |
public class BindingBuilder<S, SV, T, TV> implements PartialBindingBuilder<S, SV> { | |
private final S source; | |
private final String sourceProperty; | |
private AutoBinding<S, SV, T, TV> binding; | |
private T target; | |
private String targetProperty; | |
private BindingBuilder(S source, String sourceProperty) { | |
this.source = source; | |
this.sourceProperty = sourceProperty; | |
} | |
private BindingBuilder(S source, String sourceProperty, T target, String targetProperty) { | |
this(source, sourceProperty); | |
this.target = target; | |
this.targetProperty = targetProperty; | |
this.binding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, source, | |
BeanProperty.<S, SV> create(sourceProperty), target, | |
BeanProperty.<T, TV> create(targetProperty)); | |
bind(); | |
} | |
public static <S> PartialBindingBuilder<S, ?> bind(S source, String sourceProperty) { | |
return new BindingBuilder<S, Object, Object, Object>(source, sourceProperty); | |
} | |
@Override | |
public <T2> BindingBuilder<S, SV, T2, ?> to(T2 target, String targetProperty) { | |
return new BindingBuilder<S, SV, T2, Object>(source, sourceProperty, target, targetProperty); | |
} | |
@Override | |
public <T2> BindingBuilder<S, SV, T2, ?> to(T2 target) { | |
return new BindingBuilder<S, SV, T2, Object>(source, sourceProperty, target, sourceProperty); | |
} | |
public BindingBuilder<S, ?, T, ?> as(UpdateStrategy updateStrategy) { | |
unbind(); | |
binding = Bindings.createAutoBinding(updateStrategy, source, BeanProperty.<S, SV> create(sourceProperty), | |
target, BeanProperty.<T, TV> create(targetProperty)); | |
bind(); | |
return this; | |
} | |
@SuppressWarnings("unchecked") | |
public <SV2, TV2> BindingBuilder<S, SV2, T, TV2> using(Converter<SV2, TV2> converter) { | |
unbind(); | |
binding.setConverter((Converter<SV, TV>) converter); | |
binding.bind(); | |
return (BindingBuilder<S, SV2, T, TV2>) this; | |
} | |
private void bind() { | |
try { | |
binding.bind(); | |
} catch (ClassCastException e) { | |
// properties not compatible, converter is required | |
} | |
} | |
public void unbind() { | |
if (binding.isBound()) { | |
binding.unbind(); | |
} | |
} | |
public BindingBuilder<S, SV, T, TV> verify() { | |
if (!binding.isBound()) { | |
Object sourceValue = binding.getSourceProperty().getValue(binding.getSourceObject()); | |
Object targetValue = binding.getTargetProperty().getValue(binding.getTargetObject()); | |
throw new IllegalStateException("Source property type <" + sourceValue.getClass() | |
+ "> is not compatible with target property type <" + targetValue.getClass() | |
+ ">. Please call using(Converter) with an appropriate Converter."); | |
} | |
return this; | |
} | |
public void save() { | |
binding.save(); | |
} | |
} | |
public interface PartialBindingBuilder<S, SV> { | |
<T> BindingBuilder<S, SV, T, ?> to(T target, String targetProperty); | |
<T> BindingBuilder<S, SV, T, ?> to(T target); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment