Advertisement
Guest User

GUI

a guest
May 30th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.90 KB | None | 0 0
  1. //Submitted by Dima Yankovoy 311868442 & Snir Yacoby 201561933
  2.  
  3. package il.ac.hit.project.view;
  4. import il.ac.hit.project.model.*;
  5. import javax.swing.*;
  6. import org.apache.log4j.Logger;
  7. import java.awt.event.*;
  8. import java.awt.*;
  9.  
  10. /**
  11.  * This class is responsible for the creation of the main user interface window,
  12.  */
  13. public class GUI {
  14.  
  15.     //main window frame which will display the GUI of the project
  16.     private JFrame mainFrame = null;
  17.     ////
  18.     /**
  19.      * reader - represents the main model part of the application - responsible for data importing from online file,
  20.      * and storing the data in a local file.
  21.      * importCycle - a thread object which receives the reader as the runnable parameter - initiates the continuous
  22.      * up-to-date checkup of the data.
  23.      * currList - array of strings that stores the currency codes that are displayed as a choice - in the "from"
  24.      * and "to" fields.
  25.      * currencyTable - a reference to ana object of class TableBuilder - that is responsible of constructing the
  26.      * JTable with information that comes from the model part of the project.
  27.      */
  28.     private XMLReader reader = null;
  29.     private Thread importCycle = null;
  30.     private String[] currList = null;
  31.     private TableBuilder currencyTable = null;
  32.  
  33.     //swing components initialization
  34.     private JComboBox<String> fromList = null;
  35.     private JComboBox<String> toList = null;
  36.     private JTextField sumField = null;
  37.     private JTextField resultField = null;
  38.     private JButton convertBtn = null;
  39.     private JButton callTable = null;
  40.     //logger for the gui
  41.     private Logger guiLogger = Logger.getLogger("GUI");
  42.  
  43.     /**
  44.      * Builds the main user interface.
  45.      */
  46.     public GUI(){
  47.  
  48.         //Initialize the XMLReader reference as Currencies - the model's class written in scala
  49.         reader = new Currencies();
  50.         //Creation of separate thread for the model - does the continuous checking of online XML file, when started
  51.         importCycle = new Thread(reader,"Import-Cycle");
  52.         //start of the thread responsible for checking of the online document
  53.         boolean status = true;
  54.         importCycle.start();
  55.         //The following sleep() method activation allows the importCycle thread to finish it's function before
  56.         //the GUI is shown on screen
  57.         try
  58.         {
  59.          Thread.sleep(2000);
  60.         }
  61.         catch(InterruptedException e){
  62.             e.printStackTrace();
  63.         }
  64.  
  65.         //status that is displayed within the lblStatus on the GUI.
  66.         status = reader.getStatus();
  67.         JLabel lblStatus = new JLabel(status ? ("Online") : ("Offline"));
  68.         lblStatus.setForeground(status ? (Color.green) : (Color.red));
  69.         JLabel lblLastUpdate = new JLabel("Last Update was on: "+reader.getLastUpdate());
  70.  
  71.         //creation of the TableBuilder object with the data received from the model part.
  72.         currencyTable = new TableBuilder(reader.getData());
  73.  
  74.         //Components Initialization
  75.         JLabel headerText = new JLabel("Currency Rate Converter");
  76.         JLabel lblFrom = new JLabel("From:");
  77.         JLabel lblSum = new JLabel("Sum to calculate:");
  78.         JLabel lblTo = new JLabel("To:");
  79.         JLabel lblResult = new JLabel("Result: ");
  80.         convertBtn = new JButton("Convert Sum");
  81.         callTable = new JButton("Currencies Table");
  82.         sumField = new JTextField(4);
  83.         resultField = new JTextField(16);
  84.         resultField.setEditable(false);
  85.  
  86.         //Use getColumn method of TableBuilder class to initialize currList - names of Currencies
  87.         //for fromList and toList
  88.         currList = (String[])currencyTable.getColumn(1);
  89.         fromList = new JComboBox<String>(currList);
  90.         toList = new JComboBox<String>(currList);
  91.  
  92.         //Button Listeners addition - convert & callTable buttons
  93.         ActionListener convertListener = new BtnListener();
  94.         convertBtn.addActionListener(convertListener);
  95.         ActionListener callTableListener = new BtnListener();
  96.         callTable.addActionListener(callTableListener);
  97.  
  98.         //Sub-Containers - each panel stores other components, while some used for spacing
  99.         //each panel named with a number which indicates the order of appearance in the window
  100.         JPanel panelHeader1 = new JPanel();
  101.         JPanel panelSums2 = new JPanel();
  102.         JPanel panelConvert3 = new JPanel();
  103.         JPanel panelSpc4 = new JPanel();
  104.         JPanel panelResult5 = new JPanel();
  105.         JPanel panelSpc6 = new JPanel();
  106.         JPanel panelTable7 = new JPanel();
  107.         JPanel panelSpc8 = new JPanel();
  108.         JPanel panelStatus9 = new JPanel();
  109.         /////////////////////////////
  110.         //Sub-Containers initialization with the proper components
  111.         panelHeader1.add(headerText);
  112.         panelSums2.add(lblSum);
  113.         panelSums2.add(sumField);
  114.         panelSums2.add(lblFrom);
  115.         panelSums2.add(fromList);
  116.         panelSums2.add(lblTo);
  117.         panelSums2.add(toList);
  118.         panelConvert3.add(convertBtn);
  119.         panelResult5.add(lblResult);
  120.         panelResult5.add(resultField);
  121.         panelTable7.add(callTable);
  122.         panelStatus9.add(lblStatus);
  123.         panelStatus9.add(lblLastUpdate);
  124.  
  125.         //Adding the Sub-Containers to the main Window Frame
  126.         mainFrame = new JFrame("Currency Rate Converter");
  127.         mainFrame.setLayout(new GridLayout(10,1));
  128.         mainFrame.add(panelHeader1);
  129.         mainFrame.add(panelSums2);
  130.         mainFrame.add(panelConvert3);
  131.         mainFrame.add(panelSpc4);
  132.         mainFrame.add(panelResult5);
  133.         mainFrame.add(panelSpc6);
  134.         mainFrame.add(panelTable7);
  135.         mainFrame.add(panelSpc8);
  136.         mainFrame.add(panelStatus9);
  137.  
  138.         mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  139.     }
  140.     /**
  141.      * Used for activating the main window of the Currency Converter:
  142.      * setting the size and visibility and starting the thread which is responsible
  143.      * for the import and update of the local currencies file.
  144.      */
  145.     public void go(){
  146.         mainFrame.setSize(500, 400);
  147.         mainFrame.setVisible(true);
  148.     }
  149.  
  150.     /**
  151.      * Inner class BtnListener is used for assigning actions to take when a click on
  152.      * a specific button occurs.
  153.      * There are two scenarios : 1)pressing the CallTable JButton shows the table's window frame on screen.
  154.      * 2)pressing the convert Button calls for the convert method within the model, and does the
  155.      * required calculations.
  156.      */
  157.     class BtnListener implements ActionListener{
  158.         public void actionPerformed(ActionEvent e)
  159.         {
  160.             Object source = e.getSource();
  161.             //activates showing of the table if that button was pressed
  162.             if(source == callTable)
  163.             {
  164.                 currencyTable.go();
  165.                 guiLogger.info("Currencies table opened.");
  166.             }
  167.             //activates conversion if that button was pressed
  168.             if(source == convertBtn)
  169.             {
  170.                 try
  171.                 {
  172.                     String sum = sumField.getText();
  173.                     resultField.setText(reader.convert(sum,fromList.getSelectedIndex(),toList.getSelectedIndex()));
  174.                     guiLogger.info("Conversion was successful!");
  175.                 }
  176.                 //in case the user entered incorrect amount (0 or below)
  177.                 catch(NullSumException n)
  178.                 {
  179.                     resultField.setText(n.getMessage());
  180.                     guiLogger.info("User entered zero as the sum.");
  181.                 }
  182.             }
  183.         }
  184.     }
  185.  
  186.     /**
  187.      * Returns the XMLReader object used in the GUI class
  188.      * as it's sub-type Currencies that can be used for logging purposes.
  189.      *
  190.      */
  191.     public Currencies getReader() {return (Currencies) reader;}
  192.     /**
  193.      * Returns the logger of the GUI
  194.      */
  195.     public Logger getLogger() {return guiLogger;}
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement