KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > NumberDocument


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * NumberDocument.java
28  *
29  * Created on 10 febbraio 2003, 0.17
30  *
31  */

32
33 package it.businesslogic.ireport.gui;
34
35 import it.businesslogic.ireport.gui.event.ValueChangedEvent;
36 import it.businesslogic.ireport.util.Misc;
37 import javax.swing.text.*;
38 import java.text.*;
39 /**
40  *
41  * @author Administrator
42  */

43 public class NumberDocument extends javax.swing.text.PlainDocument JavaDoc {
44     private char groupingSeparator = ',';
45     private char decimalSeparator = '.';
46     
47     private double value = 0;
48     
49     /** Creates a new instance of NumberDocument */
50     public NumberDocument() {
51         
52         DecimalFormatSymbols dfs = new DecimalFormatSymbols();
53         groupingSeparator = dfs.getGroupingSeparator();
54         decimalSeparator = dfs.getDecimalSeparator();
55      }
56     
57     public boolean checkNumber(String JavaDoc proposedResult)
58     {
59         // We allow the use of comma like . if no decimalSeparator is present...
60
if (groupingSeparator == '.' && proposedResult.indexOf(decimalSeparator) < 0)
61         {
62             //proposedResult = proposedResult.replace(groupingSeparator, '.');
63
}
64         else
65         {
66             proposedResult = Misc.string_replace( "", ""+groupingSeparator , proposedResult);
67         }
68         proposedResult = proposedResult.replace(decimalSeparator, '.');
69         
70         try {
71             //Number n = format.parse(proposedResult);
72
if (proposedResult.trim().length() == 0) return true;
73             
74             double oldValue = getValue();
75             
76             double newValue = 0;
77             if (proposedResult.length()> 0) newValue = Double.parseDouble(proposedResult);
78
79             fireValueChangedListenerValueChanged(new ValueChangedEvent(null, oldValue,newValue));
80            
81             return true;
82         } catch (NumberFormatException JavaDoc e) {
83             //System.err.println("insertString: could not parse: "
84
// + proposedResult);
85
}
86         return false;
87     }
88     
89     /** Inserts some content into the document.
90      * Inserting content causes a write lock to be held while the
91      * actual changes are taking place, followed by notification
92      * to the observers on the thread that grabbed the write lock.
93      * <p>
94      * This method is thread safe, although most Swing methods
95      * are not. Please see
96      * <A HREF="http://java.sun.com/products/jfc/swingdoc-archive/threads.html">Threads
97      * and Swing</A> for more information.
98      *
99      * @param offs the starting offset >= 0
100      * @param str the string to insert; does nothing with null/empty strings
101      * @param a the attributes for the inserted content
102      * @exception BadLocationException the given insert position is not a valid
103      * position within the document
104      * @see Document#insertString
105      *
106      */

107     public void insertString(int offs, String JavaDoc str, AttributeSet a) throws BadLocationException {
108
109         if (str == null || str.length() == 0) {
110             super.insertString(offs, str, a);
111             return;
112         }
113         String JavaDoc currentText = getText(0, getLength());
114         String JavaDoc beforeOffset = currentText.substring(0, offs);
115         String JavaDoc afterOffset = currentText.substring(offs, currentText.length());
116         String JavaDoc proposedResult = beforeOffset + str + afterOffset;
117         if (checkNumber(proposedResult))
118         {
119             super.insertString(offs, str, a);
120         }
121     }
122             
123      public void remove(int offs, int len) throws BadLocationException {
124          
125         String JavaDoc currentText = getText(0, getLength());
126         String JavaDoc beforeOffset = currentText.substring(0, offs);
127         String JavaDoc afterOffset = currentText.substring(len + offs,
128                                                    currentText.length());
129         String JavaDoc proposedResult = beforeOffset + afterOffset;
130         if (checkNumber(proposedResult))
131         {
132              super.remove(offs, len);
133         }
134     }
135      
136      public void replace(int offset, int length, String JavaDoc str, AttributeSet attrs) throws BadLocationException {
137         
138          if (str == null || str.length() == 0) {
139              super.replace(offset, length, str, attrs);
140              return;
141          }
142          
143          String JavaDoc currentText = getText(0, getLength());
144         String JavaDoc beforeOffset = currentText.substring(0, offset);
145         String JavaDoc afterOffset = currentText.substring(length + offset,
146                                                    currentText.length());
147         String JavaDoc proposedResult = beforeOffset + str + afterOffset;
148         
149         
150         if (checkNumber(proposedResult))
151         {
152              super.replace(offset, length, str, attrs);
153         }
154     }
155
156     public double getValue() {
157         return value;
158     }
159
160     public void setValue(double value) {
161         this.value = value;
162     }
163
164     /**
165      * Utility field used by event firing mechanism.
166      */

167     private javax.swing.event.EventListenerList JavaDoc listenerList = null;
168
169     /**
170      * Registers ValueChangedListener to receive events.
171      * @param listener The listener to register.
172      */

173     public synchronized void addValueChangedListener(it.businesslogic.ireport.gui.event.ValueChangedListener listener) {
174
175         if (listenerList == null ) {
176             listenerList = new javax.swing.event.EventListenerList JavaDoc();
177         }
178         listenerList.add (it.businesslogic.ireport.gui.event.ValueChangedListener.class, listener);
179     }
180
181     /**
182      * Removes ValueChangedListener from the list of listeners.
183      * @param listener The listener to remove.
184      */

185     public synchronized void removeValueChangedListener(it.businesslogic.ireport.gui.event.ValueChangedListener listener) {
186
187         listenerList.remove (it.businesslogic.ireport.gui.event.ValueChangedListener.class, listener);
188     }
189
190     /**
191      * Notifies all registered listeners about the event.
192      *
193      * @param event The event to be fired
194      */

195     private void fireValueChangedListenerValueChanged(it.businesslogic.ireport.gui.event.ValueChangedEvent event) {
196
197         if (listenerList == null) return;
198         Object JavaDoc[] listeners = listenerList.getListenerList ();
199         for (int i = listeners.length - 2; i >= 0; i -= 2) {
200             if (listeners[i]==it.businesslogic.ireport.gui.event.ValueChangedListener.class) {
201                 ((it.businesslogic.ireport.gui.event.ValueChangedListener)listeners[i+1]).valueChanged (event);
202             }
203         }
204     }
205           
206 }
207
Popular Tags