1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components.numberfield;
21
22 import javax.swing.text.AttributeSet;
23 import javax.swing.text.BadLocationException;
24 import javax.swing.text.PlainDocument;
25
26
27
28
29
30
31
32
33
34
35
36 public abstract class AbstractNumberDocument extends PlainDocument {
37
38 private Number value;
39 protected Number min=null;
40 protected Number max=null;
41 private boolean updated = false;
42
43
44
45
46 public AbstractNumberDocument() {
47 super();
48 }
49
50
51
52
53
54
55
56
57
58
59
60 public void insertString(int offs, String str, AttributeSet attributes)
61 throws BadLocationException {
62
63 updated = true;
64
65 String currentText = getText(0, getLength());
66 String beforeOffset = currentText.substring(0, offs);
67 String afterOffset = currentText.substring(offs, currentText.length());
68
69 String proposedResult = beforeOffset + str + afterOffset;
70
71 Number newValue = parseNumber(proposedResult);
72
73 if (doChecks(proposedResult,newValue)) {
74 value = newValue;
75 super.insertString(offs, str, attributes);
76 }
77
78 }
79
80 private boolean doChecks(String proposedResult, Number newValue) {
81 return (newValue != null && checkBounds(newValue)) || proposedResult.equals("") || proposedResult.equals("-");
82 }
83
84
85
86
87 protected boolean checkBounds(Number newVal) {
88 if (newVal==null) return true;
89 if (min==null || min.doubleValue()<=newVal.doubleValue()) {
90 if (max==null || max.doubleValue()>=newVal.doubleValue()) {
91 return true;
92 }
93 }
94 return false;
95 }
96
97
98
99
100
101
102
103
104
105
106 public void remove(int offs, int len) throws BadLocationException {
107
108 updated = true;
109
110 String currentText = getText(0, getLength());
111 if (currentText == null) currentText = "";
112 String beforeOffset = currentText.substring(0, offs);
113 int s = (offs + len) < 0 ? 0 : offs + len;
114 String afterOffset = "";
115 if (s < currentText.length()) {
116 afterOffset =
117 currentText.substring(s, currentText.length());
118 }
119
120 String proposedResult = beforeOffset + afterOffset;
121
122 Number newValue = parseNumber(proposedResult);
123
124 if (doChecks(proposedResult,newValue)) {
125 value = newValue;
126 super.remove(offs, len);
127 }
128 }
129
130
131
132
133
134
135
136
137
138
139
140 protected abstract Number parseNumber(String s);
141
142
143
144
145
146
147
148 public Number getValue() {
149 return value;
150 }
151
152
153
154
155
156
157 public Number getMaxValue() {
158 return max;
159 }
160
161
162
163
164
165
166 public void setMaxValue(Number max) {
167 this.max = max;
168 }
169
170
171
172
173
174
175 public Number getMinValue() {
176 return min;
177 }
178
179
180
181
182
183
184 public void setMinValue(Number min) {
185 this.min = min;
186 }
187
188
189
190
191
192 public boolean isUpdated() {
193 return updated;
194 }
195
196
197
198
199
200
201 public void setUpdated(boolean updated) {
202 this.updated = updated;
203 }
204
205 }