From fa344463a226ffcd4f85dd6656395d53fb34a594 Mon Sep 17 00:00:00 2001 From: mdb Date: Mon, 15 Dec 2003 18:56:27 +0000 Subject: [PATCH] The DocumentValidator and DocumentTransformer system don't work worth a damn if you have a non-zero minimum value because you're fucked by not being able to enter, for example, "1" because it's below the minimum value even if you wanted to keep typing "100" which may be valid. So instead we don't enforce the minimum value with the validator/ transformer combo and instead make sure we're at the minimum when we lose focus. It's not perfect but there is no perfect solution when we're trying to predict human intention. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1353 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/swing/IntField.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/swing/IntField.java b/projects/samskivert/src/java/com/samskivert/swing/IntField.java index 0b50403d..a64ef99a 100644 --- a/projects/samskivert/src/java/com/samskivert/swing/IntField.java +++ b/projects/samskivert/src/java/com/samskivert/swing/IntField.java @@ -1,8 +1,10 @@ // -// $Id: IntField.java,v 1.4 2003/11/25 17:54:50 eric Exp $ +// $Id: IntField.java,v 1.5 2003/12/15 18:56:27 mdb Exp $ package com.samskivert.swing; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; import javax.swing.JTextField; import com.samskivert.Log; @@ -15,7 +17,8 @@ import com.samskivert.swing.util.SwingUtil; * specified range. */ public class IntField extends JTextField - implements SwingUtil.DocumentValidator, SwingUtil.DocumentTransformer + implements SwingUtil.DocumentValidator, SwingUtil.DocumentTransformer, + FocusListener { public IntField () { @@ -32,8 +35,9 @@ public class IntField extends JTextField setHorizontalAlignment(JTextField.RIGHT); setColumns(5); - // register ourselves as the validator + // register ourselves as the validator and focus listener SwingUtil.setDocumentHelpers(this, this, null); + addFocusListener(this); } /** @@ -92,13 +96,24 @@ public class IntField extends JTextField try { int value = Integer.parseInt(text); - - return ((value >= _minValue) && (value <= _maxValue)); + return (value <= _maxValue); } catch (NumberFormatException nfe) { return false; } } + // documentation inherited from interface + public void focusGained (FocusEvent e) + { + } + + // documentation inherited from interface + public void focusLost (FocusEvent e) + { + // here we ensure that we're not below our minimum + setText(transform(getText())); + } + protected int _minValue; protected int _maxValue; }