2
* @(#)HTMLChooser.java 1.5 2006-04-23
4
* Copyright (c) 2005 Werner Randelshofer
5
* Staldenmattweg 2, Immensee, CH-6405, Switzerland.
8
* This software is the confidential and proprietary information of
9
* Werner Randelshofer. ("Confidential Information"). You shall not
10
* disclose such Confidential Information and shall use it only in
11
* accordance with the terms of the license agreement you entered into
12
* with Werner Randelshofer.
15
package org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.colorchooser;
19
import javax.swing.text.*;
20
import javax.swing.event.*;
21
import javax.swing.border.*;
22
import javax.swing.colorchooser.*;
23
import javax.swing.plaf.*;
25
import org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.*;
26
import org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.util.*;
33
* @author Werner Randelshofer
34
* @version 1.5 2006-04-23 Retrieve labels from UIManager.
35
* <br>1.4 2005-11-22 Moved handler for text fields into separate class.
36
* <br>1.3.1 2005-11-07 Get "Labels" resource bundle from UIManager.
37
* <br>1.3 2005-09-05 Get font,spacing and icon from UIManager.
38
* <br>1.2 2005-08-28 Remember last selection state of "webSaveCheckBox".
39
* <br>1.1.1 2005-06-19 Sliders were not updated when a color was entered
40
* into the HTML field.
41
* <br>1.1 2005-04-18 Localized form.
42
* <br>1.0 29 March 2005 Created.
44
public class HTMLChooser extends AbstractColorChooserPanel implements UIResource {
45
private HTMLColorSliderModel ccModel = new HTMLColorSliderModel();
46
private ChangeListener htmlListener;
49
* This is used to remember the last selection state of the "webSaveCheckBox".
51
private static boolean lastWebSaveSelectionState = false;
54
* We have to prevent us from constantly updating the color model, because
55
* the gray chooser is not able to preserve all color components.
57
private int updateRecursion;
60
* W3C HTML 4.1 well known color names.
62
private final static Object[][] colorNames = {
63
{"Black", new Color(0x000000)},
64
{"Green", new Color(0x008000)},
65
{"Silver", new Color(0xC0C0C0)},
66
{"Lime", new Color(0x00FF00)},
67
{"Gray", new Color(0x808080)},
68
{"Olive", new Color(0x808000)},
69
{"White", new Color(0xFFFFFF)},
70
{"Yellow", new Color(0xFFFF00)},
71
{"Maroon", new Color(0x800000)},
72
{"Navy", new Color(0x000080)},
73
{"Red", new Color(0xFF0000)},
74
{"Blue", new Color(0x0000FF)},
75
{"Purple", new Color(0x800080)},
76
{"Teal", new Color(0x008080)},
77
{"Fuchsia", new Color(0xFF00FF)},
78
{"Aqua", new Color(0x00FF)}
80
private final static HashMap nameToColorMap = new HashMap();
82
for (int i=0; i < colorNames.length; i++) {
83
nameToColorMap.put(((String) colorNames[i][0]).toLowerCase(), colorNames[i][1]);
88
/** Creates new form. */
89
public HTMLChooser() {
93
Font font = UIManager.getFont("ColorChooser.font");
94
redLabel.setFont(font);
95
redSlider.setFont(font);
96
redField.setFont(font);
97
greenLabel.setFont(font);
98
greenField.setFont(font);
99
greenSlider.setFont(font);
100
blueLabel.setFont(font);
101
blueSlider.setFont(font);
102
blueField.setFont(font);
103
htmlLabel.setFont(font);
104
htmlField.setFont(font);
105
webSaveCheckBox.setFont(font);
107
int textSliderGap = UIManager.getInt("ColorChooser.textSliderGap");
108
if (textSliderGap != 0) {
109
Insets fieldInsets = new Insets(0,textSliderGap,0,0);
110
GridBagLayout layout = (GridBagLayout) getLayout();
111
GridBagConstraints gbc;
112
gbc = layout.getConstraints(redField);
113
gbc.insets = fieldInsets;
114
layout.setConstraints(redField, gbc);
115
gbc = layout.getConstraints(greenField);
116
gbc.insets = fieldInsets;
117
layout.setConstraints(greenField, gbc);
118
gbc = layout.getConstraints(blueField);
119
gbc.insets = fieldInsets;
120
layout.setConstraints(blueField, gbc);
123
webSaveCheckBox.setSelected(lastWebSaveSelectionState);
124
redSlider.setSnapToTicks(lastWebSaveSelectionState);
125
greenSlider.setSnapToTicks(lastWebSaveSelectionState);
126
blueSlider.setSnapToTicks(lastWebSaveSelectionState);
129
htmlListener = new ChangeListener() {
131
public void stateChanged(ChangeEvent evt) {
132
Color c = ccModel.getColor();
134
if (! c.equals(nameToColorMap.get(htmlField.getText().toLowerCase()))) {
135
if (! htmlField.hasFocus()) {
136
String hex = Integer.toHexString(0xffffff & c.getRGB());
137
StringBuffer buf = new StringBuffer(7);
139
for (int i=hex.length(); i < 6; i++) {
142
buf.append(hex.toUpperCase());
143
if (! htmlField.getText().equals(buf.toString())) {
144
htmlField.setText(buf.toString());
153
ccModel = new HTMLColorSliderModel();
154
ccModel.setWebSaveOnly(lastWebSaveSelectionState);
155
ccModel.configureColorSlider(0, redSlider);
156
ccModel.configureColorSlider(1, greenSlider);
157
ccModel.configureColorSlider(2, blueSlider);
158
new ColorSliderTextFieldHandler(redField, ccModel, 0);
159
new ColorSliderTextFieldHandler(greenField, ccModel, 1);
160
new ColorSliderTextFieldHandler(blueField, ccModel, 2);
161
ccModel.addChangeListener(htmlListener);
163
redFieldFocusLost(null);
164
greenFieldFocusLost(null);
165
blueFieldFocusLost(null);
166
htmlFieldFocusLost(null);
168
redField.setMinimumSize(redField.getPreferredSize());
169
greenField.setMinimumSize(greenField.getPreferredSize());
170
blueField.setMinimumSize(blueField.getPreferredSize());
171
htmlPanel.setMinimumSize(htmlPanel.getPreferredSize());
172
VisualMargin bm = new VisualMargin(false,false,true,false);
173
redLabel.setBorder(bm);
174
greenLabel.setBorder(bm);
175
blueLabel.setBorder(bm);
178
protected void buildChooser() {
182
public String getDisplayName() {
183
return UIManager.getString("ColorChooser.htmlSliders");
187
public Icon getLargeDisplayIcon() {
188
return UIManager.getIcon("ColorChooser.colorSlidersIcon");
192
public Icon getSmallDisplayIcon() {
193
return getLargeDisplayIcon();
197
public void updateChooser() {
198
if (updateRecursion == 0) {
200
if (ccModel.isWebSaveOnly()) {
201
Color c = getColorFromModel();
202
if (c == null || ! HTMLColorSliderModel.isWebSave(c.getRGB())) {
203
webSaveCheckBox.setSelected(false);
206
ccModel.setColor(getColorFromModel());
212
public void setColorToModel(Color color) {
213
if (updateRecursion == 0) {
215
getColorSelectionModel().setSelectedColor(color);
221
/** This method is called from within the constructor to
222
* initialize the form.
223
* WARNING: Do NOT modify this code. The content of this method is
224
* always regenerated by the Form Editor.
226
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
227
private void initComponents() {
228
java.awt.GridBagConstraints gridBagConstraints;
230
redLabel = new javax.swing.JLabel();
231
redSlider = new javax.swing.JSlider();
232
redField = new javax.swing.JTextField();
233
greenLabel = new javax.swing.JLabel();
234
greenField = new javax.swing.JTextField();
235
greenSlider = new javax.swing.JSlider();
236
blueLabel = new javax.swing.JLabel();
237
blueSlider = new javax.swing.JSlider();
238
blueField = new javax.swing.JTextField();
239
htmlPanel = new javax.swing.JPanel();
240
htmlLabel = new javax.swing.JLabel();
241
htmlField = new javax.swing.JTextField();
242
webSaveCheckBox = new javax.swing.JCheckBox();
243
springPanel = new javax.swing.JPanel();
245
setLayout(new java.awt.GridBagLayout());
247
redLabel.setText(UIManager.getString("ColorChooser.rgbRedText"));
248
gridBagConstraints = new java.awt.GridBagConstraints();
249
gridBagConstraints.gridx = 0;
250
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
251
gridBagConstraints.insets = new java.awt.Insets(1, 0, 0, 0);
252
add(redLabel, gridBagConstraints);
254
redSlider.setMajorTickSpacing(255);
255
redSlider.setMaximum(255);
256
redSlider.setMinorTickSpacing(51);
257
redSlider.setPaintTicks(true);
258
redSlider.setSnapToTicks(true);
259
gridBagConstraints = new java.awt.GridBagConstraints();
260
gridBagConstraints.gridx = 0;
261
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
262
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
263
gridBagConstraints.weightx = 1.0;
264
add(redSlider, gridBagConstraints);
266
redField.setColumns(3);
267
redField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
268
redField.setText("0");
269
redField.addFocusListener(new java.awt.event.FocusAdapter() {
271
public void focusLost(java.awt.event.FocusEvent evt) {
272
redFieldFocusLost(evt);
276
gridBagConstraints = new java.awt.GridBagConstraints();
277
gridBagConstraints.gridx = 1;
278
gridBagConstraints.gridheight = 2;
279
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
280
add(redField, gridBagConstraints);
282
greenLabel.setText(UIManager.getString("ColorChooser.rgbGreenText"));
283
gridBagConstraints = new java.awt.GridBagConstraints();
284
gridBagConstraints.gridx = 0;
285
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
286
gridBagConstraints.insets = new java.awt.Insets(1, 0, 0, 0);
287
add(greenLabel, gridBagConstraints);
289
greenField.setColumns(3);
290
greenField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
291
greenField.setText("0");
292
greenField.addFocusListener(new java.awt.event.FocusAdapter() {
294
public void focusLost(java.awt.event.FocusEvent evt) {
295
greenFieldFocusLost(evt);
299
gridBagConstraints = new java.awt.GridBagConstraints();
300
gridBagConstraints.gridx = 1;
301
gridBagConstraints.gridheight = 2;
302
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
303
add(greenField, gridBagConstraints);
305
greenSlider.setMajorTickSpacing(255);
306
greenSlider.setMaximum(255);
307
greenSlider.setMinorTickSpacing(51);
308
greenSlider.setPaintTicks(true);
309
greenSlider.setSnapToTicks(true);
310
gridBagConstraints = new java.awt.GridBagConstraints();
311
gridBagConstraints.gridx = 0;
312
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
313
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
314
gridBagConstraints.weightx = 1.0;
315
add(greenSlider, gridBagConstraints);
317
blueLabel.setText(UIManager.getString("ColorChooser.rgbBlueText"));
318
gridBagConstraints = new java.awt.GridBagConstraints();
319
gridBagConstraints.gridx = 0;
320
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
321
gridBagConstraints.insets = new java.awt.Insets(1, 0, 0, 0);
322
add(blueLabel, gridBagConstraints);
324
blueSlider.setMajorTickSpacing(255);
325
blueSlider.setMaximum(255);
326
blueSlider.setMinorTickSpacing(51);
327
blueSlider.setPaintTicks(true);
328
blueSlider.setSnapToTicks(true);
329
gridBagConstraints = new java.awt.GridBagConstraints();
330
gridBagConstraints.gridx = 0;
331
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
332
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
333
gridBagConstraints.weightx = 1.0;
334
add(blueSlider, gridBagConstraints);
336
blueField.setColumns(3);
337
blueField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
338
blueField.setText("0");
339
blueField.addFocusListener(new java.awt.event.FocusAdapter() {
341
public void focusLost(java.awt.event.FocusEvent evt) {
342
blueFieldFocusLost(evt);
346
gridBagConstraints = new java.awt.GridBagConstraints();
347
gridBagConstraints.gridx = 1;
348
gridBagConstraints.gridheight = 2;
349
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
350
add(blueField, gridBagConstraints);
352
htmlPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 5));
354
htmlLabel.setText(UIManager.getString("ColorChooser.htmlText"));
355
htmlLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 4));
356
htmlPanel.add(htmlLabel);
358
htmlField.setColumns(7);
359
htmlField.setText("#000000");
360
htmlField.addFocusListener(new java.awt.event.FocusAdapter() {
362
public void focusLost(java.awt.event.FocusEvent evt) {
363
htmlFieldFocusLost(evt);
367
htmlPanel.add(htmlField);
369
gridBagConstraints = new java.awt.GridBagConstraints();
370
gridBagConstraints.gridx = 0;
371
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
372
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
373
gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 0);
374
add(htmlPanel, gridBagConstraints);
376
webSaveCheckBox.setText(UIManager.getString("ColorChooser.htmlChooseOnlyWebSaveColorsText"));
377
webSaveCheckBox.addItemListener(new java.awt.event.ItemListener() {
379
public void itemStateChanged(java.awt.event.ItemEvent evt) {
384
gridBagConstraints = new java.awt.GridBagConstraints();
385
gridBagConstraints.gridx = 0;
386
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
387
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
388
add(webSaveCheckBox, gridBagConstraints);
390
springPanel.setLayout(new java.awt.BorderLayout());
392
gridBagConstraints = new java.awt.GridBagConstraints();
393
gridBagConstraints.gridx = 0;
394
gridBagConstraints.gridy = 100;
395
gridBagConstraints.weighty = 1.0;
396
add(springPanel, gridBagConstraints);
398
}// </editor-fold>//GEN-END:initComponents
400
private void blueFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_blueFieldFocusLost
401
String hex = Integer.toHexString(ccModel.getBoundedRangeModel(2).getValue()).toUpperCase();
402
blueField.setText((hex.length() == 1) ? "0"+hex : hex);
403
}//GEN-LAST:event_blueFieldFocusLost
405
private void greenFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_greenFieldFocusLost
406
String hex = Integer.toHexString(ccModel.getBoundedRangeModel(1).getValue()).toUpperCase();
407
greenField.setText((hex.length() == 1) ? "0"+hex : hex);
408
}//GEN-LAST:event_greenFieldFocusLost
410
private void redFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_redFieldFocusLost
411
String hex = Integer.toHexString(ccModel.getBoundedRangeModel(0).getValue()).toUpperCase();
412
redField.setText((hex.length() == 1) ? "0"+hex : hex);
413
}//GEN-LAST:event_redFieldFocusLost
415
private void htmlFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_htmlFieldFocusLost
416
Color mc = ccModel.getColor();
418
Color fc = (Color) nameToColorMap.get(htmlField.getText().toLowerCase());
419
if (fc == null || ! fc.equals(mc)) {
420
String hex = Integer.toHexString(0xffffff & mc.getRGB());
421
StringBuffer buf = new StringBuffer(7);
423
for (int i=hex.length(); i < 6; i++) {
426
buf.append(hex.toUpperCase());
427
htmlField.setText(buf.toString());
429
}//GEN-LAST:event_htmlFieldFocusLost
431
private void webSaveChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_webSaveChanged
434
// TODO add your handling code here:
435
boolean b = webSaveCheckBox.isSelected();
436
redSlider.setSnapToTicks(b);
437
greenSlider.setSnapToTicks(b);
438
blueSlider.setSnapToTicks(b);
439
lastWebSaveSelectionState = b;
442
greenSlider.repaint();
443
blueSlider.repaint();
445
ccModel.setWebSaveOnly(b);
447
}//GEN-LAST:event_webSaveChanged
450
// Variables declaration - do not modify//GEN-BEGIN:variables
451
private javax.swing.JTextField blueField;
452
private javax.swing.JLabel blueLabel;
453
private javax.swing.JSlider blueSlider;
454
private javax.swing.JTextField greenField;
455
private javax.swing.JLabel greenLabel;
456
private javax.swing.JSlider greenSlider;
457
private javax.swing.JTextField htmlField;
458
private javax.swing.JLabel htmlLabel;
459
private javax.swing.JPanel htmlPanel;
460
private javax.swing.JTextField redField;
461
private javax.swing.JLabel redLabel;
462
private javax.swing.JSlider redSlider;
463
private javax.swing.JPanel springPanel;
464
private javax.swing.JCheckBox webSaveCheckBox;
465
// End of variables declaration//GEN-END:variables