/**
 *
 * Class CurrencyDialog implements a method that shows 
 * the currencies in British pounds, Canadian 
 * dollars, Euros, Australian dollars and US 
 * dollars.
 *
 * @author  Daniel Burgner
 * @version 1.0
 * @class   CS5730
 * @date    03.04.2002
 *
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*; 
import java.lang.*;
import java.text.*;

public class CurrencyDialog extends JDialog 
	implements ActionListener, MouseListener {

	/* Conversion and CurrFactory related variables */
	protected static int MAX_CONV = 5;
	protected Conversion[] cConvArr = new Conversion[MAX_CONV];
	protected CurrFactory cfFactory;

	/* Dialog Panel, panel for buttons and exit button */
	CurrDialogPanel cdpCurrDialogPanel;
	JPanel jpButtons;
	JButton jbExit;

	/* button and textfield handler related variables */
	protected String strCurr;
	protected String strAmount;
	protected int nTFPos;
	protected double[] dResult = new double[MAX_CONV];
	protected double dAmount;
	protected DecimalFormat dfFormat = new DecimalFormat("#,##0.00");

	/**
	 *
	 * CurrencyDialog is the constructor method for the
	 * CurrencyDialog class.  It takes a Currency as a 
	 * parameter.  It sets up the text boxes and instructions and 
	 * initializes the text boxes to 0.00.  When the user mouses
	 * over, change text color in the text box.  When the user is
	 * done, the method does the conversions and show the result
	 * of the conversions.
	 * @param  currOwner - Currency - a Currency object
	 * @return none
	 *
	 */
	public CurrencyDialog(Currency currOwner) {

		/* sets the title of the application */
		setTitle("Currency Conversion Application");

		/* initializes the button */
		jbExit = new JButton("Exit");
		jbExit.addActionListener(this);

		/* sets up the button panel */
		jpButtons = new JPanel();
		jpButtons.add(jbExit);
		jpButtons.setBorder(BorderFactory.createEtchedBorder());

		/* sets up the dialog panel */
		cdpCurrDialogPanel = new CurrDialogPanel();
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(jpButtons, BorderLayout.SOUTH);
		getContentPane().add(cdpCurrDialogPanel, BorderLayout.CENTER);
		cdpCurrDialogPanel.initializeTF();
		cdpCurrDialogPanel.jtfUSD.addActionListener(this);
		cdpCurrDialogPanel.jtfGBP.addActionListener(this);
		cdpCurrDialogPanel.jtfCAD.addActionListener(this);
		cdpCurrDialogPanel.jtfEUR.addActionListener(this);
		cdpCurrDialogPanel.jtfAUD.addActionListener(this);
		cdpCurrDialogPanel.jtfUSD.addMouseListener(this);
		cdpCurrDialogPanel.jtfGBP.addMouseListener(this);
		cdpCurrDialogPanel.jtfCAD.addMouseListener(this);
		cdpCurrDialogPanel.jtfEUR.addMouseListener(this);
		cdpCurrDialogPanel.jtfAUD.addMouseListener(this);
		pack();

		/* initializes the cfFactory to a new CurrConvFactory */
		cfFactory = new CurrConvFactory(currOwner);
	}

	/**
	 *
	 * actionPerformed is the method for the text boxes and
	 * exit button.  If a user modifies the contents of a 
	 * text box and presses enter, the conversion array will 
	 * receive an array of Conversion from the cfFactory's 
	 * makeConversion method.  The method allows for exception 
	 * handling for invalid number format and null pointer.
	 * The text boxes in the dialog panel will receive the 
	 * result of the conversions.  Should the user click on the
	 * Exit button, the application shuts down.
	 * @param  aeEvent - ActionEvent - action performed
	 * @return none
	 *
	 */
	public void actionPerformed(ActionEvent aeEvent) {
		if (aeEvent.getSource() == jbExit) {
			System.out.println("Shutting down.");
			System.exit(0);
		} else {
			
			try {
				if (aeEvent.getSource() == cdpCurrDialogPanel.jtfUSD) {
					strCurr = "USD";
					nTFPos = 0;
					strAmount = cdpCurrDialogPanel.jtfUSD.getText();
				}
				if (aeEvent.getSource() == cdpCurrDialogPanel.jtfGBP) {
					strCurr = "GBP";
					nTFPos = 1;
					strAmount = cdpCurrDialogPanel.jtfGBP.getText();
				}
				if (aeEvent.getSource() == cdpCurrDialogPanel.jtfCAD) {
					strCurr = "CAD";
					nTFPos = 2;
					strAmount = cdpCurrDialogPanel.jtfCAD.getText();
				}
				if (aeEvent.getSource() == cdpCurrDialogPanel.jtfEUR) {
					strCurr = "EUR";
					nTFPos = 3;
					strAmount = cdpCurrDialogPanel.jtfEUR.getText();
				}
				if (aeEvent.getSource() == cdpCurrDialogPanel.jtfAUD) {
					strCurr = "AUD";
					nTFPos = 4;
					strAmount = cdpCurrDialogPanel.jtfAUD.getText();
				}
				dAmount = Double.valueOf(strAmount).doubleValue();
				cConvArr = cfFactory.makeConversion(strCurr);
				for (int nI = 0; nI <= nTFPos; nI++) {
					dResult[nI] = cConvArr[nI].result(dAmount, false);
				}
				for (int nI = nTFPos; nI < MAX_CONV; nI++) {
					dResult[nI] = cConvArr[nI].result(dAmount, true);
				}
				cdpCurrDialogPanel.jtfUSD.setText(dfFormat.format(dResult[0]));
 				cdpCurrDialogPanel.jtfGBP.setText(dfFormat.format(dResult[1]));
 				cdpCurrDialogPanel.jtfCAD.setText(dfFormat.format(dResult[2]));
	 			cdpCurrDialogPanel.jtfEUR.setText(dfFormat.format(dResult[3]));
				cdpCurrDialogPanel.jtfAUD.setText(dfFormat.format(dResult[4]));
			} catch (NumberFormatException e) {
				System.out.println("The number you entered is not valid.");
				dAmount = 0;
				if (aeEvent.getSource() == cdpCurrDialogPanel.jtfUSD) {
					cdpCurrDialogPanel.jtfUSD.setText(dfFormat.format(dAmount));
				}
				if (aeEvent.getSource() == cdpCurrDialogPanel.jtfGBP) {
					cdpCurrDialogPanel.jtfGBP.setText(dfFormat.format(dAmount));
				}
				if (aeEvent.getSource() == cdpCurrDialogPanel.jtfCAD) {
					cdpCurrDialogPanel.jtfCAD.setText(dfFormat.format(dAmount));
				}
				if (aeEvent.getSource() == cdpCurrDialogPanel.jtfEUR) {
					cdpCurrDialogPanel.jtfEUR.setText(dfFormat.format(dAmount));
				}
				if (aeEvent.getSource() == cdpCurrDialogPanel.jtfAUD) {
					cdpCurrDialogPanel.jtfAUD.setText(dfFormat.format(dAmount));
				}
			} catch (NullPointerException e) {}
		}
	}

	/**
	 *
	 * mouseEntered is the method in which the color of the
	 * text in the text box is changed once the mouse enters the
	 * text box.  It takes in a MouseEvent parameter.  
	 * @param  meEvent - MouseEvent - mouse entered
	 * @return none
	 *
	 */
	public void mouseEntered(MouseEvent meEvent) {
		if (meEvent.getSource() == cdpCurrDialogPanel.jtfUSD) {
			cdpCurrDialogPanel.jtfUSD.setForeground(Color.red);			
		}
		if (meEvent.getSource() == cdpCurrDialogPanel.jtfGBP) {
			cdpCurrDialogPanel.jtfGBP.setForeground(Color.red);			
		}
		if (meEvent.getSource() == cdpCurrDialogPanel.jtfCAD) {
			cdpCurrDialogPanel.jtfCAD.setForeground(Color.red);			
		}
		if (meEvent.getSource() == cdpCurrDialogPanel.jtfEUR) {
			cdpCurrDialogPanel.jtfEUR.setForeground(Color.red);			
		}
		if (meEvent.getSource() == cdpCurrDialogPanel.jtfAUD) {
			cdpCurrDialogPanel.jtfAUD.setForeground(Color.red);			
		}
	}

	/**
	 *
	 * mouseExited is the method in which the color of the
	 * text in the text box is changed once the mouse leaves the
	 * text box.  It takes in a MouseEvent parameter.  
	 * @param  meEvent - MouseEvent - mouse exited
	 * @return none
	 *
	 */
	public void mouseExited(MouseEvent meEvent) {
		if (meEvent.getSource() == cdpCurrDialogPanel.jtfUSD) {
			cdpCurrDialogPanel.jtfUSD.setForeground(Color.black);
		}
		if (meEvent.getSource() == cdpCurrDialogPanel.jtfGBP) {
			cdpCurrDialogPanel.jtfGBP.setForeground(Color.black);
		}
		if (meEvent.getSource() == cdpCurrDialogPanel.jtfCAD) {
			cdpCurrDialogPanel.jtfCAD.setForeground(Color.black);
		}
		if (meEvent.getSource() == cdpCurrDialogPanel.jtfEUR) {
			cdpCurrDialogPanel.jtfEUR.setForeground(Color.black);
		}
		if (meEvent.getSource() == cdpCurrDialogPanel.jtfAUD) {
			cdpCurrDialogPanel.jtfAUD.setForeground(Color.black);
		}
	}

	/**
	 *
	 * mouseClicked does nothing.  It takes in a MouseEvent parameter.  
	 * @param  meEvent - MouseEvent - mouse exited
	 * @return none
	 *
	 */
	public void mouseClicked(MouseEvent meEvent) {}

	/**
	 *
	 * mouseReleased does nothing.  It takes in a MouseEvent parameter.  
	 * @param  meEvent - MouseEvent - mouse exited
	 * @return none
	 *
	 */
	public void mouseReleased(MouseEvent meEvent) {}

	/**
	 *
	 * mousePressed does nothing.  It takes in a MouseEvent parameter.  
	 * @param  meEvent - MouseEvent - mouse exited
	 * @return none
	 *
	 */
	public void mousePressed(MouseEvent meEvent) {}
}

