KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > swing > DoubleDocument


1 /*
2
3    Copyright 2001 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.ext.swing;
19
20 import javax.swing.text.AttributeSet JavaDoc;
21 import javax.swing.text.BadLocationException JavaDoc;
22 import javax.swing.text.PlainDocument JavaDoc;
23
24 /**
25  * Helper class. Only allows an Double value in the document.
26  *
27  * @author <a HREF="mailto:vhardy@apache.org">Vincent Hardy</a>
28  * @version $Id: DoubleDocument.java,v 1.4 2005/03/27 08:58:33 cam Exp $
29  */

30 public class DoubleDocument extends PlainDocument JavaDoc {
31
32     /**
33      * Strip all non digit characters. The first character must be '-' or '+'.
34      * Only one '.' is allowed.
35      */

36     public void insertString(int offs, String JavaDoc str, AttributeSet JavaDoc a)
37             throws BadLocationException JavaDoc {
38
39         if (str == null) {
40             return;
41         }
42
43         // Get current value
44
String JavaDoc curVal = getText(0, getLength());
45         boolean hasDot = curVal.indexOf(".")!=-1;
46
47         // Strip non digit characters
48
char[] buffer = str.toCharArray();
49         char[] digit = new char[buffer.length];
50         int j = 0;
51
52         if(offs==0 && buffer!=null && buffer.length>0 && buffer[0]=='-')
53             digit[j++] = buffer[0];
54
55         for (int i = 0; i < buffer.length; i++) {
56             if(Character.isDigit(buffer[i]))
57                 digit[j++] = buffer[i];
58             if(!hasDot && buffer[i]=='.'){
59                 digit[j++] = '.';
60                 hasDot = true;
61             }
62         }
63
64         // Now, test that new value is within range.
65
String JavaDoc added = new String JavaDoc(digit, 0, j);
66         try{
67             StringBuffer JavaDoc val = new StringBuffer JavaDoc(curVal);
68             val.insert(offs, added);
69             if(val.toString().equals(".") || val.toString().equals("-") || val.toString().equals("-."))
70                 super.insertString(offs, added, a);
71             else{
72                 Double.valueOf(val.toString());
73                 super.insertString(offs, added, a);
74             }
75         }catch(NumberFormatException JavaDoc e){
76             // Ignore insertion, as it results in an out of range value
77
}
78     }
79
80     public void setValue(double d){
81         try{
82             remove(0, getLength());
83             insertString(0, (new Double JavaDoc(d)).toString(), null);
84         }catch(BadLocationException JavaDoc e){
85             // Will not happen because we are sure
86
// we use the proper range
87
}
88     }
89
90     public double getValue(){
91         try{
92             String JavaDoc t = getText(0, getLength());
93             if(t != null && t.length() > 0){
94                 return Double.parseDouble(t);
95             }
96             else{
97                 return 0;
98             }
99         }catch(BadLocationException JavaDoc e){
100             // Will not happen because we are sure
101
// we use the proper range
102
throw new Error JavaDoc();
103         }
104     }
105 }
106
107
108
Popular Tags