classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] FYI: Update for DefaultFormatter


From: Roman Kennke
Subject: [cp-patches] FYI: Update for DefaultFormatter
Date: Wed, 08 Jun 2005 16:43:19 +0200
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050317)

I finished the DefaultFormatter in javax.swing.text. Now it supports the properties overwriteMode (overwrite vs insert mode for JFormattedTextFields) as well as allowsInvalid (whether or not invalid values in the input field are temporarily allowed).

2005-06-08  Roman Kennke  <address@hidden>

   * javax/swing/text/DefaultFormatter.java
   (FormatterDocumentFilter.remove): Added check for valid input.
   (FormatterDocumentFilter.insertString): Added check for valid input.
   Added support for overwriteMode property.
   (FormatterDocumentFilter.replace): Added check for valid input.
   (checkValidInput): New helper method to check for valid input
   and roll it back if necessary.

/Roman

Index: javax/swing/text/DefaultFormatter.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/DefaultFormatter.java,v
retrieving revision 1.1
diff -u -r1.1 DefaultFormatter.java
--- javax/swing/text/DefaultFormatter.java      6 Jun 2005 15:19:52 -0000       
1.1
+++ javax/swing/text/DefaultFormatter.java      8 Jun 2005 14:39:24 -0000
@@ -87,6 +87,7 @@
       throws BadLocationException
     {
       super.remove(bypass, offset, length);
+      checkValidInput();
       commitIfAllowed();
     }

@@ -105,7 +106,11 @@
                               String text, AttributeSet attributes)
       throws BadLocationException
     {
-      super.insertString(bypass, offset, text, attributes);
+      if (overwriteMode == true)
+        replace(bypass, offset, text.length(), text, attributes);
+      else
+        super.insertString(bypass, offset, text, attributes);
+      checkValidInput();
       commitIfAllowed();
     }

@@ -126,6 +131,7 @@
       throws BadLocationException
     {
       super.replace(bypass, offset, length, text, attributes);
+      checkValidInput();
       commitIfAllowed();
     }

@@ -145,6 +151,39 @@
             // ignore invalid edits
           }
     }
+
+    /**
+     * Checks if the value in the input field is valid. If the
+     * property allowsInvalid is set to <code>false</code>, then
+     * the string in the input field is not allowed to be entered.
+     *
+     * @param doc the document of the input field
+     * @param value the current (old) value of the input field
+     */
+    private void checkValidInput()
+    {
+      JFormattedTextField ftf = getFormattedTextField();
+      try
+        {
+          Object newval = stringToValue(ftf.getText());
+        }
+      catch (ParseException ex)
+        {
+          if (!allowsInvalid)
+            {
+              // roll back the input if invalid edits are not allowed
+              try
+                {
+                  ftf.setText(valueToString(ftf.getValue()));
+                }
+              catch (ParseException pe)
+                {
+                  // if that happens, something serious must be wrong
+                  throw new AssertionError("values must be parseable");
+                }
+            }
+        }
+    }
   }

   /** The serialVersoinUID. */

reply via email to

[Prev in Thread] Current Thread [Next in Thread]