1 18 19 package org.objectweb.jac.aspects.gui.swing; 20 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 import java.awt.event.KeyEvent ; 24 import java.awt.event.KeyListener ; 25 import java.util.Enumeration ; 26 import javax.swing.JComboBox ; 27 import javax.swing.JPanel ; 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 35 36 public class CurrencyEditor extends TextFieldEditor 37 implements ActionListener , KeyListener 38 { 39 JComboBox currencyBox; 40 String defaultCurrency; 41 String selectedCurrency; 42 43 double realValue; 44 boolean invalide = false; 45 46 48 49 public CurrencyEditor(Object substance, FieldItem field) { 50 super(substance,field); 51 textField = new JTextField(20); 52 add(textField); 53 currencyBox = new JComboBox (); 54 55 Enumeration currencies = GuiAC.getCurrencies(); 56 defaultCurrency = GuiAC.getDefaultCurrency(); 57 while (currencies.hasMoreElements()) { 58 String currency = (String )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 e) { 70 selectCurrency((String )currencyBox.getSelectedItem()); 71 } 72 73 public void selectCurrency(String 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 newValue = new Double (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 e) { 90 invalide = true; 91 } 92 93 public void keyPressed(KeyEvent e) {} 94 public void keyReleased(KeyEvent 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 113 114 public Object getValue() { 115 selectCurrency(defaultCurrency); 116 Class cl = type.getActualClass(); 117 if ( cl == int.class || cl == Integer .class ) { 118 return( new Integer (textField.getText()) ); 119 } else if ( cl == long.class || cl == Long .class ) { 120 return( new Long (textField.getText()) ); 121 } else if ( cl == float.class || cl == Float .class ) { 122 return( new Float (textField.getText()) ); 123 } else if ( cl == double.class || cl == Double .class ) { 124 return( new Double (textField.getText()) ); 125 } else if ( cl == short.class || cl == Short .class ) { 126 return( new Short (textField.getText()) ); 127 } else if ( cl == byte.class || cl == Byte .class ) { 128 return( new Byte (textField.getText()) ); 129 } else { 130 throw new RuntimeException ("Unhandled type "+type.getName()); 131 } 132 } 133 134 139 public void setValue(Object value) { 140 textField.setText(value.toString()); 141 setRealValue(Double.parseDouble(value.toString())); 142 } 143 144 public void onClose() {} 145 } 146 | Popular Tags |