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

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 among the roles given.
 *
 * @author     Oskar Linde
 * @created    June 27, 2001
 */

public class RoleSelectionDialog extends JDialog {
    /**
     *  Description of the Field
     */
    private JList list;
    /**
     *  Description of the Field
     */
    private static RoleSelectionDialog dialog;
    /**
     *  Description of the Field
     */
    private static String value = null;


    /**
     *  Constructor for the RoleSelectionDialog object
     *
     * @param  frame  Description of Parameter
     */
    private RoleSelectionDialog(Frame frame) {
        super(frame, "Pick your role", true);

        //buttons
        JButton cancelButton = new JButton("Cancel");
        final JButton setButton = new JButton("Select");
        cancelButton.addActionListener(
            new ActionListener() {
                /**
                 *  Description of the Method
                 *
                 * @param  e  Description of Parameter
                 */
                public void actionPerformed(ActionEvent e) {
                    RoleSelectionDialog.dialog.setVisible(false);
                }
            });
        setButton.addActionListener(
            new ActionListener() {
                /**
                 *  Description of the Method
                 *
                 * @param  e  Description of Parameter
                 */
                public void actionPerformed(ActionEvent e) {
                    RoleSelectionDialog.value = (String) (list.getSelectedValue());
                    RoleSelectionDialog.dialog.setVisible(false);
                }
            });
        getRootPane().setDefaultButton(setButton);

        //main part of the dialog
        list = new JList();
        list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        list.addMouseListener(
            new MouseAdapter() {
                /**
                 *  Description of the Method
                 *
                 * @param  e  Description of Parameter
                 */
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 2) {
                        setButton.doClick();
                    }
                }
            });
        JScrollPane listScroller = new JScrollPane(list);
        listScroller.setPreferredSize(new Dimension(250, 80));
        //XXX: Must do the following, too, or else the scroller thinks
        //XXX: it's taller than it is:
        listScroller.setMinimumSize(new Dimension(250, 80));
        listScroller.setAlignmentX(LEFT_ALIGNMENT);

        //Create a container so that we can add a title around
        //the scroll pane.  Can't add a title directly to the
        //scroll pane because its background would be white.
        //Lay out the label and scroll pane from top to button.
        JPanel listPane = new JPanel();
        listPane.setLayout(new BoxLayout(listPane, BoxLayout.Y_AXIS));
        JLabel label = new JLabel("Available roles");
        label.setLabelFor(list);
        listPane.add(label);
        listPane.add(Box.createRigidArea(new Dimension(0, 5)));
        listPane.add(listScroller);
        listPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        //Lay out the buttons from left to right.
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
        buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
        buttonPane.add(Box.createHorizontalGlue());
        buttonPane.add(cancelButton);
        buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
        buttonPane.add(setButton);

        //Put everything together, using the content pane's BorderLayout.
        Container contentPane = getContentPane();
        contentPane.add(listPane, BorderLayout.CENTER);
        contentPane.add(buttonPane, BorderLayout.SOUTH);

        setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

        pack();
    }


    /**
     *  Sets the Value attribute of the RoleSelectionDialog object
     *
     * @param  newValue  The new Value value
     */
    private void setValue(String newValue) {
        value = newValue;
        list.setSelectedValue(value, true);
    }


    /**
     *  Shows the dialog.
     *
     * @param  frame           the parent frame
     * @param  possibleValues  an array of the possible roles
     * @return                 a role or NULL if Cancel was pressed.
     */
    public static String showDialog(Frame frame, String[] possibleValues) {
        if (dialog == null) {
            dialog = new RoleSelectionDialog(frame);
        }
        value = null;

        dialog.list.setListData(possibleValues);

        dialog.setLocationRelativeTo(frame);
        dialog.setVisible(true);

        return value;
    }
}
