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,9 +41,8 @@ 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"; + private static final String NUMERIC_REGEX = "[0-9]+"; protected ValidatedTextInput inputField; @@ -55,25 +52,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)) { + AIFormatteddocument formattedDoc = new AIFormatteddocument(NUMERIC_REGEX); + textField = new AITextfield (formattedDoc, null, 0); + } } else { - textField = new AITextfield(); + textField = new AITextfield(NUMERIC_REGEX); } + SwingUtils.registerListeners(this, textField); return textField; } @@ -82,28 +72,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/AIFormatteddocument.java =================================================================== --- src/org/tp23/antinstaller/renderer/swing/AIFormatteddocument.java (revision 0) +++ src/org/tp23/antinstaller/renderer/swing/AIFormatteddocument.java (revision 0) @@ -0,0 +1,52 @@ +/** + * 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; +/** + * A JTextField with altered prefered size to facilitate fixing the width but still using a GridBagLayout + * + * @author Paul Hinds + * @version $Id: AITextfield.java 542 2007-06-06 08:22:42Z antonio $ + */ +public class AIFormatteddocument extends PlainDocument { + private String formatRegex = null; + private Pattern pattern = null; + + public AIFormatteddocument(String formatRegex) { + super(); + this.formatRegex = formatRegex; + this.pattern = Pattern.compile(formatRegex); + } + + public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { + if (str == null) { + return; + } + + Matcher m = pattern.matcher(str); + if (!m.matches()) { + return; + } + + super.insertString(offs, str, a); + } +}