KickJava   Java API By Example, From Geeks To Geeks.

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


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  * JNumberComboBox.java
28  *
29  * Created on 15 febbraio 2003, 10.31
30  *
31  */

32
33 package it.businesslogic.ireport.gui;
34 import it.businesslogic.ireport.gui.event.*;
35 import javax.swing.*;
36 import java.awt.event.*;
37 import java.util.*;
38 import java.text.*;
39 /**
40  * This class is designed for the zoom combo box.
41  * It stores a vector of predefined values (see the inner class NumberEntry),
42  * added from the program using addEntry(String name, double value) method.
43  * The user can insert manually a value. It is interpreted using NumberFormat.
44  * (max fraction digits used: 2)
45  * The value is valuated on focus lost....
46  *
47  * @author Giulio Toffoli
48  */

49 public class JNumberComboBox extends JComboBox implements KeyListener, ItemListener, FocusListener {
50     
51     private NumberFormat numberFormat = null;
52     private Vector entries;
53         
54     private double value;
55     
56     private double minValue;
57     
58     private double maxValue;
59     
60     private boolean setting = false;
61     
62     /** Utility field used by event firing mechanism. */
63     private javax.swing.event.EventListenerList JavaDoc listenerList = null;
64     
65     private String JavaDoc postfix="";
66     
67     /** Creates a new instance of JNumberComboBox */
68     public JNumberComboBox() {
69       
70         super();
71         entries = new Vector();
72        this.setFocusCycleRoot(false);
73        numberFormat = NumberFormat.getNumberInstance();
74        numberFormat.setMinimumFractionDigits(0);
75        numberFormat.setMaximumFractionDigits(2);
76        this.setEditable(true);
77        
78        // this code solve sun Metal Bug Id 4137675
79
// Attention, this.setEditable(true); must be call before!!!
80
for (int i=0; i<this.getComponentCount(); i++) {
81            //System.out.println("Register to: "+ this.getComponent(i).getClass());
82
this.getComponent(i).addFocusListener(this);
83         }
84
85        this.addFocusListener(this);
86        this.addKeyListener(this);
87        //this.addItemListener(this);
88
this.addActionListener(this);
89     }
90     
91     public void addEntry(String JavaDoc name, double value)
92     {
93         // If this entry name already exists, we change the value...
94
Enumeration e = entries.elements();
95         while (e.hasMoreElements())
96         {
97             NumberEntry ne = (NumberEntry)e.nextElement();
98             if (ne.name.equals(name))
99             {
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     {
111         if (isSetting()) return;
112         //System.out.println("actionPerformed Item: "+ this.getSelectedItem() );
113
super.actionPerformed(e);
114
115         if (this.getSelectedItem() != null)
116         {
117             Object JavaDoc obj = getSelectedItem();
118             if (obj instanceof NumberEntry)
119             {
120                 if (((NumberEntry)getSelectedItem()).value != value)
121                 {
122
123                     double oldValue = this.value;
124                     this.value = ((NumberEntry)getSelectedItem()).value;
125                     fireValueChangedListenerValueChanged(new ValueChangedEvent((JComponent)this, oldValue, this.value));
126
127                 }
128                 
129             }
130             else
131             {
132                 String JavaDoc s = ""+getSelectedItem();
133                 try {
134                     Number JavaDoc nb = numberFormat.parse(s);
135                     double oldValue = this.value;
136                     this.value = nb.doubleValue();
137                     fireValueChangedListenerValueChanged(new ValueChangedEvent((JComponent)this, oldValue, this.value));
138                 } catch (Exception JavaDoc ex)
139                 {
140                     
141                 }
142              }
143             this.setSetting(true);
144             this.setSelectedItem( numberFormat.format( value ) +getPostfix());
145             this.setSetting(false);
146         }
147     }
148     
149     /** Invoked when a key has been pressed.
150      * See the class description for {@link KeyEvent} for a definition of
151      * a key pressed event.
152      *
153      */

154     public void keyPressed(KeyEvent e) {
155         if (e.getKeyCode() == e.VK_ENTER)
156         {
157             e.consume();
158                FocusManager.getCurrentManager().focusNextComponent();
159                
160         }
161     }
162     
163     /** Invoked when a key has been released.
164      * See the class description for {@link KeyEvent} for a definition of
165      * a key released event.
166      *
167      */

168     public void keyReleased(KeyEvent e) {
169     }
170     
171     /** Invoked when a key has been typed.
172      * See the class description for {@link KeyEvent} for a definition of
173      * a key typed event.
174      *
175      */

176     public void keyTyped(KeyEvent e) {
177     }
178     
179     /** Invoked when a component gains the keyboard focus.
180      *
181      */

182     public void focusGained(FocusEvent e) {
183     }
184     
185     /** Invoked when a component loses the keyboard focus.
186      *
187      */

188     public void focusLost(FocusEvent e) {
189        acceptResultNow();
190     }
191     
192     public void acceptResultNow()
193     {
194          try {
195             
196                 
197                 Number JavaDoc num = numberFormat.parse( ""+ this.getSelectedItem());
198            
199                  if (num.doubleValue() != value)
200                 {
201                     double oldValue = this.value;
202                     this.value = num.doubleValue();
203                     fireValueChangedListenerValueChanged(new ValueChangedEvent(this, oldValue, this.value));
204                 }
205             } catch (Exception JavaDoc ex)
206             {
207                 //System.out.println(ex.getMessage());
208
fireValueChangedListenerValueChanged(new ValueChangedEvent(this, this.value, this.value));
209             }
210             boolean setting = isSetting();
211             this.setSetting(true);
212             this.setSelectedItem( numberFormat.format( value )+getPostfix() );
213             this.setSetting(setting);
214     }
215     
216     /** Getter for property minValue.
217      * @return Value of property minValue.
218      *
219      */

220     public double getMinValue() {
221         return minValue;
222     }
223     
224     /** Setter for property minValue.
225      * @param minValue New value of property minValue.
226      *
227      */

228     public void setMinValue(double minValue) {
229         this.minValue = minValue;
230     }
231     
232     /** Getter for property maxValue.
233      * @return Value of property maxValue.
234      *
235      */

236     public double getMaxValue() {
237         return maxValue;
238     }
239     
240     /** Setter for property maxValue.
241      * @param maxValue New value of property maxValue.
242      *
243      */

244     public void setMaxValue(double maxValue) {
245         this.maxValue = maxValue;
246     }
247     
248     /** Getter for property value.
249      * @return Value of property value.
250      *
251      */

252     public double getValue() {
253         return value;
254     }
255     
256     /** Setter for property value.
257      * @param value New value of property value.
258      *
259      */

260     public void setValue(double value) {
261         if (this.value != value)
262         {
263             double oldValue = this.value;
264             this.value = value;
265             fireValueChangedListenerValueChanged(new ValueChangedEvent(this, oldValue, this.value));
266         }
267         boolean setting = isSetting();
268         this.setSetting(true);
269         this.setSelectedItem( numberFormat.format( this.value )+getPostfix() );
270         this.setSetting(setting);
271     }
272     
273     /** Registers ValueChangedListener to receive events.
274      * @param listener The listener to register.
275      *
276      */

277     public synchronized void addValueChangedListener(it.businesslogic.ireport.gui.event.ValueChangedListener listener) {
278         if (listenerList == null ) {
279             listenerList = new javax.swing.event.EventListenerList JavaDoc();
280         }
281         listenerList.add(it.businesslogic.ireport.gui.event.ValueChangedListener.class, 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(it.businesslogic.ireport.gui.event.ValueChangedListener listener) {
289         listenerList.remove(it.businesslogic.ireport.gui.event.ValueChangedListener.class, listener);
290     }
291     
292     /** Notifies all registered listeners about the event.
293      *
294      * @param event The event to be fired
295      *
296      */

297     private void fireValueChangedListenerValueChanged(it.businesslogic.ireport.gui.event.ValueChangedEvent event) {
298         if (listenerList == null) return;
299         Object JavaDoc[] listeners = listenerList.getListenerList();
300         for (int i = listeners.length-2; i>=0; i-=2) {
301             if (listeners[i]==it.businesslogic.ireport.gui.event.ValueChangedListener.class) {
302                 ((it.businesslogic.ireport.gui.event.ValueChangedListener)listeners[i+1]).valueChanged(event);
303             }
304         }
305     }
306     
307     /** Getter for property postfix.
308      * @return Value of property postfix.
309      *
310      */

311     public java.lang.String JavaDoc getPostfix() {
312         return postfix;
313     }
314     
315     /** Setter for property postfix.
316      * @param postfix New value of property postfix.
317      *
318      */

319     public void setPostfix(java.lang.String JavaDoc postfix) {
320         this.postfix = postfix;
321     }
322
323     public boolean isSetting() {
324         return setting;
325     }
326
327     public void setSetting(boolean setting) {
328         this.setting = setting;
329     }
330
331     public void itemStateChanged(ItemEvent e) {
332         
333         if (isSetting()) return;
334         if (e.getStateChange() == e.SELECTED)
335         {
336             actionPerformed(null);
337         }
338     }
339     
340 }
341
342
Popular Tags