1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hivegui.component; |
16 | |
17 | import java.awt.Color; |
18 | import java.awt.event.ActionEvent; |
19 | import java.awt.event.ActionListener; |
20 | |
21 | import javax.swing.JButton; |
22 | |
23 | import net.sourceforge.hivegui.application.HiveGuiApplicationMain; |
24 | |
25 | /** |
26 | * @author jean-Francois Poilpret |
27 | */ |
28 | public class ColorButton extends JButton |
29 | { |
30 | static private final Color DEFAULT_COLOR = Color.WHITE; |
31 | |
32 | public ColorButton() |
33 | { |
34 | this(DEFAULT_COLOR); |
35 | } |
36 | |
37 | public ColorButton(Color color) |
38 | { |
39 | this(color, new AlphaColorChooser()); |
40 | } |
41 | |
42 | public ColorButton(AlphaColorChooser chooser) |
43 | { |
44 | this(DEFAULT_COLOR, chooser); |
45 | } |
46 | |
47 | public ColorButton(Color color, AlphaColorChooser chooser) |
48 | { |
49 | _color = color; |
50 | _chooser = chooser; |
51 | _icon = new DynSizeColorIcon(this); |
52 | setIcon(_icon); |
53 | addActionListener(new ActionListener() |
54 | { |
55 | public void actionPerformed(ActionEvent e) |
56 | { |
57 | // Display Color Chooser |
58 | _chooser.setColor(_color); |
59 | _chooser.setTitle(_title); |
60 | _application.showDialog(_chooser); |
61 | setColor(_chooser.getColor()); |
62 | } |
63 | }); |
64 | } |
65 | |
66 | public String getTitle() |
67 | { |
68 | return _title; |
69 | } |
70 | |
71 | public void setTitle(String title) |
72 | { |
73 | _title = title; |
74 | } |
75 | |
76 | public void setApplication(HiveGuiApplicationMain application) |
77 | { |
78 | _application = application; |
79 | } |
80 | |
81 | public void setColor(Color color) |
82 | { |
83 | _color = color; |
84 | _icon.setColor(_color); |
85 | repaint(); |
86 | } |
87 | |
88 | public Color getColor() |
89 | { |
90 | return _color; |
91 | } |
92 | |
93 | // Workaround a problem with Java 6 where labels are aligned ABOVE |
94 | // the button! |
95 | public int getBaseline(int width, int height) |
96 | { |
97 | int iconHeight = getIcon().getIconHeight(); |
98 | return iconHeight + (height - iconHeight) / 2; |
99 | } |
100 | |
101 | private Color _color; |
102 | private String _title = ""; |
103 | private final ColorIcon _icon; |
104 | private final AlphaColorChooser _chooser; |
105 | private HiveGuiApplicationMain _application; |
106 | } |