OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [classpath/] [javax/] [swing/] [colorchooser/] [DefaultRGBChooserPanel.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* DefaultRGHChooserPanel.java --
2
   Copyright (C) 2004, 2005  Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
 
39
package javax.swing.colorchooser;
40
 
41
import java.awt.Color;
42
import java.awt.Graphics;
43
import java.awt.GridBagConstraints;
44
import java.awt.GridBagLayout;
45
 
46
import javax.swing.Icon;
47
import javax.swing.JColorChooser;
48
import javax.swing.JLabel;
49
import javax.swing.JSlider;
50
import javax.swing.JSpinner;
51
import javax.swing.SpinnerNumberModel;
52
import javax.swing.SwingConstants;
53
import javax.swing.event.ChangeEvent;
54
import javax.swing.event.ChangeListener;
55
 
56
/**
57
 * This is the default RGB panel for the JColorChooser. The color is selected
58
 * using three sliders that represent the RGB values.
59
 */
60
class DefaultRGBChooserPanel extends AbstractColorChooserPanel
61
{
62
  /**
63
   * This class handles the slider value changes for all three sliders.
64
   */
65
  class SliderHandler implements ChangeListener
66
  {
67
    /**
68
     * This method is called whenever any of the slider values change.
69
     *
70
     * @param e The ChangeEvent.
71
     */
72
    public void stateChanged(ChangeEvent e)
73
    {
74
      if (updateChange)
75
        return;
76
 
77
      int color = R.getValue() << 16 | G.getValue() << 8 | B.getValue();
78
 
79
      sliderChange = true;
80
      getColorSelectionModel().setSelectedColor(new Color(color));
81
      sliderChange = false;
82
    }
83
  }
84
 
85
  /**
86
   * This class handles the Spinner values changing.
87
   */
88
  class SpinnerHandler implements ChangeListener
89
  {
90
    /**
91
     * This method is called whenever any of the JSpinners change values.
92
     *
93
     * @param e The ChangeEvent.
94
     */
95
    public void stateChanged(ChangeEvent e)
96
    {
97
      if (updateChange)
98
        return;
99
 
100
      int red = ((Number) RSpinner.getValue()).intValue();
101
      int green = ((Number) GSpinner.getValue()).intValue();
102
      int blue = ((Number) BSpinner.getValue()).intValue();
103
 
104
      int color = red << 16 | green << 8 | blue;
105
 
106
      spinnerChange = true;
107
      getColorSelectionModel().setSelectedColor(new Color(color));
108
      spinnerChange = false;
109
    }
110
  }
111
 
112
  /** Whether the color change was initiated by the spinners.
113
   * This is package-private to avoid an accessor method.  */
114
  transient boolean spinnerChange = false;
115
 
116
  /** Whether the color change was initiated by the sliders.
117
   * This is package-private to avoid an accessor method.  */
118
  transient boolean sliderChange = false;
119
 
120
  /**
121
   * Whether the change was forced by the chooser (meaning the color has
122
   * already been changed).
123
   * This is package-private to avoid an accessor method.
124
   */
125
  transient boolean updateChange = false;
126
 
127
  /** The ChangeListener for the sliders. */
128
  private transient ChangeListener colorChanger;
129
 
130
  /** The ChangeListener for the spinners. */
131
  private transient ChangeListener spinnerHandler;
132
 
133
  /** The slider that handles the red values.
134
   * This is package-private to avoid an accessor method.  */
135
  transient JSlider R;
136
 
137
  /** The slider that handles the green values.
138
   * This is package-private to avoid an accessor method.  */
139
  transient JSlider G;
140
 
141
  /** The slider that handles the blue values.
142
   * This is package-private to avoid an accessor method.  */
143
  transient JSlider B;
144
 
145
  /** The label for the red slider. */
146
  private transient JLabel RLabel;
147
 
148
  /** The label for the green slider. */
149
  private transient JLabel GLabel;
150
 
151
  /** The label for the blue slider. */
152
  private transient JLabel BLabel;
153
 
154
  /** The spinner that handles the red values.
155
   * This is package-private to avoid an accessor method.  */
156
  transient JSpinner RSpinner;
157
 
158
  /** The spinner that handles the green values.
159
   * This is package-private to avoid an accessor method.  */
160
  transient JSpinner GSpinner;
161
 
162
  /** The spinner that handles the blue values.
163
   * This is package-private to avoid an accessor method.  */
164
  transient JSpinner BSpinner;
165
 
166
  /**
167
   * Creates a new DefaultRGBChooserPanel object.
168
   */
169
  public DefaultRGBChooserPanel()
170
  {
171
    super();
172
  }
173
 
174
  /**
175
   * This method returns the name displayed in the JTabbedPane.
176
   *
177
   * @return The name displayed in the JTabbedPane.
178
   */
179
  public String getDisplayName()
180
  {
181
    return "RGB";
182
  }
183
 
184
  /**
185
   * This method updates the chooser panel with the new color chosen in the
186
   * JColorChooser.
187
   */
188
  public void updateChooser()
189
  {
190
    Color c = getColorFromModel();
191
    int rgb = c.getRGB();
192
 
193
    int red = rgb >> 16 & 0xff;
194
    int green = rgb >> 8 & 0xff;
195
    int blue = rgb & 0xff;
196
 
197
    updateChange = true;
198
 
199
    if (! sliderChange)
200
      {
201
        if (R != null)
202
          R.setValue(red);
203
        if (G != null)
204
          G.setValue(green);
205
        if (B != null)
206
          B.setValue(blue);
207
      }
208
    if (! spinnerChange)
209
      {
210
        if (GSpinner != null)
211
          GSpinner.setValue(new Integer(green));
212
        if (RSpinner != null)
213
          RSpinner.setValue(new Integer(red));
214
        if (BSpinner != null)
215
          BSpinner.setValue(new Integer(blue));
216
      }
217
 
218
    updateChange = false;
219
 
220
    revalidate();
221
    repaint();
222
  }
223
 
224
  /**
225
   * This method builds the chooser panel.
226
   */
227
  protected void buildChooser()
228
  {
229
    setLayout(new GridBagLayout());
230
 
231
    RLabel = new JLabel("Red");
232
    RLabel.setDisplayedMnemonic('d');
233
    GLabel = new JLabel("Green");
234
    GLabel.setDisplayedMnemonic('n');
235
    BLabel = new JLabel("Blue");
236
    BLabel.setDisplayedMnemonic('B');
237
 
238
    R = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 255);
239
    G = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 255);
240
    B = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 255);
241
 
242
    R.setPaintTicks(true);
243
    R.setSnapToTicks(false);
244
    G.setPaintTicks(true);
245
    G.setSnapToTicks(false);
246
    B.setPaintTicks(true);
247
    B.setSnapToTicks(false);
248
 
249
    R.setLabelTable(R.createStandardLabels(85));
250
    R.setPaintLabels(true);
251
    G.setLabelTable(G.createStandardLabels(85));
252
    G.setPaintLabels(true);
253
    B.setLabelTable(B.createStandardLabels(85));
254
    B.setPaintLabels(true);
255
 
256
    R.setMajorTickSpacing(85);
257
    G.setMajorTickSpacing(85);
258
    B.setMajorTickSpacing(85);
259
 
260
    R.setMinorTickSpacing(17);
261
    G.setMinorTickSpacing(17);
262
    B.setMinorTickSpacing(17);
263
 
264
    RSpinner = new JSpinner(new SpinnerNumberModel(R.getValue(),
265
                                                   R.getMinimum(),
266
                                                   R.getMaximum(), 1));
267
    GSpinner = new JSpinner(new SpinnerNumberModel(G.getValue(),
268
                                                   G.getMinimum(),
269
                                                   G.getMaximum(), 1));
270
    BSpinner = new JSpinner(new SpinnerNumberModel(B.getValue(),
271
                                                   B.getMinimum(),
272
                                                   B.getMaximum(), 1));
273
 
274
    RLabel.setLabelFor(R);
275
    GLabel.setLabelFor(G);
276
    BLabel.setLabelFor(B);
277
 
278
    GridBagConstraints bag = new GridBagConstraints();
279
    bag.fill = GridBagConstraints.VERTICAL;
280
 
281
    bag.gridx = 0;
282
    bag.gridy = 0;
283
    add(RLabel, bag);
284
 
285
    bag.gridx = 1;
286
    add(R, bag);
287
 
288
    bag.gridx = 2;
289
    add(RSpinner, bag);
290
 
291
    bag.gridx = 0;
292
    bag.gridy = 1;
293
    add(GLabel, bag);
294
 
295
    bag.gridx = 1;
296
    add(G, bag);
297
 
298
    bag.gridx = 2;
299
    add(GSpinner, bag);
300
 
301
    bag.gridx = 0;
302
    bag.gridy = 2;
303
    add(BLabel, bag);
304
 
305
    bag.gridx = 1;
306
    add(B, bag);
307
 
308
    bag.gridx = 2;
309
    add(BSpinner, bag);
310
 
311
    installListeners();
312
  }
313
 
314
  /**
315
   * This method uninstalls the chooser panel from the JColorChooser.
316
   *
317
   * @param chooser The JColorChooser to remove this chooser panel from.
318
   */
319
  public void uninstallChooserPanel(JColorChooser chooser)
320
  {
321
    uninstallListeners();
322
    removeAll();
323
 
324
    R = null;
325
    G = null;
326
    B = null;
327
 
328
    RSpinner = null;
329
    GSpinner = null;
330
    BSpinner = null;
331
 
332
    super.uninstallChooserPanel(chooser);
333
  }
334
 
335
  /**
336
   * This method uninstalls any listeners that were added by the chooser
337
   * panel.
338
   */
339
  private void uninstallListeners()
340
  {
341
    R.removeChangeListener(colorChanger);
342
    G.removeChangeListener(colorChanger);
343
    B.removeChangeListener(colorChanger);
344
 
345
    colorChanger = null;
346
 
347
    RSpinner.removeChangeListener(spinnerHandler);
348
    GSpinner.removeChangeListener(spinnerHandler);
349
    BSpinner.removeChangeListener(spinnerHandler);
350
 
351
    spinnerHandler = null;
352
  }
353
 
354
  /**
355
   * This method installs any listeners that the chooser panel needs to
356
   * operate.
357
   */
358
  private void installListeners()
359
  {
360
    colorChanger = new SliderHandler();
361
 
362
    R.addChangeListener(colorChanger);
363
    G.addChangeListener(colorChanger);
364
    B.addChangeListener(colorChanger);
365
 
366
    spinnerHandler = new SpinnerHandler();
367
 
368
    RSpinner.addChangeListener(spinnerHandler);
369
    GSpinner.addChangeListener(spinnerHandler);
370
    BSpinner.addChangeListener(spinnerHandler);
371
  }
372
 
373
  /**
374
   * This method returns the small display icon.
375
   *
376
   * @return The small display icon.
377
   */
378
  public Icon getSmallDisplayIcon()
379
  {
380
    return null;
381
  }
382
 
383
  /**
384
   * This method returns the large display icon.
385
   *
386
   * @return The large display icon.
387
   */
388
  public Icon getLargeDisplayIcon()
389
  {
390
    return null;
391
  }
392
 
393
  /**
394
   * This method paints the default RGB chooser panel.
395
   *
396
   * @param g The Graphics object to paint with.
397
   */
398
  public void paint(Graphics g)
399
  {
400
    super.paint(g);
401
  }
402
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.