//
// SwingColorWidget.java
//

/*
ImageJ software for multidimensional image processing and analysis.

Copyright (c) 2010, ImageJDev.org.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the names of the ImageJDev.org developers nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

package imagej.plugin.ui.swing;

import imagej.plugin.ui.ColorWidget;
import imagej.plugin.ui.ParamModel;
import imagej.util.ColorRGB;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;

/**
 * Swing implementation of file selector widget.
 * 
 * @author Curtis Rueden
 */
public class SwingColorWidget extends SwingInputWidget implements
	ActionListener, ColorWidget
{

	private static final int SWATCH_WIDTH = 16, SWATCH_HEIGHT = 16;

	private final JButton choose;
	private Color color;

	public SwingColorWidget(final ParamModel model) {
		super(model);

		choose = new JButton();
		add(choose, BorderLayout.CENTER);
		choose.addActionListener(this);

		refresh();
	}

	// -- ActionListener methods --

	@Override
	public void actionPerformed(final ActionEvent e) {
		final Color choice =
			JColorChooser.showDialog(choose, "Select a color", color);
		if (choice == null) return;
		model.setValue(color);
		refresh();
	}

	// -- ColorWidget methods --

	@Override
	public ColorRGB getColor() {
		return new ColorRGB(color.getRed(), color.getGreen(), color.getBlue());
	}

	// -- InputWidget methods --

	@Override
	public void refresh() {
		color = (Color) model.getValue();
		final BufferedImage image = new BufferedImage(SWATCH_WIDTH, SWATCH_HEIGHT,
			BufferedImage.TYPE_INT_RGB);
		final Graphics g = image.getGraphics();
		g.setColor(color);
		g.fillRect(0, 0, image.getWidth(), image.getHeight());
		g.dispose();
		final ImageIcon icon = new ImageIcon(image);
		choose.setIcon(icon);
	}

}

@@ -2749,16 +2749,34 @@
 return;%0A
+%09%09color = choice;%0A
 %09%09model.
@@ -2784,21 +2784,26 @@
 etValue(
-c
+getC
 olor
+()
 );%0A%09%09ref
@@ -2895,20 +2895,18 @@
 %09return 
-new 
+to
 ColorRGB
@@ -2915,52 +2915,8 @@
 olor
-.getRed(), color.getGreen(), color.getBlue()
 );%0A%09
@@ -2987,21 +2987,36 @@
 h() %7B%0A%09%09
-color
+final ColorRGB value
  = (Colo
@@ -3016,16 +3016,19 @@
 = (Color
+RGB
 ) model.
@@ -3035,24 +3035,53 @@
 getValue();%0A
+%09%09color = toColor(value);%0A%09%09%0A
 %09%09final Buff
@@ -3387,10 +3387,454 @@
 n);%0A%09%7D%0A%0A
+%09// -- Helper methods --%0A%0A%09// NB: The following methods also exist in imagej.awt.AWTColors, but%0A%09// to avoid a dependency on ij-awt-common, we do not use them here.%0A%0A%09private Color toColor(final ColorRGB c) %7B%0A%09%09if (c == null) return null;%0A%09%09return new Color(c.getRed(), c.getGreen(), c.getBlue());%0A%09%7D%0A%0A%09private ColorRGB toColorRGB(final Color c) %7B%0A%09%09if (c == null) return null;%0A%09%09return new ColorRGB(c.getRed(), c.getGreen(), c.getBlue());%0A%09%7D%0A%0A
 %7D%0A

Fix bugs in SwingColorWidget.