root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / colorchooser / ColorChooser.java @ 17327
History | View | Annotate | Download (6.52 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.gui.beans.colorchooser; |
20 |
|
21 |
import java.awt.Color; |
22 |
import java.awt.GridBagConstraints; |
23 |
import java.awt.GridBagLayout; |
24 |
import java.util.ArrayList; |
25 |
import java.util.EventObject; |
26 |
import java.util.Iterator; |
27 |
|
28 |
import javax.swing.JPanel; |
29 |
|
30 |
import org.gvsig.gui.beans.checkslidertext.CheckColorSliderTextContainer; |
31 |
import org.gvsig.gui.beans.doubleslider.DoubleSliderEvent; |
32 |
import org.gvsig.gui.beans.doubleslider.DoubleSliderListener; |
33 |
/**
|
34 |
*
|
35 |
* @version 03/08/2007
|
36 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
37 |
*/
|
38 |
public class ColorChooser extends JPanel implements DoubleSliderListener { |
39 |
private static final long serialVersionUID = 2397304969793748653L; |
40 |
|
41 |
private ArrayList<ColorChooserListener> actionCommandListeners = new ArrayList<ColorChooserListener>(); |
42 |
|
43 |
private CheckColorSliderTextContainer sliderRed = null; |
44 |
private CheckColorSliderTextContainer sliderGreen = null; |
45 |
private CheckColorSliderTextContainer sliderBlue = null; |
46 |
private CheckColorSliderTextContainer sliderAlpha = null; |
47 |
|
48 |
public ColorChooser() {
|
49 |
initialize(); |
50 |
} |
51 |
|
52 |
private void initialize() { |
53 |
setLayout(new GridBagLayout()); |
54 |
sliderRed = new CheckColorSliderTextContainer(0, 255, 0, "R", true); |
55 |
sliderGreen = new CheckColorSliderTextContainer(0, 255, 0, "G", true); |
56 |
sliderBlue = new CheckColorSliderTextContainer(0, 255, 0, "B", true); |
57 |
sliderAlpha = new CheckColorSliderTextContainer(0, 255, 255, "A", true); |
58 |
sliderRed.setCheckboxVisible(false);
|
59 |
sliderGreen.setCheckboxVisible(false);
|
60 |
sliderBlue.setCheckboxVisible(false);
|
61 |
sliderAlpha.setCheckboxVisible(false);
|
62 |
sliderRed.addValueChangedListener(this);
|
63 |
sliderGreen.addValueChangedListener(this);
|
64 |
sliderBlue.addValueChangedListener(this);
|
65 |
sliderAlpha.addValueChangedListener(this);
|
66 |
setColor(Color.black);
|
67 |
updateColors(); |
68 |
|
69 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
70 |
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
71 |
gridBagConstraints.weightx = 1.0;
|
72 |
add(sliderRed, gridBagConstraints); |
73 |
|
74 |
gridBagConstraints = new GridBagConstraints(); |
75 |
gridBagConstraints.gridx = 0;
|
76 |
gridBagConstraints.gridy = 1;
|
77 |
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
78 |
gridBagConstraints.weightx = 1.0;
|
79 |
add(sliderGreen, gridBagConstraints); |
80 |
|
81 |
gridBagConstraints = new GridBagConstraints(); |
82 |
gridBagConstraints.gridx = 0;
|
83 |
gridBagConstraints.gridy = 2;
|
84 |
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
85 |
gridBagConstraints.weightx = 1.0;
|
86 |
add(sliderBlue, gridBagConstraints); |
87 |
|
88 |
gridBagConstraints = new GridBagConstraints(); |
89 |
gridBagConstraints.gridx = 0;
|
90 |
gridBagConstraints.gridy = 3;
|
91 |
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
92 |
gridBagConstraints.weightx = 1.0;
|
93 |
add(sliderAlpha, gridBagConstraints); |
94 |
} |
95 |
|
96 |
public void actionValueChanged(DoubleSliderEvent e) { |
97 |
updateColors(); |
98 |
callChangeValue(); |
99 |
} |
100 |
|
101 |
private void updateColors() { |
102 |
sliderRed.setColor1(new Color(0, sliderGreen.getValue(), sliderBlue.getValue(), sliderAlpha.getValue()), false); |
103 |
sliderRed.setColor2(new Color(255, sliderGreen.getValue(), sliderBlue.getValue(), sliderAlpha.getValue()), true); |
104 |
sliderGreen.setColor1(new Color(sliderRed.getValue(), 0, sliderBlue.getValue(), sliderAlpha.getValue()), false); |
105 |
sliderGreen.setColor2(new Color(sliderRed.getValue(), 255, sliderBlue.getValue(), sliderAlpha.getValue()), true); |
106 |
sliderBlue.setColor1(new Color(sliderRed.getValue(), sliderGreen.getValue(), 0, sliderAlpha.getValue()), false); |
107 |
sliderBlue.setColor2(new Color(sliderRed.getValue(), sliderGreen.getValue(), 255, sliderAlpha.getValue()), true); |
108 |
sliderAlpha.setColor1(new Color(sliderRed.getValue(), sliderGreen.getValue(), sliderBlue.getValue(), 0), false); |
109 |
sliderAlpha.setColor2(new Color(sliderRed.getValue(), sliderGreen.getValue(), sliderBlue.getValue(), 255), true); |
110 |
} |
111 |
|
112 |
/*
|
113 |
* (non-Javadoc)
|
114 |
* @see org.gvsig.gui.beans.doubleslider.DoubleSliderListener#actionValueDragged(org.gvsig.gui.beans.doubleslider.DoubleSliderEvent)
|
115 |
*/
|
116 |
public void actionValueDragged(DoubleSliderEvent e) { |
117 |
updateColors(); |
118 |
callDraggedValue(); |
119 |
} |
120 |
|
121 |
public Color getColor() { |
122 |
return new Color(sliderRed.getValue(), sliderGreen.getValue(), sliderBlue.getValue(), sliderAlpha.getValue()); |
123 |
} |
124 |
|
125 |
public void setColor(Color value) { |
126 |
sliderRed.setValue(value.getRed()); |
127 |
sliderGreen.setValue(value.getGreen()); |
128 |
sliderBlue.setValue(value.getBlue()); |
129 |
sliderAlpha.setValue(value.getAlpha()); |
130 |
updateColors(); |
131 |
} |
132 |
|
133 |
/**
|
134 |
* A?adir un listener a la lista de eventos
|
135 |
* @param listener
|
136 |
*/
|
137 |
public void addValueChangedListener(ColorChooserListener listener) { |
138 |
if (!actionCommandListeners.contains(listener))
|
139 |
actionCommandListeners.add(listener); |
140 |
} |
141 |
|
142 |
/* (non-Javadoc)
|
143 |
* @see javax.swing.JComponent#setEnabled(boolean)
|
144 |
*/
|
145 |
public void setEnabled(boolean enabled) { |
146 |
super.setEnabled(enabled);
|
147 |
sliderRed.setEnabled(enabled); |
148 |
sliderGreen.setEnabled(enabled); |
149 |
sliderBlue.setEnabled(enabled); |
150 |
sliderAlpha.setEnabled(enabled); |
151 |
} |
152 |
|
153 |
/**
|
154 |
* Borrar un listener de la lista de eventos
|
155 |
* @param listener
|
156 |
*/
|
157 |
public void removeValueChangedListener(ColorChooserListener listener) { |
158 |
actionCommandListeners.remove(listener); |
159 |
} |
160 |
|
161 |
|
162 |
/**
|
163 |
* Dispara el evento del cambio del control
|
164 |
*/
|
165 |
protected void callChangeValue() { |
166 |
Iterator<ColorChooserListener> acIterator = actionCommandListeners.iterator();
|
167 |
while (acIterator.hasNext()) {
|
168 |
ColorChooserListener listener = acIterator.next(); |
169 |
listener.actionValueChanged(new EventObject(this)); |
170 |
} |
171 |
} |
172 |
|
173 |
/**
|
174 |
* Dispara el evento del cambio del control
|
175 |
*/
|
176 |
protected void callDraggedValue() { |
177 |
Iterator<ColorChooserListener> acIterator = actionCommandListeners.iterator();
|
178 |
while (acIterator.hasNext()) {
|
179 |
ColorChooserListener listener = acIterator.next(); |
180 |
listener.actionValueDragged(new EventObject(this)); |
181 |
} |
182 |
} |
183 |
} |