All pastes #584087 Raw Edit

Something

public text v1 · immutable
·published 2007-06-22 16:30 UTC
rendered paste body
Index: src/org/tp23/antinstaller/renderer/swing/ValidatedTextInputRenderer.java
===================================================================
--- src/org/tp23/antinstaller/renderer/swing/ValidatedTextInputRenderer.java	(revision 643)
+++ src/org/tp23/antinstaller/renderer/swing/ValidatedTextInputRenderer.java	(working copy)
@@ -21,10 +21,8 @@ import java.awt.event.KeyAdapter;
 import java.awt.event.KeyEvent;
 import java.text.DecimalFormat;
 
-import javax.swing.JFormattedTextField;
 import javax.swing.JPanel;
 import javax.swing.JTextField;
-import javax.swing.JFormattedTextField.AbstractFormatter;
 import javax.swing.text.DateFormatter;
 import javax.swing.text.DefaultFormatter;
 import javax.swing.text.NumberFormatter;
@@ -43,8 +41,6 @@ public class ValidatedTextInputRenderer 
 
     private final static VersionHelper VERSION_HELPER = new VersionHelper();
 
-    private static final String DATE_FORMAT = "date";
-
     private static final String NUMERIC_FORMAT = "numeric";
 
     protected ValidatedTextInput inputField;
@@ -55,25 +51,18 @@ public class ValidatedTextInputRenderer 
 
     protected JTextField createTextField() {
         JTextField textField = null;
+
         String format = inputField.getFormat();
         if(isFormattable(format)) {
-            final Dimension prefSize = new Dimension(SizeConstants.FIELD_WIDTH, SizeConstants.FIELD_HEIGHT);
-            textField = new JFormattedTextField(createFormatter(format)) {
-                public Dimension getMinimumSize() {
-                    return prefSize;
-                }
-                public Dimension getPreferredSize() {
-                    return prefSize;
-                }
-                public Dimension getMaximumSize() {
-                    return prefSize;
-                }
-            };
-            textField.setColumns(5);
+            if(NUMERIC_FORMAT.equals(format)) {
+                AINumericdocument doc = new AINumericdocument();
+                textField = new AITextfield (doc, null, 0);
+            }
         }
         else {
-            textField = new AITextfield();
+                textField = new AITextfield();
         }
+
         SwingUtils.registerListeners(this, textField);
         return textField;
     }
@@ -82,28 +71,6 @@ public class ValidatedTextInputRenderer 
         return VERSION_HELPER.equalOrHigher(System.getProperty("java.version"), "1.5") && !SwingUtils.isVoid(format);
     }
 
-    private AbstractFormatter createFormatter(String format) {
-        DefaultFormatter formatter = null;
-        if(NUMERIC_FORMAT.equals(format)) {
-            DecimalFormat numberFormat = new DecimalFormat("#####");
-            numberFormat.setGroupingUsed(false);
-            NumberFormatter nf = new NumberFormatter(numberFormat);
-            nf.setMinimum(new Integer(0));
-            nf.setMaximum(new Integer(65534));
-            nf.setValueClass(Integer.class);
-            formatter = nf;
-        }
-        else if(DATE_FORMAT.equals(format)) {
-            formatter = new DateFormatter();
-        }
-        else {
-            formatter = new DefaultFormatter();
-        }
-//        formatter.setAllowsInvalid(false);
-        formatter.setCommitsOnValidEdit(true);
-        return formatter;
-    }
-
     public void initComponent(JPanel parent) {
         jTextField = createTextField();
         try {
Index: src/org/tp23/antinstaller/renderer/swing/AINumericdocument.java
===================================================================
--- src/org/tp23/antinstaller/renderer/swing/AINumericdocument.java	(revision 0)
+++ src/org/tp23/antinstaller/renderer/swing/AINumericdocument.java	(revision 0)
@@ -0,0 +1,53 @@
+/**
+ * Copyright 2005 Paul Hinds
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.tp23.antinstaller.renderer.swing;
+
+import java.util.regex.*;
+
+import javax.swing.text.AttributeSet;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.Document;
+import javax.swing.text.PlainDocument;
+
+public class AINumericdocument extends PlainDocument {
+    private String formatRegex = null;
+    private Pattern pattern = null;
+
+    private boolean onlyDigits(String str) {		
+    	if (str == null) {
+    		return true;
+    	}
+        int sz = str.length();
+        for (int i = 0; i < sz; i++) {
+            if (!Character.isDigit(str.charAt(i))) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
+        if (str == null) {
+            return;
+        }
+
+        if (!onlyDigits(str)) {
+            return;
+        }
+
+        super.insertString(offs, str, a);
+    }
+}