1
2 package hoplugins.commons.ui;
3
4 import javax.swing.JTextField;
5 import javax.swing.text.AttributeSet;
6 import javax.swing.text.BadLocationException;
7 import javax.swing.text.PlainDocument;
8
9 /***
10 * configurable JLabel that handles numeric values
11 *
12 * @author <a href=mailto:draghetto@users.sourceforge.net>Massimiliano Amato</a>
13 */
14 public class NumberTextField extends JTextField {
15 private int _intDigits = 0;
16 private int _maxDigits = 0;
17 private short _decDigits = 0;
18
19 /***
20 * costructor for integer values only
21 *
22 * @param columns max number of chars
23 */
24 public NumberTextField(int columns) {
25 super(columns);
26 _maxDigits = columns;
27 _decDigits = 0;
28 _intDigits = _maxDigits;
29
30 setDocument(new DecimalFieldDocument(_decDigits));
31 }
32
33 /***
34 * Returns the int value
35 *
36 * @return value in the cell, or 0 if empty
37 */
38 public int getValue() {
39 String s = getText();
40 int value = 0;
41
42 try {
43 value = Integer.parseInt(s);
44 }
45 catch (Exception e) {
46 }
47
48 return value;
49 }
50
51 /***
52 * Make sure any set String is valid.
53 *
54 * @param str String to check for validity
55 * @param offs offset to be applied
56 *
57 * @return updated string
58 */
59 private String _getNewString(String str, int offs) {
60 String currentText = getText();
61 int currLen = currentText.length();
62 String newString = "";
63
64 if (offs == 0) {
65 newString = str + currentText;
66 }
67 else if (offs >= currLen) {
68 newString = currentText + str;
69 }
70 else {
71 newString = currentText.substring(0, offs) + str
72 + currentText.substring(offs, currLen);
73 }
74
75 return newString;
76 }
77
78 /***
79 * Make sure this string is numberic
80 *
81 * @param str String to check for validity
82 * @param offs offset to be applied
83 *
84 * @return true if number, or false if not
85 */
86 private boolean _validateNumberString(String str, int offs) {
87 char c;
88
89 String newString = this._getNewString(str, offs);
90
91 int newLength = newString.length();
92
93 if (newLength > _intDigits) {
94 return false;
95 }
96
97
98 for (int i = 0; i < str.length(); i++) {
99 c = str.charAt(i);
100
101 if ((c < '0') || (c > '9')) {
102 return false;
103 }
104 }
105
106 newString = this._getNewString(str, offs);
107
108 if (!newString.equals("") && !newString.equals(".")) {
109 try {
110 new Double(newString);
111 }
112 catch (NumberFormatException e) {
113 return false;
114 }
115
116 return true;
117 }
118 else {
119 return false;
120 }
121 }
122
123 /***
124 * Document to manage conversion to number
125 *
126 * @author <a href=mailto:draghetto@users.sourceforge.net>Massimiliano Amato</a>
127 */
128 private class DecimalFieldDocument extends PlainDocument {
129 private short decimal;
130
131 /***
132 * Creates a new DecimalFieldDocument object.
133 *
134 * @param length lenght of numbetr
135 */
136 public DecimalFieldDocument(short length) {
137 decimal = length;
138 }
139
140 /***
141 * Method to insert string in the right position
142 *
143 * @param offs offset
144 * @param str String to be inserted
145 * @param a AttributeSet
146 *
147 * @throws BadLocationException an exception is thrown if the new string is not a number
148 */
149 public void insertString(int offs, String str, AttributeSet a)
150 throws BadLocationException {
151 if (str != null) {
152 if (!_validateNumberString(str.trim(), offs)) {
153 throw new BadLocationException(null, offs);
154 }
155 else {
156 super.insertString(offs, str.trim(), a);
157 }
158 }
159 }
160 }
161 }