package com.supinfo.jastermindclient.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.supinfo.jastermindclient.JMClient;
/**
* The "New game" dialog box. It allows the user to specify the required
* informations to connect to a JasterMind server and start a new game.
*
* @author Mathias Védrines
*
*/
public class JMNewGameDialog extends JDialog implements ActionListener
{
private static final long serialVersionUID = 1L;
private JMFrame parent;
private JTextField ipField;
private JTextField portField;
private JButton cancel;
private JButton connect;
private JCheckBox allowDupCheck;
private Integer[] levels = { 8, 10, 12 };
private JComboBox levelSelector;
public JMNewGameDialog(JMFrame parent)
{
super(parent);
this.parent = parent;
this.setModal(true);
this.setTitle("New game");
this.setLocationRelativeTo(parent);
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel formLayout = new JPanel(new GridBagLayout());
JPanel btnLayout = new JPanel(new FlowLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel label = new JLabel("Server IP: ");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
formLayout.add(label, c);
ipField = new JTextField("localhost", 10);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
formLayout.add(ipField, c);
label = new JLabel("Server port: ");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
formLayout.add(label, c);
portField = new JTextField("42000", 4);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
formLayout.add(portField, c);
label = new JLabel("Allow duplicate pegs: ");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
formLayout.add(label, c);
allowDupCheck = new JCheckBox();
allowDupCheck.setSelected(true);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
formLayout.add(allowDupCheck, c);
label = new JLabel("Max number of attempts: ");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
formLayout.add(label, c);
levelSelector = new JComboBox(levels);
levelSelector.setSelectedIndex(1);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 3;
formLayout.add(levelSelector, c);
cancel = new JButton("Cancel");
connect = new JButton("Connect");
cancel.addActionListener(this);
connect.addActionListener(this);
btnLayout.add(cancel);
btnLayout.add(connect);
mainPanel.add(formLayout, BorderLayout.CENTER);
mainPanel.add(btnLayout, BorderLayout.SOUTH);
this.setContentPane(mainPanel);
// allows to validate the dialog box by pressing [Enter]
this.getRootPane().setDefaultButton(connect);
// the dialog box size will be set to fit to its content
this.pack();
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource().equals(cancel))
{
setVisible(false);
}
else if (e.getSource().equals(connect))
{
String ip = ipField.getText();
int port = 42000;
boolean allowDup = allowDupCheck.isSelected();
Integer maxAttempts = (Integer) levelSelector.getSelectedItem();
try
{
port = Integer.parseInt(portField.getText());
} catch (NumberFormatException nfe)
{
}
JMClient game = new JMClient(ip, port, allowDup,
maxAttempts.intValue());
parent.setGame(game);
setVisible(false);
}
}
}