TextProperty extends StringProperty implements DocumentListener, PropertyChangeListener { @NotNull @Override public String get() { if (myComponent instanceof JTextComponent) { return ((JTextComponent)myComponent).getText(); } else if (myComponent instanceof AbstractButton) { return ((AbstractButton)myComponent).getText(); } else if (myComponent instanceof JLabel) { return ((JLabel)myComponent).getText(); } else { throw new IllegalStateException("Unexpected text component type: " + myComponent.getClass().getSimpleName()); } } TextProperty(JTextComponent textComponent);  TextProperty(AbstractButton button);  TextProperty(JLabel label); @Override void insertUpdate(DocumentEvent documentEvent); @Override void removeUpdate(DocumentEvent documentEvent); @Override void changedUpdate(DocumentEvent documentEvent); @Override void propertyChange(PropertyChangeEvent propertyChangeEvent); @NotNull @Override String get();  }
@Test public void textPropertyCanWrapLabel() { JLabel label = new JLabel("New Label"); TextProperty textProperty = new TextProperty(label); CountListener listener = new CountListener(); textProperty.addListener(listener); assertThat(textProperty.get()).isEqualTo("New Label"); assertThat(listener.getCount()).isEqualTo(0); label.setText("Label text updated directly"); assertThat(textProperty.get()).isEqualTo("Label text updated directly"); assertThat(listener.getCount()).isEqualTo(1); textProperty.set("Label text updated via property"); assertThat(label.getText()).isEqualTo("Label text updated via property"); assertThat(listener.getCount()).isEqualTo(2); }
@Test public void textPropertyCanWrapButton() { JButton button = new JButton("New Button"); TextProperty textProperty = new TextProperty(button); CountListener listener = new CountListener(); textProperty.addListener(listener); assertThat(textProperty.get()).isEqualTo("New Button"); assertThat(listener.getCount()).isEqualTo(0); button.setText("Button text updated directly"); assertThat(textProperty.get()).isEqualTo("Button text updated directly"); assertThat(listener.getCount()).isEqualTo(1); textProperty.set("Button text updated via property"); assertThat(button.getText()).isEqualTo("Button text updated via property"); assertThat(listener.getCount()).isEqualTo(2); }
@Test public void textPropertyCanWrapTextField() { JTextField field = new JTextField("New Field"); TextProperty textProperty = new TextProperty(field); CountListener listener = new CountListener(); textProperty.addListener(listener); assertThat(textProperty.get()).isEqualTo("New Field"); assertThat(listener.getCount()).isEqualTo(0); field.setText("Field text updated directly"); assertThat(textProperty.get()).isEqualTo("Field text updated directly"); assertThat(listener.getCount()).isEqualTo(2); textProperty.set("Field text updated via property"); assertThat(field.getText()).isEqualTo("Field text updated via property"); assertThat(listener.getCount()).isEqualTo(3); }