Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / app / gui / panels / ColorChooserPanel.java @ 40561

History | View | Annotate | Download (12.6 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
/*
26
 * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
27
 * for visualizing and manipulating spatial features with geometry and attributes.
28
 *
29
 * Copyright (C) 2003 Vivid Solutions
30
 *
31
 * This program is free software; you can redistribute it and/or
32
 * modify it under the terms of the GNU General Public License
33
 * as published by the Free Software Foundation; either version 2
34
 * of the License, or (at your option) any later version.
35
 *
36
 * This program is distributed in the hope that it will be useful,
37
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39
 * GNU General Public License for more details.
40
 *
41
 * You should have received a copy of the GNU General Public License
42
 * along with this program; if not, write to the Free Software
43
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
44
 *
45
 * For more information, contact:
46
 *
47
 * Vivid Solutions
48
 * Suite #1A
49
 * 2328 Government Street
50
 * Victoria BC  V8T 5G5
51
 * Canada
52
 *
53
 * (250)385-6040
54
 * www.vividsolutions.com
55
 */
56

    
57
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
58
 *
59
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
60
 *
61
 * This program is free software; you can redistribute it and/or
62
 * modify it under the terms of the GNU General Public License
63
 * as published by the Free Software Foundation; either version 2
64
 * of the License, or (at your option) any later version.
65
 *
66
 * This program is distributed in the hope that it will be useful,
67
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
68
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
69
 * GNU General Public License for more details.
70
 *
71
 * You should have received a copy of the GNU General Public License
72
 * along with this program; if not, write to the Free Software
73
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
74
 *
75
 * For more information, contact:
76
 *
77
 *  Generalitat Valenciana
78
 *   Conselleria d'Infraestructures i Transport
79
 *   Av. Blasco Ib??ez, 50
80
 *   46010 VALENCIA
81
 *   SPAIN
82
 *
83
 *      +34 963862235
84
 *   gvsig@gva.es
85
 *      www.gvsig.gva.es
86
 *
87
 *    or
88
 *
89
 *   IVER T.I. S.A
90
 *   Salamanca 50
91
 *   46005 Valencia
92
 *   Spain
93
 *
94
 *   +34 963163400
95
 *   dac@iver.es
96
 */
97
package org.gvsig.app.gui.panels;
98

    
99
import java.awt.Color;
100
import java.awt.Dimension;
101
import java.awt.GridBagConstraints;
102
import java.awt.GridBagLayout;
103
import java.awt.GridLayout;
104
import java.awt.event.ActionEvent;
105
import java.awt.event.ActionListener;
106
import java.util.ArrayList;
107
import java.util.Iterator;
108

    
109
import javax.swing.BorderFactory;
110
import javax.swing.JButton;
111
import javax.swing.JCheckBox;
112
import javax.swing.JColorChooser;
113
import javax.swing.JFrame;
114
import javax.swing.JLabel;
115
import javax.swing.JPanel;
116
import javax.swing.JSlider;
117
import javax.swing.SwingUtilities;
118
import javax.swing.event.ChangeEvent;
119
import javax.swing.event.ChangeListener;
120

    
121
import org.gvsig.i18n.Messages;
122

    
123

    
124

    
125
/**
126
 * A custom Colour Scheme Browser - custom choices based on JColorChooser.
127
 */
128

    
129
public class ColorChooserPanel extends JPanel {
130
        private GridBagLayout gridBagLayout1 = new GridBagLayout();
131
        private JButton changeButton = new JButton();
132

    
133

    
134
    //{DEFECT-PREVENTION} In accessors, it's better to use "workbench = newWorkbench"
135
    //rather than "this.workbench = workbench", because otherwise if you misspell the
136
    //argument, Java won't complain! We should do this everywhere. [Jon Aquino]
137
    private JPanel outerColorPanel = new JPanel();
138
    private ColorPanel colorPanel = new ColorPanel();
139
    private Color color = Color.black;
140
//    private int alpha;
141
    private Dimension dim = new Dimension(100, 22);
142
    private Dimension dim2 = new Dimension(dim.width, dim.height*2+5);
143
    private ArrayList<ActionListener> actionListeners = new ArrayList<ActionListener>();
144
    private boolean withTransp;
145
    private boolean withTranspPerc;
146
        private boolean withNoFill;
147
    private JCheckBox chkUseColor;
148
        private JSlider sldTransparency;
149
        private JLabel lblTransparency;
150
        private boolean muteSldTransparency = false;
151
        private double perc = 0.392156863 ;/*100/255*/
152
        private ChangeListener sldAction = new ChangeListener() {
153
                public void stateChanged(ChangeEvent e) {
154
                        if (!muteSldTransparency) {
155
                                int alphaValue = sldTransparency.getValue();
156
                                setAlpha(alphaValue);
157

    
158
                                if(withTranspPerc) {
159
                                        int percValue = (int) (alphaValue * perc);
160
                                        lblTransparency.setText( String.valueOf(percValue) + "%");
161
                                }
162
                                fireActionPerformed();
163
                        }
164
                }
165

    
166
        };
167

    
168

    
169
    public ColorChooserPanel() {
170
        this(false);
171
    }
172

    
173
    /**
174
         * Constructor method
175
         *
176
         * @param withTransparencySlider boolean that specifies if the user
177
         * wants a slider with the color chooser panel to control the transparency
178
         * of the selected color.Also, it will appear a text which specifies the transparency percentage.
179
         * @param withNoFill boolean that specifies if the color chooser panel
180
         * allows a null value for the color selected (true case). If this values
181
         * is false, when the color selected is null then black color is assigned
182
         * automatically.
183
         */
184
    public ColorChooserPanel(boolean withTransparencySlider) {
185
            this(withTransparencySlider,false);
186

    
187
    }
188

    
189
        /**
190
         * Constructor method
191
         *
192
         * @param withTransparencySlider boolean that specifies if the user
193
         * wants a slider with the color chooser panel to control the transparency
194
         * of the selected color.
195
         * @param withNoFill boolean that specifies if the color chooser panel
196
         * allows a null value for the color selected (true case). If this values
197
         * is false, when the color selected is null then black color is assigned
198
         * automatically.
199
         */
200
        public ColorChooserPanel(boolean withTransparencySlider, boolean withNoFill) {
201

    
202
                this.withTransp=withTransparencySlider;
203
                this.withTranspPerc=withTransparencySlider;
204
                this.withNoFill=withNoFill;
205

    
206
                try {
207
                        if (withTransp) {
208
                                sldTransparency = new JSlider(0, 255);
209
                                sldTransparency.addChangeListener(sldAction);
210
                                int width = withNoFill ? dim2.width - 40 : dim2.width;
211
                                sldTransparency.setPreferredSize(new Dimension(width-5, dim2.height/2));
212
                                sldTransparency.setToolTipText(Messages.getText("transparencia"));
213

    
214
                                if(withTranspPerc) {
215
                                        lblTransparency = new JLabel();
216
                                        int percValue = (int) (sldTransparency.getValue() * perc);
217
                                        lblTransparency.setText(String.valueOf(percValue) +"%");
218
                                        lblTransparency.setPreferredSize(new Dimension(40,20));
219
                                }
220

    
221
                                sldTransparency.setValue(255);
222

    
223
                        }
224
                        jbInit();
225
                        colorPanel.setLineColor(null);
226
                        changeButton.setToolTipText(Messages.getText("browse"));
227

    
228
                } catch (Exception e) {
229
                        e.printStackTrace();
230
                }
231
        }
232

    
233
        private void jbInit() throws Exception {
234
                JPanel pane = new JPanel(new GridBagLayout());
235
                GridBagConstraints c = new GridBagConstraints();
236

    
237
                changeButton.setText("...");
238
                changeButton.addActionListener(new java.awt.event.ActionListener() {
239
                        public void actionPerformed(ActionEvent e) {
240
                                changeButton_actionPerformed(e);
241
                        }
242
                });
243
                outerColorPanel.setBorder(BorderFactory.createLoweredBevelBorder());
244
                outerColorPanel.setPreferredSize(new Dimension(dim.width/2, dim.height));
245
                outerColorPanel.setBackground(Color.white);
246

    
247

    
248
                c.fill = GridBagConstraints.HORIZONTAL;
249
                c.gridx = 1;
250
                c.gridy = 0;
251
                c.gridwidth = 2;
252
                pane.add(outerColorPanel,c);
253

    
254
                c.fill = GridBagConstraints.HORIZONTAL;
255
                c.gridx = 3;
256
                c.gridy = 0;
257
                c.gridwidth = 1;
258
                pane.add(changeButton,c);
259
                outerColorPanel.add(colorPanel);
260

    
261
                if(withNoFill) {
262
                        chkUseColor = new JCheckBox();
263
                        chkUseColor.setSelected(true);
264
                        c.fill = GridBagConstraints.HORIZONTAL;
265
                        c.gridwidth = 1;
266
                        c.gridx = 0;
267
                        c.gridy = 1;
268

    
269

    
270
                        chkUseColor.addActionListener(new java.awt.event.ActionListener() {
271
                                public void actionPerformed(ActionEvent e) {
272
                                        useColor_actionPerformed(e);
273
                                }
274
                        });
275
                        pane.add(chkUseColor,c);
276
                }
277

    
278

    
279
                if (withTransp) {
280

    
281
                        c.fill = GridBagConstraints.HORIZONTAL;
282
                        c.gridwidth = 3;
283
                        c.gridx = 1;
284
                        c.gridy = 1;
285
                        pane.add(sldTransparency,c);
286

    
287

    
288
                        if(withTranspPerc) {
289
                                c.fill = GridBagConstraints.HORIZONTAL;
290
                                c.gridwidth = 3;
291
                                c.gridx = 4;
292
                                c.gridy = 1;
293
                                pane.add(lblTransparency,c);
294
                        }
295

    
296
                }
297

    
298
                add(pane);
299
        }
300

    
301
    private void changeButton_actionPerformed(ActionEvent e) {
302
            Color newColor = JColorChooser.showDialog(SwingUtilities.windowForComponent(this), Messages.getText("choose_color"), color);
303

    
304
        if (newColor == null) {
305
            return;
306
        }
307

    
308
        setColor(newColor);
309
        fireActionPerformed();
310
    }
311

    
312
    /**
313
         * Returns true if the check box of the color chooser panel is selected.
314
         * False otherwise.
315
         * @return boolean true if the check box is selected. Otherwise, false.
316
         */
317
        public boolean getUseColorisSelected() {
318
                if(withNoFill)
319
                        return chkUseColor.isSelected();
320
                return false;
321
        }
322
        /**
323
         * Sets the check box of the color chooser panel
324

325
         * @param b boolean true if the user wants to select it.Otherwise, false.
326
         */
327
        public void setUseColorIsSelected(boolean b) {
328
                if(withNoFill)
329
                        chkUseColor.setSelected(b);
330
        }
331
        /**
332
         * Controls the events when the check box of the color chooser panel
333
         * is modified
334
         *
335
         * @param e Action Event
336
         */
337
        private void useColor_actionPerformed(ActionEvent e) {
338
                if(chkUseColor.isSelected())
339
                        setColor(color);
340
                else setColor(null);
341
                fireActionPerformed();
342
        }
343

    
344
     public void addActionListener(ActionListener l) {
345
        actionListeners.add(l);
346
    }
347

    
348
    public void removeActionListener(ActionListener l) {
349
        actionListeners.remove(l);
350
    }
351

    
352
    protected void fireActionPerformed() {
353
        for (Iterator<ActionListener> i = actionListeners.iterator(); i.hasNext();) {
354
            i.next().actionPerformed(new ActionEvent(this, 0, null));
355
        }
356
    }
357
        /**
358
         * Sets the color of the chooser panel.
359
         *
360
         * @param color Color to be selected.
361
         */
362
        public void setColor(Color color) {
363

    
364
                if( color != null) {
365
                        this.color = color;
366
                        setAlpha(color.getAlpha());
367
                        updateColorPanel();
368
                }
369

    
370
        }
371

    
372
        /**
373
         * Sets the alpha value of the color selected in the color chooser
374
         * panel.
375
         *
376
         * @param alpha Alpha value of the color.
377
         */
378
        public void setAlpha(int alpha) {
379
                muteSldTransparency = true;
380

    
381
                if (color != null)
382
                        color=alphaColor(color,alpha);
383

    
384
                if (withTransp) {
385
                        sldTransparency.setValue(alpha);
386
                        sldTransparency.validate();
387
                        int percValue = (int) (alpha * perc);
388
                        lblTransparency.setText( String.valueOf(percValue)+ "%");
389
                }
390

    
391
                updateColorPanel();
392
                muteSldTransparency = false;
393
//                fireActionPerformed();
394
        }
395

    
396
        public int getAlpha() {
397
                if (withTransp) {
398
                        return sldTransparency.getValue();
399
                } else {
400
                        return color.getAlpha();
401
                }
402

    
403
        }
404

    
405
    private void updateColorPanel() {
406
        colorPanel.setFillColor(color);
407
        colorPanel.repaint();
408
    }
409

    
410

    
411
        /**
412
         * Obtains the selected color in the color chooser panel.
413
         *
414
         * @return color the selected color
415
         */
416
        public Color getColor() {
417
                return color;
418
        }
419

    
420
        public void disabledWithTransparency() {
421
                changeButton.setEnabled(false);
422
                if(withNoFill)
423
                        chkUseColor.setEnabled(false);
424

    
425
        }
426

    
427
        @Override
428
        public void setEnabled(boolean newEnabled) {
429

    
430
                changeButton.setEnabled(newEnabled);
431
                if(withTransp) {
432
                        sldTransparency.setEnabled(newEnabled);
433
                }
434
                if(withTranspPerc) {
435
                        lblTransparency.setEnabled(newEnabled);
436
                }
437
                if(withNoFill) {
438
                        chkUseColor.setEnabled(newEnabled);
439
                }
440

    
441
        }
442

    
443
        public static void main(String[] args) {
444
                JFrame f = new JFrame();
445

    
446
                final ColorChooserPanel ce2 = new ColorChooserPanel(true,true);
447

    
448
                JPanel content = new JPanel(new GridLayout(2, 1));
449

    
450
                content.add(ce2);
451

    
452

    
453
                f.setContentPane(content);
454
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
455
                f.pack();
456
                f.setVisible(true);
457

    
458
        }
459

    
460
    private Color alphaColor(Color color, int alpha) {
461
        return new Color(color.getRed(), color.getGreen(), color.getBlue(),
462
            alpha);
463
    }
464

    
465
}