/*
 * @(#)SwatchNameRenderer.java    2.3.3  10 December 2004
 *
 * Copyright 2004
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.gui;

import edu.neu.ccs.*;
import edu.neu.ccs.gui.*;
import edu.neu.ccs.quick.*;
import edu.neu.ccs.util.*;

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;

/**
 * <p>Class <code>SwatchNameRenderer</code> is designed to implement
 * <code>ListCellRenderer</code> by providing pairs each with a
 * paint swatch and its name.</p>
 *
 * @author  Richard Rasala
 * @version 2.3.3
 * @since   2.3.2
 */
public class SwatchNameRenderer
    implements ListCellRenderer
{
    /** The hash table of color names and swatch-name pairs. */
    private StringObjectMap nameCellMap;
    
    /** The maximum dimension of all swatch-name pairs. */
    private Dimension maxDimension = new Dimension(0, 0);
    
    
    /**
     * </p>This constructor provides a renderer for swatch-name
     * pairs generated from the color names obtained via
     * </code>Colors.getColorNamesAsArray</code>.</p>
     */ 
    public SwatchNameRenderer() {
        String[] names = Colors.getColorNamesAsArray();
        
        int length = names.length;
        
        Object[][] pairs = new Object[length][2];
        
        for (int i = 0; i < length; i++) {
            SwatchNamePair swatchpair = new SwatchNamePair(names[i]);
            
            pairs[i][0] = names[i];
            pairs[i][1] = swatchpair;
            
            maxDimension =
                DimensionUtilities.max
                    (maxDimension, swatchpair.getPreferredSize());
        }
        
        nameCellMap = new StringObjectMap(pairs);
    }
    
    
    /**
     * <p>Returns the list cell corresponding to the given color name.</p>
     *
     * @param name the color name
     */
    private SwatchNamePair getListCellFromMap(String name) {
        return (SwatchNamePair) nameCellMap.getObject(name);
    }
    
    
    /**
     * <p>Returns the array of color name strings installed in this
     * renderer.  This returns the strings installed at construction
     * and avoids problems if additional color name pairs are later
     * installed globally.</p>
     *
     * @return the array of color name strings
     */
    public String[] getStrings() {
        return nameCellMap.getStrings();
    }
    
    
    /**
     * <p>Returns the maximum preferred size of all swatch-name pairs
     * in this renderer.</p>
     *
     * @return the maximum preferred size of all swatch-name pairs
     */
    public Dimension getMaximumPreferredSize() {
        return DimensionUtilities.createDimension(maxDimension);
    }
    
    
    /**
     * <p>Returns the swatch name pair component associated with the
     * given <code>String</code> value in the given list;  sets the
     * foreground and background of the swatch name pair depending
     * on the given isSelected parameter.</p>
     *
     * <p>This method implements the <code>ListCellRenderer</code>
     * interface.</p>
     *
     * <p>Returns <code>null</code> if an unexpected error occurs.</p>
     *
     * @param  list         the JList to paint
     * @param  value        the value returned by <code>list.getModel().getElementAt(index)</code>
     * @param  index        the index of the cell
     * @param  isSelected   true if the specified cell was selected
     * @param  cellHasFocus true if the specified cell has the focus
     * @return this object as the list cell renderer component
     */
    public Component getListCellRendererComponent(
        JList list,
        Object value,
        int index,
        boolean isSelected,
        boolean cellHasFocus)
    {
        if (! (value instanceof String))
            return null;
        
        String name = (String) value;
        
        SwatchNamePair swatchpair = getListCellFromMap(name);
        
        if (swatchpair == null)
            return null;
        
        if (isSelected) {
            swatchpair.setBackground(list.getSelectionBackground());
            swatchpair.setForeground(list.getSelectionForeground());
        } else {
            swatchpair.setBackground(list.getBackground());
            swatchpair.setForeground(list.getForeground());
        }
        
        return swatchpair;
    }
    
}


