import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

/**
 * A GUI Component that supports the drag-and-drop of Appointment objects.
 * 
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class AppointmentPanel extends JPanel implements AppointmentTransferer
{
  private static final long serialVersionUID = 1L;

  private Appointment appointment;
  private boolean     dragEnabled;
  private JLabel      dateLabel, descriptionLabel;
  
  /**
   * Construct an AppointmentPanel without a title.
   */
  public AppointmentPanel()
  {
    this(null);
  }
  
  /**
   * Construct an AppointmentPanel with a title.
   * 
   * @param title  The title.
   */
  public AppointmentPanel(String title)
  {
    super();
    setLayout(new GridLayout(2,1));
    if (title == null) setBorder(BorderFactory.createLineBorder(Color.BLACK));
    else               setBorder(BorderFactory.createTitledBorder(title));
      
    descriptionLabel = new JLabel();
    descriptionLabel.setBorder(BorderFactory.createTitledBorder("Description"));
    dateLabel = new JLabel();
    dateLabel.setBorder(BorderFactory.createTitledBorder("Date/Time"));
    add(descriptionLabel);
    add(dateLabel);
    
    setDragEnabled(true);
    setTransferHandler(new AppointmentTransferHandler());
    
    // Add a MouseListener that will forward mousePressed messages
    // to a TransferHandler as drags
    addMouseListener(
        new MouseAdapter()
        // An anonymous inner class that overrides mousePressed() in MouseAdapter
        {
          public void mousePressed(MouseEvent e) 
          {
            JComponent c = (JComponent)e.getSource();
            TransferHandler handler = c.getTransferHandler();
            handler.exportAsDrag(c, e, TransferHandler.MOVE);
          }
        }
    );
  }

  // Methods required by AppointmentTransferer
  
  /**
   * Get the Appointment on this AppointmentPanel.
   * 
   * @return  The Appointment
   */
  public Appointment getAppointment()
  {
    return appointment;
  }
  
  /**
   * Get the source actions supported by this ImageTransferer.
   * 
   * @return The supported source actions
   */
  public int getSourceActions()
  {
    return TransferHandler.COPY_OR_MOVE;
  }
  
  /**
   * Set the Appointment to display on this AppointmentPanel.
   * 
   * @param appointment  The Appointment
   */
  public void setAppointment(Appointment appointment)
  {
    this.appointment = appointment;
    if (appointment != null)
    {
      descriptionLabel.setText(appointment.getDescription());
      dateLabel.setText(appointment.getDate().toString());
    }
    else
    {
      descriptionLabel.setText("");
      dateLabel.setText("");
    }
  }

  // Other useful methods

  /**
   * Is drag enabled on this Component?
   * 
   * @return true if enabled; false otherwise
   */
  public boolean getDrageEnabled()
  {
    return dragEnabled;
  }

  /**
   * Set whether or not drag is enabled on this Component.
   * 
   * @param enabled  true to enable; false to disable
   */
  public void setDragEnabled(boolean enabled)
  {
    this.dragEnabled= enabled;
  }
}
