~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/internal/contrib/randelshofer/quaqua/colorchooser/HTMLChooser.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * @(#)HTMLChooser.java  1.5  2006-04-23
 
3
 *
 
4
 * Copyright (c) 2005 Werner Randelshofer
 
5
 * Staldenmattweg 2, Immensee, CH-6405, Switzerland.
 
6
 * All rights reserved.
 
7
 *
 
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.
 
13
 */
 
14
 
 
15
package org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.colorchooser;
 
16
 
 
17
import java.awt.*;
 
18
import javax.swing.*;
 
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.*;
 
24
 
 
25
import org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.*;
 
26
import org.pushingpixels.substance.internal.contrib.randelshofer.quaqua.util.*;
 
27
 
 
28
 
 
29
import java.util.*;
 
30
/**
 
31
 * HTMLChooser.
 
32
 *
 
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.
 
43
 */
 
44
public class HTMLChooser extends AbstractColorChooserPanel implements UIResource {
 
45
    private HTMLColorSliderModel ccModel = new HTMLColorSliderModel();
 
46
    private ChangeListener htmlListener;
 
47
    
 
48
    /**
 
49
     * This is used to remember the last selection state of the "webSaveCheckBox".
 
50
     */
 
51
    private static boolean lastWebSaveSelectionState = false;
 
52
    
 
53
    /**
 
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.
 
56
     */
 
57
    private int updateRecursion;
 
58
    
 
59
    /**
 
60
     * W3C HTML 4.1 well known color names.
 
61
     */
 
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)}
 
79
    };
 
80
    private final static HashMap nameToColorMap = new HashMap();
 
81
    static {
 
82
        for (int i=0; i < colorNames.length; i++) {
 
83
            nameToColorMap.put(((String) colorNames[i][0]).toLowerCase(), colorNames[i][1]);
 
84
        }
 
85
    }
 
86
    
 
87
    
 
88
    /** Creates new form. */
 
89
    public HTMLChooser() {
 
90
        initComponents();
 
91
        
 
92
        //
 
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);
 
106
        //
 
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);
 
121
        }
 
122
        
 
123
        webSaveCheckBox.setSelected(lastWebSaveSelectionState);
 
124
        redSlider.setSnapToTicks(lastWebSaveSelectionState);
 
125
        greenSlider.setSnapToTicks(lastWebSaveSelectionState);
 
126
        blueSlider.setSnapToTicks(lastWebSaveSelectionState);
 
127
        
 
128
 
 
129
        htmlListener = new ChangeListener() {
 
130
            @Override
 
131
            public void stateChanged(ChangeEvent evt) {
 
132
                Color c = ccModel.getColor();
 
133
                setColorToModel(c);
 
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);
 
138
                        buf.append('#');
 
139
                        for (int i=hex.length(); i < 6; i++) {
 
140
                            buf.append('0');
 
141
                        }
 
142
                        buf.append(hex.toUpperCase());
 
143
                        if (! htmlField.getText().equals(buf.toString())) {
 
144
                            htmlField.setText(buf.toString());
 
145
                        }
 
146
                    }
 
147
                }
 
148
            }
 
149
        };
 
150
        
 
151
        updateRecursion++;
 
152
        
 
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);
 
162
        
 
163
        redFieldFocusLost(null);
 
164
        greenFieldFocusLost(null);
 
165
        blueFieldFocusLost(null);
 
166
        htmlFieldFocusLost(null);
 
167
        updateRecursion--;
 
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);
 
176
    }
 
177
    @Override
 
178
    protected void buildChooser() {
 
179
    }
 
180
    
 
181
    @Override
 
182
    public String getDisplayName() {
 
183
        return UIManager.getString("ColorChooser.htmlSliders");
 
184
    }
 
185
    
 
186
    @Override
 
187
    public Icon getLargeDisplayIcon() {
 
188
        return UIManager.getIcon("ColorChooser.colorSlidersIcon");
 
189
    }
 
190
    
 
191
    @Override
 
192
    public Icon getSmallDisplayIcon() {
 
193
        return getLargeDisplayIcon();
 
194
    }
 
195
    
 
196
    @Override
 
197
    public void updateChooser() {
 
198
        if (updateRecursion == 0) {
 
199
            updateRecursion++;
 
200
            if (ccModel.isWebSaveOnly()) {
 
201
                Color c = getColorFromModel();
 
202
                if (c == null || ! HTMLColorSliderModel.isWebSave(c.getRGB())) {
 
203
                    webSaveCheckBox.setSelected(false);
 
204
                }
 
205
            }
 
206
            ccModel.setColor(getColorFromModel());
 
207
            
 
208
            updateRecursion--;
 
209
        }
 
210
    }
 
