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

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.infopanels;
import gecco.client.*;
import java.awt.*;

import java.util.*;
import javax.swing.*;

/**
 *  This is a panel that contains info about a Piece. It contains a property
 *  display, an action display and a message display.
 *
 * @author     Andreas Enblom
 * @created    June 27, 2001
 */
public class PieceInfoPanel extends JPanel {

    /**
     *  Description of the Field
     */
    Piece piece;
    /**
     *  Description of the Field
     */
    JLabel pieceName;
    /**
     *  Description of the Field
     */
    JLabel pieceType;
    /**
     *  Description of the Field
     */
    ActionDisplay actionDisplay;
    /**
     *  Description of the Field
     */
    PropertyDisplay propertyDisplay;
    /**
     *  Description of the Field
     */
    MessageDisplay messageDisplay;
    /**
     *  The amount (in pixels) of white space between things.
     */
    public final static int FILL = 10;


    /**
     * @param  g  The Game object that whose pieces are displayed in this panel.
     *      Used for aborting actions.
     */
    public PieceInfoPanel(Game g) {
        // initilize name labels and info displays
        actionDisplay =
            new ActionDisplay(g) {
                /**
                 *  Gets the PreferredSize attribute of the PieceInfoPanel
                 *  object
                 *
                 * @return    The PreferredSize value
                 */
                public Dimension getPreferredSize() {
                    return this.getMinimumSize();
                }


                /**
                 *  Gets the MaximumSize attribute of the PieceInfoPanel object
                 *
                 * @return    The MaximumSize value
                 */
                public Dimension getMaximumSize() {
                    return this.getMinimumSize();
                }
            };
        actionDisplay.setAlignmentX(Component.CENTER_ALIGNMENT);
        propertyDisplay = new PropertyDisplay();
        propertyDisplay.setAlignmentX(Component.CENTER_ALIGNMENT);
        messageDisplay =
            new MessageDisplay() {
                /**
                 *  Gets the PreferredSize attribute of the PieceInfoPanel
                 *  object
                 *
                 * @return    The PreferredSize value
                 */
                public Dimension getPreferredSize() {
                    return new Dimension(400, 150);
                }
            };
        messageDisplay.setAlignmentX(Component.CENTER_ALIGNMENT);
        pieceName = new JLabel(" ");
        pieceName.setHorizontalAlignment(SwingConstants.CENTER);
        pieceName.setAlignmentX(Component.CENTER_ALIGNMENT);
        pieceType = new JLabel(" ");
        pieceType.setHorizontalAlignment(SwingConstants.CENTER);
        pieceType.setAlignmentX(Component.CENTER_ALIGNMENT);

        // set up layout
        Box propertiesAndActions = Box.createHorizontalBox();
        propertiesAndActions.add(propertyDisplay);
        propertiesAndActions.add(Box.createHorizontalStrut(FILL));
        propertiesAndActions.add(actionDisplay);

        JPanel pieceInfo = new JPanel();
        pieceInfo.setLayout(new BoxLayout(pieceInfo, BoxLayout.Y_AXIS));
        pieceInfo.add(Box.createVerticalStrut(FILL));
        pieceInfo.add(pieceName);
        pieceInfo.add(pieceType);
        pieceInfo.add(Box.createVerticalStrut(FILL));
        pieceInfo.add(propertiesAndActions);
        pieceInfo.add(Box.createVerticalStrut(FILL));
        pieceInfo.add(messageDisplay);
        pieceInfo.add(Box.createVerticalGlue());
        pieceInfo.add(Box.createVerticalStrut(FILL));

        JPanel all = new JPanel();
        all.setLayout(new BoxLayout(all, BoxLayout.X_AXIS));
        all.add(Box.createHorizontalStrut(FILL));
        all.add(pieceInfo);
        all.add(Box.createHorizontalStrut(FILL));
        all.setBorder(BorderFactory.createEtchedBorder());

        this.setLayout(new BorderLayout());
        this.add(Box.createHorizontalStrut(FILL), BorderLayout.EAST);
        this.add(Box.createHorizontalStrut(FILL), BorderLayout.WEST);
        this.add(Box.createVerticalStrut(FILL), BorderLayout.NORTH);
        this.add(Box.createVerticalStrut(FILL), BorderLayout.SOUTH);
        this.add(all, BorderLayout.CENTER);
    }


    /**
     *  Sets the piece whose information is displayed in this info panel.
     *
     * @param  p  The new Piece value
     */
    public void setPiece(Piece p) {
        piece = p;
        if ((p == null) || (p.getName() == null) || (p.getName().equals(""))) {
            // XXX this preserves the size
            pieceName.setText(" ");
            pieceType.setText(" ");
        } else {
            pieceName.setText(p.getName());
            pieceType.setText(p.getType());
        }
        propertyDisplay.setPiece(p);
        actionDisplay.setPiece(p);
        messageDisplay.setPiece(p);
    }


    /**
     *  Call this when the ongoing or queued actions of the piece has changed.
     */
    public void updateActionDisplay() {
        actionDisplay.setPiece(piece);
    }


    /**
     *  Call this when the properties of the piece has changed.
     */
    public void updatePropertyDisplay() {
        propertyDisplay.setPiece(piece);
    }


    /**
     *  Call this when the messages of the piece has changed.
     */
    public void updateMessageDisplay() {
        messageDisplay.setPiece(piece);
    }
}
