KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > CurrencyEditor


1 /*
2   Copyright (C) 2001 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.aspects.gui.swing;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.awt.event.KeyEvent JavaDoc;
24 import java.awt.event.KeyListener JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import javax.swing.JComboBox JavaDoc;
27 import javax.swing.JPanel JavaDoc;
28 import org.objectweb.jac.aspects.gui.Currency;
29 import org.objectweb.jac.aspects.gui.GuiAC;
30 import org.objectweb.jac.core.rtti.FieldItem;
31
32 /**
33  * A Swing editor component for fields values (primitive types).
34  */

35
36 public class CurrencyEditor extends TextFieldEditor
37     implements ActionListener JavaDoc, KeyListener JavaDoc
38 {
39     JComboBox JavaDoc currencyBox;
40     String JavaDoc defaultCurrency;
41     String JavaDoc selectedCurrency;
42
43     double realValue;
44     boolean invalide = false;
45
46     /**
47      * Constructs a new primitive field editor. */

48
49     public CurrencyEditor(Object JavaDoc substance, FieldItem field) {
50         super(substance,field);
51         textField = new JTextField(20);
52         add(textField);
53         currencyBox = new JComboBox JavaDoc();
54       
55         Enumeration JavaDoc currencies = GuiAC.getCurrencies();
56         defaultCurrency = GuiAC.getDefaultCurrency();
57         while (currencies.hasMoreElements()) {
58             String JavaDoc currency = (String JavaDoc)currencies.nextElement();
59             currencyBox.addItem(currency);
60         }
61         currencyBox.setSelectedItem(defaultCurrency);
62         currencyBox.addActionListener(this);
63         textField.addActionListener(this);
64         textField.addKeyListener(this);
65         selectedCurrency = defaultCurrency;
66         add(currencyBox);
67     }
68
69     public void actionPerformed(ActionEvent JavaDoc e) {
70         selectCurrency((String JavaDoc)currencyBox.getSelectedItem());
71     }
72
73     public void selectCurrency(String JavaDoc currency) {
74         if (!textField.getText().trim().equals("")) {
75             Currency c1 = GuiAC.getCurrency(currency);
76             Currency c2 = GuiAC.getCurrency(selectedCurrency);
77             setRealValue(getRealValue()*c1.getRate()/c2.getRate());
78             String JavaDoc newValue = new Double JavaDoc(getRealValue()).toString();
79             int dot = newValue.indexOf(".");
80             if (dot!=-1 && dot+1+c1.getPrecision() <= newValue.length()) {
81                 textField.setText(newValue.substring(0,dot+1+c1.getPrecision()));
82             } else {
83                 textField.setText(newValue);
84             }
85         }
86         selectedCurrency = currency;
87     }
88
89     public void keyTyped(KeyEvent JavaDoc e) {
90         invalide = true;
91     }
92
93     public void keyPressed(KeyEvent JavaDoc e) {}
94     public void keyReleased(KeyEvent JavaDoc e) {}
95
96
97     protected void setRealValue(double value) {
98         realValue = value;
99         invalide = false;
100     }
101
102     protected double getRealValue() {
103         if (invalide) {
104             realValue = Double.parseDouble(textField.getText());
105         }
106         return realValue;
107     }
108    
109     /**
110      * Gets the value of the edited field.
111      *
112      * @return an object for the value */

113  
114     public Object JavaDoc getValue() {
115         selectCurrency(defaultCurrency);
116         Class JavaDoc cl = type.getActualClass();
117         if ( cl == int.class || cl == Integer JavaDoc.class ) {
118             return( new Integer JavaDoc (textField.getText()) );
119         } else if ( cl == long.class || cl == Long JavaDoc.class ) {
120             return( new Long JavaDoc (textField.getText()) );
121         } else if ( cl == float.class || cl == Float JavaDoc.class ) {
122             return( new Float JavaDoc (textField.getText()) );
123         } else if ( cl == double.class || cl == Double JavaDoc.class ) {
124             return( new Double JavaDoc (textField.getText()) );
125         } else if ( cl == short.class || cl == Short JavaDoc.class ) {
126             return( new Short JavaDoc (textField.getText()) );
127         } else if ( cl == byte.class || cl == Byte JavaDoc.class ) {
128             return( new Byte JavaDoc (textField.getText()) );
129         } else {
130             throw new RuntimeException JavaDoc("Unhandled type "+type.getName());
131         }
132     }
133
134     /**
135      * Sets the value of the edited field.
136      *
137      * @param value a value
138      */

139     public void setValue(Object JavaDoc value) {
140         textField.setText(value.toString());
141         setRealValue(Double.parseDouble(value.toString()));
142     }
143
144     public void onClose() {}
145 }
146
Popular Tags