Revision 26897 branches/v2_0_0_prep/libraries/libUIComponent/src/org/gvsig/gui/beans/swing/ValidatingTextField.java
ValidatingTextField.java | ||
---|---|---|
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 |
long integerPart = (long) d; |
|
173 |
long integerPart = (long) d; //(int) d;
|
|
174 | 174 |
double decimalPart = d - integerPart; |
175 | 175 |
int truncatedDecimalPart = Math.abs((int) Math.round(decimalPart*100)); |
176 | 176 |
text = ""; |
... | ... | |
190 | 190 |
return text; |
191 | 191 |
} |
192 | 192 |
}; |
193 |
|
|
193 | 194 |
// public static class NumberWithDefinedDecimalNumberCuntCleaner extends NumberCleaner { |
194 | 195 |
// private int decimalCount = 2; |
195 | 196 |
// public NumberWithDefinedDecimalNumberCuntCleaner(String textToAppend) { |
... | ... | |
241 | 242 |
|
242 | 243 |
//Clean the text, mainly so that parties wishing to install a BlankCleaner |
243 | 244 |
//need only pass "" for the text. [Jon Aquino] |
244 |
setText(getText());
|
|
245 |
|
|
245 |
// setText(cleaner.clean(text));
|
|
246 |
setText(text); |
|
246 | 247 |
//Bonus: workaround for how GridBagLayout shrinks components to |
247 | 248 |
//minimum sizes if it can't accomodate their preferred sizes. [Jon Aquino] |
248 | 249 |
setMinimumSize(getPreferredSize()); |
... | ... | |
250 | 251 |
|
251 | 252 |
//Hopefully this will let us add validation behaviour to combo boxes. [Jon Aquino] |
252 | 253 |
public static void installValidationBehavior(final JTextField textField, |
253 |
final Validator validator, final Cleaner cleaner) {
|
|
254 |
textField.setDocument(new PlainDocument() {
|
|
255 |
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;
|
|
256 | 257 |
|
257 |
public void insertString(int offs, String str, AttributeSet a)
|
|
258 |
throws BadLocationException {
|
|
259 |
String currentText = this.getText(0, getLength());
|
|
260 |
String beforeOffset = currentText.substring(0, offs);
|
|
261 |
String afterOffset = currentText.substring(offs,
|
|
262 |
currentText.length());
|
|
263 |
String proposedResult = beforeOffset + str + afterOffset;
|
|
264 |
if (validator.isValid(cleaner.clean(proposedResult))) {
|
|
265 |
super.insertString(offs, str, a);
|
|
266 |
}
|
|
267 |
}
|
|
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 |
}
|
|
268 | 269 |
|
269 |
public void remove(int offs, int len)
|
|
270 |
throws BadLocationException {
|
|
271 |
String currentText = this.getText(0, getLength());
|
|
272 |
String beforeOffset = currentText.substring(0, offs);
|
|
273 |
String afterOffset = currentText.substring(len + offs,
|
|
274 |
currentText.length());
|
|
275 |
String proposedResult = beforeOffset + afterOffset;
|
|
276 |
if (validator.isValid(cleaner.clean(proposedResult))) {
|
|
277 |
super.remove(offs, len);
|
|
278 |
}
|
|
279 |
}
|
|
280 |
});
|
|
281 |
textField.addFocusListener(new FocusAdapter() {
|
|
282 |
public void focusLost(FocusEvent e) {
|
|
283 |
textField.setText(cleaner.clean(textField.getText()));
|
|
284 |
}
|
|
285 |
});
|
|
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 |
});
|
|
286 | 287 |
} |
287 | 288 |
|
288 | 289 |
public String getText() { |
289 |
//Focus may not be lost yet (e.g. when syncing with scrollbar) [Jon Aquino]
|
|
290 |
//Focus may not be lost yet (e.g. when syncing with scrollbar) [Jon Aquino]
|
|
290 | 291 |
return cleaner.clean(super.getText()); |
291 | 292 |
} |
292 | 293 |
|
Also available in: Unified diff