159 |
159 |
|
160 |
160 |
}
|
161 |
161 |
};
|
162 |
|
|
163 |
|
|
|
162 |
|
|
163 |
|
164 |
164 |
/**
|
165 |
165 |
* The validators allow the user to simply enter "+", "-", or ".". If the user
|
166 |
166 |
* doesn't go any farther, this cleaner will set the text to 0, which is reasonable.
|
167 |
167 |
*/
|
168 |
|
public static Cleaner NUMBER_CLEANER_2_DECIMALS = new Cleaner() {
|
|
168 |
public static Cleaner NUMBER_CLEANER_2_DECIMALS = new Cleaner() {
|
169 |
169 |
public String clean(String text) {
|
170 |
170 |
if (text!=null) {
|
171 |
171 |
try {
|
172 |
172 |
double d = Double.parseDouble(text.trim());
|
173 |
173 |
int integerPart = (int) d;
|
174 |
174 |
double decimalPart = d - integerPart;
|
175 |
|
text = integerPart + "." + (int) Math.round(decimalPart*100);
|
|
175 |
int truncatedDecimalPart = Math.abs((int) Math.round(decimalPart*100));
|
|
176 |
text = "";
|
|
177 |
if (d<0.0 && integerPart >=0){
|
|
178 |
text = "-";
|
|
179 |
}
|
|
180 |
if (truncatedDecimalPart<10){
|
|
181 |
text = text + integerPart + ".0" + truncatedDecimalPart;
|
|
182 |
} else {
|
|
183 |
text = text + integerPart + "." + truncatedDecimalPart;
|
|
184 |
}
|
176 |
185 |
} catch (NumberFormatException e) {
|
177 |
186 |
return "0";
|
178 |
187 |
}
|
179 |
188 |
}
|
180 |
|
|
|
189 |
|
181 |
190 |
return text;
|
182 |
191 |
}
|
183 |
192 |
};
|
|
193 |
|
184 |
194 |
// public static class NumberWithDefinedDecimalNumberCuntCleaner extends NumberCleaner {
|
185 |
195 |
// private int decimalCount = 2;
|
186 |
196 |
// public NumberWithDefinedDecimalNumberCuntCleaner(String textToAppend) {
|
... | ... | |
190 |
200 |
// super(textToAppend);
|
191 |
201 |
// this.decimalCount = decimalCount;
|
192 |
202 |
// }
|
193 |
|
//
|
|
203 |
//
|
194 |
204 |
// @Override
|
195 |
205 |
// public String clean(String text) {
|
196 |
206 |
// String s = super.clean(text);
|
197 |
207 |
// if (s.indexOf(ch)
|
198 |
|
//
|
|
208 |
//
|
199 |
209 |
// return s;
|
200 |
210 |
// }
|
201 |
211 |
// }
|
... | ... | |
225 |
235 |
public ValidatingTextField(String text, int columns,
|
226 |
236 |
int horizontalAlignment, final Validator validator,
|
227 |
237 |
final Cleaner cleaner) {
|
228 |
|
super(text, columns);
|
|
238 |
super(columns);
|
229 |
239 |
this.cleaner = cleaner;
|
230 |
240 |
setHorizontalAlignment(horizontalAlignment);
|
231 |
241 |
installValidationBehavior(this, validator, cleaner);
|
232 |
242 |
|
233 |
243 |
//Clean the text, mainly so that parties wishing to install a BlankCleaner
|
234 |
244 |
//need only pass "" for the text. [Jon Aquino]
|
235 |
|
setText(cleaner.clean(getText()));
|
236 |
|
|
|
245 |
// setText(cleaner.clean(text));
|
|
246 |
setText(text);
|
237 |
247 |
//Bonus: workaround for how GridBagLayout shrinks components to
|
238 |
248 |
//minimum sizes if it can't accomodate their preferred sizes. [Jon Aquino]
|
239 |
249 |
setMinimumSize(getPreferredSize());
|
... | ... | |
241 |
251 |
|
242 |
252 |
//Hopefully this will let us add validation behaviour to combo boxes. [Jon Aquino]
|
243 |
253 |
public static void installValidationBehavior(final JTextField textField,
|
244 |
|
final Validator validator, final Cleaner cleaner) {
|
245 |
|
textField.setDocument(new PlainDocument() {
|
246 |
|
private static final long serialVersionUID = 7097829094600558963L;
|
|
254 |
final Validator validator, final Cleaner cleaner) {
|
|
255 |
textField.setDocument(new PlainDocument() {
|
|
256 |
private static final long serialVersionUID = 7097829094600558963L;
|
247 |
257 |
|
248 |
|
public void insertString(int offs, String str, AttributeSet a)
|
249 |
|
throws BadLocationException {
|
250 |
|
String currentText = this.getText(0, getLength());
|
251 |
|
String beforeOffset = currentText.substring(0, offs);
|
252 |
|
String afterOffset = currentText.substring(offs,
|
253 |
|
currentText.length());
|
254 |
|
String proposedResult = beforeOffset + str + afterOffset;
|
255 |
|
if (validator.isValid(cleaner.clean(proposedResult))) {
|
256 |
|
super.insertString(offs, str, a);
|
257 |
|
}
|
258 |
|
}
|
|
258 |
public void insertString(int offs, String str, AttributeSet a)
|
|
259 |
throws BadLocationException {
|
|
260 |
String currentText = this.getText(0, getLength());
|
|
261 |
String beforeOffset = currentText.substring(0, offs);
|
|
262 |
String afterOffset = currentText.substring(offs,
|
|
263 |
currentText.length());
|
|
264 |
String proposedResult = beforeOffset + str + afterOffset;
|
|
265 |
if (validator.isValid(cleaner.clean(proposedResult))) {
|
|
266 |
super.insertString(offs, str, a);
|
|
267 |
}
|
|
268 |
}
|
259 |
269 |
|
260 |
|
public void remove(int offs, int len)
|
261 |
|
throws BadLocationException {
|
262 |
|
String currentText = this.getText(0, getLength());
|
263 |
|
String beforeOffset = currentText.substring(0, offs);
|
264 |
|
String afterOffset = currentText.substring(len + offs,
|
265 |
|
currentText.length());
|
266 |
|
String proposedResult = beforeOffset + afterOffset;
|
267 |
|
if (validator.isValid(cleaner.clean(proposedResult))) {
|
268 |
|
super.remove(offs, len);
|
269 |
|
}
|
270 |
|
}
|
271 |
|
});
|
272 |
|
textField.addFocusListener(new FocusAdapter() {
|
273 |
|
public void focusLost(FocusEvent e) {
|
274 |
|
textField.setText(cleaner.clean(textField.getText()));
|
275 |
|
}
|
276 |
|
});
|
|
270 |
public void remove(int offs, int len)
|
|
271 |
throws BadLocationException {
|
|
272 |
String currentText = this.getText(0, getLength());
|
|
273 |
String beforeOffset = currentText.substring(0, offs);
|
|
274 |
String afterOffset = currentText.substring(len + offs,
|
|
275 |
currentText.length());
|
|
276 |
String proposedResult = beforeOffset + afterOffset;
|
|
277 |
if (validator.isValid(cleaner.clean(proposedResult))) {
|
|
278 |
super.remove(offs, len);
|
|
279 |
}
|
|
280 |
}
|
|
281 |
});
|
|
282 |
textField.addFocusListener(new FocusAdapter() {
|
|
283 |
public void focusLost(FocusEvent e) {
|
|
284 |
textField.setText(cleaner.clean(textField.getText()));
|
|
285 |
}
|
|
286 |
});
|
277 |
287 |
}
|
278 |
288 |
|
279 |
289 |
public String getText() {
|
280 |
290 |
//Focus may not be lost yet (e.g. when syncing with scrollbar) [Jon Aquino]
|
281 |
|
return cleaner.clean(super.getText());
|
|
291 |
return cleaner.clean(super.getText());
|
282 |
292 |
}
|
283 |
293 |
|
|
294 |
public void setText(String text) {
|
|
295 |
super.setText(cleaner.clean(text));
|
|
296 |
}
|
|
297 |
|
284 |
298 |
public double getDouble() {
|
285 |
299 |
return Double.parseDouble(getText().trim());
|
286 |
300 |
}
|
... | ... | |
400 |
414 |
}
|
401 |
415 |
}
|
402 |
416 |
}
|
403 |
|
|
404 |
|
|
405 |
417 |
|
|
418 |
|
|
419 |
|
406 |
420 |
public static class MinIntCleaner implements Cleaner {
|
407 |
421 |
private int minimum;
|
408 |
422 |
|