211
    
 
212
    public void setColorToModel(Color color) {
 
213
        if (updateRecursion == 0) {
 
214
            updateRecursion++;
 
215
            getColorSelectionModel().setSelectedColor(color);
 
216
            updateRecursion--;
 
217
        }
 
218
    }
 
219
    
 
220
    
 
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.
 
225
     */
 
226
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
 
227
    private void initComponents() {
 
228
        java.awt.GridBagConstraints gridBagConstraints;
 
229
 
 
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();
 
244
 
 
245
        setLayout(new java.awt.GridBagLayout());
 
246
 
 
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);
 
253
 
 
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);
 
265
 
 
266
        redField.setColumns(3);
 
267
        redField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
 
268
        redField.setText("0");
 
269
        redField.addFocusListener(new java.awt.event.FocusAdapter() {
 
270
            @Override
 
271
            public void focusLost(java.awt.event.FocusEvent evt) {
 
272
                redFieldFocusLost(evt);
 
273
            }
 
274
        });
 
275
 
 
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);
 
281
 
 
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);
 
288
 
 
289
        greenField.setColumns(3);
 
290
        greenField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
 
291
        greenField.setText("0");
 
292
        greenField.addFocusListener(new java.awt.event.FocusAdapter() {
 
293
            @Override
 
294
            public void focusLost(java.awt.event.FocusEvent evt) {
 
295
                greenFieldFocusLost(evt);
 
296
            }
 
297
        });
 
298
 
 
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);
 
304
 
 
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);
 
316
 
 
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);
 
323
 
 
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);
 
335
 
 
336
        blueField.setColumns(3);
 
337
        blueField.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
 
338
        blueField.setText("0");
 
339
        blueField.addFocusListener(new java.awt.event.FocusAdapter() {
 
340
            @Override
 
341
            public void focusLost(java.awt.event.FocusEvent evt) {
 
342
                blueFieldFocusLost(evt);
 
343
            }
 
344
        });
 
345
 
 
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);
 
351
 
 
352
        htmlPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 5));
 
353
 
 
354
        htmlLabel.setText(UIManager.getString("ColorChooser.htmlText"));
 
355
        htmlLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 4));
 
356
        htmlPanel.add(htmlLabel);
 
357
 
 
358
        htmlField.setColumns(7);
 
359
        htmlField.setText("#000000");
 
360
        htmlField.addFocusListener(new java.awt.event.FocusAdapter() {
 
361
            @Override
 
362
            public void focusLost(java.awt.event.FocusEvent evt) {
 
363
                htmlFieldFocusLost(evt);
 
364
            }
 
365
        });
 
366
 
 
367
        htmlPanel.add(htmlField);
 
368
 
 
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);
 
375
 
 
376
        webSaveCheckBox.setText(UIManager.getString("ColorChooser.htmlChooseOnlyWebSaveColorsText"));
 
377
        webSaveCheckBox.addItemListener(new java.awt.event.ItemListener() {
 
378
            @Override
 
379
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
 
380
                webSaveChanged(evt);
 
381
            }
 
382
        });
 
383
 
 
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);
 
389
 
 
390
        springPanel.setLayout(new java.awt.BorderLayout());
 
391
 
 
392
        gridBagConstraints = new java.awt.GridBagConstraints();
 
393
        gridBagConstraints.gridx = 0;
 
394
        gridBagConstraints.gridy = 100;
 
395
        gridBagConstraints.weighty = 1.0;
 
396
        add(springPanel, gridBagConstraints);
 
397
 
 
398
    }// </editor-fold>//GEN-END:initComponents
 
399
    
 
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
 
404
    
 
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
 
409
    
 
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
 
414
    
 
415
    private void htmlFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_htmlFieldFocusLost
 
416
        Color mc = ccModel.getColor();
 
417
        
 
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);
 
422
            buf.append('#');
 
423
            for (int i=hex.length(); i < 6; i++) {
 
424
                buf.append('0');
 
425
            }
 
426
            buf.append(hex.toUpperCase());
 
427
            htmlField.setText(buf.toString());
 
428
        }
 
429
    }//GEN-LAST:event_htmlFieldFocusLost
 
430
    
 
431
    private void webSaveChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_webSaveChanged
 
432
      
 
433
        
 
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;
 
440
        /*
 
441
        redSlider.repaint();
 
442
        greenSlider.repaint();
 
443
        blueSlider.repaint();
 
444
         */
 
445
        ccModel.setWebSaveOnly(b);
 
446
        
 
447
    }//GEN-LAST:event_webSaveChanged
 
448
    
 
449
    
 
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
 
466
    
 
467
}