39
39
import javax .swing .BoxLayout ;
40
40
import javax .swing .ImageIcon ;
41
41
import javax .swing .JButton ;
42
- import javax .swing .JColorChooser ;
43
- import javax .swing .JDialog ;
44
42
import javax .swing .JPanel ;
45
- import javax .swing .colorchooser .AbstractColorChooserPanel ;
46
43
import java .awt .Color ;
47
- import java .awt .Component ;
48
44
import java .awt .Dimension ;
49
45
import java .awt .Graphics ;
50
- import java .awt .HeadlessException ;
51
46
import java .awt .event .ActionEvent ;
52
47
import java .awt .event .ActionListener ;
53
48
import java .awt .image .BufferedImage ;
54
- import java .util .Arrays ;
55
- import java .util .Comparator ;
56
49
57
50
/**
58
51
* Swing implementation of color chooser widget including alpha.
64
57
public class SwingColorAlphaWidget extends SwingInputWidget <ColorRGBA > implements
65
58
ActionListener , InputWidget <ColorRGBA , JPanel >
66
59
{
67
-
68
- private static final int SWATCH_WIDTH = 64 , SWATCH_HEIGHT = 16 ;
69
-
70
- private static final String HSB_CLASS_NAME =
71
- "javax.swing.colorchooser.DefaultHSBChooserPanel" ;
72
-
73
- protected static final String RGB_CLASS_NAME =
74
- "javax.swing.colorchooser.DefaultRGBChooserPanel" ;
75
-
76
- protected static final String SWATCHES_CLASS_NAME =
77
- "javax.swing.colorchooser.DefaultSwatchChooserPanel" ;
78
-
79
60
private JButton choose ;
80
61
private Color color ;
81
62
82
63
// -- ActionListener methods --
83
64
84
65
@ Override
85
66
public void actionPerformed (final ActionEvent e ) {
86
- final Color choice = showColorDialog (choose , "Select a color" , color );
67
+ final Color choice = SwingColorWidget . showColorDialog (choose , "Select a color" , color );
87
68
if (choice == null ) return ;
88
69
color = choice ;
89
70
updateModel ();
@@ -126,88 +107,6 @@ public boolean supports(final WidgetModel model) {
126
107
return super .supports (model ) && model .isType (ColorRGBA .class );
127
108
}
128
109
129
- // -- Utility methods --
130
-
131
- /**
132
- * This method is identical to
133
- * {@link JColorChooser#showDialog(Component, String, Color)} except that it
134
- * reorders the panels of the color chooser to be more desirable. It uses
135
- * (HSB, RGB, Swatches) rather than the default of (Swatches, HSB, RGB).
136
- */
137
- public static Color showColorDialog (final Component component ,
138
- final String title , final Color initialColor ) throws HeadlessException
139
- {
140
- final JColorChooser pane = createColorChooser (initialColor );
141
-
142
- class ColorTracker implements ActionListener {
143
-
144
- private final JColorChooser chooser ;
145
- private Color color ;
146
-
147
- public ColorTracker (final JColorChooser c ) {
148
- chooser = c ;
149
- }
150
-
151
- @ Override
152
- public void actionPerformed (final ActionEvent e ) {
153
- color = chooser .getColor ();
154
- }
155
-
156
- public Color getColor () {
157
- return color ;
158
- }
159
- }
160
- final ColorTracker ok = new ColorTracker (pane );
161
-
162
- final JDialog dialog =
163
- JColorChooser .createDialog (component , title , true , pane , ok , null );
164
-
165
- dialog .setVisible (true );
166
-
167
- return ok .getColor ();
168
- }
169
-
170
- // -- Helper methods --
171
-
172
- /**
173
- * Creates a new {@link JColorChooser} with panels in a desirable order.
174
- * <p>
175
- * All of this code exists solely to reorder the panels from (Swatches, HSB,
176
- * RGB) to (HSB, RGB, Swatches) since we believe the HSB tab is the most
177
- * commonly useful.
178
- * </p>
179
- */
180
- private static JColorChooser createColorChooser (final Color initialColor ) {
181
- final JColorChooser chooser =
182
- new JColorChooser (initialColor != null ? initialColor : Color .white );
183
-
184
- // get the list of panels
185
- final AbstractColorChooserPanel [] panels =
186
- chooser .getChooserPanels ().clone ();
187
-
188
- // sort panels into the desired order
189
- Arrays .sort (panels , new Comparator <Object >() {
190
-
191
- @ Override
192
- public int compare (final Object o1 , final Object o2 ) {
193
- return value (o1 ) - value (o2 );
194
- }
195
-
196
- private int value (final Object o ) {
197
- final String className = o .getClass ().getName ();
198
- if (className .equals (HSB_CLASS_NAME )) return 1 ;
199
- if (className .equals (RGB_CLASS_NAME )) return 2 ;
200
- if (className .equals (SWATCHES_CLASS_NAME )) return 3 ;
201
- return 4 ;
202
- }
203
- });
204
-
205
- // reset the panels to match the sorted list
206
- chooser .setChooserPanels (panels );
207
-
208
- return chooser ;
209
- }
210
-
211
110
// -- AbstractUIInputWidget methods ---
212
111
213
112
@ Override
@@ -216,7 +115,7 @@ public void doRefresh() {
216
115
color = AWTColors .getColor (value );
217
116
218
117
final BufferedImage image =
219
- new BufferedImage (SWATCH_WIDTH , SWATCH_HEIGHT , BufferedImage .TYPE_INT_ARGB );
118
+ new BufferedImage (SwingColorWidget . SWATCH_WIDTH , SwingColorWidget . SWATCH_HEIGHT , BufferedImage .TYPE_INT_ARGB );
220
119
final Graphics g = image .getGraphics ();
221
120
g .setColor (color );
222
121
g .fillRect (0 , 0 , image .getWidth (), image .getHeight ());
0 commit comments