X-Git-Url: https://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=src%2Ftim%2Fprune%2Fgui%2FWholeNumberField.java;fp=src%2Ftim%2Fprune%2Fgui%2FWholeNumberField.java;h=b12d299b626c960101a8aed1f917d4ead1da918e;hp=0000000000000000000000000000000000000000;hb=ce6f2161b8596f7018d6a76bff79bc9e571f35fd;hpb=2d8cb72e84d5cc1089ce77baf1e34ea3ea2f8465 diff --git a/src/tim/prune/gui/WholeNumberField.java b/src/tim/prune/gui/WholeNumberField.java new file mode 100644 index 0000000..b12d299 --- /dev/null +++ b/src/tim/prune/gui/WholeNumberField.java @@ -0,0 +1,103 @@ +package tim.prune.gui; + +import javax.swing.JTextField; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import javax.swing.text.AttributeSet; +import javax.swing.text.BadLocationException; +import javax.swing.text.PlainDocument; + +/** + * text field for holding a single integer with validation + */ +public class WholeNumberField extends JTextField +{ + /** + * Inner class to act as document for validation + */ + protected static class WholeNumberDocument extends PlainDocument + { + /** Num digits to allow */ + private int _maxDigits = 0; + + /** + * Constructor + * @param inMaxDigits max digits to allow + */ + public WholeNumberDocument(int inMaxDigits) + { + _maxDigits = inMaxDigits; + } + + + /** + * Override the insert string method + * @param offs offset + * @param str string + * @param a attributes + * @throws BadLocationException on insert failure + */ + public void insertString(int offs, String str, AttributeSet a) + throws BadLocationException + { + if (getLength() >= _maxDigits) return; + char[] source = str.toCharArray(); + char[] result = new char[source.length]; + int j = 0; + for (int i = 0; i < result.length && j < _maxDigits; i++) { + if (Character.isDigit(source[i])) + result[j++] = source[i]; + } + super.insertString(offs, new String(result, 0, j), a); + } + } + + + /** + * Constructor + * @param inMaxDigits max digits to allow + */ + public WholeNumberField(int inMaxDigits) + { + super(inMaxDigits); + setDocument(new WholeNumberDocument(inMaxDigits)); + getDocument().addDocumentListener(new DocumentListener() { + public void removeUpdate(DocumentEvent arg0) {fireActionPerformed();} + public void insertUpdate(DocumentEvent arg0) {fireActionPerformed();} + public void changedUpdate(DocumentEvent arg0) {fireActionPerformed();} + }); + } + + /** + * @return integer value + */ + public int getValue() + { + return parseValue(getText()); + } + + /** + * @param inValue value to set + */ + public void setValue(int inValue) + { + setText("" + inValue); + } + + /** + * @param inText text to parse + * @return value as integer + */ + private static int parseValue(String inText) + { + int value = 0; + try { + value = Integer.parseInt(inText); + } + catch (NumberFormatException nfe) {} + if (value < 0) { + value = 0; + } + return value; + } +}