Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / gui / panels / ColorChooserPanel.java @ 29596

History | View | Annotate | Download (11.6 KB)

1

    
2
/*
3
 * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
4
 * for visualizing and manipulating spatial features with geometry and attributes.
5
 *
6
 * Copyright (C) 2003 Vivid Solutions
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
 *
22
 * For more information, contact:
23
 *
24
 * Vivid Solutions
25
 * Suite #1A
26
 * 2328 Government Street
27
 * Victoria BC  V8T 5G5
28
 * Canada
29
 *
30
 * (250)385-6040
31
 * www.vividsolutions.com
32
 */
33

    
34
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
35
 *
36
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
37
 *
38
 * This program is free software; you can redistribute it and/or
39
 * modify it under the terms of the GNU General Public License
40
 * as published by the Free Software Foundation; either version 2
41
 * of the License, or (at your option) any later version.
42
 *
43
 * This program is distributed in the hope that it will be useful,
44
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46
 * GNU General Public License for more details.
47
 *
48
 * You should have received a copy of the GNU General Public License
49
 * along with this program; if not, write to the Free Software
50
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
51
 *
52
 * For more information, contact:
53
 *
54
 *  Generalitat Valenciana
55
 *   Conselleria d'Infraestructures i Transport
56
 *   Av. Blasco Ib??ez, 50
57
 *   46010 VALENCIA
58
 *   SPAIN
59
 *
60
 *      +34 963862235
61
 *   gvsig@gva.es
62
 *      www.gvsig.gva.es
63
 *
64
 *    or
65
 *
66
 *   IVER T.I. S.A
67
 *   Salamanca 50
68
 *   46005 Valencia
69
 *   Spain
70
 *
71
 *   +34 963163400
72
 *   dac@iver.es
73
 */
74
package org.gvsig.app.gui.panels;
75

    
76
import java.awt.Color;
77
import java.awt.Dimension;
78
import java.awt.GridBagConstraints;
79
import java.awt.GridBagLayout;
80
import java.awt.GridLayout;
81
import java.awt.event.ActionEvent;
82
import java.awt.event.ActionListener;
83
import java.util.ArrayList;
84
import java.util.Iterator;
85

    
86
import javax.swing.BorderFactory;
87
import javax.swing.JButton;
88
import javax.swing.JCheckBox;
89
import javax.swing.JColorChooser;
90
import javax.swing.JFrame;
91
import javax.swing.JLabel;
92
import javax.swing.JPanel;
93
import javax.swing.JSlider;
94
import javax.swing.SwingUtilities;
95
import javax.swing.event.ChangeEvent;
96
import javax.swing.event.ChangeListener;
97

    
98
import org.gvsig.andami.PluginServices;
99
import org.gvsig.app.gui.GUIUtil;
100

    
101

    
102

    
103
/**
104
 * A custom Colour Scheme Browser - custom choices based on JColorChooser.
105
 */
