001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.toolkit.swing;
025:
026: import java.awt.event.ActionEvent;
027: import java.awt.event.KeyEvent;
028:
029: import javax.swing.AbstractAction;
030: import javax.swing.JTextField;
031: import javax.swing.KeyStroke;
032: import javax.swing.text.Keymap;
033:
034: /**
035: * A text field which just allows input of long numbers. BUGS: You can insert non-integer text with
036: * copy&paste at the moment.
037: *
038: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
039: * @version 1.0
040: */
041: public class LongTextField extends JTextField {
042: /**
043: * Creates a new long text field with the specified number of columns. The initial text value
044: * is set to 0.
045: *
046: * @param columns the number of columns.
047: */
048: public LongTextField(int columns) {
049: this (0, columns);
050: }
051:
052: /**
053: * Creates a new long text field with the specified initial value and number of columns.
054: *
055: * @param value the initial integer value
056: * @param columns the number of columns.
057: */
058: public LongTextField(long value, int columns) {
059: super (columns);
060: setValue(value);
061:
062: AddToTextAction addToTextAction = new AddToTextAction();
063: Keymap keymap = addKeymap("LongTextFieldKeyMap", getKeymap());
064: keymap.setDefaultAction(new AbstractAction() {
065: public void actionPerformed(ActionEvent e) {
066: };
067: });
068:
069: for (int i = 0; i <= 9; i++) {
070: keymap.addActionForKeyStroke(KeyStroke
071: .getKeyStroke(("" + i).charAt(0)), addToTextAction);
072: }
073:
074: keymap.addActionForKeyStroke(KeyStroke.getKeyStroke(
075: KeyEvent.VK_MINUS, 0), addToTextAction);
076: setKeymap(keymap);
077: }
078:
079: /**
080: * Sets the value of the text field to the specified value
081: *
082: * @param value the new value of the text field
083: */
084: public void setValue(long value) {
085: setText("" + value);
086: }
087:
088: /**
089: * Returns the long value of the text field, or 0 if the the actual text is no number ("" or
090: * "-", for example).
091: *
092: * @return the value
093: */
094: public long getValue() {
095: try {
096: return (new Long(getText())).longValue();
097: } catch (NumberFormatException n) {
098: return 0;
099: }
100: }
101:
102: /**
103: * Sets the text of the textfield. You should not use this method, but the method {@link
104: * #setValue(long)}.
105: *
106: * @param newText The new text of the text field. The text must be a long or the empty string
107: * or <code>null</code>. In the second and the third case the text will be set to 0.
108: */
109: public void setText(String newText) {
110: try {
111: if ((newText == null) || newText.equals("")) {
112: super .setText("0");
113: } else if (newText.equals("-")) {
114: super .setText("-");
115: } else {
116: super .setText(newText);
117: }
118:
119: setCaretPosition(0);
120: } catch (NumberFormatException n) {
121: // ignored
122: }
123: }
124:
125: class AddToTextAction extends AbstractAction {
126: //~ Methods --------------------------------------------------------------------------------
127:
128: public void actionPerformed(ActionEvent e) {
129: if (!(e.getActionCommand().equals("-"))
130: || ((getCaretPosition() == 0) && !getText().equals(
131: "0"))) {
132: replaceSelection(e.getActionCommand());
133: }
134: }
135: }
136: }
|