001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.gui.component.button;
020
021import java.awt.Color;
022import java.awt.Dimension;
023import java.awt.event.ActionEvent;
024import java.awt.event.ActionListener;
025import java.util.EventListener;
026
027import javax.swing.Box;
028import javax.swing.JButton;
029import javax.swing.JColorChooser;
030
031/**
032 * Color button used to select a specific color.
033 * 
034 * @author Stephane
035 */
036public class ColorChooserButton extends JButton implements ActionListener
037{
038    public static interface ColorChangeListener extends EventListener
039    {
040        void colorChanged(ColorChooserButton source);
041    }
042
043    /**
044     * 
045     */
046    private static final long serialVersionUID = -5130821224410911737L;
047
048    private String colorChooseText;
049
050    /**
051     * 
052     */
053    public ColorChooserButton()
054    {
055        this(Color.black);
056    }
057
058    /**
059     * @param color
060     */
061    public ColorChooserButton(Color color)
062    {
063        super();
064
065        // setBorderPainted(false);
066        setFocusPainted(false);
067
068        final Dimension dim = new Dimension(24, 18);
069        add(new Box.Filler(dim, dim, dim));
070
071        // save color information in background color
072        setBackground(color);
073        colorChooseText = "Choose color";
074
075        addActionListener(this);
076    }
077
078    /**
079     * @return the color
080     */
081    public Color getColor()
082    {
083        return getBackground();
084    }
085
086    /**
087     * @param color
088     *        the color to set
089     */
090    public void setColor(Color color)
091    {
092        if (getColor() != color)
093        {
094            setBackground(color);
095            // notify about color change
096            fireColorChanged();
097        }
098    }
099
100    /**
101     * @return the colorChooseText
102     */
103    public String getColorChooseText()
104    {
105        return colorChooseText;
106    }
107
108    /**
109     * @param colorChooseText
110     *        the colorChooseText to set
111     */
112    public void setColorChooseText(String colorChooseText)
113    {
114        this.colorChooseText = colorChooseText;
115    }
116
117    protected void fireColorChanged()
118    {
119        for (ColorChangeListener listener : listenerList.getListeners(ColorChangeListener.class))
120            listener.colorChanged(this);
121    }
122
123    /**
124     * Adds a <code>ColorChangeListener</code> to the button.
125     * 
126     * @param l
127     *        the listener to be added
128     */
129    public void addColorChangeListener(ColorChangeListener l)
130    {
131        listenerList.add(ColorChangeListener.class, l);
132    }
133
134    /**
135     * Removes a ColorChangeListener from the button.
136     * 
137     * @param l
138     *        the listener to be removed
139     */
140    public void removeColorChangeListener(ColorChangeListener l)
141    {
142        listenerList.remove(ColorChangeListener.class, l);
143    }
144
145    // @Override
146    // protected void paintComponent(Graphics g)
147    // {
148    // super.paintComponent(g);
149    //
150    // g.setColor(color);
151    // g.fillRect(1, 1, getWidth() - 2, getHeight() - 2);
152    // }
153
154    @Override
155    public void actionPerformed(ActionEvent e)
156    {
157        final Color c = JColorChooser.showDialog(this, colorChooseText, getColor());
158
159        if (c != null)
160            setColor(c);
161    }
162
163}