/**
 * Copyright (c) 2005 Red Hat, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Component of: Red Hat Application Server
 *
 * Initial Developers: Gregory Lapouchnian
 *                     Patrick Smith
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */
package olstore.client;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.TransferHandler;
import javax.swing.border.TitledBorder;

/**
 * A panel to display all the available information about an item to the user.
 */
public class ItemPanel extends JPanel implements MouseListener,
        MouseMotionListener {

    /** Used to determine whether the mouse motion is considered to be a drag */
    private MouseEvent firstMouseEvent = null;

    /** The product item that this panel represents. */
    private Item item;

    /** The alternating background color. */
    private Color background;

    /** The number of pixels that define a mouse drag */
    private static final int PIXEL_DISTANCE = 5;

    /**
     * Panel to display information about an item.
     * @param title the name of the product
     * @param description the description of the item
     * @param price the item's price
     * @param category the item's product category
     * @param id the item's unique ID
     * @param image the image showing the item
     * @param bgColor which of the alternating colors to use for this panel
     */
    public ItemPanel(String title, String description, String price,
            String category, String id, BufferedImage image, Color bgColor) {

        background = bgColor;
        addMouseMotionListener(this);
        addMouseListener(this);

        item = new Item(title, description, price, category, id, image);

        buildPanel();
    }

    /**
     * Build up the components that make up this panel.
     */
    public void buildPanel() {
        Icon icon = new ImageIcon(item.getImage());

        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        gbl.setConstraints(this, c);
        setLayout(gbl);

        Component spacer = Box.createRigidArea(new Dimension(20, 75));

        JLabel imageLabel = new JLabel(icon);

        add(imageLabel);
        add(spacer);

        JPanel descrip = new JPanel();
        descrip.setLayout(new GridLayout(3, 1));

        descrip.add(new JLabel(item.getTitle()));
        JTextArea descriptionLabel = new JTextArea(item.getDescription());

        descriptionLabel.setSize(350, 250);
        descriptionLabel.setOpaque(false);
        descriptionLabel.setEditable(false);
        descriptionLabel.setFocusable(false);
        descriptionLabel.setEnabled(false);
        descriptionLabel.setDisabledTextColor(Color.BLACK);
        descriptionLabel.setBorder(null);
        descriptionLabel.setLineWrap(true);
        descriptionLabel.setWrapStyleWord(true);

        descrip.add(descriptionLabel);
        descrip.setBackground(background);
        descrip.add(new JLabel("Price: $" + item.getPrice()));
        add(descrip);

        TitledBorder border = BorderFactory.createTitledBorder(item
                .getCategory());
        border.setTitleJustification(TitledBorder.CENTER);
        setBorder(border);
    }

    /**
     * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
     */
    public void mousePressed(MouseEvent e) {
        firstMouseEvent = e;
        e.consume();
    }

    /**
     * Determine whether the mouse drag is long enough to initiate drag and drop
     * operation.
     * @param e the mouse event
     */
    public void mouseDragged(MouseEvent e) {
        if (firstMouseEvent != null) {
            e.consume();

            int dx = Math.abs(e.getX() - firstMouseEvent.getX());
            int dy = Math.abs(e.getY() - firstMouseEvent.getY());
            //Arbitrarily define a 5-pixel shift as the
            //official beginning of a drag.
            if (dx > PIXEL_DISTANCE || dy > PIXEL_DISTANCE) {
                //This is a drag, not a click.
                JComponent c = (JComponent) e.getSource();
                TransferHandler handler = c.getTransferHandler();
                //Tell the transfer handler to initiate the drag.
                handler.exportAsDrag(c, firstMouseEvent, TransferHandler.COPY);
                firstMouseEvent = null;
            }
        }
    }

    /**
     * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
     */
    public void mouseReleased(MouseEvent e) {
        firstMouseEvent = null;
    }

    /**
     * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
     */
    public void mouseMoved(MouseEvent e) {
    }

    /**
     * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
     */
    public void mouseClicked(MouseEvent arg0) {
    }

    /**
     * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
     */
    public void mouseEntered(MouseEvent arg0) {
    }

    /**
     * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
     */
    public void mouseExited(MouseEvent arg0) {
    }

    /**
     * Get the item from this panel.
     * @return Returns the item.
     */
    public Item getItem() {
        return item;
    }

    /**
     * Set the item for this panel.
     * @param item The item to set.
     */
    public void setItem(Item item) {
        this.item = item;
    }
}
