1    package com.instantbank.common.uiutils;
2    
3    import java.awt.Toolkit;
4    import javax.swing.text.PlainDocument;
5    import javax.swing.text.AttributeSet;
6    import javax.swing.text.BadLocationException;
7    
8    /**
9     * Auxiliary class suited for restricting input for a JTextField to
10    * numeric "double" strings as expected by Java. It allows to end
11    * the string with a single period.
12    *
13    * @author InstantBank (Rodrigo Lopez)
14    * @created dic-2002
15    */
16   public class DoubleDocument extends PlainDocument {
17   
18     public DoubleDocument() { }
19   
20   
21     /**
22      * Redefinition of the method from the PlainDocument class.
23      *
24      * @param offset Description of the Parameter
25      * @param s Description of the Parameter
26      * @param attributeSet Description of the Parameter
27      * @throws BadLocationException Description of the Exception
28      */
29     public void insertString(int offset, String s, AttributeSet attributeSet)
30        throws BadLocationException {
31       try {
32         String saux;
33         if(s.endsWith(".")) {
34           saux = s + "0";
35         }
36         else {
37           saux = s;
38         }
39   
40         Double.parseDouble(saux);
41       }
42       catch(Exception ex) {
43         Toolkit.getDefaultToolkit().beep();
44         return;
45       }
46       super.insertString(offset, s, attributeSet);
47     }
48   }
49