svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libUIComponent / src / org / gvsig / app / gui / panels / ColorChooserPanel.java @ 34300
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.i18n.Messages; |
99 |
|
100 |
|
101 |
|
102 |
/**
|
103 |
* A custom Colour Scheme Browser - custom choices based on JColorChooser.
|
104 |
*/
|
105 |
|
106 |
public class ColorChooserPanel extends JPanel { |
107 |
private GridBagLayout gridBagLayout1 = new GridBagLayout(); |
108 |
private JButton changeButton = new JButton(); |
109 |
|
110 |
|
111 |
//{DEFECT-PREVENTION} In accessors, it's better to use "workbench = newWorkbench"
|
112 |
//rather than "this.workbench = workbench", because otherwise if you misspell the
|
113 |
//argument, Java won't complain! We should do this everywhere. [Jon Aquino]
|
114 |
private JPanel outerColorPanel = new JPanel(); |
115 |
private ColorPanel colorPanel = new ColorPanel(); |
116 |
private Color color = Color.black; |
117 |
// private int alpha;
|
118 |
private Dimension dim = new Dimension(100, 22); |
119 |
private Dimension dim2 = new Dimension(dim.width, dim.height*2+5); |
120 |
private ArrayList<ActionListener> actionListeners = new ArrayList<ActionListener>(); |
121 |
private boolean withTransp; |
122 |
private boolean withTranspPerc; |
123 |
private boolean withNoFill; |
124 |
private JCheckBox chkUseColor; |
125 |
private JSlider sldTransparency; |
126 |
private JLabel lblTransparency; |
127 |
private boolean muteSldTransparency = false; |
128 |
private double perc = 0.392156863 ;/*100/255*/ |
129 |
private ChangeListener sldAction = new ChangeListener() { |
130 |
public void stateChanged(ChangeEvent e) { |
131 |
if (!muteSldTransparency) {
|
132 |
int alphaValue = sldTransparency.getValue();
|
133 |
setAlpha(alphaValue); |
134 |
|
135 |
if(withTranspPerc) {
|
136 |
int percValue = (int) (alphaValue * perc); |
137 |
lblTransparency.setText( String.valueOf(percValue) + "%"); |
138 |
} |
139 |
fireActionPerformed(); |
140 |
} |
141 |
} |
142 |
|
143 |
}; |
144 |
|
145 |
|
146 |
public ColorChooserPanel() {
|
147 |
this(false); |
148 |
} |
149 |
|
150 |
/**
|
151 |
* Constructor method
|
152 |
*
|
153 |
* @param withTransparencySlider boolean that specifies if the user
|
154 |
* wants a slider with the color chooser panel to control the transparency
|
155 |
* of the selected color.Also, it will appear a text which specifies the transparency percentage.
|
156 |
* @param withNoFill boolean that specifies if the color chooser panel
|
157 |
* allows a null value for the color selected (true case). If this values
|
158 |
* is false, when the color selected is null then black color is assigned
|
159 |
* automatically.
|
160 |
*/
|
161 |
public ColorChooserPanel(boolean withTransparencySlider) { |
162 |
this(withTransparencySlider,false); |
163 |
|
164 |
} |
165 |
|
166 |
/**
|
167 |
* Constructor method
|
168 |
*
|
169 |
* @param withTransparencySlider boolean that specifies if the user
|
170 |
* wants a slider with the color chooser panel to control the transparency
|
171 |
* of the selected color.
|
172 |
* @param withNoFill boolean that specifies if the color chooser panel
|
173 |
* allows a null value for the color selected (true case). If this values
|
174 |
* is false, when the color selected is null then black color is assigned
|
175 |
* automatically.
|
176 |
*/
|
177 |
public ColorChooserPanel(boolean withTransparencySlider, boolean withNoFill) { |
178 |
|
179 |
this.withTransp=withTransparencySlider;
|
180 |
this.withTranspPerc=withTransparencySlider;
|
181 |
this.withNoFill=withNoFill;
|
182 |
|
183 |
try {
|
184 |
if (withTransp) {
|
185 |
sldTransparency = new JSlider(0, 255); |
186 |
sldTransparency.addChangeListener(sldAction); |
187 |
int width = withNoFill ? dim2.width - 40 : dim2.width; |
188 |
sldTransparency.setPreferredSize(new Dimension(width-5, dim2.height/2)); |
189 |
sldTransparency.setToolTipText(Messages.getText("transparencia"));
|
190 |
|
191 |
if(withTranspPerc) {
|
192 |
lblTransparency = new JLabel(); |
193 |
int percValue = (int) (sldTransparency.getValue() * perc); |
194 |
lblTransparency.setText(String.valueOf(percValue) +"%"); |
195 |
lblTransparency.setPreferredSize(new Dimension(40,20)); |
196 |
} |
197 |
|
198 |
sldTransparency.setValue(255);
|
199 |
|
200 |
} |
201 |
jbInit(); |
202 |
colorPanel.setLineColor(null);
|
203 |
changeButton.setToolTipText(Messages.getText("browse"));
|
204 |
|
205 |
} catch (Exception e) { |
206 |
e.printStackTrace(); |
207 |
} |
208 |
} |
209 |
|
210 |
private void jbInit() throws Exception { |
211 |
JPanel pane = new JPanel(new GridBagLayout()); |
212 |
GridBagConstraints c = new GridBagConstraints(); |
213 |
|
214 |
changeButton.setText("...");
|
215 |
changeButton.addActionListener(new java.awt.event.ActionListener() {
|
216 |
public void actionPerformed(ActionEvent e) { |
217 |
changeButton_actionPerformed(e); |
218 |
} |
219 |
}); |
220 |
outerColorPanel.setBorder(BorderFactory.createLoweredBevelBorder());
|
221 |
outerColorPanel.setPreferredSize(new Dimension(dim.width/2, dim.height)); |
222 |
outerColorPanel.setBackground(Color.white);
|
223 |
|
224 |
|
225 |
c.fill = GridBagConstraints.HORIZONTAL;
|
226 |
c.gridx = 1;
|
227 |
c.gridy = 0;
|
228 |
c.gridwidth = 2;
|
229 |
pane.add(outerColorPanel,c); |
230 |
|
231 |
c.fill = GridBagConstraints.HORIZONTAL;
|
232 |
c.gridx = 3;
|
233 |
c.gridy = 0;
|
234 |
c.gridwidth = 1;
|
235 |
pane.add(changeButton,c); |
236 |
outerColorPanel.add(colorPanel); |
237 |
|
238 |
if(withNoFill) {
|
239 |
chkUseColor = new JCheckBox(); |
240 |
chkUseColor.setSelected(true);
|
241 |
c.fill = GridBagConstraints.HORIZONTAL;
|
242 |
c.gridwidth = 1;
|
243 |
c.gridx = 0;
|
244 |
c.gridy = 1;
|
245 |
|
246 |
|
247 |
chkUseColor.addActionListener(new java.awt.event.ActionListener() {
|
248 |
public void actionPerformed(ActionEvent e) { |
249 |
useColor_actionPerformed(e); |
250 |
} |
251 |
}); |
252 |
pane.add(chkUseColor,c); |
253 |
} |
254 |
|
255 |
|
256 |
if (withTransp) {
|
257 |
|
258 |
c.fill = GridBagConstraints.HORIZONTAL;
|
259 |
c.gridwidth = 3;
|
260 |
c.gridx = 1;
|
261 |
c.gridy = 1;
|
262 |
pane.add(sldTransparency,c); |
263 |
|
264 |
|
265 |
if(withTranspPerc) {
|
266 |
c.fill = GridBagConstraints.HORIZONTAL;
|
267 |
c.gridwidth = 3;
|
268 |
c.gridx = 4;
|
269 |
c.gridy = 1;
|
270 |
pane.add(lblTransparency,c); |
271 |
} |
272 |
|
273 |
} |
274 |
|
275 |
add(pane); |
276 |
} |
277 |
|
278 |
private void changeButton_actionPerformed(ActionEvent e) { |
279 |
Color newColor = JColorChooser.showDialog(SwingUtilities.windowForComponent(this), Messages.getText("choose_color"), color); |
280 |
|
281 |
if (newColor == null) { |
282 |
return;
|
283 |
} |
284 |
|
285 |
setColor(newColor); |
286 |
fireActionPerformed(); |
287 |
} |
288 |
|
289 |
/**
|
290 |
* Returns true if the check box of the color chooser panel is selected.
|
291 |
* False otherwise.
|
292 |
* @return boolean true if the check box is selected. Otherwise, false.
|
293 |
*/
|
294 |
public boolean getUseColorisSelected() { |
295 |
if(withNoFill)
|
296 |
return chkUseColor.isSelected();
|
297 |
return false; |
298 |
} |
299 |
/**
|
300 |
* Sets the check box of the color chooser panel
|
301 |
|
302 |
* @param b boolean true if the user wants to select it.Otherwise, false.
|
303 |
*/
|
304 |
public void setUseColorIsSelected(boolean b) { |
305 |
if(withNoFill)
|
306 |
chkUseColor.setSelected(b); |
307 |
} |
308 |
/**
|
309 |
* Controls the events when the check box of the color chooser panel
|
310 |
* is modified
|
311 |
*
|
312 |
* @param e Action Event
|
313 |
*/
|
314 |
private void useColor_actionPerformed(ActionEvent e) { |
315 |
if(chkUseColor.isSelected())
|
316 |
setColor(color); |
317 |
else setColor(null); |
318 |
fireActionPerformed(); |
319 |
} |
320 |
|
321 |
public void addActionListener(ActionListener l) { |
322 |
actionListeners.add(l); |
323 |
} |
324 |
|
325 |
public void removeActionListener(ActionListener l) { |
326 |
actionListeners.remove(l); |
327 |
} |
328 |
|
329 |
protected void fireActionPerformed() { |
330 |
for (Iterator<ActionListener> i = actionListeners.iterator(); i.hasNext();) { |
331 |
i.next().actionPerformed(new ActionEvent(this, 0, null)); |
332 |
} |
333 |
} |
334 |
/**
|
335 |
* Sets the color of the chooser panel.
|
336 |
*
|
337 |
* @param color Color to be selected.
|
338 |
*/
|
339 |
public void setColor(Color color) { |
340 |
|
341 |
if( color != null) { |
342 |
this.color = color;
|
343 |
setAlpha(color.getAlpha()); |
344 |
updateColorPanel(); |
345 |
} |
346 |
|
347 |
} |
348 |
|
349 |
/**
|
350 |
* Sets the alpha value of the color selected in the color chooser
|
351 |
* panel.
|
352 |
*
|
353 |
* @param alpha Alpha value of the color.
|
354 |
*/
|
355 |
public void setAlpha(int alpha) { |
356 |
muteSldTransparency = true;
|
357 |
|
358 |
if (color != null) |
359 |
color=alphaColor(color,alpha); |
360 |
|
361 |
if (withTransp) {
|
362 |
sldTransparency.setValue(alpha); |
363 |
sldTransparency.validate(); |
364 |
int percValue = (int) (alpha * perc); |
365 |
lblTransparency.setText( String.valueOf(percValue)+ "%"); |
366 |
} |
367 |
|
368 |
updateColorPanel(); |
369 |
muteSldTransparency = false;
|
370 |
// fireActionPerformed();
|
371 |
} |
372 |
|
373 |
public int getAlpha() { |
374 |
if (withTransp) {
|
375 |
return sldTransparency.getValue();
|
376 |
} else {
|
377 |
return color.getAlpha();
|
378 |
} |
379 |
|
380 |
} |
381 |
|
382 |
private void updateColorPanel() { |
383 |
colorPanel.setFillColor(color); |
384 |
colorPanel.repaint(); |
385 |
} |
386 |
|
387 |
|
388 |
/**
|
389 |
* Obtains the selected color in the color chooser panel.
|
390 |
*
|
391 |
* @return color the selected color
|
392 |
*/
|
393 |
public Color getColor() { |
394 |
return color;
|
395 |
} |
396 |
|
397 |
public void disabledWithTransparency() { |
398 |
changeButton.setEnabled(false);
|
399 |
if(withNoFill)
|
400 |
chkUseColor.setEnabled(false);
|
401 |
|
402 |
} |
403 |
|
404 |
@Override
|
405 |
public void setEnabled(boolean newEnabled) { |
406 |
|
407 |
changeButton.setEnabled(newEnabled); |
408 |
if(withTransp) {
|
409 |
sldTransparency.setEnabled(newEnabled); |
410 |
} |
411 |
if(withTranspPerc) {
|
412 |
lblTransparency.setEnabled(newEnabled); |
413 |
} |
414 |
if(withNoFill) {
|
415 |
chkUseColor.setEnabled(newEnabled); |
416 |
} |
417 |
|
418 |
} |
419 |
|
420 |
public static void main(String[] args) { |
421 |
JFrame f = new JFrame(); |
422 |
|
423 |
final ColorChooserPanel ce2 = new ColorChooserPanel(true,true); |
424 |
|
425 |
JPanel content = new JPanel(new GridLayout(2, 1)); |
426 |
|
427 |
content.add(ce2); |
428 |
|
429 |
|
430 |
f.setContentPane(content); |
431 |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
432 |
f.pack(); |
433 |
f.setVisible(true);
|
434 |
|
435 |
} |
436 |
|
437 |
private Color alphaColor(Color color, int alpha) { |
438 |
return new Color(color.getRed(), color.getGreen(), color.getBlue(), |
439 |
alpha); |
440 |
} |
441 |
|
442 |
} |