Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Submitted by Dima Yankovoy 311868442 & Snir Yacoby 201561933
- package il.ac.hit.project.view;
- import il.ac.hit.project.model.*;
- import javax.swing.*;
- import org.apache.log4j.Logger;
- import java.awt.event.*;
- import java.awt.*;
- /**
- * This class is responsible for the creation of the main user interface window,
- */
- public class GUI {
- //main window frame which will display the GUI of the project
- private JFrame mainFrame = null;
- ////
- /**
- * reader - represents the main model part of the application - responsible for data importing from online file,
- * and storing the data in a local file.
- * importCycle - a thread object which receives the reader as the runnable parameter - initiates the continuous
- * up-to-date checkup of the data.
- * currList - array of strings that stores the currency codes that are displayed as a choice - in the "from"
- * and "to" fields.
- * currencyTable - a reference to ana object of class TableBuilder - that is responsible of constructing the
- * JTable with information that comes from the model part of the project.
- */
- private XMLReader reader = null;
- private Thread importCycle = null;
- private String[] currList = null;
- private TableBuilder currencyTable = null;
- //swing components initialization
- private JComboBox<String> fromList = null;
- private JComboBox<String> toList = null;
- private JTextField sumField = null;
- private JTextField resultField = null;
- private JButton convertBtn = null;
- private JButton callTable = null;
- //logger for the gui
- private Logger guiLogger = Logger.getLogger("GUI");
- /**
- * Builds the main user interface.
- */
- public GUI(){
- //Initialize the XMLReader reference as Currencies - the model's class written in scala
- reader = new Currencies();
- //Creation of separate thread for the model - does the continuous checking of online XML file, when started
- importCycle = new Thread(reader,"Import-Cycle");
- //start of the thread responsible for checking of the online document
- boolean status = true;
- importCycle.start();
- //The following sleep() method activation allows the importCycle thread to finish it's function before
- //the GUI is shown on screen
- try
- {
- Thread.sleep(2000);
- }
- catch(InterruptedException e){
- e.printStackTrace();
- }
- //status that is displayed within the lblStatus on the GUI.
- status = reader.getStatus();
- JLabel lblStatus = new JLabel(status ? ("Online") : ("Offline"));
- lblStatus.setForeground(status ? (Color.green) : (Color.red));
- JLabel lblLastUpdate = new JLabel("Last Update was on: "+reader.getLastUpdate());
- //creation of the TableBuilder object with the data received from the model part.
- currencyTable = new TableBuilder(reader.getData());
- //Components Initialization
- JLabel headerText = new JLabel("Currency Rate Converter");
- JLabel lblFrom = new JLabel("From:");
- JLabel lblSum = new JLabel("Sum to calculate:");
- JLabel lblTo = new JLabel("To:");
- JLabel lblResult = new JLabel("Result: ");
- convertBtn = new JButton("Convert Sum");
- callTable = new JButton("Currencies Table");
- sumField = new JTextField(4);
- resultField = new JTextField(16);
- resultField.setEditable(false);
- //Use getColumn method of TableBuilder class to initialize currList - names of Currencies
- //for fromList and toList
- currList = (String[])currencyTable.getColumn(1);
- fromList = new JComboBox<String>(currList);
- toList = new JComboBox<String>(currList);
- //Button Listeners addition - convert & callTable buttons
- ActionListener convertListener = new BtnListener();
- convertBtn.addActionListener(convertListener);
- ActionListener callTableListener = new BtnListener();
- callTable.addActionListener(callTableListener);
- //Sub-Containers - each panel stores other components, while some used for spacing
- //each panel named with a number which indicates the order of appearance in the window
- JPanel panelHeader1 = new JPanel();
- JPanel panelSums2 = new JPanel();
- JPanel panelConvert3 = new JPanel();
- JPanel panelSpc4 = new JPanel();
- JPanel panelResult5 = new JPanel();
- JPanel panelSpc6 = new JPanel();
- JPanel panelTable7 = new JPanel();
- JPanel panelSpc8 = new JPanel();
- JPanel panelStatus9 = new JPanel();
- /////////////////////////////
- //Sub-Containers initialization with the proper components
- panelHeader1.add(headerText);
- panelSums2.add(lblSum);
- panelSums2.add(sumField);
- panelSums2.add(lblFrom);
- panelSums2.add(fromList);
- panelSums2.add(lblTo);
- panelSums2.add(toList);
- panelConvert3.add(convertBtn);
- panelResult5.add(lblResult);
- panelResult5.add(resultField);
- panelTable7.add(callTable);
- panelStatus9.add(lblStatus);
- panelStatus9.add(lblLastUpdate);
- //Adding the Sub-Containers to the main Window Frame
- mainFrame = new JFrame("Currency Rate Converter");
- mainFrame.setLayout(new GridLayout(10,1));
- mainFrame.add(panelHeader1);
- mainFrame.add(panelSums2);
- mainFrame.add(panelConvert3);
- mainFrame.add(panelSpc4);
- mainFrame.add(panelResult5);
- mainFrame.add(panelSpc6);
- mainFrame.add(panelTable7);
- mainFrame.add(panelSpc8);
- mainFrame.add(panelStatus9);
- mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- /**
- * Used for activating the main window of the Currency Converter:
- * setting the size and visibility and starting the thread which is responsible
- * for the import and update of the local currencies file.
- */
- public void go(){
- mainFrame.setSize(500, 400);
- mainFrame.setVisible(true);
- }
- /**
- * Inner class BtnListener is used for assigning actions to take when a click on
- * a specific button occurs.
- * There are two scenarios : 1)pressing the CallTable JButton shows the table's window frame on screen.
- * 2)pressing the convert Button calls for the convert method within the model, and does the
- * required calculations.
- */
- class BtnListener implements ActionListener{
- public void actionPerformed(ActionEvent e)
- {
- Object source = e.getSource();
- //activates showing of the table if that button was pressed
- if(source == callTable)
- {
- currencyTable.go();
- guiLogger.info("Currencies table opened.");
- }
- //activates conversion if that button was pressed
- if(source == convertBtn)
- {
- try
- {
- String sum = sumField.getText();
- resultField.setText(reader.convert(sum,fromList.getSelectedIndex(),toList.getSelectedIndex()));
- guiLogger.info("Conversion was successful!");
- }
- //in case the user entered incorrect amount (0 or below)
- catch(NullSumException n)
- {
- resultField.setText(n.getMessage());
- guiLogger.info("User entered zero as the sum.");
- }
- }
- }
- }
- /**
- * Returns the XMLReader object used in the GUI class
- * as it's sub-type Currencies that can be used for logging purposes.
- *
- */
- public Currencies getReader() {return (Currencies) reader;}
- /**
- * Returns the logger of the GUI
- */
- public Logger getLogger() {return guiLogger;}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement