public static Binding bindJTextComponent(final Mutator mutator, final JTextComponent textField) { if (mutator.getSetter() != null) { textField .getDocument() .addDocumentListener( new DocumentListener() { public void removeUpdate(DocumentEvent e) { updateModel(); } public void insertUpdate(DocumentEvent e) { updateModel(); } public void changedUpdate(DocumentEvent e) { updateModel(); } private void updateModel() { try { mutator.set(textField.getText()); } catch (Exception ex) { Wiring.logger.error("exception in JTextField binding", ex); } } }); } Binding binding = null; if (mutator.getGetter() != null) { binding = new Binding() { public <T extends Enum<?> & ModelUpdate> void update(T... changed) { try { String string = (String) mutator.get(); if (string == null) { string = ""; } if (!string.equals(textField.getText())) { textField.setText(string); } } catch (Exception ex) { Wiring.logger.error("exception in JTextField binding", ex); } } }; mutator.getModel().bind(binding); } return binding; }
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); }