001 /*
002 * $Id: JXColorSelectionButton.java,v 1.1 2006/04/26 04:01:41 joshy Exp $
003 *
004 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020 */
021
022 package org.jdesktop.swingx;
023
024 import java.awt.Color;
025 import java.awt.Graphics;
026 import java.awt.Insets;
027 import java.awt.event.ActionEvent;
028 import java.awt.event.ActionListener;
029 import java.awt.image.BufferedImage;
030 import javax.imageio.ImageIO;
031 import javax.swing.JButton;
032 import javax.swing.JColorChooser;
033 import javax.swing.JComponent;
034 import javax.swing.JDialog;
035 import javax.swing.JFrame;
036 import javax.swing.JLabel;
037 import javax.swing.JPanel;
038 import javax.swing.event.ChangeEvent;
039 import javax.swing.event.ChangeListener;
040 import org.jdesktop.swingx.color.*;
041
042 /**
043 * A button which allows the user to select a single color. The button has a platform
044 * specific look. Ex: on Mac OS X it will mimic an NSColorWell. When the user
045 * clicks the button it will open a color chooser set to the current background
046 * color of the button. The new selected color will be stored in the background
047 * property and can be retrieved using the getBackground() method. As the user is
048 * choosing colors within the color chooser the background property will be updated.
049 * By listening to this property developers can make other parts of their program
050 * update.
051 *
052 * @author joshua.marinacci@sun.com
053 */
054 public class JXColorSelectionButton extends JButton {
055
056 private JDialog dialog = null;
057 private JColorChooser chooser = null;
058
059 /**
060 * Creates a new instance of JXColorSelectionButton
061 */
062 public JXColorSelectionButton() {
063 this(Color.red);
064 }
065 public JXColorSelectionButton(Color col) {
066 setBackground(col);
067 this.addActionListener(new ActionListener() {
068 public void actionPerformed(ActionEvent actionEvent) {
069 if(dialog == null) {
070 dialog = JColorChooser.createDialog(
071 JXColorSelectionButton.this,
072 "Choose a color",
073 true,
074 getChooser(),
075 new ActionListener() {
076 public void actionPerformed(ActionEvent actionEvent) {
077 //System.out.println("okay");
078 }
079 },
080 new ActionListener() {
081 public void actionPerformed(ActionEvent actionEvent) {
082 //System.out.println("cancel");
083 }
084 }
085 );
086 dialog.getContentPane().add(getChooser());
087 getChooser().getSelectionModel().addChangeListener(new ColorChangeListener(JXColorSelectionButton.this));
088 }
089 dialog.setVisible(true);
090 Color color = getChooser().getColor();
091
092 if (color != null) {
093 setBackground(color);
094 }
095
096 }
097 });
098 this.setContentAreaFilled(false);
099 this.setOpaque(false);
100
101 try {
102 colorwell = ImageIO.read(this.getClass().getResourceAsStream("/org/jdesktop/swingx/color/colorwell.png"));
103 } catch (Exception ex) {
104 ex.printStackTrace();
105 }
106 }
107
108 private BufferedImage colorwell;
109
110 private class ColorChangeListener implements ChangeListener {
111 public JXColorSelectionButton button;
112 public ColorChangeListener(JXColorSelectionButton button) {
113 this.button = button;
114 }
115 public void stateChanged(ChangeEvent changeEvent) {
116 button.setBackground(button.getChooser().getColor());
117 }
118 }
119
120 protected void paintComponent(Graphics g) {
121 Insets ins = new Insets(5,5,5,5);
122 if(colorwell != null) {
123 ColorUtil.tileStretchPaint(g, this, colorwell, ins);
124 }
125
126 // 0, 23, 255 = 235o, 100%, 100%
127 // 31, 0, 204 = 249o, 100%, 80%
128 g.setColor(ColorUtil.removeAlpha(getBackground()));
129 g.fillRect(ins.left, ins.top,
130 getWidth() - ins.left - ins.right,
131 getHeight() - ins.top - ins.bottom);
132 g.setColor(ColorUtil.setBrightness(getBackground(),0.85f));
133 g.drawRect(ins.left, ins.top,
134 getWidth() - ins.left - ins.right - 1,
135 getHeight() - ins.top - ins.bottom - 1);
136 g.drawRect(ins.left + 1, ins.top + 1,
137 getWidth() - ins.left - ins.right - 3,
138 getHeight() - ins.top - ins.bottom - 3);
139 }
140
141
142 public static void main(String[] args) {
143 JFrame frame = new JFrame("Color Button Test");
144 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
145 JPanel panel = new JPanel();
146 panel.add(new JXColorSelectionButton());
147 panel.add(new JLabel("ColorSelectionButton test"));
148
149 frame.add(panel);
150 frame.pack();
151 frame.setVisible(true);
152 }
153
154 /** Get the chooser that is used by this JXColorSelectionButton. This
155 * chooser instance is shared between all invocations of the chooser, but is unique to
156 * this instance of JXColorSelectionButton.
157 */
158 public JColorChooser getChooser() {
159 if(chooser == null) {
160 chooser = new JColorChooser();
161 }
162 return chooser;
163 }
164
165 }