/**
 * @(#)ColorPaneDialog.java    2.6.0   31 August 2007
 *
 * Copyright 2007
 * 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.util.MathUtilities;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

/**
 * <p>Class <code>ColorPaneDialog</code> constructs an
 * OK-Cancel dialog whose content is a <code>ColorPane</code>.
 * 
 * @author  Richard Rasala
 * @version 2.6.0
 * @since   2.6.0
 */
public class ColorPaneDialog
    extends GeneralDialog
{
    /** The color pane forming the content area of the dialog. */
    ColorPane colorPane = null;
    
    
    /**
     * The default constructor which initializes the color pane
     * with white.
     */
    public ColorPaneDialog() {
        this(Colors.white);
    }
    
    
    /**
     * The default constructor which initializes the color pane
     * with the given color.
     * 
     * @param color the initial color for the color pane
     */
    public ColorPaneDialog(Color color) {
        super(new ColorPane(color));
        
        colorPane = (ColorPane) getDialogContents();
        Font buttonFont = colorPane.getButtonFont();
        
        Action ok     = DialogAction.makeTrivialAction("OK");
        Action cancel = DialogAction.makeTrivialAction("Cancel");
        
        JButton okButton =
            addAction(ok, DialogAction.AUTO_CLOSE);
        
        JButton cancelButton =
            addAction(cancel, DialogAction.SET_CANCEL);
        
        setWindowClosingAction(cancel, DialogAction.SET_CANCEL);
        
        okButton.setFont(buttonFont);
        cancelButton.setFont(buttonFont);
        
        setDeepBackground(Colors.white);
    }
    
    
    /** Returns the current color of the internal color pane. */
    public Color getColor() {
        return colorPane.getColor();
    }
    
    
    /**
     * Sets the current color of the internal color pane.
     * 
     * @param color the color to set
     */
    public void setColor(Color color) {
        colorPane.setColor(color);
    }
    
    
    /** Returns the initial color of the internal color pane. */
    public Color getInitialColor() {
        return colorPane.getInitialColor();
    }
    
    
    /**
     * Sets the initial color of the internal color pane.
     * 
     * @param color the color to set
     */
    public void setInitialColor(Color color) {
        colorPane.setInitialColor(color);
    }
    
    
    /**
     * Sets both the current color and the initial color
     * of the internal color pane.
     * 
     * @param color the color to set
     */
    public void setBothColors(Color color) {
        colorPane.setColor(color);
        colorPane.setInitialColor(color);
    }
    
    
    /**
     * <p>This test program shows a <code>ColorPaneDialog</code>.
     * When the color pane dialog is closed,
     * either by OK or Cancel,
     * then an informative dialog is shown.</p>
     * 
     * @param args ignored
     */
    public static void main(String[] args) {
        int r = MathUtilities.randomInt(0, 255);
        int g = MathUtilities.randomInt(0, 255);
        int b = MathUtilities.randomInt(0, 255);
        
        Color color = new Color(r, g, b);
        
        ColorPaneDialog dialog = new ColorPaneDialog(color);
        
        dialog.setVisible(true);
        
        String message = "";
        
        if (dialog.wasCancelled()) {
            message = "Color pane dialog was cancelled";
            
            GeneralDialog.showOKDialog(message, "");
        }
        else {
            color = dialog.getColor();
            
            PaintSwatch swatch = new PaintSwatch(color, 200, 200);
            
            message = "Selected color = " + XColor.colorToString(color);
            
            Object[] stuff = { swatch, message };
            
            VTable panel = new VTable(stuff, 10, 10, CENTER);
            
            panel.emptyBorder(10);
            
            GeneralDialog.showOKDialog(panel, "");
        }
    }
    
}

