001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * JNumberField.java
028: *
029: * Created on 10 febbraio 2003, 0.22
030: *
031: */
032:
033: package it.businesslogic.ireport.gui;
034:
035: import javax.swing.FocusManager;
036: import javax.swing.text.*;
037: import java.text.*;
038: import java.awt.event.*;
039: import it.businesslogic.ireport.gui.event.*;
040:
041: /**
042: *
043: * @author Administrator
044: */
045: public class JNumberField extends javax.swing.JTextField implements
046: java.awt.event.FocusListener, ValueChangedListener, KeyListener {
047:
048: /** Holds value of property decimals. */
049: private int decimals = 2;
050:
051: /** Holds value of property integer. */
052: private boolean integer = false;
053:
054: /** Holds value of property value. */
055: private double value = 0.0;
056: private double oldValue = 0.0;
057:
058: private java.text.NumberFormat numberFormat = null;
059:
060: /** Holds value of property grouping. */
061: private boolean grouping = true;
062:
063: /** Utility field used by event firing mechanism. */
064: private javax.swing.event.EventListenerList listenerList = null;
065:
066: private boolean init = false;
067:
068: private NumberDocument nd = new NumberDocument();
069:
070: /** Creates a new instance of JNumberField */
071: public JNumberField() {
072:
073: super ();
074: this .addFocusListener(this );
075: numberFormat = NumberFormat.getInstance();
076: numberFormat.setMaximumFractionDigits(2);
077: numberFormat.setMinimumFractionDigits(2);
078: numberFormat.setGroupingUsed(true);
079: this .setText(numberFormat.format(0));
080:
081: nd.addValueChangedListener(this );
082:
083: this .addKeyListener(this );
084:
085: this .setDocument(nd);
086:
087: }
088:
089: public void keyPressed(KeyEvent e) {
090: if (e.getKeyCode() == KeyEvent.VK_ENTER) {
091: FocusManager.getCurrentManager().clearGlobalFocusOwner();
092: e.consume();
093: }
094: }
095:
096: public void keyReleased(KeyEvent e) {
097: if (e.getKeyCode() == KeyEvent.VK_ENTER) {
098: FocusManager.getCurrentManager().clearGlobalFocusOwner();
099: e.consume();
100: }
101: }
102:
103: public void keyTyped(KeyEvent e) {
104: if (e.getKeyCode() == KeyEvent.VK_ENTER) {
105: FocusManager.getCurrentManager().clearGlobalFocusOwner();
106: e.consume();
107: }
108: }
109:
110: public void valueChanged(ValueChangedEvent evt) {
111:
112: if (isInit())
113: return;
114: value = evt.getNewValue();
115: //System.out.println("Changed value: " + value);
116: }
117:
118: public void textChanged(javax.swing.event.DocumentEvent evt) {
119:
120: }
121:
122: /** Creates the default implementation of the model
123: * to be used at construction if one isn't explicitly
124: * given. An instance of <code>PlainDocument</code> is returned.
125: *
126: * @return the default model implementation
127: *
128: */
129: protected Document createDefaultModel() {
130: //Document retValue;
131:
132: //retValue = super.createDefaultModel();
133: return new NumberDocument();
134: }
135:
136: /** Getter for property decimals.
137: * @return Value of property decimals.
138: *
139: */
140: public int getDecimals() {
141: return this .decimals;
142: }
143:
144: /** Setter for property decimals.
145: * @param decimals New value of property decimals.
146: *
147: * @throws PropertyVetoException
148: *
149: */
150: public void setDecimals(int decimals)
151: throws java.beans.PropertyVetoException {
152: this .decimals = decimals;
153: if (!isInteger()) {
154: numberFormat.setMaximumFractionDigits(decimals);
155: numberFormat.setMinimumFractionDigits(decimals);
156: }
157: this .setText(numberFormat.format(getValue()));
158: }
159:
160: /** Getter for property integer.
161: * @return Value of property integer.
162: *
163: */
164: public boolean isInteger() {
165: return this .integer;
166: }
167:
168: /** Setter for property integer.
169: * @param integer New value of property integer.
170: *
171: * @throws PropertyVetoException
172: *
173: */
174: public void setInteger(boolean integer)
175: throws java.beans.PropertyVetoException {
176: if (integer) {
177: numberFormat.setMaximumFractionDigits(0);
178: numberFormat.setMinimumFractionDigits(0);
179: } else {
180:
181: if (getDecimals() == -1) {
182: numberFormat.setMinimumFractionDigits(1);
183: numberFormat.setMaximumFractionDigits(100);
184: } else {
185: numberFormat.setMinimumFractionDigits(getDecimals());
186: numberFormat.setMaximumFractionDigits(getDecimals());
187: }
188: }
189: this .integer = integer;
190: this .setText(numberFormat.format(getValue()));
191: }
192:
193: /** Getter for property value.
194: * @return Value of property value.
195: *
196: */
197: public double getValue() {
198: return this .value;
199: }
200:
201: /** Setter for property value.
202: * @param value New value of property value.
203: *
204: * @throws PropertyVetoException
205: *
206: */
207: public void setValue(double value)
208: throws java.beans.PropertyVetoException {
209: this .setInit(true);
210: this .value = value;
211: this .oldValue = value;
212: nd.setValue(value);
213: this .setText(numberFormat.format(getValue()));
214: this .setInit(false);
215: }
216:
217: public void setValue(int value)
218: throws java.beans.PropertyVetoException {
219: setValue((double) value);
220: }
221:
222: public void setText(String text) {
223: this .setInit(true);
224: super .setText(text);
225: this .setInit(false);
226: }
227:
228: /** Invoked when a component gains the keyboard focus.
229: *
230: */
231: public void focusGained(FocusEvent e) {
232:
233: if (getText().length() == 0)
234: return;
235: this .oldValue = value;
236: numberFormat.setGroupingUsed(false);
237: setText(numberFormat.format(value));
238: numberFormat.setGroupingUsed(this .isGrouping());
239: selectAll();
240: }
241:
242: /** Invoked when a component loses the keyboard focus.
243: *
244: */
245: public void focusLost(FocusEvent e) {
246:
247: if (getText().length() == 0)
248: return;
249: setText(numberFormat.format(value));
250: if (oldValue != value)
251: this
252: .fireActionListenerActionPerformed(new java.awt.event.ActionEvent(
253: this , 0, ""));
254: }
255:
256: /** Getter for property grouping.
257: * @return Value of property grouping.
258: *
259: */
260: public boolean isGrouping() {
261: return this .grouping;
262: }
263:
264: /** Setter for property grouping.
265: * @param grouping New value of property grouping.
266: *
267: * @throws PropertyVetoException
268: *
269: */
270: public void setGrouping(boolean grouping)
271: throws java.beans.PropertyVetoException {
272: this .grouping = grouping;
273: }
274:
275: /** Registers ActionListener to receive events.
276: * @param listener The listener to register.
277: *
278: */
279: public synchronized void addActionListener(
280: java.awt.event.ActionListener listener) {
281: if (listenerList == null) {
282: listenerList = new javax.swing.event.EventListenerList();
283: }
284: listenerList.add(java.awt.event.ActionListener.class, listener);
285: }
286:
287: /** Removes ActionListener from the list of listeners.
288: * @param listener The listener to remove.
289: *
290: */
291: public synchronized void removeActionListener(
292: java.awt.event.ActionListener listener) {
293: listenerList.remove(java.awt.event.ActionListener.class,
294: listener);
295: }
296:
297: /** Notifies all registered listeners about the event.
298: *
299: * @param event The event to be fired
300: *
301: */
302: private void fireActionListenerActionPerformed(
303: java.awt.event.ActionEvent event) {
304: if (listenerList == null)
305: return;
306: Object[] listeners = listenerList.getListenerList();
307: for (int i = listeners.length - 2; i >= 0; i -= 2) {
308: if (listeners[i] == java.awt.event.ActionListener.class) {
309: ((java.awt.event.ActionListener) listeners[i + 1])
310: .actionPerformed(event);
311: }
312: }
313: }
314:
315: public boolean isInit() {
316: return init;
317: }
318:
319: public void setInit(boolean init) {
320: this.init = init;
321: }
322:
323: }
|