KickJava   Java API By Example, From Geeks To Geeks.

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


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  * JNumberField.java
28  *
29  * Created on 10 febbraio 2003, 0.22
30  *
31  */

32
33 package it.businesslogic.ireport.gui;
34
35 import javax.swing.FocusManager JavaDoc;
36 import javax.swing.text.*;
37 import java.text.*;
38 import java.awt.event.*;
39 import it.businesslogic.ireport.gui.event.*;
40
41 /**
42  *
43  * @author Administrator
44  */

45 public class JNumberField extends javax.swing.JTextField JavaDoc implements java.awt.event.FocusListener JavaDoc, ValueChangedListener, KeyListener {
46     
47     /** Holds value of property decimals. */
48     private int decimals=2;
49     
50     /** Holds value of property integer. */
51     private boolean integer=false;
52     
53     /** Holds value of property value. */
54     private double value = 0.0;
55     private double oldValue = 0.0;
56     
57     private java.text.NumberFormat JavaDoc numberFormat=null;
58     
59     /** Holds value of property grouping. */
60     private boolean grouping=true;
61     
62     /** Utility field used by event firing mechanism. */
63     private javax.swing.event.EventListenerList JavaDoc listenerList = null;
64     
65     private boolean init = false;
66     
67     private NumberDocument nd = new NumberDocument();
68     
69     /** Creates a new instance of JNumberField */
70     public JNumberField() {
71         
72         super();
73         this.addFocusListener(this);
74         numberFormat=NumberFormat.getInstance();
75         numberFormat.setMaximumFractionDigits(2);
76         numberFormat.setMinimumFractionDigits(2);
77         numberFormat.setGroupingUsed(true);
78         this.setText(numberFormat.format(0));
79          
80         nd.addValueChangedListener( this );
81         
82         this.addKeyListener(this);
83         
84         this.setDocument( nd );
85         
86     }
87     
88     public void keyPressed(KeyEvent e) {
89         if (e.getKeyCode() == KeyEvent.VK_ENTER)
90         {
91             FocusManager.getCurrentManager().focusNextComponent();
92         }
93     }
94     public void keyReleased(KeyEvent e) {
95     }
96     public void keyTyped(KeyEvent e) {
97     }
98     
99     public void valueChanged(ValueChangedEvent evt) {
100         
101         if (isInit()) return;
102         value = evt.getNewValue();
103         //System.out.println("Changed value: " + value);
104
}
105     
106     public void textChanged(javax.swing.event.DocumentEvent JavaDoc evt) {
107         
108         
109     }
110     
111     
112     /** Creates the default implementation of the model
113      * to be used at construction if one isn't explicitly
114      * given. An instance of <code>PlainDocument</code> is returned.
115      *
116      * @return the default model implementation
117      *
118      */

119     protected Document createDefaultModel() {
120         //Document retValue;
121

122         //retValue = super.createDefaultModel();
123
return new NumberDocument();
124     }
125     
126     /** Getter for property decimals.
127      * @return Value of property decimals.
128      *
129      */

130     public int getDecimals() {
131         return this.decimals;
132     }
133     
134     /** Setter for property decimals.
135      * @param decimals New value of property decimals.
136      *
137      * @throws PropertyVetoException
138      *
139      */

140     public void setDecimals(int decimals) throws java.beans.PropertyVetoException JavaDoc {
141         this.decimals = decimals;
142         if (!isInteger()) {
143             numberFormat.setMaximumFractionDigits(decimals);
144             numberFormat.setMinimumFractionDigits(decimals);
145         }
146         this.setText( numberFormat.format(getValue()));
147     }
148     
149     /** Getter for property integer.
150      * @return Value of property integer.
151      *
152      */

153     public boolean isInteger() {
154         return this.integer;
155     }
156     
157     /** Setter for property integer.
158      * @param integer New value of property integer.
159      *
160      * @throws PropertyVetoException
161      *
162      */

163     public void setInteger(boolean integer) throws java.beans.PropertyVetoException JavaDoc {
164         if (integer) {
165             numberFormat.setMaximumFractionDigits(0);
166             numberFormat.setMinimumFractionDigits(0);
167         } else {
168             
169             if (getDecimals() == -1) {
170                 numberFormat.setMinimumFractionDigits(1);
171                 numberFormat.setMaximumFractionDigits(100);
172             } else {
173                 numberFormat.setMinimumFractionDigits(getDecimals());
174                 numberFormat.setMaximumFractionDigits(getDecimals());
175             }
176         }
177         this.integer = integer;
178         this.setText( numberFormat.format(getValue()));
179     }
180     
181     /** Getter for property value.
182      * @return Value of property value.
183      *
184      */

185     public double getValue() {
186         return this.value;
187     }
188     
189     /** Setter for property value.
190      * @param value New value of property value.
191      *
192      * @throws PropertyVetoException
193      *
194      */

195     public void setValue(double value) throws java.beans.PropertyVetoException JavaDoc {
196         this.setInit(true);
197         this.value = value;
198         this.oldValue = value;
199         nd.setValue(value);
200         this.setText( numberFormat.format(getValue()) );
201         this.setInit(false);
202     }
203     
204     public void setValue(int value) throws java.beans.PropertyVetoException JavaDoc {
205         setValue((double)value);
206     }
207     
208     
209     public void setText(String JavaDoc text) {
210         this.setInit(true);
211         super.setText(text);
212         this.setInit(false);
213     }
214     
215     /** Invoked when a component gains the keyboard focus.
216      *
217      */

218     public void focusGained(FocusEvent e) {
219         
220         if (getText().length() == 0) return;
221         this.oldValue = value;
222         numberFormat.setGroupingUsed(false);
223         setText( numberFormat.format(value));
224         numberFormat.setGroupingUsed(this.isGrouping());
225     }
226     
227     /** Invoked when a component loses the keyboard focus.
228      *
229      */

230     public void focusLost(FocusEvent e) {
231         
232         if (getText().length() == 0) return;
233         setText( numberFormat.format(value));
234         if (oldValue != value)
235             this.fireActionListenerActionPerformed(new java.awt.event.ActionEvent JavaDoc( this, 0, "") );
236     }
237     
238     /** Getter for property grouping.
239      * @return Value of property grouping.
240      *
241      */

242     public boolean isGrouping() {
243         return this.grouping;
244     }
245     
246     /** Setter for property grouping.
247      * @param grouping New value of property grouping.
248      *
249      * @throws PropertyVetoException
250      *
251      */

252     public void setGrouping(boolean grouping) throws java.beans.PropertyVetoException JavaDoc {
253         this.grouping = grouping;
254     }
255     
256     /** Registers ActionListener to receive events.
257      * @param listener The listener to register.
258      *
259      */

260     public synchronized void addActionListener(java.awt.event.ActionListener JavaDoc listener) {
261         if (listenerList == null ) {
262             listenerList = new javax.swing.event.EventListenerList JavaDoc();
263         }
264         listenerList.add(java.awt.event.ActionListener JavaDoc.class, listener);
265     }
266     
267     /** Removes ActionListener from the list of listeners.
268      * @param listener The listener to remove.
269      *
270      */

271     public synchronized void removeActionListener(java.awt.event.ActionListener JavaDoc listener) {
272         listenerList.remove(java.awt.event.ActionListener JavaDoc.class, listener);
273     }
274     
275     /** Notifies all registered listeners about the event.
276      *
277      * @param event The event to be fired
278      *
279      */

280     private void fireActionListenerActionPerformed(java.awt.event.ActionEvent JavaDoc event) {
281         if (listenerList == null) return;
282         Object JavaDoc[] listeners = listenerList.getListenerList();
283         for (int i = listeners.length-2; i>=0; i-=2) {
284             if (listeners[i]==java.awt.event.ActionListener JavaDoc.class) {
285                 ((java.awt.event.ActionListener JavaDoc)listeners[i+1]).actionPerformed(event);
286             }
287         }
288     }
289     
290     public boolean isInit() {
291         return init;
292     }
293     
294     public void setInit(boolean init) {
295         this.init = init;
296     }
297     
298 }
299
300
Popular Tags