/***********************************************************************

GECCO, a platform for command and control gaming

Copyright (C) 2001 Henrik Bäärnhielm, Andreas Enblom, Niklas
Hallenfur, Karl Hasselström, Henrik Hägerström, Oskar Linde, Jing Fu
Zi and Jon Åslund

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.

***********************************************************************/

package gecco.client.dialogs;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

/**
 *  Creates a modal dialog for selecting the host and port to connect to.
 *
 * @author     Oskar Linde
 * @author     Andreas Enblom
 * @created    June 27, 2001
 */

public class ConnectionDialog extends JDialog {

    /**
     *  Description of the Field
     */
    private JTextField hostField;
    /**
     *  Description of the Field
     */
    private JTextField portField;
    /**
     *  Description of the Field
     */
    private static ConnectionDialog dialog;
    /**
     *  Description of the Field
     */
    private static HostPort hostPort;


    /**
     *  Constructor for the ConnectionDialog object
     *
     * @param  frame  Description of Parameter
     */
    private ConnectionDialog(Frame frame) {
        super(frame, "Connect to game", true);

        if (hostPort == null) {
            hostPort = new HostPort();
        }

        JButton cancelButton = new JButton("Cancel");
        JButton connectButton = new JButton("Connect");

        cancelButton.addActionListener(
            new ActionListener() {
                /**
                 *  Description of the Method
                 *
                 * @param  e  Description of Parameter
                 */
                public void actionPerformed(ActionEvent e) {
                    hostPort = null;
                    ConnectionDialog.dialog.setVisible(false);
                }
            });

        connectButton.addActionListener(
            new ActionListener() {
                /**
                 *  Description of the Method
                 *
                 * @param  e  Description of Parameter
                 */
                public void actionPerformed(ActionEvent e) {
                    String s = hostField.getText();
                    if (s.indexOf(' ') != -1) {
                        JOptionPane.showMessageDialog(ConnectionDialog.dialog,
                                "Invalid hostname.",
                                "Error",
                                JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    ConnectionDialog.hostPort.host = s;

                    try {
                        ConnectionDialog.hostPort.port =
                                Integer.parseInt(portField.getText());
                        ConnectionDialog.dialog.setVisible(false);
                    } catch (NumberFormatException ex) {
                        JOptionPane.showMessageDialog(ConnectionDialog.dialog,
                                "Invalid port.",
                                "Error",
                                JOptionPane.ERROR_MESSAGE);
                    }
                }
            });

        getRootPane().setDefaultButton(connectButton);

        JLabel hostLabel = new JLabel("Hostname");
        hostLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
        JLabel portLabel = new JLabel("Port");

        hostField = new JTextField(25);
        portField = new JTextField(6) {
		/**
		 *  Gets the MaximumSize attribute of the ConnectionDialog
		 *  object
		 *
		 * @return    The MaximumSize value
		 */
		public Dimension getMaximumSize() {
		    return getPreferredSize();
		}
	    };
	
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();
        JPanel inputPane = new JPanel();
        inputPane.setLayout(gridbag);

        constraints.anchor = GridBagConstraints.WEST;
        gridbag.setConstraints(hostLabel, constraints);
        inputPane.add(hostLabel);
        Component fill = Box.createHorizontalStrut(10);
        gridbag.setConstraints(fill, constraints);
        inputPane.add(fill);
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        gridbag.setConstraints(hostField, constraints);
        inputPane.add(hostField);

        constraints.gridwidth = GridBagConstraints.RELATIVE;
        gridbag.setConstraints(portLabel, constraints);
        inputPane.add(portLabel);
        constraints.gridwidth = GridBagConstraints.REMAINDER;
        gridbag.setConstraints(portField, constraints);
        inputPane.add(portField);

        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
        buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
        buttonPane.add(Box.createHorizontalGlue());
        buttonPane.add(cancelButton);
        buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
        buttonPane.add(connectButton);

        Container contentPane = getContentPane();
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        content.add(inputPane, BorderLayout.CENTER);
        content.add(buttonPane, BorderLayout.SOUTH);
        content.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        contentPane.add(content, BorderLayout.CENTER);

        setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        setResizable(false);
        pack();
    }


    /**
     *  Shows the dialog.
     *
     * @param  frame        the parent frame
     * @param  initialHost  the default hostname
     * @param  initialPort  the default port
     * @return              a {@link HostPort} structure containing the hostname
     *      and port or NULL if Cancel was pressed.
     */
    public static HostPort showDialog(Frame frame, String initialHost,
            int initialPort) {
        if (hostPort == null) {
            hostPort = new HostPort();
        }

        hostPort.host = initialHost;
        hostPort.port = initialPort;

        if (dialog == null) {
            dialog = new ConnectionDialog(frame);
        }

        dialog.hostField.setText(initialHost);
        dialog.portField.setText("" + initialPort);
        dialog.setLocationRelativeTo(frame);
        dialog.setVisible(true);

        return hostPort;
    }
}

