001 /* $Id: JColorButton.java 12 2007-07-27 09:48:53Z ringler $
002 * This class is part of the de.mospace.swing library.
003 * Copyright (C) 2005-2006 Moritz Ringler
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018 */
019 package de.mospace.swing;
020
021 import java.awt.Color;
022 import java.awt.event.ActionEvent;
023 import java.awt.event.ActionListener;
024 import javax.swing.JButton;
025 import javax.swing.JColorChooser;
026
027 /** A button for picking colors with a JColorChooser.
028 *
029 * @author Moritz Ringler
030 * @version $Revision: 12 $ ($Date: 2007-07-27 11:48:53 +0200 (Fr, 27 Jul 2007) $)
031 */
032 public class JColorButton extends JButton{
033 private boolean fg = false;
034 Color myColor;
035 String title="";
036 ActionListener onClick = new ActionListener(){
037 public void actionPerformed(ActionEvent e){
038 Color c;
039 c = JColorChooser.showDialog(JColorButton.this,title, myColor);
040 if (c != null){
041 myColor = c;
042 }
043 if(fg){
044 setForeground(myColor);
045 } else {
046 setBackground(myColor);
047 }
048 }
049 };
050
051 /** (Re-)initializes this button with the given name and color. **/
052 protected final void init(String name, Color c){
053 myColor = c;
054 title = name;
055 setBackground(myColor);
056 addActionListener(onClick);
057 }
058
059 /** Constructs a new JColorButton with no name and white as its
060 associated color. **/
061 public JColorButton(){
062 super(" ");
063 init("",new Color(255,255,255));
064 }
065
066 /** Constructs a new JColorButton with the given name and color. **/
067 public JColorButton(String name, Color c){
068 super(" ");
069 init(name,c);
070 }
071
072 /** Constructs a new JColorButton with the given name and color.
073 * @param c the combined RGB components of the color
074 * @see java.awt.Color#Color(int rgb)
075 **/
076 public JColorButton(String name, int c){
077 super(" ");
078 init(name, new Color(c));
079 }
080
081 /** Constructs a new JColorButton with the given name and white as its
082 * associated color. **/
083 public JColorButton(String name){
084 super(" ");
085 init(name,new Color(255,255,255));
086 }
087
088 /** Constructs a new ColorButton with the given name and color. **/
089 public JColorButton(String name, Color c, boolean foreground){
090 super(" ");
091 fg = foreground;
092 if(foreground){
093 myColor = c;
094 title = name;
095 setForeground(myColor);
096 addActionListener(onClick);
097 } else {
098 init(name,c);
099 }
100 }
101
102 /** Returns the color currently associated with this button as a
103 * Color object. **/
104 public Color getColor(){
105 return myColor;
106 }
107
108 /** Returns the color currently associated with this button as a
109 * combined sRGB int.
110 * @see java.awt.Color#getRGB()
111 **/
112 public int getRGB(){
113 return myColor.getRGB();
114 }
115 }