Package gui

Source Code of gui.AddMileStoneFrame

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

package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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.JTextField;

import nodes.MileStone;

import person.MileStonePlace;
import javax.swing.DefaultComboBoxModel;

/**
* This class extends JFrame and represents the dialog to add a milestone to a
* person
*
* @author Eugene Krapivin
*/
public class AddMileStoneFrame extends JFrame implements ActionListener
{
  private static final long serialVersionUID = -8843896728509853147L;

  private JFrame caller;

  private JLabel fromYearLabel;
  private JLabel toYearLabel;
  private JLabel tookPlaceLabel;
  private JLabel mileStoneLabel;
  private JTextField fromYearTextField;
  private JTextField toYearTextField;
  private JComboBox<String> mileStonePlaceComboBox;

  private JButton saveButton;
  private JButton cancelButton;
  private JPanel mainPanel;
  private JPanel upperPanel;
  private JComboBox mileStoneComboBox;

  /**
   * Class constructor.
   * <p>
   * This constructor sets the size and title of the frame, and the possition
   * calls the initialization method for the components.
   *
   * @param caller
   *            a reference to the calling frame.
   */
  public AddMileStoneFrame(JFrame caller)
  {
    super("Add milestone to person");

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation((d.width / 2) - 200, (d.height / 2) - 200);

    setPreferredSize(new Dimension(300, 170));
    setResizable(false);

    this.caller = caller;

    initializeComponents();
  }

  /**
   * Component initialization components.
   * <p>
   * This method initializes the components and sets them to their places it
   * initializes the layout of the main frame.
   */
  private void initializeComponents()
  {
    BorderLayout mainLayout = new BorderLayout();
    getContentPane().setLayout(mainLayout);

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

    JPanel bottomPanel = new JPanel(new FlowLayout());
    mainPanel.add(bottomPanel, BorderLayout.CENTER);
    saveButton = new JButton("Save");
    cancelButton = new JButton("Cancel");
    saveButton.addActionListener(this);
    cancelButton.addActionListener(this);
    bottomPanel.add(saveButton);
    bottomPanel.add(cancelButton);

    upperPanel = new JPanel();
    mainPanel.add(upperPanel, BorderLayout.NORTH);
    upperPanel.setLayout(new GridLayout(0, 2, 0, 0));

    fromYearLabel = new JLabel(" From year: ");
    upperPanel.add(fromYearLabel);
    fromYearTextField = new JTextField();
    upperPanel.add(fromYearTextField);
    toYearLabel = new JLabel(" To year: ");
    upperPanel.add(toYearLabel);
    toYearTextField = new JTextField();
    upperPanel.add(toYearTextField);
    mileStoneLabel = new JLabel("Milestone:");
    upperPanel.add(mileStoneLabel);

    mileStoneComboBox = new JComboBox();
    mileStoneComboBox.setModel(new DefaultComboBoxModel(new String[] {"Army", "School", "High school", "College", "University", "First job", "Marriage", "Devorce", "Tour", "Trip", "Agriculture minister", "Business minister", "Commerce minister", "Communication minister", "Culture minister", "Defence minister", "Education minister", "Energy minister", "Environment minister", "Finance minister", "Foreign minister", "Health minister", "Industry minister", "Information minister", "Inland revenue minister", "Interior minister", "Justice minister", "Labour minister", "Prime minister", "Public works minister", "Science minister", "Sports minister", "Tourism minister", "Transport minister"}));
    upperPanel.add(mileStoneComboBox);
    tookPlaceLabel = new JLabel(" Place: ");
    upperPanel.add(tookPlaceLabel);
    mileStonePlaceComboBox = new JComboBox<String>(
        MileStonePlace.countryList);
    upperPanel.add(mileStonePlaceComboBox);

    this.pack();
  }

  /**
   * ActionListener interface implementation
   * <p>
   * The method routes the actions to the correct handling method
   */
  @Override
  public void actionPerformed(ActionEvent e)
  {
    if (e.getSource() == saveButton)
    {
      saveButton_Clicked();
    }
    else if (e.getSource() == cancelButton)
    {
      cancelButton_Clicked();
    }
  }

  /**
   * Cancel clicked action handler
   */
  private void cancelButton_Clicked()
  {
    caller.setEnabled(true);
    this.dispose();
  }

  /**
   * Save clicked action handler
   */
  private void saveButton_Clicked()
  {
    MileStone m = null;

    try
    {
      m = new MileStone(Integer.parseInt(fromYearTextField.getText()),
          Integer.parseInt(toYearTextField.getText()),
          new MileStonePlace((String) mileStonePlaceComboBox
              .getSelectedItem()),
          mileStoneComboBox.getSelectedItem().toString());

      ((MainFrame) caller).addMileStone(m);

      // JOptionPane.showMessageDialog(this, "Added successfully");

      caller.setEnabled(true);
      this.dispose();
    }
    catch (NumberFormatException e)
    {
      JOptionPane.showMessageDialog(this, "Bad year input");
    }
    catch (Exception e)
    {
      JOptionPane.showMessageDialog(this, e.getMessage());
    }
    finally
    {
      if (m != null)
        System.out.println(m.toString());
    }
  }
}
TOP

Related Classes of gui.AddMileStoneFrame

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.