[Schmitzm-commits] r1685 - trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sun Aug 21 12:28:37 CEST 2011


Author: mojays
Date: 2011-08-21 12:28:37 +0200 (Sun, 21 Aug 2011)
New Revision: 1685

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/TextComponentChangeListener.java
Log:
new TextComponentChangeListener (combining FocusListener and PropertyChangeListener) to determine value change for "normal" JTextFields as well as for JFormattedTextFields.

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/TextComponentChangeListener.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/TextComponentChangeListener.java	2011-08-21 09:09:44 UTC (rev 1684)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/TextComponentChangeListener.java	2011-08-21 10:28:37 UTC (rev 1685)
@@ -0,0 +1,96 @@
+package de.schmitzm.swing.event;
+
+import java.awt.Component;
+import java.awt.event.FocusAdapter;
+import java.awt.event.FocusEvent;
+import java.awt.event.FocusListener;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+
+import javax.swing.JFormattedTextField;
+import javax.swing.text.JTextComponent;
+
+/**
+ * Listener to react on value changes of {@link JTextComponent}.
+ * This is done either by {@link PropertyChangeListener} for
+ * {@link JFormattedTextField} or {@link FocusListener} for other
+ * {@link JTextComponent}.<br>
+ * To force the use of {@link TextComponentChangeListener#connectTo(JTextComponent)}
+ * and avoid adding this listener manually as {@link FocusListener}
+ * or {@link PropertyChangeListener}, these interfaces are not implemented
+ * in this class directly, but in the sub-class  {@link ChangeListener}.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public abstract class TextComponentChangeListener {
+  /**
+   * Called when the text component value changes.
+   * @param source {@link JTextComponent} which has changed
+   * @param oldValue old value
+   * @param newValue new value
+   */
+  public abstract void textChanged(Object source, Object oldValue, Object newValue);
+  
+  /**
+   * Connects this listener to a {@link JTextComponent}. If the component is
+   * a {@link JFormattedTextField} the listener is connected as
+   * {@link PropertyChangeListener}. Otherwise the listener is
+   * connected
+   */
+  public void connectTo(JTextComponent comp) {
+    ChangeListener l = new ChangeListener();
+    // For JFormattedTextField the FocusListener can not be used
+    // because the new value is set, AFTER the FocusListeners are
+    // called (see processFocusEvent(FocusEvent))
+    // --> use the PropertyChangeListener to determine the value
+    //     change
+    if ( comp instanceof JFormattedTextField ) {
+      comp.addPropertyChangeListener("value", l);
+      return;
+    }
+    comp.addFocusListener(l);
+  }
+
+  /**
+   * Listener to encapsulate the PropertyChangeListener and
+   * FocusListener.
+   * These listeners are not implemented in TextComponentChangeListener
+   * directly to force the use of {@link TextComponentChangeListener#connectTo(JTextComponent)}
+   * and avoid adding the TextComponentChangeListener manually as {@link FocusListener}
+   * or {@link PropertyChangeListener}.
+   * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+   */
+  protected class ChangeListener extends FocusAdapter implements FocusListener, PropertyChangeListener {
+    /** Holds the old {@link JTextComponent} value (on focus gain). */
+    private String oldText = null;
+    
+    /**
+     * Performs the text value change for {@link JFormattedTextField}.
+     */
+    public void propertyChange(PropertyChangeEvent evt) {
+      if ( !"value".equals(evt.getPropertyName()) )
+        return;
+      textChanged(evt.getSource(),evt.getOldValue(),evt.getNewValue());
+    }
+
+    /**
+     * Stores the current text field value.
+     */
+    @Override
+    public void focusGained(FocusEvent evt) {
+      oldText = ((JTextComponent)evt.getSource()).getText();
+    }
+
+    /**
+     * Performs the text value change for other text fields than
+     * {@link JFormattedTextField}.
+     */
+    @Override
+    public void focusLost(FocusEvent evt) {
+      String newText = ((JTextComponent)evt.getSource()).getText();
+      if ( newText == null && oldText != null ||
+           !newText.equals(oldText) )
+      textChanged(evt.getSource(),oldText,newText);
+    }
+    
+  }
+}



More information about the Schmitzm-commits mailing list