/*
 * @(#)SwatchNamePair.java    2.3   2 May 2006
 *
 * Copyright 2006
 * 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 java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;

/**
 * <p>Class <code>SwatchNamePair</code> provides a component with a
 * paint swatch and its name.  The swatch has the default size and
 * border.</p>
 *
 * <p>This class extends <code>TablePanel</code> for implementation
 * reasons only.  The user should not add or remove items from this
 * table or change its settings in any way.</p>
 *
 * @author  Richard Rasala
 * @version 2.3.2
 * @since   2.3.2
 */
public class SwatchNamePair
    extends TablePanel
{
    /** The paint. */
    private Paint paint;
    
    /** The paint name. */
    private String name;
    
    /** The paint swatch. */
    private PaintSwatch swatch;
    
    /** The label. */
    private JLabel label;
    
    
    /**
     * </p>This constructor provides a color name and locates the
     * corresponding color using the method
     * <code>Colors.getColorFromName</code>.</p>
     *
     * <p>Throws <code>NullPointerException</code> if the given name
     * is <code>null</code> or if the value returned from the method
     * <code>Colors.getColorFromName</code> is null.</p>
     *
     * @param name the name of the color from the <code>Colors</code>
     *             class
     */ 
    public SwatchNamePair(String name) {
        if (name == null)
            throw new NullPointerException
                ("Null color name passed to SwatchNamePair constructor");
        
        Color color = Colors.getColorFromName(name);
        
        if (color == null)
            throw new NullPointerException
                ("Invalid color name passed to SwatchNamePair constructor");
        
        initialize(color, name);
    }
    
    
    /**
     * </p>This constructor provides the paint to define the swatch
     * and its name.</p>
     *
     * <p>Throws <code>NullPointerException</code> if the given paint
     * or the given name is <code>null</code>.</p>
     *
     * @param paint the paint to define the swatch
     * @param name  the name of the paint
     */ 
    public SwatchNamePair(Paint paint, String name) {
        if (name == null)
            throw new NullPointerException
                ("Null paint name passed to SwatchNamePair constructor");
        
        if (paint == null)
            throw new NullPointerException
                ("Null paint passed to SwatchNamePair constructor");
        
        initialize(paint, name);
    }
    
    
    /** Private initialization method.  Assumes non-<code>null</code> parameters */
    private void initialize(Paint paint, String name) {
        this.paint  = paint;
        this.name   = name;
        this.swatch = new PaintSwatch(paint);
        this.label  = new JLabel(name);
        
        setOpaque(true);
        
        setRows(1);
        setColumns(2);
        
        setHorizontalGap(6);
        
        setTableAlignment(CENTER);
        
        addObject(swatch, 0, 0);
        addObject(label,  0, 1);
    }
    
    
    /**
     * <p>Returns the encapsulated paint.</p>
     *
     * @return the encapsulated paint
     */
    public Paint toPaint() {
        return paint;
    }
    
    
    /**
     * <p>Returns the encapsulated paint name.</p>
     *
     * @return the encapsulated paint name
     */
    public String toString() {
        return name;
    }
    
    
    /**
     * <p>Returns the encapsulated paint swatch.</p>
     *
     * @return the encapsulated paint swatch
     */
    public PaintSwatch toSwatch() {
        return swatch;
    }
    
}


