/*
* Created on 13.01.2005 for PIROL
*
* SVN header information:
* $Author: javamap $
* $Rev: 856 $
* $Date: 2007-06-19 06:15:27 +0200 (Di, 19. Jun 2007) $
* $Id: NumberInputDocument.java 856 2007-06-19 04:15:27Z javamap $
*/
package de.fho.jump.pirol.ui.documents;
import java.text.DecimalFormatSymbols;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
/**
*
* TODO: comment class
*
* @author Ole Rahn, Stefan Ostermann
* <br>
* <br>FH Osnabrück - University of Applied Sciences Osnabrück,
* <br>Project: PIROL (2005),
* <br>Subproject: Daten- und Wissensmanagement
*
* @version $Rev: 856 $
*
*/
public class NumberInputDocument extends PlainDocument {
private static final long serialVersionUID = 8158679380473471643L;
protected String actionCommand = "";
public String getActionCommand() {
return actionCommand;
}
public void setActionCommand(String actionCommand) {
this.actionCommand = actionCommand;
}
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
char decimalSeparator = dfs.getDecimalSeparator();
String clearedStr = str.substring(0);
if (this.getText(0, this.getLength()).indexOf('.') > -1){
clearedStr = clearedStr.replaceAll("[^0-9-]","");
} else if ( decimalSeparator!='.' && this.getText(0, this.getLength()).indexOf(decimalSeparator) > -1) {
clearedStr = clearedStr.replaceAll("[^0-9-]","");
} else {
clearedStr = clearedStr.replaceAll("[^0-9-."+decimalSeparator+"]","");
}
if (clearedStr.length() > 0){
super.insertString(offs, clearedStr, a);
}
}
}