/**
 *
 * Class CurrDialogPanel implements a panel that shows 
 * the currencies in British pounds, Canadian 
 * dollars, Euros, Australian dollars and US 
 * dollars.
 *
 * @author  Daniel Burgner
 * @version 1.0
 * @class   CS5730
 * @date    03.09.2002
 *
 */
class CurrDialogPanel extends JPanel {

	/* what goes into the Dialog Panel */
	JLabel jlTitle;
	JLabel jlInstructionTitle;
	JLabel jlInstructionOne;
	JLabel jlInstructionTwo;
	JLabel jlInstructionThree;
	JLabel jlUSD;
	JLabel jlGBP;
	JLabel jlCAD;
	JLabel jlEUR;
	JLabel jlAUD;
	JTextField jtfUSD;
	JTextField jtfGBP;
	JTextField jtfCAD;
	JTextField jtfEUR;
	JTextField jtfAUD;

	/**
	 *
	 * CurrDialogPanel method initializes the CurrDialogPanel.  
	 * @param  none
	 * @return none
	 *
	 */
	CurrDialogPanel() {
		/* initializes the components of the CurrDialogPanel */
		jlTitle = new JLabel("Currency Conversion Application");
		jlInstructionTitle = new JLabel("Instructions:");
		jlInstructionOne = new JLabel("1.  Choose a currency.");
		jlInstructionTwo = new JLabel("2.  Enter an amount.");
		jlInstructionThree = 
			new JLabel("3.  Press Enter to do conversions, or click Exit to exit.");
		jlUSD = new JLabel("US Dollars");
		jlGBP = new JLabel("British Pounds");
		jlCAD = new JLabel("Canadian Dollars");
		jlEUR = new JLabel("Euros");
		jlAUD = new JLabel("Australian Dollars");
		jtfUSD = new JTextField();
		jtfGBP = new JTextField();
		jtfCAD = new JTextField();
		jtfEUR = new JTextField();
		jtfAUD = new JTextField();

		/* adds the components to the CurrDialogPanel */
		add(jlTitle);
		add(jlInstructionTitle);
		add(jlInstructionOne);
		add(jlInstructionTwo);
		add(jlInstructionThree);
		add(jlUSD);
		add(jlGBP);
		add(jlCAD);
		add(jlEUR);
		add(jlAUD);
		add(jtfUSD);
		add(jtfGBP);
		add(jtfCAD);
		add(jtfEUR);
		add(jtfAUD);
	}

