I implemented missing parts and improved other parts of
JFormattedTextField.  The changes are accompanied by the following Mauve
test:

gnu.testlet.javax.swing.JFormattedTextField.JFormattedTextFieldTests    

More to come, as JFormattedTextField is still quite buggy.


2005-11-30  Anthony Balkissoon  <[EMAIL PROTECTED]>

        * javax/swing/JFormattedTextField.java:
        (AbstractFormatter.install): Try to set the text of the formatted text
        field using this formatter.
        (FormatterFactoryWrapper): Removed this unneeded class, this is handled
        by DefaultFormatterFactory now.
        (formatter): New field.
        (JFormattedTextField(Format)): Implemented.
        (JFormattedTextField(AbstractFormatter)): Instantiate a 
        DefaultFormatterFactory instead of a FormatterFactoryWrapper.
        (JFormattedTextField(AbstractFormatterFactory, Object)): Call setValue
        and setFormatterFactory instead of setting variables directly because
        more needs to be done than just setting the value of the variables.
        (JFormattedTextField(Object value)): Call setValue instead of setting 
        the variable directly.
        (getAppropriateFormatterFactory): New implementation method.
        (getFormatter): Don't use the formatter factory here, just return 
        the cached formatter.
        (processFocusEvent): Set the formatter to the appropriate one as 
        determined by the formatter factory.
        (setFormatter): Don't get the old formatter from the factory, just use
        the cached formatter.  Uninstall the old formatter. Install the new
        formatter. Cache the new formatter.  Don't create a new formatter
        factory.
        (setFormatterFactory): Set the formatter according to the new formatter
        factory.
        (setValue): Moved the setting of the value variable to above the call
        to createFormatter so that an appropriate formatter is created.  Cache
        the formatter that is created.
        (createFormatter): If argument is a Number, use a NumberFormatter.
        * javax/swing/text/DefaultFormatter.java:
        (valueToString): If argument is null return empty String.

--Tony
Index: javax/swing/JFormattedTextField.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JFormattedTextField.java,v
retrieving revision 1.17
diff -u -r1.17 JFormattedTextField.java
--- javax/swing/JFormattedTextField.java	5 Sep 2005 14:05:28 -0000	1.17
+++ javax/swing/JFormattedTextField.java	30 Nov 2005 22:43:13 -0000
@@ -40,15 +40,20 @@
 
 import java.awt.event.FocusEvent;
 import java.io.Serializable;
+import java.text.DateFormat;
 import java.text.Format;
+import java.text.NumberFormat;
 import java.text.ParseException;
 import java.util.Date;
 
 import javax.swing.text.DateFormatter;
 import javax.swing.text.DefaultFormatter;
+import javax.swing.text.DefaultFormatterFactory;
 import javax.swing.text.Document;
 import javax.swing.text.DocumentFilter;
+import javax.swing.text.InternationalFormatter;
 import javax.swing.text.NavigationFilter;
+import javax.swing.text.NumberFormatter;
 
 /**
  * A text field that makes use of a formatter to display and edit a specific
@@ -114,9 +119,21 @@
     public void install(JFormattedTextField textField)
     {
       if (this.textField != null)
-	uninstall();
+        uninstall();
       
       this.textField = textField;
+      
+      if (textField != null)
+        {
+          try
+          {
+            textField.setText(valueToString(textField.getValue()));
+          }
+          catch (ParseException pe)
+          {
+            // FIXME: Not sure what to do here.
+          }
+        }
     }
 
     public void uninstall ()
@@ -155,21 +172,6 @@
     public abstract AbstractFormatter getFormatter (JFormattedTextField tf);
   }
 
-  static class FormatterFactoryWrapper extends AbstractFormatterFactory
-  {
-    AbstractFormatter formatter;
-
-    public FormatterFactoryWrapper(AbstractFormatter formatter)
-    {
-      this.formatter = formatter;
-    }
-
-    public AbstractFormatter getFormatter(JFormattedTextField tf)
-    {
-      return formatter;
-    }
-  }
-
   public static final int COMMIT = 0;
   public static final int COMMIT_OR_REVERT = 1;
   public static final int REVERT = 2;
@@ -178,6 +180,7 @@
   private Object value;
   private int focusLostBehavior = COMMIT_OR_REVERT;
   private AbstractFormatterFactory formatterFactory;
+  private AbstractFormatter formatter;
   // Package-private to avoid an accessor method.
   boolean editValid = true;
   
@@ -188,12 +191,13 @@
 
   public JFormattedTextField (Format format)
   {
-    throw new InternalError ("not implemented");
+    this ();
+    setFormatterFactory(getAppropriateFormatterFactory(format));
   }
 
   public JFormattedTextField (AbstractFormatter formatter)
   {
-    this(new FormatterFactoryWrapper(formatter), null);
+    this(new DefaultFormatterFactory (formatter), null);
   }
 
   public JFormattedTextField (AbstractFormatterFactory factory)
@@ -203,13 +207,33 @@
 
   public JFormattedTextField (AbstractFormatterFactory factory, Object value)
   {
-    this.formatterFactory = factory;
-    this.value = value;
+    setValue(value);
+    setFormatterFactory(factory);    
   }
 
   public JFormattedTextField (Object value)
   {
-    this.value = value;
+    setValue(value);
+  }
+  
+  /**
+   * Returns an AbstractFormatterFactory that will give an appropriate
+   * AbstractFormatter for the given Format.
+   * @param format the Format to match with an AbstractFormatter.
+   * @return a DefaultFormatterFactory whose defaultFormatter is appropriate
+   * for the given Format.
+   */
+  private AbstractFormatterFactory getAppropriateFormatterFactory (Format format)
+  {
+    AbstractFormatter newFormatter;
+    if (format instanceof DateFormat)
+      newFormatter = new DateFormatter((DateFormat)format);
+    else if (format instanceof NumberFormat)
+      newFormatter = new NumberFormatter ((NumberFormat)format);
+    else
+      newFormatter = new InternationalFormatter(format);
+    
+    return new DefaultFormatterFactory(newFormatter);
   }
 
   public void commitEdit ()
