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

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 a modal dialog box that indicates that something is
 *  loading. There is a progress bar, that is to be updated as the loading
 *  progresses. At most one loading dialog box should be open at any given time.
 *
 * @author     Andreas Enblom
 * @created    June 27, 2001
 */
public class LoadingInfo {
    /**
     *  Description of the Field
     */
    private static JDialog dialog;
    // the error dialog
    /**
     *  Description of the Field
     */
    private static JPanel content;
    // dialog box's contents
    /**
     *  Description of the Field
     */
    private static JLabel message;
    // label containing the message
    /**
     *  Description of the Field
     */
    private static double progress;
    // how much that has been completed (0-1)


    /**
     *  No contructor is necessary.
     */
    private LoadingInfo() {
    }


    /**
     *  Displays a new dialog with the given message. The progress bar is set to
     *  zero.
     *
     * @param  msg    The 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 msg, Frame owner) {
        progress = 0;
        dialog = new JDialog(owner, "Please wait...", true);
        message.setText(msg);
        dialog.getContentPane().setLayout(new BorderLayout());
        dialog.getContentPane().add(content);
        dialog.pack();
        dialog.setLocationRelativeTo(owner);
        dialog.setVisible(true);
    }


    /**
     *  Update the progress bar.
     *
     * @param  progress  A number between 0 and 1, indicating how much of the
     *      task that has been completed (0 = nothing, 1 = all).
     */
    public static void update(double progress) {
        LoadingInfo.progress = progress;
        content.repaint();
    }


    /**
     *  Hides the dialog.
     */
    public static void hide() {
        dialog.setVisible(false);
    }

    static {
        int fill = 10;
        // amount of white space

        message = new JLabel();
        message.setAlignmentX(Component.CENTER_ALIGNMENT);

        JComponent statusBar =
            new JComponent() {
                /**
                 *  Gets the PreferredSize attribute of the LoadingInfo object
                 *
                 * @return    The PreferredSize value
                 */
                public Dimension getPreferredSize() {
                    return new Dimension(300, 20);
                }


                /**
                 *  Description of the Method
                 *
                 * @param  g  Description of Parameter
                 */
                public void paint(Graphics g) {
                    int width = this.getBounds().width - 2;
                    int height = this.getBounds().height - 2;
                    g.setColor(Color.red);
                    g.fillRect(1, 1, (int) Math.rint(width * progress), height);
                    g.setColor(new Color(255, 0, 0, 64));
                    g.fillRect(1 + (int) Math.rint(width * progress), 1,
                            width - (int) Math.rint(width * progress), height);
                    g.setColor(Color.black);
                    g.drawRect(0, 0, width + 1, height + 1);
                }
            };

        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(statusBar);
    }
}