	/**
	 *
	 * getPreferredSize gets the dimensions of the
	 * dialog panel at its preferred size.
	 * @param  none
	 * @return none
	 *
	 */
	public Dimension getPreferredSize() {
		return new Dimension(350, 350);
	}

	/**
	 *
	 * getMinimumSize gets the dimensions of the
	 * dialog panel at its preferred size.
	 * @param  none
	 * @return none
	 *
	 */
	public Dimension getMinimumSize() {
		return new Dimension(350, 350);
	}

	/**
	 *
	 * doLayout does the layout of the dialog panel.
	 * @param  none
	 * @return none
	 *
	 */
	public void doLayout() {
		jlTitle.setBounds(10, 10, 300, 30);
		jlInstructionTitle.setBounds(10, 40, 200, 30);
		jlInstructionOne.setBounds(10, 70, 200, 30);
		jlInstructionTwo.setBounds(10, 100, 200, 30);
		jlInstructionThree.setBounds(10, 130, 340, 30);
		jlUSD.setBounds(10, 160, 150, 30);
		jlGBP.setBounds(10, 190, 150, 30);
		jlCAD.setBounds(10, 220, 150, 30);
		jlEUR.setBounds(10, 250, 150, 30);
		jlAUD.setBounds(10, 280, 150, 30);
		jtfUSD.setBounds(160, 165, 100, 20);
		jtfGBP.setBounds(160, 195, 100, 20);
		jtfCAD.setBounds(160, 225, 100, 20);
		jtfEUR.setBounds(160, 255, 100, 20);
		jtfAUD.setBounds(160, 285, 100, 20);
	}

	/**
	 *
	 * initializeTF sets the textfields to 0.00.
	 * @param  none
	 * @return none
	 *
	 */
	public void initializeTF() {
		jtfUSD.setText("0.00");
		jtfGBP.setText("0.00");
		jtfCAD.setText("0.00");
		jtfEUR.setText("0.00");
		jtfAUD.setText("0.00");
	}
}
