Package gui

Source Code of gui.MainFrame

/* ************************
* Name : Eugene Krapivin *
* ID   : 306255084       *
* ***********************/

package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import java.io.File;
import java.util.Vector;

import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

import nodes.MileStone;
import person.Person;
import person.PersonParser;
import threading.IntegratorThread;
import threading.SearchThread;
import exceptions.ParserWriteException;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;

/**
* Main frame of the GUI
* <p>
* This is the main interaction windows with the user.
* <P>
* This program is designed to save information about VIPs through their lives.
* Allows insert remove VIPs Allows insert and remove of milestones in VIPs life
* Allows search in a specific VIP for a specific year and for an interval of
* time Allows cross search in all the VIPs in the list for an interval of time
*
* @author Eugene Krapivin
*
*/
public class MainFrame extends JFrame implements ActionListener
{
  private static final long serialVersionUID = -8026416994513756565L;
  private static String file = "test.txt";

  private ExecutorService pool = Executors.newFixedThreadPool(8);

  /**
   * Class constructor
   * <p>
   * Constructs JFrame, sets size, place and closing event handling.
   * Initizlizes the frame.
   */
  public MainFrame()
  {
    super("Main Menu");

    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setPreferredSize(new Dimension(482, 599));
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

    setLocation((d.width / 2 - 241), (d.height / 2 - 300));
    setResizable(false);
    initializeComponents();

    this.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        if (comboBoxModel.getSize() >= 0)
        {
          try
          {
            PersonParser.write(comboBoxModel, file);
            JOptionPane.showMessageDialog(null,
                "Information save successfully in " + file);
          }
          catch (ParserWriteException ex)
          {
            JOptionPane.showMessageDialog(null, ex.getMessage());
          }
          finally
          {
            System.exit(0);
          }
        }
        else
        {
          File f = new File(file);
          f.delete();
        }
      }
    });
  }

  /**
   * Component initializer
   * <p>
   * This method is a helper for the constructor, it will initialize all the
   * components and space them on the screen
   */
  private void initializeComponents()
  {
    JPanel mainPanel = new JPanel();
    comboBoxModel = null;

    comboBoxModel = new DefaultComboBoxModel<Person>();

    try
    {
      comboBoxModel = PersonParser.read(file);
    }
    catch (Exception e)
    {
      System.out.println(e.getMessage());
    }
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));

    JPanel leftPanel = new JPanel();
    mainPanel.add(leftPanel);
    leftPanel.setLayout(new BorderLayout(0, 0));

    upperWestPanel = new JPanel();
    leftPanel.add(upperWestPanel, BorderLayout.NORTH);

    vipComboBox = new JComboBox<Person>(comboBoxModel);
    upperWestPanel.add(vipComboBox);
    vipComboBox.addItemListener(new ItemListener()
    {
      public void itemStateChanged(ItemEvent e)
      {
        if (vipComboBox.getSelectedItem() != null)
        {
          summaryArea.setText(((Person) vipComboBox.getSelectedItem())
              .lifeHistoryToString());
        }
      }
    });

    vipComboBox.setPreferredSize(new Dimension(150, 20));

    lowerWestPanel = new JPanel();
    leftPanel.add(lowerWestPanel, BorderLayout.SOUTH);
    lowerWestPanel.setLayout(new GridLayout(4, 1));
    addPersonButton = new JButton("Add person");
    lowerWestPanel.add(addPersonButton);
    addPersonButton.addActionListener(this);
    removePersonButton = new JButton("Remove person");
    lowerWestPanel.add(removePersonButton);

    dummyLabel1 = new JLabel("");
    lowerWestPanel.add(dummyLabel1);

    dummyLabel2 = new JLabel("");
    lowerWestPanel.add(dummyLabel2);
    removePersonButton.addActionListener(this);
    getContentPane().add(mainPanel);

    rightPanel = new JPanel();
    mainPanel.add(rightPanel);
    rightPanel.setLayout(new BorderLayout(0, 0));

    JPanel bottomPanel = new JPanel();
    rightPanel.add(bottomPanel, BorderLayout.SOUTH);
    bottomPanel.setLayout(new GridLayout(4, 2, 0, 0));
    addMileStoneButton = new JButton("Add Milestone");
    searchPersonByYearButton = new JButton("Search vip By year");
    removeMileStoneButton = new JButton("Remove Milestone");
    searchListByintervalButton = new JButton("Search list by interval");
    advancedSearchButton = new JButton("Advanced search");
    advancedSearchButton.addActionListener(this);
    addMileStoneButton.addActionListener(this);
    searchPersonByYearButton.addActionListener(this);
    removeMileStoneButton.addActionListener(this);
    searchListByintervalButton.addActionListener(this);
    bottomPanel.add(addMileStoneButton);
    bottomPanel.add(searchPersonByYearButton);
    bottomPanel.add(removeMileStoneButton);
    searchPersonByIntervalButton = new JButton("Search vip by interval");
    searchPersonByIntervalButton.addActionListener(this);
    bottomPanel.add(searchPersonByIntervalButton);
   
        dummyLabel3 = new JLabel("");
        bottomPanel.add(dummyLabel3);
    bottomPanel.add(searchListByintervalButton);

    dummyLabel4 = new JLabel("");
    bottomPanel.add(dummyLabel4);
    bottomPanel.add(advancedSearchButton);

    panel = new JPanel();
    rightPanel.add(panel);
    panel.setLayout(new BorderLayout(0, 0));
   
    summaryArea = new JTextArea();
    scrollPane = new JScrollPane(summaryArea);
    panel.add(scrollPane, BorderLayout.CENTER);
   
    summaryArea.setEditable(false);

    this.pack();
    this.update();
  }

  /**
   * Action listener interface implementation
   * <p>
   * This method routes all the actions performed to their specific methods.
   * The action handler it self is in the format <Component>_<event>().
   *
   * @param e
   *            The action event cought
   */
  @Override
  public void actionPerformed(ActionEvent e)
  {
    if (e.getSource() == addPersonButton)
    {
      addPersonButton_Clicked();
    }
    else if (e.getSource() == removePersonButton)
    {
      if (vipComboBox.getSelectedItem() != null)
      {
        removePersonButton_Clicked();
      }
      else
      {
        JOptionPane.showMessageDialog(this, "Choose a person first");
      }
    }
    else if (e.getSource() == addMileStoneButton)
    {
      if (vipComboBox.getSelectedItem() != null)
      {
        addMileStoneButton_Clicked();
      }
      else
      {
        JOptionPane.showMessageDialog(this, "Choose a person first");
      }
    }
    else if (e.getSource() == removeMileStoneButton)
    {
      if (vipComboBox.getSelectedItem() != null)
      {
        removeMileStoneButton_Clicked();
      }
      else
      {
        JOptionPane.showMessageDialog(this, "Choose a person first");
      }
    }
    else if (e.getSource() == searchPersonByYearButton)
    {
      if (vipComboBox.getSelectedItem() != null)
      {
        searchPersonByYearButton_Clicked();
      }
      else
      {
        JOptionPane.showMessageDialog(this, "Choose a person first");
      }
    }
    else if (e.getSource() == searchPersonByIntervalButton)
    {
      if (vipComboBox.getSelectedItem() != null)
      {
        searchPersonByIntervalButton_Clicked();
      }
      else
      {
        JOptionPane.showMessageDialog(this, "Choose a person first");
      }
    }
    else if (e.getSource() == searchListByintervalButton)
    {
      if (vipComboBox.getModel().getSize() > 0)
      {
        searchListByIntervalButton_Clicked();
      }
      else
      {
        JOptionPane.showMessageDialog(this, "List is empty");
      }
    }
    else if (e.getSource() == advancedSearchButton)
    {
      if (vipComboBox.getModel().getSize() >0)
      {
        advancedSearchButton_Clicked();
      }
      else
      {
        JOptionPane.showMessageDialog(this, "List is empty");
      }
    }
  }

  private void advancedSearchButton_Clicked()
  {
    System.out.println("advancedSearchButton_Clicked");
    this.setEnabled(false);
    this.setVisible(false);
    AdvancedSearchFrame adv = new AdvancedSearchFrame(this);
    adv.setVisible(true);
  }

 
 
  /**
   * Click Action handler for addPersonButton
   * <p>
   * Action handler sets the calling frame disabled and calls the next frame
   * needed
   */
  private void addPersonButton_Clicked()
  {
    System.out.println("addPersonButton_Clicked");
    this.setEnabled(false);
    AddPersonFrame addPersonFrame = new AddPersonFrame(this);
    addPersonFrame.setVisible(true);
  }

  /**
   * Click Action handler for addPersonButton
   * <p>
   * Action handler sets the calling frame disabled and calls the next frame
   * needed
   */
  private void removePersonButton_Clicked()
  {
    if (vipComboBox.getSelectedItem() != null)
    {
      comboBoxModel.removeElementAt(vipComboBox.getSelectedIndex());
      if ((Person) vipComboBox.getSelectedItem() != null)
      {
        summaryArea.setText(((Person) vipComboBox.getSelectedItem())
            .lifeHistoryToString());
      }
      else
      {
        summaryArea.setText("");
      }
      JOptionPane.showMessageDialog(this, "Deleted Successfully");
    }

    System.out.println("removePersonButton_Clicked");
  }

  /**
   * Click Action handler for addMileStoneButton
   * <p>
   * Action handler sets the calling frame disabled and calls the next frame
   * needed
   */
  private void addMileStoneButton_Clicked()
  {
    System.out.println("addMileStoneButton_Clicked");
    this.setEnabled(false);
    AddMileStoneFrame fr = new AddMileStoneFrame(this);
    fr.setVisible(true);

  }

  /**
   * Click Action handler for removeMileStoneButton
   * <p>
   * Action handler sets the calling frame disabled and calls the next frame
   * needed
   */
  private void removeMileStoneButton_Clicked()
  {
    System.out.println("removeMileStoneButton_Clicked");

    this.setEnabled(false);
    RemoveMileStoneFrame fr = new RemoveMileStoneFrame(this);
  }

  /**
   * Click Action handler for searchPersonByYearButton
   * <p>
   * Action handler sets the calling frame disabled and calls the next frame
   * needed
   */
  private void searchPersonByYearButton_Clicked()
  {
    System.out.println("searchPersonByYearButton_Clicked");
    this.setEnabled(false);
    JFrame fr = new SearchYearSelector(this);
  }

  /**
   * Click Action handler for searchPersonByIntervalButton
   * <p>
   * Action handler sets the calling frame disabled and calls the next frame
   * needed
   */
  private void searchPersonByIntervalButton_Clicked()
  {
    System.out.println("searchPersonByIntervalButton_Clicked");
    this.setEnabled(false);
    JFrame fr = new SearchVipByInterval(this);
  }

  /**
   * Click Action handler for searchListByIntervalButton
   * <p>
   * Action handler sets the calling frame disabled and calls the next frame
   * needed
   */
  private void searchListByIntervalButton_Clicked()
  {
    System.out.println("searchListByIntervalButton_Clicked");
    this.setEnabled(false);
    JFrame fr = new SearchVipListByInterval(this);
  }

  /**
   * Insertion method
   * <p>
   * This method receives a Person, inserts it to the DefaultComboBoxModel and
   * refreshes the screen in case any information changed
   *
   * @param p
   *            The person to insert
   */
  public void addPerson(Person p)
  {
    comboBoxModel.addElement(p);
    vipComboBox.setSelectedItem(p);
  }

  /**
   * Insertion method
   * <p>
   * This method receives a Milestone and inserts it to the selected person in
   * the vipComboBox
   *
   * @param m
   *            The milestone to insert
   */
  public void addMileStone(MileStone m)
  {
    ((Person) (vipComboBox.getSelectedItem())).addMilestone(m);
    summaryArea.setText(((Person) vipComboBox.getSelectedItem())
        .lifeHistoryToString());

    this.update();
  }

  /**
   * Removal method
   * <p>
   * This method receives the year of the milestone to remove and removes it.
   *
   * @param n
   *            Year of the milestone to be removed
   */
  public void removeMileStone(int n)
  {
    ((Person) (vipComboBox.getSelectedItem())).removeMilestone(n);
  }

  /**
   * Search method
   * <p>
   * Method receives a specific year and searches the selected person in the
   * list for that specific year in this persons milestone tree, the results
   * are printed on the screen
   *
   * @param y
   *            The year to search
   */
  public Person searchPerson(Person person, int low, int high)
  {
    SearchThread job = new SearchThread(person, low, high);
    IntegratorThread intergrator = new IntegratorThread();
    intergrator.incJob();
    pool.execute(job);
   
    try
    {
      pool.awaitTermination(10, TimeUnit.MILLISECONDS);
    }
    catch (InterruptedException e)
    {
      e.printStackTrace();
    }
   
    summaryArea.setText(job.getResult().lifeHistoryToString());

    return job.getResult();
  }

  /**
   * Search method
   * <p>
   * This method receives an interval to search for in the person. it will
   * look for overlapping periods in the list of vips
   *
   * @param low
   *            The lower bound of the interval
   * @param high
   *            The higher bound of the interval
   */
  public void searchListSearchByInterval(int low, int high)
  {
    Vector<MileStone> result = new Vector<MileStone>();
    SearchThread job;
    Person p;
   
    IntegratorThread integrator = new IntegratorThread();
    pool.execute(integrator);
   
    for (int i = 0 ; i < vipComboBox.getItemCount() ; i++)
    {
      p = vipComboBox.getItemAt(i);
      job = new SearchThread(p, low, high);
      job.addListener(integrator);
      integrator.incJob();
      pool.execute(job);
    }
   
    try
    {
      pool.awaitTermination(100, TimeUnit.MILLISECONDS);
    }
    catch (InterruptedException e)
    {
      e.printStackTrace();
    }

    summaryArea.setText("");
   
    for (Person person : integrator.getResult())
    {
      summaryArea.setText(summaryArea.getText() + person.lifeHistoryToString());
    }
  }

  /**
   * Updates the text area after possible change.
   */
  public void update()
  {
    if (vipComboBox.getModel().getSelectedItem() != null)
    {
      summaryArea.setText(((Person) (vipComboBox.getModel()
          .getSelectedItem())).lifeHistoryToString());
    }
  }

  public Person getSelectedPerson()
  {
    return (Person) (vipComboBox.getSelectedItem());
  }

  public JComboBox<Person> vipComboBox;
  private DefaultComboBoxModel<Person> comboBoxModel;
  private JButton addPersonButton;
  private JButton removePersonButton;
  private JButton addMileStoneButton;
  private JButton removeMileStoneButton;
  private JButton searchPersonByYearButton;
  private JButton searchPersonByIntervalButton;
  private JButton searchListByintervalButton;
  public JTextArea summaryArea;
  private JPanel upperWestPanel;
  private JPanel lowerWestPanel;
  private JButton advancedSearchButton;
  private JPanel rightPanel;
  private JPanel panel;
  private JLabel dummyLabel3;
  private JLabel dummyLabel1;
  private JLabel dummyLabel2;
  private JLabel dummyLabel4;
  private JScrollBar scrollBar;
  private JScrollPane scrollPane;
}
TOP

Related Classes of gui.MainFrame

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.