//Author: ^-^Veerle^-^ import java.awt.Color; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JComboBox; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JOptionPane; import javax.swing.BorderFactory; import javax.swing.DefaultComboBoxModel; import java.text.DecimalFormat; public class calculatorPanCurrency extends JPanel { private final Color clrBackground = new Color(75,141,221); private final Color clrForeground = new Color(255,255,255); private final DecimalFormat decFormat = new DecimalFormat("#0.00"); private JComboBox cboxSource, cboxDes; private JButton cmdConvert; private JLabel lblSource, lblDes; private JPanel panRoot, panGrid, panSouthArea, panConvert; private String[] strCurrencies; private float[] conversionsFrom, conversionsTo; public calculatorPanCurrency (ActionListener mainApp) { lblSource = new JLabel("Convert From", JLabel.CENTER); lblDes = new JLabel("Convert To", JLabel.CENTER); cboxSource = new JComboBox(); cboxDes = new JComboBox(); cmdConvert = new JButton("Convert"); cmdConvert.setActionCommand("currency"); cmdConvert.addActionListener(mainApp); //Parses the xml file and gets the currencies and conversion rates, returns false if an error occurs //Could had put this in the if statement without the boolean, but it might be confusing for others as it wouldn't be apprant about an error boolean isError = setupXML(); if (isError) { cboxSource.setEnabled(false); //stops the user from using the application cboxDes.setEnabled(false); cmdConvert.setEnabled(false); } else { cboxSource.setModel(getComboModel()); cboxDes.setModel(getComboModel()); if (strCurrencies.length > 1) //if more then one currency is used, set the second combo box to the next currency, instead of e.g. Pound to Pound { cboxDes.setSelectedIndex(1); } } panRoot = new JPanel(new BorderLayout()); panGrid = new JPanel(new GridLayout(3,2,3,3)); panSouthArea = new JPanel(new BorderLayout()); panConvert = new JPanel(); this.add(panRoot, BorderLayout.CENTER); panRoot.add(panGrid, BorderLayout.NORTH); panGrid.add(lblSource); panGrid.add(lblDes); panGrid.add(cboxSource); panGrid.add(cboxDes); panRoot.add(panSouthArea, BorderLayout.SOUTH); panSouthArea.add(panConvert, BorderLayout.NORTH); panConvert.add(cmdConvert); this.setBackground(clrBackground); panRoot.setBackground(clrBackground); panGrid.setBackground(clrBackground); panSouthArea.setBackground(clrBackground); panConvert.setBackground(clrBackground); panRoot.setBorder(BorderFactory.createEmptyBorder(3,3,3,3)); } private DefaultComboBoxModel getComboModel () { return new DefaultComboBoxModel(strCurrencies); } private boolean setupXML () { boolean isError = false; calcReadXML conversionFile = new calcReadXML(); try { conversionFile.parseConversionFile("conversions.xml"); } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage(), "conversions.xml syntax error", JOptionPane.ERROR_MESSAGE); if (!conversionFile.getSecondChance()) { isError = true; } } if (!isError) { strCurrencies = conversionFile.getCurrencies(); conversionsFrom = conversionFile.getConversionsFrom(); conversionsTo = conversionFile.getConversionsTo(); } return isError; } public String performConversion (float amount) { return "" + decFormat.format(((amount * conversionsFrom[cboxSource.getSelectedIndex()]) * conversionsTo[cboxDes.getSelectedIndex()])); } public void setLanguage (String strLanguage) { if (strLanguage.equalsIgnoreCase("Dutch")) { lblSource.setText("Omwisselen van"); lblDes.setText("Omwisselen tot"); cmdConvert.setText("Omwisselen"); cmdConvert.setToolTipText("Omwisselen van één tot andere"); } else if (strLanguage.equalsIgnoreCase("Spanish")) { lblSource.setText("Convierta de"); lblDes.setText("Convierta a"); cmdConvert.setText("Convierta"); cmdConvert.setToolTipText("Convierta de uno a otro"); } else { lblSource.setText("Convert from"); lblDes.setText("Convert to"); cmdConvert.setText("Convert"); cmdConvert.setToolTipText("Convert from one currency to the other"); } } }