KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > sheet > NumberComboBoxSheetProperty


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  * NumberComboBoxSheetProperty.java
28  *
29  * Created on 16 febbraio 2005, 19.13
30  *
31  */

32
33 package it.businesslogic.ireport.gui.sheet;
34
35 import it.businesslogic.ireport.gui.JNumberComboBox;
36 import it.businesslogic.ireport.gui.event.SheetPropertyValueChangedEvent;
37 import it.businesslogic.ireport.gui.event.ValueChangedEvent;
38 import it.businesslogic.ireport.gui.event.ValueChangedListener;
39 import java.util.*;
40 import javax.swing.*;
41 import it.businesslogic.ireport.util.*;
42
43 public class NumberComboBoxSheetProperty extends SheetProperty
44 {
45     
46     JNumberComboBox jNumberComboBox=null;
47     
48     public NumberComboBoxSheetProperty(String JavaDoc key, String JavaDoc name) {
49         super( key, name, SheetProperty.COMBOBOX);
50     }
51     
52     public Object JavaDoc getEditorValue(JComponent component)
53     {
54         return ""+(int)jNumberComboBox.getValue();
55     }
56     
57     public void setValue(Object JavaDoc value)
58     {
59         //if (this.value == value || (this.value+"").equals(value+"")) return;
60

61         this.value = value;
62         this.setEditorValue(getEditor(), (value == null) ? getDefaultValue() : value);
63         updateLabel();
64     }
65     
66     public void setEditorValue(JComponent component, Object JavaDoc str)
67     {
68         if (str == null || str.equals(""))
69         {
70             str = getDefaultValue();
71         }
72         if (str == null) str = "";
73    
74         boolean setting = isSetting();
75         this.setSetting(true);
76         try {
77             ((JComboBox)component).setSelectedItem(str);
78         } catch (Exception JavaDoc ex) {
79             ex.printStackTrace();
80         }
81         this.setSetting(setting);
82     }
83     
84     public void addEntry(String JavaDoc name, int value)
85     {
86         ((JNumberComboBox)getEditor()).addEntry(name, (double)value);
87     }
88     
89     public JComponent getEditor()
90     {
91         if (jNumberComboBox != null) return jNumberComboBox;
92         
93         jNumberComboBox = new JNumberComboBox();
94         jNumberComboBox.setEditable(true);
95
96         /*
97         for (int i=0; i<this.getTags().size(); ++i)
98         {
99             Tag t = (Tag)getTags().elementAt(i);
100             jNumberComboBox.addEntry( t.getName(), Double.parseDouble( t.getValue()+""));
101         }
102         */

103         
104         jNumberComboBox.setBorder(null);
105         
106         /*
107         ((JComboBox)jNumberComboBox).setEditor(new SimpleComboBoxEditor() );
108         for (int i=0; i< jNumberComboBox.getComponentCount(); ++i)
109         {
110                 try {
111                         Object obj = jNumberComboBox.getComponent(i);
112                         //System.out.println( obj );
113                         if (obj != null && obj.getClass().getMethod("setBorder", new Class[]{javax.swing.border.Border.class}) != null)
114                         {
115                             java.lang.reflect.Method mtd = obj.getClass().getMethod("setBorder", new Class[]{javax.swing.border.Border.class});
116                             mtd.invoke(obj, new Object[]{null});
117                         }
118                 } catch (Exception ex) { }
119             }
120        
121         */

122        jNumberComboBox.addItemListener(new java.awt.event.ItemListener JavaDoc() {
123             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
124                 
125                 if (evt.getStateChange() == evt.SELECTED)
126                 {
127                     actionPerformed(null);
128                 }
129             }
130         });
131         jNumberComboBox.addActionListener(this);
132         
133         return jNumberComboBox;
134     }
135     
136      public void actionPerformed(java.awt.event.ActionEvent JavaDoc event)
137      {
138         if (isSetting()) return;
139         
140         //System.out.println("Value changed!" + evt.getOldValue() + " --> " + evt.getNewValue());
141

142
143         Object JavaDoc new_value = getEditorValue( this.getEditor() );
144         if (new_value != null && new_value.equals(value)) return;
145         if (new_value != null)
146         {
147             try {
148                 Double.parseDouble(new_value+"");
149             } catch (Exception JavaDoc ex)
150             {
151                 return;
152             }
153         }
154         
155         Object JavaDoc oldValue = value;
156         value = new_value;
157         //System.out.println(this.getName() + ": " + getValue());
158
if(oldValue == null || value == null)
159         {
160             updateLabel();
161         }
162
163         fireSheetPropertyValueChangedListenerSheetPropertyValueChanged(
164             new SheetPropertyValueChangedEvent(getKeyName(),oldValue,value, this));
165      }
166      
167      public void updateValues(Vector values, boolean addNullEntry){
168      
169          
170         Misc.updateComboBox( (JComboBox)getEditor(), values, addNullEntry);
171      
172      }
173
174 }
175
Popular Tags