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

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

/**
 *  This class supplies alert boxes that display error messages.
 *
 * @author     Andreas Enblom
 * @created    June 27, 2001
 */
public class Error {
    /**
     *  No contructor is necessary.
     */
    private Error() {
    }


    /**
     *  Displays a modal alert dialog with the given message. There is an OK
     *  button that closes the dialog.
     *
     * @param  error  The error message to display. If the message should be
     *      formatted in a way different from a one line standard JLabel, use
     *      html-format of the text.
     * @param  owner  The frame that owns the modal error dialog.
     */
    public static void display(String error, Frame owner) {
        int fill = 10;
        // amount of white space
        JDialog dialog = new JDialog(owner, "Error", true);

        JLabel message = new JLabel(error);
        message.setAlignmentX(Component.CENTER_ALIGNMENT);
        JButton okButton = new JButton("OK");
        okButton.addActionListener(new OKAction(dialog));
        JPanel content = new JPanel();
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
        content.setBorder(BorderFactory.createEmptyBorder(fill, fill,
                fill, fill));
        content.add(message);
        content.add(Box.createVerticalStrut(fill));
        content.add(okButton);

        dialog.getContentPane().setLayout(new BorderLayout());
        dialog.getContentPane().add(content);
        dialog.getRootPane().setDefaultButton(okButton);
        dialog.pack();
        dialog.setLocationRelativeTo(owner);
        dialog.setVisible(true);
    }


    /**
     *  ActionListener that closes the given dialog when the action is
     *  performed.
     *
     * @author     d98-kha
     * @created    June 27, 2001
     */
    static class OKAction implements ActionListener {
        /**
         *  Description of the Field
         */
        JDialog dialog;


        /**
         *  Constructor for the OKAction object
         *
         * @param  dialog  Description of Parameter
         */
        public OKAction(JDialog dialog) {
            this.dialog = dialog;
        }


        /**
         *  Description of the Method
         *
         * @param  e  Description of Parameter
         */
        public void actionPerformed(ActionEvent e) {
            dialog.setVisible(false);
        }
    }
}
