-
Notifications
You must be signed in to change notification settings - Fork 0
/
JNumberField.java
129 lines (105 loc) · 3.78 KB
/
JNumberField.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
/**
Swing-component for input and output of numeric values.
*/
public class JNumberField extends JTextField {
/** constructor for a JNumberField */
public JNumberField() {
enableEvents(AWTEvent.KEY_EVENT_MASK);
}
/** Gets a double-value from the JNumberField. */
public double getDouble() {
Double d = new Double(getText());
return d.doubleValue();
}
/** Gets a float-value from the JNumberField. */
public float getFloat() {
Double d = new Double(getText());
return d.floatValue();
}
/** Gets an int-value from the JNumberField. */
public int getInt() {
Double d = new Double(getText());
return d.intValue();
}
/** Gets a long-value from the JNumberField. */
public long getLong() {
Double d = new Double(getText());
return d.longValue();
}
/** Checks wether the JNumberField contains a valid numeric value. */
public boolean isNumeric() {
final String Digits = "(\\p{Digit}+)";
final String HexDigits = "(\\p{XDigit}+)";
// an exponent is 'e' or 'E' followed by an optionally
// signed decimal integer.
final String Exp = "[eE][+-]?"+Digits;
final String fpRegex =
("[\\x00-\\x20]*"+ // Optional leading "whitespace"
"[+-]?(" + // Optional sign character
"NaN|" + // "NaN" string
"Infinity|" + // "Infinity" string
// A decimal floating-point string representing a finite positive
// number without a leading sign has at most five basic pieces:
// Digits . Digits ExponentPart FloatTypeSuffix
//
// Since this method allows integer-only strings as input
// in addition to strings of floating-point literals, the
// two sub-patterns below are simplifications of the grammar
// productions from the Java Language Specification, 2nd
// edition, section 3.10.2.
// Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
"((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
// . Digits ExponentPart_opt FloatTypeSuffix_opt
"(\\.("+Digits+")("+Exp+")?)|"+
// Hexadecimal strings
"((" +
// 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
"(0[xX]" + HexDigits + "(\\.)?)|" +
// 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
"(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
")[pP][+-]?" + Digits + "))" +
"[fFdD]?))" +
"[\\x00-\\x20]*");// Optional trailing "whitespace"
return java.util.regex.Pattern.matches(fpRegex, getText());
}
/** Sets a double-value into the JNumberField. */
public void setDouble(double d) {
setText(String.valueOf(d));
}
/** Sets a double-value with N digits into the JNumberField. */
public void setDouble(double d, int N) {
setText(String.format(Locale.ENGLISH, "%." + N + "f", d));
}
/** Sets a float-value into the JNumberField. */
public void setFloat(float f) {
setText(String.valueOf(f));
}
/** Sets a float-value with N digits into the JNumberField. */
public void setFloat(float f, int N) {
setText(String.format(Locale.ENGLISH, "%." + N + "f", f));
}
/** Sets an int-value into the JNumberField. */
public void setInt(int i) {
setText(String.valueOf(i));
}
/** Sets a long-value into the JNumberField. */
public void setLong(long l) {
setText(String.valueOf(l));
}
/** Clears the JNumberField */
public void clear() {
setText("");
}
protected void processKeyEvent(KeyEvent e) {
super.processKeyEvent(e);
if (isNumeric() || getText().equals("-") ||
getText().equals("") || getText().equals("."))
setBackground(Color.white);
else
setBackground(Color.red);
}
}