Format our values with a NumberFormat (gives us commas).

Don't transform the text when focus is lost unless it needs to change.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@1444 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray
2004-06-08 21:15:18 +00:00
parent c4c240020d
commit 4a203b0720
@@ -1,10 +1,14 @@
// //
// $Id: IntField.java,v 1.7 2004/05/27 22:57:47 ray Exp $ // $Id: IntField.java,v 1.8 2004/06/08 21:15:18 ray Exp $
package com.samskivert.swing; package com.samskivert.swing;
import java.awt.event.FocusEvent; import java.awt.event.FocusEvent;
import java.awt.event.FocusListener; import java.awt.event.FocusListener;
import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.JTextField; import javax.swing.JTextField;
import com.samskivert.Log; import com.samskivert.Log;
@@ -33,8 +37,8 @@ public class IntField extends JTextField
setHorizontalAlignment(JTextField.RIGHT); setHorizontalAlignment(JTextField.RIGHT);
setColumns(5); setColumns(5);
// register ourselves as the validator and focus listener // register ourselves as the validator
SwingUtil.setDocumentHelpers(this, this, null); SwingUtil.setDocumentHelpers(this, this, this);
addFocusListener(this); addFocusListener(this);
} }
@@ -44,16 +48,15 @@ public class IntField extends JTextField
public int getValue () public int getValue ()
{ {
String s = getText(); String s = getText();
if ("".equals(s)) { if (!"".equals(s)) {
return 0; try {
} return _formatter.parse(s).intValue();
try { } catch (ParseException pe) {
return Integer.parseInt(s); Log.warning("This shouldn't happen.");
} catch (NumberFormatException nfe) { }
Log.warning("This should never, ever, happen");
} }
return -1; return 0;
} }
/** /**
@@ -61,7 +64,7 @@ public class IntField extends JTextField
*/ */
public void setValue (int value) public void setValue (int value)
{ {
setText(Integer.toString(value)); setText(_formatter.format(value));
} }
/** /**
@@ -70,7 +73,7 @@ public class IntField extends JTextField
public void setMinValue (int minValue) public void setMinValue (int minValue)
{ {
_minValue = minValue; _minValue = minValue;
setText(getText()); // jiggle the text validateText();
} }
/** /**
@@ -79,18 +82,9 @@ public class IntField extends JTextField
public void setMaxValue (int maxValue) public void setMaxValue (int maxValue)
{ {
_maxValue = maxValue; _maxValue = maxValue;
setText(getText()); // jiggle the text validateText();
} }
// from interface SwingUtil.DocumentTransformer
public String transform (String s)
{
if ("".equals(s)) {
return s;
}
int val = Integer.parseInt(s);
return Integer.toString(Math.min(_maxValue, Math.max(_minValue, val)));
}
// from interface SwingUtil.DocumentValidator // from interface SwingUtil.DocumentValidator
public boolean isValid (String text) public boolean isValid (String text)
@@ -100,25 +94,59 @@ public class IntField extends JTextField
} }
try { try {
int value = Integer.parseInt(text); int value = _formatter.parse(text).intValue();
return (value <= _maxValue); return (value <= _maxValue);
} catch (NumberFormatException nfe) { } catch (ParseException nfe) {
return false; return false;
} }
} }
// from interface SwingUtil.DocumentTransformer
public String transform (String text)
{
try {
return _formatter.format(_formatter.parse(text));
} catch (ParseException nfe) {
return text;
}
}
// documentation inherited from interface // documentation inherited from interface
public void focusGained (FocusEvent e) public void focusGained (FocusEvent e)
{ {
// nada
} }
// documentation inherited from interface // documentation inherited from interface
public void focusLost (FocusEvent e) public void focusLost (FocusEvent e)
{ {
// here we ensure that we're not below our minimum validateText();
setText(transform(getText()));
} }
protected int _minValue; /**
protected int _maxValue; * Ensure that the value we're displaying is between the minimum and
* the maximum.
*/
protected void validateText ()
{
String text = getText();
int val;
try {
val = _formatter.parse(text).intValue();
} catch (ParseException pe) {
val = 0;
}
// bound it in
String validated = _formatter.format(
Math.min(_maxValue, Math.max(_minValue, val)));
if (!text.equals(validated)) {
// only do it if it changes the text- otherwise
// we booch the focus when this is called from focusLost
setText(validated);
}
}
protected int _minValue, _maxValue;
protected NumberFormat _formatter = NumberFormat.getIntegerInstance();
} }