Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package il.ac.hit;
- import java.awt.BorderLayout;
- import java.awt.FlowLayout;
- import java.awt.Font;
- import java.awt.GridLayout;
- import java.awt.event.*;
- import java.io.IOException;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTable;
- import javax.swing.JTextField;
- import org.apache.log4j.BasicConfigurator;
- import org.apache.log4j.BasicConfigurator;
- import org.apache.log4j.FileAppender;
- import org.apache.log4j.Logger;
- import org.apache.log4j.SimpleLayout;
- public class View {
- private JPanel mainP; // that panel will have all the currencies operations
- private JPanel dateP; // that panel will have the details about the current date
- private JFrame frame; // the frame
- private JTable currencyT; // the currency table
- // all the titles\labels :
- private JLabel l_title;
- private JLabel l_date;
- private JLabel l_convert;
- private JLabel l_from;
- private JLabel l_to;
- private JButton b_calc; // the calculate button
- private JTextField tf_amount; // in this textField the user enter the amount to calculate
- private JTextField tf_result; // in this textField the user will get the answer
- private JComboBox<?> cb_from; // this comboBox will include all the currencies that the xml have
- private JComboBox<?> cb_to; // this comboBox will include all the currencies that the xml have
- private DateFormat currentDateUpdated; // the current date
- private Date date; // the current date
- private ActionListener al; // will listen to any operation that the user do
- private String[] curreciesNames; // will include all the currenciesNames that the xml have
- private double[] curreciesRate; // will include all the currenciesRates that the xml have
- private Timer timer; // will care the data update every scheduled time
- public static Logger logger; // will manage the log4j
- public View()
- {
- al = new ButtonListener(); // initialize the listener to be ButtonListener
- timer = new Timer(); // initialize the timer object
- logger = Logger.getLogger("View"); // set the Logger object to this ( class View )
- BasicConfigurator.configure(); // configure the Logger object
- try { // create new log text file
- logger.addAppender(new FileAppender(new SimpleLayout(), "logFile.txt"));
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- logger.info("\n\nEnterence time: " + new Date()); // write in the log file the enterence time
- }
- // in this function the 2 arrays of currencies data will be initialize
- public void initializeCurrenciesArrays() { // maybe change it
- int numOfCurrencies = XMLreader.connectToUrl();
- curreciesNames = new String[numOfCurrencies];
- curreciesRate = new double[numOfCurrencies];
- currentDateUpdated = new SimpleDateFormat("yyyy-MM-dd");// define format for date
- date = XMLreader.updateData(curreciesNames, curreciesRate);
- }
- // find specific element in the CurrencyNames array
- public int findCurrencyNameInArray(String toFind){
- int ans = -1; // -1 means doesn't found
- for(int i = 0 ; i < curreciesNames.length ; i++){ // check any cell to find
- if ( curreciesNames[i].equals(toFind) ) // the "toFind" in currency
- ans = i; // names array
- }
- return ans;
- }
- // this class role is to handle buttons press
- public class ButtonListener implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent arg0)
- {
- Object pressed = arg0.getSource(); // to know what button was clicked
- Object cb_toSelected = cb_to.getSelectedItem(); // to know which element was
- Object cb_fromSelected = cb_from.getSelectedItem(); // chosen from the comboBoxes
- if ( pressed == b_calc ){ // if calculate button was pressed
- String temp = tf_amount.getText(); // get the amount
- if (temp.equals("") || temp.equals("insert amount")) // if the amount textFiled is empty
- {
- JOptionPane.showMessageDialog(null,"Please Enter Amount To Convert From");
- }
- else { // if the amount textFiled isn't empty
- int toIndex = findCurrencyNameInArray(cb_toSelected.toString()); // find that specific currency index
- int fromIndex = findCurrencyNameInArray(cb_fromSelected.toString());// from the currencyName array
- tf_result.setText( Double.toString // Calculate currency
- (Double.parseDouble(tf_amount.getText()) * curreciesRate[fromIndex] / curreciesRate[toIndex]) );
- } // end of else
- } // end if
- } // end of actionPerformed
- } // end of ButtonListener
- public void fillTabel(){
- // fill names
- for ( int i = 0 ; i < curreciesRate.length ; i++){
- currencyT.setValueAt(curreciesNames[i], i+1, 0);
- currencyT.setValueAt(curreciesNames[i], 0, i+1);
- }
- // fill rates
- for ( int i = 0 ; i < curreciesRate.length ; i++){
- for ( int j = 0 ; j < curreciesRate.length ; j++){
- currencyT.setValueAt(curreciesRate[i] / curreciesRate[j], i+1, j+1);
- }
- }
- }
- // the main method goal is to run the GUI
- public void start()
- {
- initializeCurrenciesArrays();
- mainP = new JPanel();
- dateP = new JPanel();
- l_title = new JLabel("Currency Table");
- l_title.setFont(new Font("Serif", Font.BOLD, 15));
- l_date = new JLabel(currentDateUpdated.format(date));
- l_date.setFont(new Font("Serif", Font.BOLD, 15));
- l_convert = new JLabel("Convert");
- l_from = new JLabel("From");
- l_to = new JLabel("To");
- b_calc = new JButton("Calculate");
- b_calc.addActionListener(al);
- cb_from = new JComboBox<String>(curreciesNames);
- cb_to = new JComboBox<String>(curreciesNames);
- cb_from.addActionListener(al);
- cb_to.addActionListener(al);
- tf_amount = new JTextField(10);
- tf_result = new JTextField(10);
- frame = new JFrame("Currency converter");
- tf_amount.addFocusListener(new FocusListener(){ // this lines purpose
- // is to set the words
- @Override // insert currency in
- public void focusGained(FocusEvent arg0) { // the amount text field
- tf_amount.setText(""); // when there is no text
- } // there at all.
- @Override
- public void focusLost(FocusEvent arg0) {
- if (tf_amount.getText().equals(""))
- tf_amount.setText("insert amount");
- }
- }
- );
- currencyT = new JTable(curreciesNames.length+1, curreciesNames.length+1);
- currencyT.setEnabled(false);
- fillTabel();
- frame.setLayout(new BorderLayout());
- dateP.setLayout(new FlowLayout());
- frame.add(dateP, BorderLayout.NORTH);
- dateP.add(l_title);
- dateP.add(l_date);
- frame.add(currencyT, BorderLayout.SOUTH);
- mainP.setLayout(new GridLayout(6, 1));
- tf_amount.setFont(new Font("Serif", Font.BOLD, 15));
- mainP.add(l_convert);
- mainP.add(tf_amount);
- mainP.add(l_from);
- mainP.add(cb_from);
- mainP.add(l_to);
- mainP.add(cb_to);
- mainP.add(b_calc);
- mainP.add(tf_result);
- frame.pack();
- frame.add(mainP, BorderLayout.CENTER);
- frame.setResizable(false);
- frame.setVisible(true); // set the frame to be visible
- frame.setSize(600, 400); // set the frame default size
- frame.addWindowListener(new WindowAdapter()
- {
- @Override
- public void windowClosing(WindowEvent e) {
- logger.info("Exit time: " + new Date());
- System.exit(0);
- }
- }
- );
- timer.schedule(new TimerTask() {
- @Override
- public void run() {
- date = XMLreader.updateData(curreciesNames, curreciesRate);
- logger.info("UPDATE AT: " + date); // write in the log file the update time
- fillTabel();
- }
- }, 0, 10000000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement