01: /*
02: * Created on 13.01.2005 for PIROL
03: *
04: * SVN header information:
05: * $Author: javamap $
06: * $Rev: 856 $
07: * $Date: 2007-06-18 21:15:27 -0700 (Mon, 18 Jun 2007) $
08: * $Id: NumberInputDocument.java 856 2007-06-19 04:15:27Z javamap $
09: */
10: package de.fho.jump.pirol.ui.documents;
11:
12: import java.text.DecimalFormatSymbols;
13:
14: import javax.swing.text.AttributeSet;
15: import javax.swing.text.BadLocationException;
16: import javax.swing.text.PlainDocument;
17:
18: /**
19: *
20: * TODO: comment class
21: *
22: * @author Ole Rahn, Stefan Ostermann
23: * <br>
24: * <br>FH Osnabrück - University of Applied Sciences Osnabrück,
25: * <br>Project: PIROL (2005),
26: * <br>Subproject: Daten- und Wissensmanagement
27: *
28: * @version $Rev: 856 $
29: *
30: */
31: public class NumberInputDocument extends PlainDocument {
32:
33: private static final long serialVersionUID = 8158679380473471643L;
34:
35: protected String actionCommand = "";
36:
37: public String getActionCommand() {
38: return actionCommand;
39: }
40:
41: public void setActionCommand(String actionCommand) {
42: this .actionCommand = actionCommand;
43: }
44:
45: public void insertString(int offs, String str, AttributeSet a)
46: throws BadLocationException {
47: DecimalFormatSymbols dfs = new DecimalFormatSymbols();
48: char decimalSeparator = dfs.getDecimalSeparator();
49: String clearedStr = str.substring(0);
50:
51: if (this .getText(0, this .getLength()).indexOf('.') > -1) {
52: clearedStr = clearedStr.replaceAll("[^0-9-]", "");
53: } else if (decimalSeparator != '.'
54: && this .getText(0, this .getLength()).indexOf(
55: decimalSeparator) > -1) {
56: clearedStr = clearedStr.replaceAll("[^0-9-]", "");
57: } else {
58: clearedStr = clearedStr.replaceAll("[^0-9-."
59: + decimalSeparator + "]", "");
60: }
61:
62: if (clearedStr.length() > 0) {
63: super.insertString(offs, clearedStr, a);
64: }
65: }
66: }
|