1 /*
2 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
6 * conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistribution in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any
21 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
22 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
24 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
25 * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
26 * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
27 * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
28 * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
29 * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
30 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
31 * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
32 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33 *
34 * You acknowledge that this software is not designed, licensed or
35 * intended for use in the design, construction, operation or
36 * maintenance of any nuclear facility.
37 */
38
39 package javax.swing.beaninfo;
40
41 import javax.swing.text.AttributeSet;
42 import javax.swing.text.BadLocationException;
43 import javax.swing.text.PlainDocument;
44
45
46 /**
47 * A text document which will reject any characters that are not
48 * digits.
49 *
50 * @version 1.3 02/27/02
51 * @author Mark Davidson
52 */
53 public class FloatNumberDocument extends PlainDocument
54 {
55 public void insertString(int offs, String str, AttributeSet atts) throws BadLocationException
56 {
57 int length = getLength();
58 String newtext = null;
59 // is their already a number in the document?
60 if( length > 0 )
61 {
62 // if so, insert the string
63 String text1 = getText(0, offs );
64 String text2 = getText(offs, length - offs );
65 newtext = text1 + str + text2;
66 }
67 else
68 {
69 // document is empty, so take just the new string
70 newtext = str;
71 }
72 try {
73 // test wether its pasrable
74 Double.parseDouble( newtext );
75 }
76 catch( NumberFormatException e )
77 {
78 // it will not be a number if we insert the string
79 return;
80 }
81 super.insertString(offs, str, atts);
82 }
83
84 /*
85 public void remove(int offs, int len) throws BadLocationException
86 {
87 int length = getLength();
88 String newtext = null;
89 // is their already a number in the document?
90 if( length > 0 )
91 {
92 // if so, insert the string
93 String text1 = getText(0, offs );
94 String text2 = getText(offs+len, length - offs - len );
95 newtext = text1 + text2;
96 try {
97 // test wether its pasrable
98 Double.parseDouble( newtext );
99 }
100 catch( NumberFormatException e )
101 {
102 // it will not be a number if we insert the string
103 return;
104 }
105 super.remove(offs, len);
106 }
107 }
108 */
109 }
110