001: /*
002: * NumberCellEditor.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.table;
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:
035: /* ----------------------------------------------------------
036: * CVS NOTE: Changes to the CVS repository prior to the
037: * release of version 3.0.0beta1 has meant a
038: * resetting of CVS revision numbers.
039: * ----------------------------------------------------------
040: */
041:
042: /**
043: * Simple numeric value table column cell editor.
044: *
045: * @author Takis Diakoumis
046: * @version $Revision: 1.6 $
047: * @date $Date: 2006/07/16 15:45:52 $
048: */
049: public class NumberCellEditor extends JTextField implements
050: TableCellEditorValue {
051:
052: private int maxLength;
053: private Toolkit toolkit;
054: private NumberFormat integerFormatter;
055:
056: public NumberCellEditor() {
057: this (-1, false);
058: }
059:
060: public NumberCellEditor(boolean alignLeft) {
061: this (-1, alignLeft);
062: }
063:
064: public NumberCellEditor(int maxLength) {
065: this (maxLength, false);
066: }
067:
068: public NumberCellEditor(int maxLength, boolean alignLeft) {
069: super ();
070: this .maxLength = maxLength;
071: toolkit = Toolkit.getDefaultToolkit();
072: integerFormatter = NumberFormat.getNumberInstance();
073: integerFormatter.setParseIntegerOnly(true);
074: setBorder(null);
075: setHorizontalAlignment(alignLeft ? JTextField.LEFT
076: : JTextField.RIGHT);
077: }
078:
079: public int getValue() {
080: int retVal = 0;
081: try {
082: retVal = integerFormatter.parse(getText()).intValue();
083: } catch (ParseException e) {
084: toolkit.beep();
085: }
086: return retVal;
087: }
088:
089: /**
090: * Returns the current editor value from the component
091: * defining this object.
092: *
093: * @return the editor's value
094: */
095: public String getEditorValue() {
096: return getStringValue();
097: }
098:
099: public String getStringValue() {
100: return Integer.toString(getValue());
101: }
102:
103: public int getCharsLength() {
104: return getText().length();
105: }
106:
107: public boolean isZero() {
108: return getValue() == 0;
109: }
110:
111: public void setValue(int value) {
112: setText(integerFormatter.format(value));
113: }
114:
115: public int getMaxLength() {
116: return maxLength;
117: }
118:
119: public void setMaxLength(int maxLength) {
120: this .maxLength = maxLength;
121: }
122:
123: protected Document createDefaultModel() {
124: return new WholeNumberDocument();
125: }
126:
127: protected class WholeNumberDocument extends PlainDocument {
128: public void insertString(int offs, String str, AttributeSet a)
129: throws BadLocationException {
130:
131: if (maxLength != -1) {
132:
133: if (getLength() >= maxLength) {
134: toolkit.beep();
135: return;
136: }
137:
138: }
139:
140: char[] source = str.toCharArray();
141: char[] result = new char[source.length];
142: int j = 0;
143:
144: for (int i = 0; i < result.length; i++) {
145: if (Character.isDigit(source[i]))
146: result[j++] = source[i];
147: else {
148: toolkit.beep();
149: }
150: }
151: super .insertString(offs, new String(result, 0, j), a);
152: }
153: } // class WholeNumberDocument
154:
155: }
|