2
* Copyright (c) 2005-2010 Substance Kirill Grouchnikov. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
10
* o Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
14
* o Neither the name of Substance Kirill Grouchnikov nor the names of
15
* its contributors may be used to endorse or promote products derived
16
* from this software without specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
package tools.jitterbug;
33
import java.awt.event.MouseAdapter;
34
import java.awt.event.MouseEvent;
38
import org.pushingpixels.lafwidget.utils.RenderingUtils;
40
public class JColorComponent extends JComponent {
41
private JRadioButton radio;
43
private Color selectedColor;
47
private ColorVisualizer visualizer;
49
private class ColorVisualizer extends JComponent {
52
public ColorVisualizer() {
53
this.addMouseListener(new MouseAdapter() {
55
public void mouseClicked(MouseEvent e) {
59
SwingUtilities.invokeLater(new Runnable() {
62
radio.setSelected(true);
63
Color selected = JColorChooser.showDialog(
64
ColorVisualizer.this, "Color chooser",
66
if (selected != null) {
67
Color old = selectedColor;
68
selectedColor = selected;
69
JColorComponent.this.firePropertyChange(
70
"selectedColor", old, selectedColor);
77
public void mouseEntered(MouseEvent e) {
86
public void mouseExited(MouseEvent e) {
94
this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
95
this.setToolTipText("Open color chooser and change the color");
96
this.isRollover = false;
100
protected void paintComponent(Graphics g) {
101
Graphics2D g2d = (Graphics2D) g.create();
102
RenderingUtils.installDesktopHints(g2d, this);
103
g2d.setFont(UIManager.getFont("Label.font"));
105
if (selectedColor != null) {
106
g2d.setColor(selectedColor);
107
g2d.fillRect(2, 2, 100, getHeight() - 4);
108
g2d.setStroke(new BasicStroke(isRollover ? 2.5f : 1.0f));
109
g2d.setColor(selectedColor.darker());
110
g2d.drawRect(2, 2, 99, getHeight() - 5);
112
g2d.setColor(Color.black);
113
g2d.drawString(getEncodedColor(), 108, (getHeight() + g2d
114
.getFontMetrics().getHeight())
115
/ 2 - g2d.getFontMetrics().getDescent());
117
g2d.setColor(isEnabled() ? Color.gray : Color.lightGray);
118
g2d.drawString("click to choose", 5, (getHeight() + g2d
119
.getFontMetrics().getHeight())
120
/ 2 - g2d.getFontMetrics().getDescent());
127
public Dimension getPreferredSize() {
128
return new Dimension(200, 25);
132
public JColorComponent(String name, Color color) {
133
this.radio = new JRadioButton(name);
134
this.radio.setFocusable(false);
135
this.selectedColor = color;
136
this.visualizer = new ColorVisualizer();
137
this.setLayout(new ColorComponentLayout());
139
this.add(this.radio);
140
this.add(this.visualizer);
143
private class ColorComponentLayout implements LayoutManager {
145
public void addLayoutComponent(String name, Component comp) {
149
public void layoutContainer(Container parent) {
150
JColorComponent cc = (JColorComponent) parent;
151
int width = cc.getWidth();
152
int height = cc.getHeight();
154
ColorVisualizer cv = cc.visualizer;
155
Dimension cvPref = cv.getPreferredSize();
156
cv.setBounds(width - cvPref.width, 0, cvPref.width, height);
157
cc.radio.setBounds(0, 0, width - cvPref.width, height);
161
public Dimension minimumLayoutSize(Container parent) {
162
return preferredLayoutSize(parent);
166
public Dimension preferredLayoutSize(Container parent) {
167
JColorComponent cc = (JColorComponent) parent;
168
ColorVisualizer cv = cc.visualizer;
169
Dimension cvPref = cv.getPreferredSize();
170
return new Dimension(100 + cvPref.width, cvPref.height);
174
public void removeLayoutComponent(Component comp) {
178
public String getEncodedColor() {
179
return "#" + encodeColorComponent(selectedColor.getRed())
180
+ encodeColorComponent(selectedColor.getGreen())
181
+ encodeColorComponent(selectedColor.getBlue());
184
private static String encodeColorComponent(int colorComp) {
185
String hex = "0123456789ABCDEF";
186
return "" + hex.charAt(colorComp / 16) + hex.charAt(colorComp % 16);
189
public JRadioButton getRadio() {
193
public void setColor(Color color, boolean firePropertyChange) {
194
Color old = this.selectedColor;
195
this.selectedColor = color;
197
if (firePropertyChange) {
198
this.firePropertyChange("selectedColor", old, selectedColor);
202
public Color getColor() {
203
return this.selectedColor;
206
public boolean isDefined() {
207
return (this.selectedColor != null);