import java.awt.*;
import javax.swing.*;

/**
 * A cell renderer that can be used for cells containing Color objects.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class ColorCellRenderer extends JLabel implements ListCellRenderer<Color> 
{
  private static final long serialVersionUID = 1L;
  private static final Font FONT = new Font(Font.MONOSPACED, Font.PLAIN, 12);
  

  /**
   * Default Constructor.
   */
  public ColorCellRenderer() 
  {
    super();
    setOpaque(true);
    setFont(FONT);
  }

  /**
   * Get the component that does the rendering.
   *
   * @param table      The JTable that is being rendered
   * @param value      The value of the cell to be rendered
   * @param isSelected true if the cell is to be rendered with highlighting
   * @param hasFocise  true if the cell has the focus
   * @param row        The row of the cell being rendered
   * @param column     The column of the cell being rendered
   * @return           The Component to use as the renderer
   */
  public Component getListCellRendererComponent(JList<? extends Color> list, Color value, int index, boolean isSelected, boolean hasFocus) 
  {
    int rr = value.getRed();
    int gg = value.getGreen();
    int bb = value.getBlue();
    setText(String.format("%3d,%3d,%3d", rr, gg, bb));

    setForeground(value);
    
    if (isSelected) setBorder(BorderFactory.createLineBorder(value));
    else            setBorder(BorderFactory.createLineBorder(getBackground()));
    
    return this;
  }
}
