001    /*
002      Copyright (C) 2001 Laurent Martelli <laurent@aopsys.com>
003      
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful, but
010      WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012      Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.aspects.gui.swing;
020    
021    import org.objectweb.jac.aspects.gui.Currency;
022    import org.objectweb.jac.aspects.gui.GuiAC;
023    
024    import java.util.Enumeration;
025    
026    import java.awt.event.ActionEvent;
027    import java.awt.event.ActionListener;
028    import java.awt.event.KeyEvent;
029    import java.awt.event.KeyListener;
030    import javax.swing.JComboBox;
031    import javax.swing.JPanel;
032    import javax.swing.JTextField;
033    
034    /**
035     * A Swing editor component for fields values (primitive types).
036     */
037    
038    public class CurrencyEditor extends JPanel 
039        implements /* ValueEditor,*/ ActionListener, KeyListener
040    {   
041        Class type;
042        JTextField textField;
043        JComboBox currencyBox;
044        String defaultCurrency;
045        String selectedCurrency;
046    
047        double realValue;
048        boolean invalide = false;
049    
050        /**
051         * Constructs a new primitive field editor. */
052    
053        public CurrencyEditor(Class type) {
054            this.type = type;
055            textField = new JTextField(20);
056            add(textField);
057            currencyBox = new JComboBox();
058          
059            Enumeration currencies = GuiAC.getCurrencies();
060            defaultCurrency = GuiAC.getDefaultCurrency();
061            while (currencies.hasMoreElements()) {
062                String currency = (String)currencies.nextElement();
063                currencyBox.addItem(currency);
064            }
065            currencyBox.setSelectedItem(defaultCurrency);
066            currencyBox.addActionListener(this);
067            textField.addActionListener(this);
068            textField.addKeyListener(this);
069            selectedCurrency = defaultCurrency;
070            add(currencyBox);
071        }
072    
073        public void actionPerformed(ActionEvent e) {
074            selectCurrency((String)currencyBox.getSelectedItem());
075        }
076    
077        public void selectCurrency(String currency) {
078            if (!textField.getText().trim().equals("")) {
079                Currency c1 = GuiAC.getCurrency(currency);
080                Currency c2 = GuiAC.getCurrency(selectedCurrency);
081                setRealValue(getRealValue()*c1.getRate()/c2.getRate());
082                String newValue = new Double(getRealValue()).toString();
083                int dot = newValue.indexOf(".");
084                if (dot!=-1 && dot+1+c1.getPrecision() <= newValue.length()) {
085                    textField.setText(newValue.substring(0,dot+1+c1.getPrecision()));
086                } else {
087                    textField.setText(newValue);
088                }
089            }
090            selectedCurrency = currency;
091        }
092    
093        public void keyTyped(KeyEvent e) {
094            invalide = true;
095        }
096    
097        public void keyPressed(KeyEvent e) {}
098        public void keyReleased(KeyEvent e) {}
099    
100    
101        protected void setRealValue(double value) {
102            realValue = value;
103            invalide = false;
104        }
105    
106        protected double getRealValue() {
107            if (invalide) {
108                realValue = Double.parseDouble(textField.getText());
109            }
110            return realValue;
111        }
112       
113        /**
114         * Gets the value of the edited field.
115         *
116         * @return an object for the value */
117     
118        public Object getValue() {
119            selectCurrency(defaultCurrency);
120            if ( type == int.class || type == Integer.class ) {
121                return( new Integer (textField.getText()) );
122            } else if ( type == long.class || type == Long.class ) {
123                return( new Long (textField.getText()) );
124            } else if ( type == float.class || type == Float.class ) {
125                return( new Float (textField.getText()) );
126            } else if ( type == double.class || type == Double.class ) {
127                return( new Double (textField.getText()) );
128            } else if ( type == short.class || type == Short.class ) {
129                return( new Short (textField.getText()) );
130            } else if ( type == byte.class || type == Byte.class ) {
131                return( new Byte (textField.getText()) );
132            } else {
133                throw new RuntimeException("Unhandled type "+type.getName());
134            }         
135        }
136    
137        /**
138         * Sets the value of the edited field.
139         *
140         * @param value a value */
141    
142        public void setValue(Object value) {
143            textField.setText(value.toString());
144            setRealValue(Double.parseDouble(value.toString()));
145        }
146    
147        /**
148         * Set the focus on the textField
149         */
150        public void requestFocus() {
151            textField.requestFocus();
152        }
153    
154        public void setWidth(int width) {
155            textField.setColumns(width);
156        }
157    
158        public void setHeight(int height) {}
159    
160        public void onClose() {}
161    }