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: * JNumberComboBox.java
028: *
029: * Created on 15 febbraio 2003, 10.31
030: *
031: */
032:
033: package it.businesslogic.ireport.gui;
034:
035: import it.businesslogic.ireport.gui.event.*;
036: import javax.swing.*;
037: import java.awt.event.*;
038: import java.util.*;
039: import java.text.*;
040:
041: /**
042: * This class is designed for the zoom combo box.
043: * It stores a vector of predefined values (see the inner class NumberEntry),
044: * added from the program using addEntry(String name, double value) method.
045: * The user can insert manually a value. It is interpreted using NumberFormat.
046: * (max fraction digits used: 2)
047: * The value is valuated on focus lost....
048: *
049: * @author Giulio Toffoli
050: */
051: public class JNumberComboBox extends JComboBox implements KeyListener,
052: ItemListener, FocusListener {
053:
054: private NumberFormat numberFormat = null;
055: private Vector entries;
056:
057: private double value;
058:
059: private double minValue;
060:
061: private double maxValue;
062:
063: private boolean setting = false;
064:
065: /** Utility field used by event firing mechanism. */
066: private javax.swing.event.EventListenerList listenerList = null;
067:
068: private String postfix = "";
069:
070: /** Creates a new instance of JNumberComboBox */
071: public JNumberComboBox() {
072:
073: super ();
074: entries = new Vector();
075: this .setFocusCycleRoot(false);
076: numberFormat = NumberFormat.getNumberInstance();
077: numberFormat.setMinimumFractionDigits(0);
078: numberFormat.setMaximumFractionDigits(2);
079: this .setEditable(true);
080:
081: // this code solve sun Metal Bug Id 4137675
082: // Attention, this.setEditable(true); must be call before!!!
083: for (int i = 0; i < this .getComponentCount(); i++) {
084: //System.out.println("Register to: "+ this.getComponent(i).getClass());
085: this .getComponent(i).addFocusListener(this );
086: }
087:
088: this .addFocusListener(this );
089: this .addKeyListener(this );
090: //this.addItemListener(this);
091: this .addActionListener(this );
092: }
093:
094: public void addEntry(String name, double value) {
095: // If this entry name already exists, we change the value...
096: Enumeration e = entries.elements();
097: while (e.hasMoreElements()) {
098: NumberEntry ne = (NumberEntry) e.nextElement();
099: if (ne.name.equals(name)) {
100: ne.value = value;
101: return;
102: }
103: }
104: NumberEntry entry = new NumberEntry(name, value);
105: this .addItem(entry);
106: this .entries.addElement(entry);
107: }
108:
109: public void actionPerformed(ActionEvent e) {
110: if (isSetting())
111: return;
112: //System.out.println("actionPerformed Item: "+ this.getSelectedItem() );
113: super .actionPerformed(e);
114:
115: if (this .getSelectedItem() != null) {
116: Object obj = getSelectedItem();
117: if (obj instanceof NumberEntry) {
118: if (((NumberEntry) getSelectedItem()).value != value) {
119:
120: double oldValue = this .value;
121: this .value = ((NumberEntry) getSelectedItem()).value;
122: fireValueChangedListenerValueChanged(new ValueChangedEvent(
123: (JComponent) this , oldValue, this .value));
124:
125: }
126:
127: } else {
128: String s = "" + getSelectedItem();
129: try {
130: Number nb = numberFormat.parse(s);
131: double oldValue = this .value;
132: this .value = nb.doubleValue();
133: fireValueChangedListenerValueChanged(new ValueChangedEvent(
134: (JComponent) this , oldValue, this .value));
135: } catch (Exception ex) {
136:
137: }
138: }
139: this .setSetting(true);
140: this .setSelectedItem(numberFormat.format(value)
141: + getPostfix());
142: this .setSetting(false);
143: }
144: }
145:
146: /** Invoked when a key has been pressed.
147: * See the class description for {@link KeyEvent} for a definition of
148: * a key pressed event.
149: *
150: */
151: public void keyPressed(KeyEvent e) {
152: if (e.getKeyCode() == e.VK_ENTER) {
153: e.consume();
154: FocusManager.getCurrentManager().focusNextComponent();
155:
156: }
157: }
158:
159: /** Invoked when a key has been released.
160: * See the class description for {@link KeyEvent} for a definition of
161: * a key released event.
162: *
163: */
164: public void keyReleased(KeyEvent e) {
165: }
166:
167: /** Invoked when a key has been typed.
168: * See the class description for {@link KeyEvent} for a definition of
169: * a key typed event.
170: *
171: */
172: public void keyTyped(KeyEvent e) {
173: }
174:
175: /** Invoked when a component gains the keyboard focus.
176: *
177: */
178: public void focusGained(FocusEvent e) {
179: }
180:
181: /** Invoked when a component loses the keyboard focus.
182: *
183: */
184: public void focusLost(FocusEvent e) {
185: acceptResultNow();
186: }
187:
188: public void acceptResultNow() {
189: try {
190:
191: Number num = numberFormat
192: .parse("" + this .getSelectedItem());
193:
194: if (num.doubleValue() != value) {
195: double oldValue = this .value;
196: this .value = num.doubleValue();
197: fireValueChangedListenerValueChanged(new ValueChangedEvent(
198: this , oldValue, this .value));
199: }
200: } catch (Exception ex) {
201: //System.out.println(ex.getMessage());
202: fireValueChangedListenerValueChanged(new ValueChangedEvent(
203: this , this .value, this .value));
204: }
205: boolean setting = isSetting();
206: this .setSetting(true);
207: this .setSelectedItem(numberFormat.format(value) + getPostfix());
208: this .setSetting(setting);
209: }
210:
211: /** Getter for property minValue.
212: * @return Value of property minValue.
213: *
214: */
215: public double getMinValue() {
216: return minValue;
217: }
218:
219: /** Setter for property minValue.
220: * @param minValue New value of property minValue.
221: *
222: */
223: public void setMinValue(double minValue) {
224: this .minValue = minValue;
225: }
226:
227: /** Getter for property maxValue.
228: * @return Value of property maxValue.
229: *
230: */
231: public double getMaxValue() {
232: return maxValue;
233: }
234:
235: /** Setter for property maxValue.
236: * @param maxValue New value of property maxValue.
237: *
238: */
239: public void setMaxValue(double maxValue) {
240: this .maxValue = maxValue;
241: }
242:
243: /** Getter for property value.
244: * @return Value of property value.
245: *
246: */
247: public double getValue() {
248: return value;
249: }
250:
251: /** Setter for property value.
252: * @param value New value of property value.
253: *
254: */
255: public void setValue(double value) {
256: if (this .value != value) {
257: double oldValue = this .value;
258: this .value = value;
259: fireValueChangedListenerValueChanged(new ValueChangedEvent(
260: this , oldValue, this .value));
261: }
262: boolean setting = isSetting();
263: this .setSetting(true);
264: this .setSelectedItem(numberFormat.format(this .value)
265: + getPostfix());
266: this .setSetting(setting);
267: }
268:
269: /** Registers ValueChangedListener to receive events.
270: * @param listener The listener to register.
271: *
272: */
273: public synchronized void addValueChangedListener(
274: it.businesslogic.ireport.gui.event.ValueChangedListener listener) {
275: if (listenerList == null) {
276: listenerList = new javax.swing.event.EventListenerList();
277: }
278: listenerList
279: .add(
280: it.businesslogic.ireport.gui.event.ValueChangedListener.class,
281: listener);
282: }
283:
284: /** Removes ValueChangedListener from the list of listeners.
285: * @param listener The listener to remove.
286: *
287: */
288: public synchronized void removeValueChangedListener(
289: it.businesslogic.ireport.gui.event.ValueChangedListener listener) {
290: listenerList
291: .remove(
292: it.businesslogic.ireport.gui.event.ValueChangedListener.class,
293: listener);
294: }
295:
296: /** Notifies all registered listeners about the event.
297: *
298: * @param event The event to be fired
299: *
300: */
301: private void fireValueChangedListenerValueChanged(
302: it.businesslogic.ireport.gui.event.ValueChangedEvent event) {
303: if (listenerList == null)
304: return;
305: Object[] listeners = listenerList.getListenerList();
306: for (int i = listeners.length - 2; i >= 0; i -= 2) {
307: if (listeners[i] == it.businesslogic.ireport.gui.event.ValueChangedListener.class) {
308: ((it.businesslogic.ireport.gui.event.ValueChangedListener) listeners[i + 1])
309: .valueChanged(event);
310: }
311: }
312: }
313:
314: /** Getter for property postfix.
315: * @return Value of property postfix.
316: *
317: */
318: public java.lang.String getPostfix() {
319: return postfix;
320: }
321:
322: /** Setter for property postfix.
323: * @param postfix New value of property postfix.
324: *
325: */
326: public void setPostfix(java.lang.String postfix) {
327: this .postfix = postfix;
328: }
329:
330: public boolean isSetting() {
331: return setting;
332: }
333:
334: public void setSetting(boolean setting) {
335: this .setting = setting;
336: }
337:
338: public void itemStateChanged(ItemEvent e) {
339:
340: if (isSetting())
341: return;
342: if (e.getStateChange() == e.SELECTED) {
343: actionPerformed(null);
344: }
345: }
346:
347: }
|