106

    
107
public class ColorChooserPanel extends JPanel {
108
        private GridBagLayout gridBagLayout1 = new GridBagLayout();
109
        private JButton changeButton = new JButton();
110

    
111

    
112
    //{DEFECT-PREVENTION} In accessors, it's better to use "workbench = newWorkbench"
113
    //rather than "this.workbench = workbench", because otherwise if you misspell the
114
    //argument, Java won't complain! We should do this everywhere. [Jon Aquino]
115
    private JPanel outerColorPanel = new JPanel();
116
    private ColorPanel colorPanel = new ColorPanel();
117
    private Color color = Color.black;
118
//    private int alpha;
119
    private Dimension dim = new Dimension(100, 22);
120
    private Dimension dim2 = new Dimension(dim.width, dim.height*2+5);
121
    private ArrayList<ActionListener> actionListeners = new ArrayList<ActionListener>();
122
    private boolean withTransp;
123
    private boolean withTranspPerc;
124
        private boolean withNoFill;
125
    private JCheckBox chkUseColor;
126
        private JSlider sldTransparency;
127
        private JLabel lblTransparency;
128
        private boolean muteSldTransparency = false;
129
        private double perc = 0.392156863 ;/*100/255*/
130
        private ChangeListener sldAction = new ChangeListener() {
131
                public void stateChanged(ChangeEvent e) {
132
                        if (!muteSldTransparency) {
133
                                int alphaValue = sldTransparency.getValue();
134
                                setAlpha(alphaValue);
135

    
136
                                if(withTranspPerc) {
137
                                        int percValue = (int) (alphaValue * perc);
138
                                        lblTransparency.setText( String.valueOf(percValue) + "%");
139
                                }
140
                                fireActionPerformed();
141
                        }
142
                }
143

    
144
        };
145

    
146

    
147
    public ColorChooserPanel() {
148
        this(false);
149
    }
150

    
151
    /**
152
         * Constructor method
153
         *
154
         * @param withTransparencySlider boolean that specifies if the user
155
         * wants a slider with the color chooser panel to control the transparency
156
         * of the selected color.Also, it will appear a text which specifies the transparency percentage.
157
         * @param withNoFill boolean that specifies if the color chooser panel
158
         * allows a null value for the color selected (true case). If this values
159
         * is false, when the color selected is null then black color is assigned
160
         * automatically.
161
         */
162
    public ColorChooserPanel(boolean withTransparencySlider) {
163
            this(withTransparencySlider,false);
164

    
165
    }
166

    
167
        /**
168
         * Constructor method
169
         *
170
         * @param withTransparencySlider boolean that specifies if the user
171
         * wants a slider with the color chooser panel to control the transparency
172
         * of the selected color.
173
         * @param withNoFill boolean that specifies if the color chooser panel
174
         * allows a null value for the color selected (true case). If this values
175
         * is false, when the color selected is null then black color is assigned
176
         * automatically.
177
         */
178
        public ColorChooserPanel(boolean withTransparencySlider, boolean withNoFill) {
179

    
180
                this.withTransp=withTransparencySlider;
181
                this.withTranspPerc=withTransparencySlider;
182
                this.withNoFill=withNoFill;
183

    
184
                try {
185
                        if (withTransp) {
186
                                sldTransparency = new JSlider(0, 255);
187
                                sldTransparency.addChangeListener(sldAction);
188
                                int width = withNoFill ? dim2.width - 40 : dim2.width;
189
                                sldTransparency.setPreferredSize(new Dimension(width-5, dim2.height/2));
190
                                sldTransparency.setToolTipText(PluginServices.getText(this, "transparencia"));
191

    
192
                                if(withTranspPerc) {
193
                                        lblTransparency = new JLabel();
194
                                        int percValue = (int) (sldTransparency.getValue() * perc);
195
                                        lblTransparency.setText(String.valueOf(percValue) +"%");
196
                                        lblTransparency.setPreferredSize(new Dimension(40,20));
197
                                }
198

    
199
                                sldTransparency.setValue(255);
200

    
201
                        }
202
                        jbInit();
203
                        colorPanel.setLineColor(null);
204
                        changeButton.setToolTipText(PluginServices.getText(this,"browse"));
205

    
206
                } catch (Exception e) {
207
                        e.printStackTrace();
208
                }
209
        }
210

    
211
        private void jbInit() throws Exception {
212
                JPanel pane = new JPanel(new GridBagLayout());
213
                GridBagConstraints c = new GridBagConstraints();
214

    
215
                changeButton.setText("...");
216
                changeButton.addActionListener(new java.awt.event.ActionListener() {
217
                        public void actionPerformed(ActionEvent e) {
218
                                changeButton_actionPerformed(e);
219
                        }
220
                });
221
                outerColorPanel.setBorder(BorderFactory.createLoweredBevelBorder());
222
                outerColorPanel.setPreferredSize(new Dimension(dim.width/2, dim.height));
223
                outerColorPanel.setBackground(Color.white);
224

    
225

    
226
                c.fill = GridBagConstraints.HORIZONTAL;
227
                c.gridx = 1;
228
                c.gridy = 0;
229
                c.gridwidth = 2;
230
                pane.add(outerColorPanel,c);
231

    
232
                c.fill = GridBagConstraints.HORIZONTAL;
233
                c.gridx = 3;
234
                c.gridy = 0;
235
                c.gridwidth = 1;
236
                pane.add(changeButton,c);
237
                outerColorPanel.add(colorPanel);
238

    
239
                if(withNoFill) {
240
                        chkUseColor = new JCheckBox();
241
                        chkUseColor.setSelected(true);
242
                        c.fill = GridBagConstraints.HORIZONTAL;
243
                        c.gridwidth = 1;
244
                        c.gridx = 0;
245
                        c.gridy = 1;
246

    
247

    
248
                        chkUseColor.addActionListener(new java.awt.event.ActionListener() {
249
                                public void actionPerformed(ActionEvent e) {
250
                                        useColor_actionPerformed(e);
251
                                }
252
                        });
253
                        pane.add(chkUseColor,c);
254
                }
255

    
256

    
257
                if (withTransp) {
258

    
259
                        c.fill = GridBagConstraints.HORIZONTAL;
260
                        c.gridwidth = 3;
261
                        c.gridx = 1;
262
                        c.gridy = 1;
263
                        pane.add(sldTransparency,c);
264

    
265

    
266
                        if(withTranspPerc) {
267
                                c.fill = GridBagConstraints.HORIZONTAL;
268
                                c.gridwidth = 3;
269
                                c.gridx = 4;
270
                                c.gridy = 1;
271
                                pane.add(lblTransparency,c);
272
                        }
273

    
274
                }
275

    
276
                add(pane);
277
        }
278

    
279
    private void changeButton_actionPerformed(ActionEvent e) {
280
            Color newColor = JColorChooser.showDialog(SwingUtilities.windowForComponent(this), PluginServices.getText(this,"choose_color"), color);
281

    
282
        if (newColor == null) {
283
            return;
284
        }
285

    
286
        setColor(newColor);
287
        fireActionPerformed();
288
    }
289

    
290
    /**
291
         * Returns true if the check box of the color chooser panel is selected.
292
         * False otherwise.
293
         * @return boolean true if the check box is selected. Otherwise, false.
294
         */
295
        public boolean getUseColorisSelected() {
296
                if(withNoFill)
297
                        return chkUseColor.isSelected();
298
                return false;
299
        }
300
        /**
301
         * Sets the check box of the color chooser panel
302

303
         * @param b boolean true if the user wants to select it.Otherwise, false.
304
         */
305
        public void setUseColorIsSelected(boolean b) {
306
                if(withNoFill)
307
                        chkUseColor.setSelected(b);
308
        }
309
        /**
310
         * Controls the events when the check box of the color chooser panel
311
         * is modified
312
         *
313
         * @param e Action Event
314
         */
315
        private void useColor_actionPerformed(ActionEvent e) {
316
                if(chkUseColor.isSelected())
317
                        setColor(color);
318
                else setColor(null);
319
                fireActionPerformed();
320
        }
321

    
322
     public void addActionListener(ActionListener l) {
323
        actionListeners.add(l);
324
    }
325

    
326
    public void removeActionListener(ActionListener l) {
327
        actionListeners.remove(l);
328
    }
329

    
330
    protected void fireActionPerformed() {
331
        for (Iterator<ActionListener> i = actionListeners.iterator(); i.hasNext();) {
332
            i.next().actionPerformed(new ActionEvent(this, 0, null));
333
        }
334
    }
335
        /**
336
         * Sets the color of the chooser panel.
337
         *
338
         * @param color Color to be selected.
339
         */
340
        public void setColor(Color color) {
341

    
342
                if( color != null) {
343
                        this.color = color;
344
                        setAlpha(color.getAlpha());
345
                        updateColorPanel();
346
                }
347

    
348
        }
349

    
350
        /**
351
         * Sets the alpha value of the color selected in the color chooser
352
         * panel.
353
         *
354
         * @param alpha Alpha value of the color.
355
         */
356
        public void setAlpha(int alpha) {
357
                muteSldTransparency = true;
358

    
359
                if (color != null)
360
                        color=GUIUtil.alphaColor(color,alpha);
361

    
362
                if (withTransp) {
363
                        sldTransparency.setValue(alpha);
364
                        sldTransparency.validate();
365
                        int percValue = (int) (alpha * perc);
366
                        lblTransparency.setText( String.valueOf(percValue)+ "%");
367
                }
368

    
369
                updateColorPanel();
370
                muteSldTransparency = false;
371
//                fireActionPerformed();
372
        }
373

    
374
        public int getAlpha() {
375
                if (withTransp) {
376
                        return sldTransparency.getValue();
377
                } else {
378
                        return color.getAlpha();
379
                }
380

    
381
        }
382

    
383
    private void updateColorPanel() {
384
        colorPanel.setFillColor(color);
385
        colorPanel.repaint();
386
    }
387

    
388

    
389
        /**
390
         * Obtains the selected color in the color chooser panel.
391
         *
392
         * @return color the selected color
393
         */
394
        public Color getColor() {
395
                return color;
396
        }
397

    
398
        public void disabledWithTransparency() {
399
                changeButton.setEnabled(false);
400
                if(withNoFill)
401
                        chkUseColor.setEnabled(false);
402

    
403
        }
404

    
405
        @Override
406
        public void setEnabled(boolean newEnabled) {
407

    
408
                changeButton.setEnabled(newEnabled);
409
                if(withTransp) {
410
                        sldTransparency.setEnabled(newEnabled);
411
                }
412
                if(withTranspPerc) {
413
                        lblTransparency.setEnabled(newEnabled);
414
                }
415
                if(withNoFill) {
416
                        chkUseColor.setEnabled(newEnabled);
417
                }
418

    
419
        }
420

    
421
        public static void main(String[] args) {
422
                JFrame f = new JFrame();
423

    
424
                final ColorChooserPanel ce2 = new ColorChooserPanel(true,true);
425

    
426
                JPanel content = new JPanel(new GridLayout(2, 1));
427

    
428
                content.add(ce2);
429

    
430

    
431
                f.setContentPane(content);
432
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
433
                f.pack();
434
                f.setVisible(true);
435

    
436
        }
437

    
438
}