001: /*
002: * NumberTextField.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.awt.Toolkit;
025:
026: import java.text.NumberFormat;
027: import java.text.ParseException;
028:
029: import javax.swing.JTextField;
030: import javax.swing.text.AttributeSet;
031: import javax.swing.text.BadLocationException;
032: import javax.swing.text.Document;
033: import javax.swing.text.PlainDocument;
034: import org.underworldlabs.util.MiscUtils;
035:
036: /* ----------------------------------------------------------
037: * CVS NOTE: Changes to the CVS repository prior to the
038: * release of version 3.0.0beta1 has meant a
039: * resetting of CVS revision numbers.
040: * ----------------------------------------------------------
041: */
042:
043: /**
044: *
045: * @author Takis Diakoumis
046: * @version $Revision: 1.5 $
047: * @date $Date: 2006/09/30 11:40:47 $
048: */
049: public class NumberTextField extends JTextField {
050:
051: private Toolkit toolkit;
052: private NumberFormat integerFormatter;
053: private WholeNumberDocument numberDocument;
054: private int digits;
055:
056: public NumberTextField() {
057: super ();
058:
059: if (numberDocument == null)
060: numberDocument = new WholeNumberDocument();
061:
062: digits = -1;
063: numberDocument.setDigits(digits);
064: toolkit = Toolkit.getDefaultToolkit();
065: integerFormatter = NumberFormat.getNumberInstance();
066: integerFormatter.setParseIntegerOnly(true);
067: }
068:
069: public NumberTextField(int digits) {
070: this ();
071: numberDocument.setDigits(digits);
072: this .digits = digits;
073: }
074:
075: public void setDigits(int digits) {
076: this .digits = digits;
077: }
078:
079: public int getDigits() {
080: return digits;
081: }
082:
083: public int getValue() {
084: int retVal = 0;
085: try {
086: String value = getText();
087: if (MiscUtils.isNull(value)) {
088: value = "0";
089: }
090: retVal = integerFormatter.parse(value).intValue();
091: } catch (ParseException e) {
092: //toolkit.beep();
093: }
094: return retVal;
095: }
096:
097: public String getStringValue() {
098: return Integer.toString(getValue());
099: }
100:
101: public boolean isZero() {
102: return getValue() == 0;
103: }
104:
105: public void setValue(int value) {
106: setText(integerFormatter.format(value));
107: }
108:
109: protected Document createDefaultModel() {
110:
111: if (numberDocument == null)
112: numberDocument = new WholeNumberDocument();
113:
114: return numberDocument;
115:
116: }
117:
118: }
119:
120: class WholeNumberDocument extends PlainDocument {
121:
122: private Toolkit toolkit;
123: private int digits;
124:
125: public WholeNumberDocument() {
126: toolkit = Toolkit.getDefaultToolkit();
127: }
128:
129: public int getDigits() {
130: return digits;
131: }
132:
133: public void setDigits(int digits) {
134: this .digits = digits;
135: }
136:
137: public void insertString(int offs, String str, AttributeSet a)
138: throws BadLocationException {
139:
140: if (digits != -1) {
141:
142: if (getLength() >= digits) {
143: toolkit.beep();
144: return;
145: }
146:
147: }
148:
149: int j = 0;
150: char[] source = str.toCharArray();
151: char[] result = new char[source.length];
152:
153: for (int i = 0; i < result.length; i++) {
154:
155: if (Character.isDigit(source[i])
156: || (offs == 0 && i == 0 && source[i] == '-')) {
157: result[j++] = source[i];
158: } else {
159: toolkit.beep();
160: }
161:
162: }
163:
164: super .insertString(offs, new String(result, 0, j), a);
165: }
166:
167: } // class WholeNumberDocument
|