Advertisement
Guest User

View

a guest
May 17th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 8.90 KB | None | 0 0
  1. package il.ac.hit;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.FlowLayout;
  5. import java.awt.Font;
  6. import java.awt.GridLayout;
  7. import java.awt.event.*;
  8. import java.io.IOException;
  9. import java.text.DateFormat;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Date;
  12. import java.util.Timer;
  13. import java.util.TimerTask;
  14.  
  15. import javax.swing.JButton;
  16. import javax.swing.JComboBox;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JOptionPane;
  20. import javax.swing.JPanel;
  21. import javax.swing.JTable;
  22. import javax.swing.JTextField;
  23. import org.apache.log4j.BasicConfigurator;
  24.  
  25.  
  26. import org.apache.log4j.BasicConfigurator;
  27. import org.apache.log4j.FileAppender;
  28. import org.apache.log4j.Logger;
  29. import org.apache.log4j.SimpleLayout;
  30.  
  31.  
  32. public class View {
  33.  
  34.     private JPanel mainP;       // that panel will have all the currencies operations
  35.     private JPanel dateP;       // that panel will have the details about the current date
  36.     private JFrame frame;       // the frame
  37.  
  38.     private JTable currencyT;   // the currency table
  39.                                 // all the titles\labels :
  40.     private JLabel l_title;
  41.     private JLabel l_date;
  42.     private JLabel l_convert;
  43.     private JLabel l_from;
  44.     private JLabel l_to;
  45.  
  46.     private JButton b_calc;                 // the calculate button
  47.  
  48.     private JTextField tf_amount;           // in this textField the user enter the amount to calculate
  49.     private JTextField tf_result;           // in this textField the user will get the answer
  50.  
  51.     private JComboBox<?> cb_from;           // this comboBox will include all the currencies that the xml have
  52.     private JComboBox<?> cb_to;             // this comboBox will include all the currencies that the xml have
  53.     private DateFormat currentDateUpdated;  // the current date
  54.     private Date date;                      // the current date
  55.     private ActionListener al;              // will listen to any operation that the user do
  56.     private String[] curreciesNames;        // will include all the currenciesNames that the xml have
  57.     private double[] curreciesRate;         // will include all the currenciesRates that the xml have
  58.     private Timer timer;                    // will care the data update every scheduled time
  59.     public static Logger logger;           // will manage the log4j
  60.  
  61.  
  62.     public View()
  63.     {
  64.         al = new ButtonListener();          // initialize the listener to be ButtonListener
  65.         timer = new Timer();                // initialize the timer object
  66.         logger = Logger.getLogger("View");  // set the Logger object to this ( class View )
  67.         BasicConfigurator.configure();      // configure the Logger object
  68.         try {                               // create new log text file
  69.             logger.addAppender(new FileAppender(new SimpleLayout(), "logFile.txt"));
  70.         }
  71.         catch (IOException e) {
  72.             e.printStackTrace();
  73.         }
  74.         logger.info("\n\nEnterence time: " + new Date());   // write in the log file the enterence time
  75.     }
  76.  
  77.  
  78.     // in this function the 2 arrays of currencies data will be initialize
  79.     public void initializeCurrenciesArrays() {       // maybe change it
  80.         int numOfCurrencies = XMLreader.connectToUrl();
  81.         curreciesNames = new String[numOfCurrencies];
  82.         curreciesRate = new double[numOfCurrencies];
  83.  
  84.             currentDateUpdated = new SimpleDateFormat("yyyy-MM-dd");// define format for date
  85.             date =  XMLreader.updateData(curreciesNames, curreciesRate);
  86.     }
  87.  
  88.  
  89.     // find specific element in the CurrencyNames array
  90.     public int findCurrencyNameInArray(String toFind){
  91.  
  92.         int ans = -1;   // -1 means doesn't found
  93.         for(int i = 0 ; i < curreciesNames.length ; i++){   // check any cell to find
  94.             if ( curreciesNames[i].equals(toFind) )         // the "toFind" in currency
  95.                 ans = i;                                    // names array
  96.         }
  97.         return ans;
  98.     }
  99.  
  100.  
  101.     // this class role is to handle buttons press
  102.     public class ButtonListener implements ActionListener{
  103.         @Override
  104.         public void actionPerformed(ActionEvent arg0)
  105.         {
  106.             Object pressed = arg0.getSource();                  // to know what button was clicked
  107.             Object cb_toSelected = cb_to.getSelectedItem();     // to know which element was
  108.             Object cb_fromSelected = cb_from.getSelectedItem(); // chosen from the comboBoxes
  109.  
  110.             if ( pressed == b_calc ){   // if calculate button was pressed
  111.                 String temp = tf_amount.getText();  // get the amount
  112.                 if (temp.equals("") || temp.equals("insert amount"))    // if the amount textFiled is empty
  113.                 {
  114.                     JOptionPane.showMessageDialog(null,"Please Enter Amount To Convert From");
  115.                 }
  116.                 else {  // if the amount textFiled isn't empty
  117.  
  118.                     int toIndex = findCurrencyNameInArray(cb_toSelected.toString());    // find that specific currency index
  119.                     int fromIndex = findCurrencyNameInArray(cb_fromSelected.toString());// from the currencyName array
  120.                     tf_result.setText( Double.toString  // Calculate currency
  121.                             (Double.parseDouble(tf_amount.getText()) * curreciesRate[fromIndex] / curreciesRate[toIndex])  );
  122.                 }   // end of else
  123.             }   // end if
  124.  
  125.         }   // end of actionPerformed
  126.     }   // end of ButtonListener
  127.  
  128.  
  129.     public void fillTabel(){
  130.  
  131.         // fill names
  132.         for ( int i = 0 ; i < curreciesRate.length ; i++){
  133.             currencyT.setValueAt(curreciesNames[i], i+1, 0);
  134.             currencyT.setValueAt(curreciesNames[i], 0, i+1);
  135.         }
  136.  
  137.         // fill rates
  138.         for ( int i = 0 ; i < curreciesRate.length ; i++){
  139.             for ( int j = 0 ; j < curreciesRate.length ; j++){
  140.                 currencyT.setValueAt(curreciesRate[i] / curreciesRate[j], i+1, j+1);
  141.             }
  142.         }
  143.     }
  144.  
  145.  
  146.     // the main method goal is to run the GUI
  147.     public void start()
  148.     {
  149.         initializeCurrenciesArrays();
  150.         mainP = new JPanel();
  151.         dateP = new JPanel();
  152.         l_title = new JLabel("Currency Table");
  153.         l_title.setFont(new Font("Serif", Font.BOLD, 15));
  154.         l_date = new JLabel(currentDateUpdated.format(date));
  155.         l_date.setFont(new Font("Serif", Font.BOLD, 15));
  156.         l_convert = new JLabel("Convert");
  157.         l_from = new JLabel("From");
  158.         l_to = new JLabel("To");
  159.  
  160.         b_calc = new JButton("Calculate");
  161.         b_calc.addActionListener(al);
  162.         cb_from = new JComboBox<String>(curreciesNames);
  163.         cb_to = new JComboBox<String>(curreciesNames);
  164.         cb_from.addActionListener(al);
  165.         cb_to.addActionListener(al);
  166.         tf_amount = new JTextField(10);
  167.  
  168.         tf_result = new JTextField(10);
  169.         frame = new JFrame("Currency converter");
  170.  
  171.         tf_amount.addFocusListener(new FocusListener(){     // this lines purpose
  172.             // is to set the words
  173.             @Override                                       // insert currency in
  174.             public void focusGained(FocusEvent arg0) {      // the amount text field
  175.                 tf_amount.setText("");                      // when there is no text
  176.             }                                               // there at all.
  177.  
  178.             @Override
  179.             public void focusLost(FocusEvent arg0) {
  180.                 if (tf_amount.getText().equals(""))
  181.                     tf_amount.setText("insert amount");
  182.             }
  183.  
  184.         }
  185.         );
  186.  
  187.  
  188.         currencyT = new JTable(curreciesNames.length+1, curreciesNames.length+1);
  189.         currencyT.setEnabled(false);
  190.  
  191.         fillTabel();
  192.  
  193.         frame.setLayout(new BorderLayout());
  194.         dateP.setLayout(new FlowLayout());
  195.  
  196.         frame.add(dateP, BorderLayout.NORTH);
  197.         dateP.add(l_title);
  198.         dateP.add(l_date);
  199.  
  200.         frame.add(currencyT, BorderLayout.SOUTH);
  201.  
  202.         mainP.setLayout(new GridLayout(6, 1));
  203.         tf_amount.setFont(new Font("Serif", Font.BOLD, 15));
  204.         mainP.add(l_convert);
  205.         mainP.add(tf_amount);
  206.         mainP.add(l_from);
  207.         mainP.add(cb_from);
  208.         mainP.add(l_to);
  209.         mainP.add(cb_to);
  210.         mainP.add(b_calc);
  211.  
  212.         mainP.add(tf_result);
  213.  
  214.         frame.pack();
  215.  
  216.         frame.add(mainP, BorderLayout.CENTER);
  217.         frame.setResizable(false);
  218.         frame.setVisible(true);     // set the frame to be visible
  219.         frame.setSize(600, 400);    // set the frame default size
  220.  
  221.         frame.addWindowListener(new WindowAdapter()
  222.         {
  223.             @Override
  224.             public void windowClosing(WindowEvent e) {
  225.                 logger.info("Exit time: " + new Date());
  226.                 System.exit(0);
  227.             }
  228.         }
  229.         );
  230.  
  231.  
  232.         timer.schedule(new TimerTask() {
  233.  
  234.             @Override
  235.             public void run() {
  236.  
  237.                 date = XMLreader.updateData(curreciesNames, curreciesRate);
  238.                 logger.info("UPDATE AT: " + date);   // write in the log file the update time
  239.                 fillTabel();
  240.             }
  241.  
  242.         }, 0, 10000000);
  243.  
  244.  
  245.     }
  246.  
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement