class CommentsView extends BaseView<CommentableModel> { @Bound(to = "comment") private JLabel countLabel = new JLabel(); public CommentsView(CommentableModel model) { super(model); panel.add(countLabel, BorderLayout.EAST); bindings.bind(this); } }
public Collection<Binding> wire(Bound bound, BindingContext context, Field field) throws IllegalAccessException, IntrospectionException { String target = bound.to(); JComboBox combo = context.getFieldObject(field, JComboBox.class); Mutator mutator = Mutator.create(context, target); final String nullValue = (String)Utilities.getNullValue(context, bound.nullValue()); return ImmutableList.of(bindJComboBox(bound, mutator, combo, nullValue)); }
public Collection<Binding> wire(Bound bound, BindingContext context, Field field) throws IllegalAccessException, IntrospectionException { String target = bound.to(); Mutator mutator = Mutator.create(context, target); JToggleButton toggle = context.getFieldObject(field, JToggleButton.class); Class<?>[] paramTypes = mutator.getSetter().getMethod().getParameterTypes(); if (paramTypes.length == 1 && paramTypes[0].isEnum()) { Class<?> enumType = paramTypes[0]; String value = bound.value(); return ImmutableList.of(bindJToggleButtonToEnum(value, enumType, mutator, toggle)); } else if (paramTypes.length == 1 && paramTypes[0] == boolean.class) { String value = bound.value(); if (Strings.isNullOrEmpty(value)) { return ImmutableList.of(AbstractButtonWiringHarness.bindAbstractButton(mutator, toggle)); } else { return ImmutableList.of(bindJToggleButtonToBoolean(bound.value(), mutator, toggle)); } } else { throw new BindingException("can only bind JToggleButtons to enums or booleans"); //$NON-NLS-1$ } }
public Collection<Binding> wire(Bound bound, BindingContext context, Field field) throws IllegalAccessException, IntrospectionException { JTextComponent textComponent = context.getFieldObject(field, JTextComponent.class); Mutator mutator = Mutator.create(context, bound.to()); Binding binding = bindJTextComponent(mutator, textComponent); if (binding == null) { return ImmutableList.of(); } return ImmutableList.of(binding); }
private Binding bindJList(final Bound bound, final Mutator mutator, final JList list) { final List<Object> ons = BindingContext.getOnObjects(bound.on(), mutator.getModel()); Binding binding = new Binding() { public <T extends Enum<?> & ModelUpdate> void update(T... changed) { if (!BindingContext.isOn(ons, changed)) { return; } try { updateListModel(list, (List<?>)mutator.get()); } catch (Exception ex) { Wiring.logger.error("exception in JList binding", ex); } } }; mutator.getModel().bind(binding); return binding; }
public Collection<Binding> wire(Bound bound, BindingContext context, Field field) throws IllegalAccessException, IntrospectionException { Mutator mutator = Mutator.create(context, bound.to()); AbstractButton abstractButton = context.getFieldObject(field, AbstractButton.class); return ImmutableList.of(bindAbstractButton(mutator, abstractButton)); }
private Binding bindJComboBox(final Bound bound, final Mutator mutator, final JComboBox combo, final String nullValue) { final List<Object> ons = BindingContext.getOnObjects(bound.on(), mutator.getModel()); Binding binding = new Binding() { public <T extends Enum<?> & ModelUpdate> void update(T... changed) { if (!BindingContext.isOn(ons, changed)) { return; } try { updateComboModel(combo, (List<?>)mutator.get(), nullValue); } catch (Exception ex) { Wiring.logger.error("exception in JList binding", ex); } } }; mutator.getModel().bind(binding); return binding; }
class BaseView<T extends BaseModel> { protected final T model; protected Bindings bindings = new Bindings(); protected JPanel panel = new JPanel(new BorderLayout()); // "model." is required to prevent subclasses from clobbering @Bound(to = "model.displayText") private final JLabel label = new JLabel(); public BaseView(T model) { this.model = model; panel.add(label, BorderLayout.CENTER); } }
public Collection<Binding> wire(Bound bound, BindingContext context, Field field) throws IllegalAccessException, IntrospectionException { String target = bound.to(); Mutator mutator = Mutator.create(context, target); JSlider slider = context.getFieldObject(field, JSlider.class); return ImmutableList.of(bindJSlider(mutator, slider)); }
public class CommentsView2 extends BaseView<CommentableModel> { private final CommentableModel commentableModel; @Bound(to="comment") private JLabel countLabel = new JLabel(); private Bindings bindings = new Bindings(); public CommentsView2(CommentableModel model) { super(model); this.commentableModel = model; panel.add(countLabel, BorderLayout.EAST); bindings.bind(this); } }
public Collection<Binding> wire(Bound bound, BindingContext context, Field field) throws IllegalAccessException, IntrospectionException { JList list = context.getFieldObject(field, JList.class); Mutator mutator = Mutator.create(context, bound.to()); return ImmutableList.of(bindJList(bound, mutator, list)); }
public class CantFindGetterSetterTest extends TestCase { final NegativeModel model = new NegativeModel(); @Bound(to = "cantFind") private final JRadioButton button = new JRadioButton("button"); final Bindings bindings = new Bindings(); public void testFailure() { try { bindings.bind(this); fail("should not allow binding if can't find getter or setter"); } catch (Exception e) { System.err.println(e.getMessage()); } } }
public Collection<Binding> wire(Bound bound, BindingContext context, Field field) throws IllegalAccessException, IntrospectionException { JLabel label = context.getFieldObject(field, JLabel.class); Mutator mutator = Mutator.create(context, bound.to()); return ImmutableList.of(bindJLabel(mutator, label)); }
public class WrongTypeTest extends TestCase { final NegativeModel model = new NegativeModel(); @Bound(to = "badEnum") private final JRadioButton button = new JRadioButton("button"); final Bindings bindings = new Bindings(); public void testFailure() { try { bindings.bind(this); fail("should not allow binding to wrong type"); } catch (Exception e) { System.err.println(e.getMessage()); } } }
public Collection<Binding> wire(Bound bound, BindingContext context, Field field) throws IllegalAccessException, IntrospectionException { String target = bound.to(); JProgressBar bar = context.getFieldObject(field, JProgressBar.class); // ObjectFieldMethod setter = context.findSetter(target); ObjectFieldMethod getter = context.findGetter(target); if (getter == null) { throw new IllegalArgumentException("could not find getter/setter for " + target); } BindableModel model = context.getFieldObject(getter.getField(), BindableModel.class); // verify type parameters return bindJProgressBar(model, bar, getter.getMethod()); }
private static class BooleanComponent { final BooleanModel model; @Bound(to = "state") final JCheckBox box = new JCheckBox("State"); final Bindings bindings = new Bindings(); public BooleanComponent(BooleanModel model) { this.model = model; this.bindings.bind(this); } public JComponent getDisplayComponent() { return box; } public void dispose() { bindings.release(model); } }
public Collection<Binding> wire(Bound bound, BindingContext context, Field field) throws IllegalAccessException, IntrospectionException { String target = bound.to(); JPasswordField pwdField = context.getFieldObject(field, JPasswordField.class); ObjectFieldMethod setter = context.findSetter(target); ObjectFieldMethod getter = context.findGetter(target); if (setter == null || getter == null) { throw new IllegalArgumentException("could not find getter/setter for " + target); } BindableModel model1 = context.getFieldObject(setter.getField(), BindableModel.class); BindableModel model2 = context.getFieldObject(getter.getField(), BindableModel.class); Preconditions.checkArgument(model1 == model2, "setter not bound to same field as getter"); // verify type parameters return bindJPasswordField(model1, pwdField, getter.getMethod(), setter.getMethod()); }
public class BoundJCheckBoxMenuItemTest extends TestCase { final BooleanModel model = new BooleanModel(); @Bound(to = "state") final JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(); final Bindings bindings = Bindings.standard(); @Override protected void setUp() throws Exception { bindings.bind(this); } public void testSimple() { assertFalse(model.isState()); assertFalse(menuItem.isSelected()); model.setState(true); assertTrue(menuItem.isSelected()); assertTrue(model.isState()); menuItem.doClick(); assertFalse(model.isState()); assertFalse(menuItem.isSelected()); } }
public class BoundJCheckBoxTest extends TestCase { final BooleanModel model = new BooleanModel(); @Bound(to = "state") final JCheckBox box = new JCheckBox(); final Bindings bindings = Bindings.standard(); @Override protected void setUp() throws Exception { bindings.bind(this); } public void testSimple() { assertFalse(model.isState()); assertFalse(box.isSelected()); model.setState(true); assertTrue(box.isSelected()); assertTrue(model.isState()); box.doClick(); assertFalse(model.isState()); assertFalse(box.isSelected()); } }
@Bound(to = "value") private final JSlider slider = new JSlider();