//
// $Id: IntField.java,v 1.3 2003/11/25 16:47:11 mdb Exp $

package com.samskivert.swing;

import javax.swing.JTextField;

import com.samskivert.Log;
import com.samskivert.swing.util.SwingUtil.DocumentValidator;
import com.samskivert.swing.util.SwingUtil;

/**
 * A text field that will only take digit entries within the optional
 * specified range.
 */
public class IntField extends JTextField
    implements SwingUtil.DocumentValidator
{
    public IntField ()
    {
        this(0, 0, Integer.MAX_VALUE);
    }

    public IntField (int initial, int minValue, int maxValue)
    {
        setValue(initial);

        _minValue = minValue;
        _maxValue = maxValue;

        setHorizontalAlignment(JTextField.RIGHT);
        setColumns(5);

        // register ourselves as the validator
        SwingUtil.setDocumentHelpers(this, this, null);
    }

    /**
     * Return the int that is represented by this field.
     */
    public int getValue ()
    {
        try {
            return Integer.parseInt(getText());
        } catch (NumberFormatException nfe) {
            Log.warning("This should never, ever, happen");
        }

        return -1;
    }

    /**
     * Set the text to the value specified.
     */
    public void setValue (int value)
    {
        setText(Integer.toString(value));
    }
    
    /**
     * Change the current min value.
     */
    public void setMinValue (int minValue)
    {
        _minValue = minValue;
        setText(getText()); // jiggle the text
    }

    /**
     * Change the current max value.
     */
    public void setMaxValue (int maxValue)
    {
        _maxValue = maxValue;
        setText(getText()); // jiggle the text
    }

    // from interface SwingUtil.DocumentValidator
    public boolean isValid (String text)
    {
        if ("".equals(text)) {
            return true;
        }

        try {
            int value = Integer.parseInt(text);

            if (value > _maxValue) {
                setText("" + _maxValue);
            }

            return ((value >= _minValue) && (value <= _maxValue));
        } catch (NumberFormatException nfe) {
            return false;
        }
    }

    protected int _minValue;
    protected int _maxValue;
}

@@ -22,17 +22,17 @@
 ava,v 1.
-3
+4
  2003/11
@@ -40,19 +40,20 @@
 25 1
-6:47:11 mdb
+7:54:50 eric
  Exp
@@ -208,16 +208,80 @@
 idator;%0A
+import com.samskivert.swing.util.SwingUtil.DocumentTransformer;%0A
 import c
@@ -498,16 +498,47 @@
 alidator
+, SwingUtil.DocumentTransformer
 %0A%7B%0A    p
@@ -1414,13 +1414,9 @@
   %7D%0A
-    
 %0A
+
     
@@ -1779,24 +1779,247 @@
 text%0A    %7D%0A%0A
+    // from interface SwingUtil.DocumentTransformer%0A    public String transform (String s)%0A    %7B%0A        int val = Integer.parseInt(s);%0A        return Integer.toString(Math.min(_maxValue, Math.max(_minValue, val)));%0A    %7D%0A%0A
     // from 
@@ -2176,32 +2176,32 @@
 %0A%0A        try %7B%0A
+
             int 
@@ -2237,101 +2237,8 @@
 );%0A%0A
-            if (value %3E _maxValue) %7B%0A                setText(%22%22 + _maxValue);%0A            %7D%0A%0A
     

Document Transform it up.