svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / gui / panels / ColorChooserPanel.java @ 34002
History | View | Annotate | Download (11.6 KB)
1 | 6117 | jaume | |
---|---|---|---|
2 | /*
|
||
3 | 6602 | caballero | * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
|
4 | 6117 | jaume | * for visualizing and manipulating spatial features with geometry and attributes.
|
5 | *
|
||
6 | * Copyright (C) 2003 Vivid Solutions
|
||
7 | 6602 | caballero | *
|
8 | 6117 | jaume | * 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 | 6602 | caballero | *
|
13 | 6117 | jaume | * 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 | 6602 | caballero | *
|
18 | 6117 | jaume | * 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 | 6602 | caballero | *
|
22 | 6117 | jaume | * 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 | 29596 | jpiera | package org.gvsig.app.gui.panels; |
75 | 6117 | jaume | |
76 | import java.awt.Color; |
||
77 | import java.awt.Dimension; |
||
78 | import java.awt.GridBagConstraints; |
||
79 | import java.awt.GridBagLayout; |
||
80 | 14757 | jdominguez | import java.awt.GridLayout; |
81 | 6117 | jaume | 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 | 18623 | jdominguez | import javax.swing.JCheckBox; |
89 | 6117 | jaume | import javax.swing.JColorChooser; |
90 | 18623 | jdominguez | import javax.swing.JFrame; |
91 | 20779 | vcaballero | import javax.swing.JLabel; |
92 | 6117 | jaume | import javax.swing.JPanel; |
93 | 14739 | jdominguez | import javax.swing.JSlider; |
94 | 6117 | jaume | import javax.swing.SwingUtilities; |
95 | 14739 | jdominguez | import javax.swing.event.ChangeEvent; |
96 | import javax.swing.event.ChangeListener; |
||
97 | 6117 | jaume | |
98 | 29596 | jpiera | import org.gvsig.andami.PluginServices; |
99 | import org.gvsig.app.gui.GUIUtil; |
||
100 | 6117 | jaume | |
101 | 11433 | caballero | |
102 | 29596 | jpiera | |
103 | 6117 | jaume | /**
|
104 | * A custom Colour Scheme Browser - custom choices based on JColorChooser.
|
||
105 | */
|
||
106 | |||
107 | public class ColorChooserPanel extends JPanel { |
||
108 | 14739 | jdominguez | private GridBagLayout gridBagLayout1 = new GridBagLayout(); |
109 | private JButton changeButton = new JButton(); |
||
110 | 6117 | jaume | |
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 | 14739 | jdominguez | private JPanel outerColorPanel = new JPanel(); |
116 | private ColorPanel colorPanel = new ColorPanel(); |
||
117 | 6117 | jaume | private Color color = Color.black; |
118 | 14739 | jdominguez | // private int alpha;
|
119 | 16283 | jdominguez | private Dimension dim = new Dimension(100, 22); |
120 | 14757 | jdominguez | private Dimension dim2 = new Dimension(dim.width, dim.height*2+5); |
121 | 18623 | jdominguez | private ArrayList<ActionListener> actionListeners = new ArrayList<ActionListener>(); |
122 | 14739 | jdominguez | private boolean withTransp; |
123 | 20779 | vcaballero | private boolean withTranspPerc; |
124 | private boolean withNoFill; |
||
125 | 18623 | jdominguez | private JCheckBox chkUseColor; |
126 | private JSlider sldTransparency; |
||
127 | 20779 | vcaballero | private JLabel lblTransparency; |
128 | private boolean muteSldTransparency = false; |
||
129 | private double perc = 0.392156863 ;/*100/255*/ |
||
130 | 14739 | jdominguez | private ChangeListener sldAction = new ChangeListener() { |
131 | public void stateChanged(ChangeEvent e) { |
||
132 | if (!muteSldTransparency) {
|
||
133 | int alphaValue = sldTransparency.getValue();
|
||
134 | setAlpha(alphaValue); |
||
135 | 22653 | vcaballero | |
136 | 20779 | vcaballero | if(withTranspPerc) {
|
137 | int percValue = (int) (alphaValue * perc); |
||
138 | 20903 | vcaballero | lblTransparency.setText( String.valueOf(percValue) + "%"); |
139 | 20779 | vcaballero | } |
140 | 14739 | jdominguez | fireActionPerformed(); |
141 | } |
||
142 | } |
||
143 | 6117 | jaume | |
144 | 14739 | jdominguez | }; |
145 | |||
146 | 22653 | vcaballero | |
147 | 6117 | jaume | public ColorChooserPanel() {
|
148 | 14739 | jdominguez | this(false); |
149 | 6117 | jaume | } |
150 | |||
151 | 20779 | vcaballero | /**
|
152 | * Constructor method
|
||
153 | 22653 | vcaballero | *
|
154 | 20779 | vcaballero | * @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 | 22653 | vcaballero | * allows a null value for the color selected (true case). If this values
|
159 | 20779 | vcaballero | * is false, when the color selected is null then black color is assigned
|
160 | 22653 | vcaballero | * automatically.
|
161 | 20779 | vcaballero | */
|
162 | 14739 | jdominguez | public ColorChooserPanel(boolean withTransparencySlider) { |
163 | 18623 | jdominguez | this(withTransparencySlider,false); |
164 | 22653 | vcaballero | |
165 | 14739 | jdominguez | } |
166 | 22653 | vcaballero | |
167 | 18623 | jdominguez | /**
|
168 | * Constructor method
|
||
169 | 22653 | vcaballero | *
|
170 | 18623 | jdominguez | * @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 | 22653 | vcaballero | * allows a null value for the color selected (true case). If this values
|
175 | 18623 | jdominguez | * is false, when the color selected is null then black color is assigned
|
176 | 22653 | vcaballero | * automatically.
|
177 | 18623 | jdominguez | */
|
178 | public ColorChooserPanel(boolean withTransparencySlider, boolean withNoFill) { |
||
179 | 14739 | jdominguez | |
180 | 18623 | jdominguez | this.withTransp=withTransparencySlider;
|
181 | 20779 | vcaballero | this.withTranspPerc=withTransparencySlider;
|
182 | 18623 | jdominguez | this.withNoFill=withNoFill;
|
183 | 6117 | jaume | |
184 | 18623 | jdominguez | try {
|
185 | if (withTransp) {
|
||
186 | sldTransparency = new JSlider(0, 255); |
||
187 | sldTransparency.addChangeListener(sldAction); |
||
188 | int width = withNoFill ? dim2.width - 40 : dim2.width; |
||
189 | 20903 | vcaballero | sldTransparency.setPreferredSize(new Dimension(width-5, dim2.height/2)); |
190 | 18623 | jdominguez | sldTransparency.setToolTipText(PluginServices.getText(this, "transparencia")); |
191 | 20779 | vcaballero | |
192 | 22653 | vcaballero | if(withTranspPerc) {
|
193 | 20779 | vcaballero | lblTransparency = new JLabel(); |
194 | int percValue = (int) (sldTransparency.getValue() * perc); |
||
195 | 20903 | vcaballero | lblTransparency.setText(String.valueOf(percValue) +"%"); |
196 | 22653 | vcaballero | lblTransparency.setPreferredSize(new Dimension(40,20)); |
197 | 20779 | vcaballero | } |
198 | |||
199 | sldTransparency.setValue(255);
|
||
200 | |||
201 | 18623 | jdominguez | } |
202 | jbInit(); |
||
203 | colorPanel.setLineColor(null);
|
||
204 | changeButton.setToolTipText(PluginServices.getText(this,"browse")); |
||
205 | 6117 | jaume | |
206 | 18623 | jdominguez | } 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 | 20779 | vcaballero | chkUseColor.setSelected(true);
|
242 | 18623 | jdominguez | 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 | 20779 | vcaballero | |
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 | 18623 | jdominguez | } |
275 | |||
276 | add(pane); |
||
277 | 22653 | vcaballero | } |
278 | 18623 | jdominguez | |
279 | private void changeButton_actionPerformed(ActionEvent e) { |
||
280 | Color newColor = JColorChooser.showDialog(SwingUtilities.windowForComponent(this), PluginServices.getText(this,"choose_color"), color); |
||
281 | |||
282 | 6117 | jaume | if (newColor == null) { |
283 | return;
|
||
284 | } |
||
285 | |||
286 | setColor(newColor); |
||
287 | fireActionPerformed(); |
||
288 | } |
||
289 | 22653 | vcaballero | |
290 | 18623 | jdominguez | /**
|
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 | 6117 | jaume | |
303 | 18623 | jdominguez | * @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 | 22653 | vcaballero | *
|
313 | 18623 | jdominguez | * @param e Action Event
|
314 | */
|
||
315 | private void useColor_actionPerformed(ActionEvent e) { |
||
316 | 20779 | vcaballero | if(chkUseColor.isSelected())
|
317 | setColor(color); |
||
318 | else setColor(null); |
||
319 | 18623 | jdominguez | fireActionPerformed(); |
320 | } |
||
321 | 14739 | jdominguez | |
322 | 20779 | vcaballero | 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 | 18623 | jdominguez | /**
|
336 | * Sets the color of the chooser panel.
|
||
337 | 22653 | vcaballero | *
|
338 | 18623 | jdominguez | * @param color Color to be selected.
|
339 | */
|
||
340 | public void setColor(Color color) { |
||
341 | |||
342 | 20779 | vcaballero | if( color != null) { |
343 | this.color = color;
|
||
344 | 18623 | jdominguez | setAlpha(color.getAlpha()); |
345 | 20779 | vcaballero | updateColorPanel(); |
346 | } |
||
347 | 18623 | jdominguez | |
348 | } |
||
349 | 22653 | vcaballero | |
350 | 18623 | jdominguez | /**
|
351 | 22653 | vcaballero | * Sets the alpha value of the color selected in the color chooser
|
352 | 18623 | jdominguez | * panel.
|
353 | 22653 | vcaballero | *
|
354 | 18623 | jdominguez | * @param alpha Alpha value of the color.
|
355 | */
|
||
356 | public void setAlpha(int alpha) { |
||
357 | muteSldTransparency = true;
|
||
358 | |||
359 | if (color != null) |
||
360 | 22653 | vcaballero | color=GUIUtil.alphaColor(color,alpha); |
361 | 18623 | jdominguez | |
362 | if (withTransp) {
|
||
363 | sldTransparency.setValue(alpha); |
||
364 | sldTransparency.validate(); |
||
365 | 20779 | vcaballero | int percValue = (int) (alpha * perc); |
366 | 20903 | vcaballero | lblTransparency.setText( String.valueOf(percValue)+ "%"); |
367 | 18623 | jdominguez | } |
368 | 22653 | vcaballero | |
369 | 18623 | jdominguez | updateColorPanel(); |
370 | muteSldTransparency = false;
|
||
371 | 23712 | vcaballero | // fireActionPerformed();
|
372 | 18623 | jdominguez | } |
373 | 22653 | vcaballero | |
374 | 18623 | jdominguez | public int getAlpha() { |
375 | if (withTransp) {
|
||
376 | return sldTransparency.getValue();
|
||
377 | } else {
|
||
378 | return color.getAlpha();
|
||
379 | } |
||
380 | 22653 | vcaballero | |
381 | 18623 | jdominguez | } |
382 | 22653 | vcaballero | |
383 | 6117 | jaume | private void updateColorPanel() { |
384 | 14739 | jdominguez | colorPanel.setFillColor(color); |
385 | 6117 | jaume | colorPanel.repaint(); |
386 | } |
||
387 | |||
388 | 22653 | vcaballero | |
389 | 18623 | jdominguez | /**
|
390 | 22653 | vcaballero | * Obtains the selected color in the color chooser panel.
|
391 | *
|
||
392 | 18623 | jdominguez | * @return color the selected color
|
393 | */
|
||
394 | public Color getColor() { |
||
395 | return color;
|
||
396 | } |
||
397 | 22653 | vcaballero | |
398 | 18623 | jdominguez | public void disabledWithTransparency() { |
399 | changeButton.setEnabled(false);
|
||
400 | 22653 | vcaballero | if(withNoFill)
|
401 | 18623 | jdominguez | chkUseColor.setEnabled(false);
|
402 | 6117 | jaume | |
403 | 18623 | jdominguez | } |
404 | |||
405 | @Override
|
||
406 | 14739 | jdominguez | public void setEnabled(boolean newEnabled) { |
407 | 18623 | jdominguez | |
408 | changeButton.setEnabled(newEnabled); |
||
409 | if(withTransp) {
|
||
410 | sldTransparency.setEnabled(newEnabled); |
||
411 | } |
||
412 | 20779 | vcaballero | if(withTranspPerc) {
|
413 | lblTransparency.setEnabled(newEnabled); |
||
414 | } |
||
415 | 18623 | jdominguez | if(withNoFill) {
|
416 | chkUseColor.setEnabled(newEnabled); |
||
417 | } |
||
418 | |||
419 | } |
||
420 | 22653 | vcaballero | |
421 | 18623 | jdominguez | 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 | 22653 | vcaballero | |
428 | 18623 | jdominguez | content.add(ce2); |
429 | |||
430 | 22653 | vcaballero | |
431 | 18623 | jdominguez | f.setContentPane(content); |
432 | f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||
433 | f.pack(); |
||
434 | f.setVisible(true);
|
||
435 | |||
436 | } |
||
437 | 22653 | vcaballero | |
438 | 14739 | jdominguez | } |