/* ************************
* 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.DefaultComboBoxModel;
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 person.Person;
import java.awt.Window.Type;
/**
* This class extends JFrame and represents the dialog to add a milestone to a
* person
*
* @author Eugene Krapivin
*/
public class AddPersonFrame extends JFrame implements ActionListener
{
private static final long serialVersionUID = -8843896728509853147L;
private MainFrame 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;
private JLabel firstNameLabel;
private JLabel lastNameLabel;
private JTextField firstNameTextField;
private JTextField lastNameTextField;
/**
* 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 AddPersonFrame(MainFrame caller)
{
super("Add person");
this.setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((d.width / 2) - 200, (d.height / 2) - 200);
this.setPreferredSize(new Dimension(300, 220));
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));
upperPanel = new JPanel();
mainPanel.add(upperPanel, BorderLayout.NORTH);
upperPanel.setLayout(new GridLayout(6, 2, 0, 0));
firstNameLabel = new JLabel("First name:");
upperPanel.add(firstNameLabel);
firstNameTextField = new JTextField();
upperPanel.add(firstNameTextField);
firstNameTextField.setColumns(10);
lastNameLabel = new JLabel("Last name:");
upperPanel.add(lastNameLabel);
lastNameTextField = new JTextField();
upperPanel.add(lastNameTextField);
lastNameTextField.setColumns(10);
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);
JPanel bottomPanel = new JPanel(new FlowLayout());
mainPanel.add(bottomPanel);
saveButton = new JButton("Save");
cancelButton = new JButton("Cancel");
saveButton.addActionListener(this);
cancelButton.addActionListener(this);
bottomPanel.add(saveButton);
bottomPanel.add(cancelButton);
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()
{
Person p = null;
try
{
p = new Person(firstNameTextField.getText(), lastNameTextField.getText(),
new MileStone(Integer.parseInt(fromYearTextField.getText()),
Integer.parseInt(toYearTextField.getText()),
new MileStonePlace(mileStonePlaceComboBox.getSelectedItem().toString()),
mileStoneComboBox.getSelectedItem().toString())
);
caller.setEnabled(true);
this.dispose();
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(this, "Bad year input");
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this, e.getMessage());
}
finally
{
if (p != null)
{
System.out.println(p.toString());
caller.addPerson(p);
}
}
}
}