@@ -231,10 +255,7 @@
 
   public AbstractFormatter getFormatter ()
   {
-    if (formatterFactory == null)
-      return null;
-    
-    return formatterFactory.getFormatter(this);
+    return formatter;
   }
 
   public AbstractFormatterFactory getFormatterFactory ()
@@ -264,10 +285,10 @@
 
   protected void processFocusEvent (FocusEvent evt)
   {
-    // it's safe to simply call super for now, until it gets clear
-    // what this method is supposed to do
-    // throw new InternalError ("not implemented");
     super.processFocusEvent(evt);
+    // Let the formatterFactory change the formatter for this text field
+    // based on whether or not it has focus.
+    setFormatter (formatterFactory.getFormatter(this));
   }
 
   public void setDocument(Document newDocument)
@@ -295,13 +316,19 @@
   {
     AbstractFormatter oldFormatter = null;
     
-    if (formatterFactory != null)
-      oldFormatter = formatterFactory.getFormatter(this);
+    oldFormatter = this.formatter;
 
     if (oldFormatter == formatter)
       return;
+    
+    if (oldFormatter != null)
+      oldFormatter.uninstall();
+    
+    this.formatter = formatter;
+    
+    if (formatter != null)
+      formatter.install(this);
 
-    setFormatterFactory(new FormatterFactoryWrapper(formatter));
     firePropertyChange("formatter", oldFormatter, formatter);
   }
 
@@ -313,6 +340,7 @@
     AbstractFormatterFactory oldFactory = formatterFactory;
     formatterFactory = factory;
     firePropertyChange("formatterFactory", oldFactory, factory);
+    setFormatter(formatterFactory.getFormatter(this));
   }
 
   public void setValue (Object newValue)
@@ -320,8 +348,11 @@
     if (value == newValue)
       return;
 
+    Object oldValue = value;
+    value = newValue;
+    
     // format value
-    AbstractFormatter formatter = createFormatter(newValue);
+    formatter = createFormatter(newValue);
     try
       {
         setText(formatter.valueToString(newValue));
@@ -330,9 +361,6 @@
       {
         // TODO: what should we do with this?
       }
-
-    Object oldValue = value;
-    value = newValue;
     firePropertyChange("value", oldValue, newValue);
   }
 
@@ -352,16 +380,18 @@
   AbstractFormatter createFormatter(Object value)
   {
     AbstractFormatter formatter = null;
-    if (formatterFactory != null
-        && formatterFactory.getFormatter(this) != null)
-     formatter = formatterFactory.getFormatter(this);
-   else
-     {
-       if (value instanceof Date)
-         formatter = new DateFormatter();
-       else
-         formatter = new DefaultFormatter();
-     }
+    if (formatterFactory != null && formatterFactory.getFormatter(this) != null)
+      formatter = formatterFactory.getFormatter(this);
+    else
+      {
+        if (value instanceof Date)
+          formatter = new DateFormatter();
+        else if (value instanceof Number)
+          formatter = new NumberFormatter();
+        else
+          formatter = new DefaultFormatter();
+        formatterFactory = new DefaultFormatterFactory (formatter);
+      }
     return formatter;
   }
 }
Index: javax/swing/text/DefaultFormatter.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/DefaultFormatter.java,v
retrieving revision 1.5
diff -u -r1.5 DefaultFormatter.java
--- javax/swing/text/DefaultFormatter.java	30 Oct 2005 18:02:53 -0000	1.5
+++ javax/swing/text/DefaultFormatter.java	30 Nov 2005 22:43:13 -0000
@@ -400,6 +400,8 @@
   public String valueToString(Object value)
     throws ParseException
   {
+    if (value == null)
+      return "";
     return value.toString();
   }
 
_______________________________________________
Classpath-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to