svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / slidertext / ColorSliderTextContainer.java @ 40561
History | View | Annotate | Download (8.5 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 |
package org.gvsig.gui.beans.slidertext; |
25 |
|
26 |
import java.awt.BorderLayout; |
27 |
import java.awt.Color; |
28 |
import java.awt.Dimension; |
29 |
import java.awt.GridBagConstraints; |
30 |
import java.awt.GridBagLayout; |
31 |
import java.util.ArrayList; |
32 |
import java.util.Iterator; |
33 |
|
34 |
import javax.swing.JPanel; |
35 |
import javax.swing.JSpinner; |
36 |
import javax.swing.event.ChangeEvent; |
37 |
import javax.swing.event.ChangeListener; |
38 |
|
39 |
import org.gvsig.gui.beans.doubleslider.DoubleSlider; |
40 |
import org.gvsig.gui.beans.doubleslider.DoubleSliderEvent; |
41 |
import org.gvsig.gui.beans.doubleslider.DoubleSliderListener; |
42 |
/**
|
43 |
* Barra de deslizamiento con una ventana de texto que tiene el valor de la
|
44 |
* posici?n de la barra. En este control podr? controlarse mediante la entrada
|
45 |
* de datos por la caja de texto la posibilidad de introducir valores decimales.
|
46 |
*
|
47 |
* @version 15/06/2007
|
48 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
49 |
*/
|
50 |
public class ColorSliderTextContainer extends JPanel implements ChangeListener, DoubleSliderListener { |
51 |
private static final long serialVersionUID = 1876415954410511634L; |
52 |
private ArrayList<DoubleSliderListener> actionCommandListeners = new ArrayList<DoubleSliderListener>(); |
53 |
|
54 |
private JPanel pText = null; |
55 |
private DoubleSlider slider = null; |
56 |
private JSpinner jSpinner = null; |
57 |
private int min = 0; |
58 |
private int max = 255; |
59 |
private int defaultPos = 0; |
60 |
|
61 |
/**
|
62 |
* Contructor
|
63 |
* @param min Valor m?nimo de la barra
|
64 |
* @param max Valor m?ximo de la barra
|
65 |
* @param defaultPos Posici?n por defecto
|
66 |
*/
|
67 |
public ColorSliderTextContainer(int min, int max, int defaultPos) { |
68 |
super();
|
69 |
this.min = min;
|
70 |
this.max = max;
|
71 |
this.defaultPos = defaultPos;
|
72 |
|
73 |
initialize(); |
74 |
slider.setTwoSliders(false);
|
75 |
} |
76 |
|
77 |
/**
|
78 |
* Constructor vacio
|
79 |
*/
|
80 |
public ColorSliderTextContainer() {
|
81 |
this(0, 100, 0); |
82 |
} |
83 |
|
84 |
/**
|
85 |
* This method initializes this
|
86 |
*
|
87 |
*/
|
88 |
private void initialize() { |
89 |
this.setLayout(new BorderLayout(5, 5)); |
90 |
this.add(getSlider(), BorderLayout.CENTER); |
91 |
this.add(getPText(), BorderLayout.EAST); |
92 |
} |
93 |
|
94 |
/**
|
95 |
* This method initializes jPanel1
|
96 |
*
|
97 |
* @return javax.swing.JPanel
|
98 |
*/
|
99 |
private JPanel getPText() { |
100 |
if (pText == null) { |
101 |
pText = new JPanel(); |
102 |
GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
103 |
gridBagConstraints1.insets = new java.awt.Insets(0, 10, 8, 0); |
104 |
pText.setLayout(new GridBagLayout()); |
105 |
pText.add(getJSpinner(), gridBagConstraints1); |
106 |
} |
107 |
return pText;
|
108 |
} |
109 |
|
110 |
/**
|
111 |
* This method initializes jSlider
|
112 |
*
|
113 |
* @return javax.swing.JSlider
|
114 |
*/
|
115 |
public DoubleSlider getSlider() {
|
116 |
if (slider == null) { |
117 |
slider = new DoubleSlider();
|
118 |
slider.setMinimum(min); |
119 |
slider.setMaximum(max); |
120 |
slider.setValue(defaultPos); |
121 |
slider.addValueChangedListener(this);
|
122 |
} |
123 |
return slider;
|
124 |
} |
125 |
|
126 |
/**
|
127 |
* This method initializes jTextField
|
128 |
*
|
129 |
* @return javax.swing.JTextField
|
130 |
*/
|
131 |
public JSpinner getJSpinner() { |
132 |
if (jSpinner == null) { |
133 |
jSpinner = new JSpinner(); |
134 |
jSpinner.setValue(new Integer(defaultPos)); |
135 |
jSpinner.setPreferredSize(new Dimension(50, 26)); |
136 |
jSpinner.setMinimumSize(new Dimension(50, 26)); |
137 |
jSpinner.addChangeListener(this);
|
138 |
} |
139 |
return jSpinner;
|
140 |
} |
141 |
|
142 |
public void setComponentSize(int w, int h){ |
143 |
} |
144 |
|
145 |
/**
|
146 |
* Obtiene el valor del control.
|
147 |
* @return Valor del control en formato double.
|
148 |
*/
|
149 |
public int getValue() { |
150 |
return new Integer(getJSpinner().getValue() + "").intValue(); |
151 |
} |
152 |
|
153 |
/**
|
154 |
* Asigna el valor del control.
|
155 |
* @return Valor del control en formato double.
|
156 |
*/
|
157 |
public void setValue(int value) { |
158 |
getJSpinner().setValue(new Integer(value)); |
159 |
getSlider().setValue(value); |
160 |
} |
161 |
|
162 |
/**
|
163 |
* Activa o desactiva el control del panel
|
164 |
* @param active
|
165 |
*/
|
166 |
public void setControlEnabled(boolean active){ |
167 |
getSlider().setEnabled(active); |
168 |
getJSpinner().setEnabled(active); |
169 |
} |
170 |
|
171 |
/**
|
172 |
* Obtiene el valor m?ximo del slider
|
173 |
* @return Entero con el valor m?ximo
|
174 |
*/
|
175 |
public int getMax() { |
176 |
return max;
|
177 |
} |
178 |
|
179 |
/**
|
180 |
* Asigna el valor m?ximo del slider
|
181 |
* @param Entero con el valor m?ximo
|
182 |
* @deprecated Usar setMaximum en su lugar
|
183 |
*/
|
184 |
public void setMax(int max) { |
185 |
this.setMaximum(max);
|
186 |
} |
187 |
|
188 |
/**
|
189 |
* Asigna el valor m?ximo del slider
|
190 |
* @param Entero con el valor m?ximo
|
191 |
*/
|
192 |
public void setMaximum(int max) { |
193 |
this.max = max;
|
194 |
updateInterval(); |
195 |
} |
196 |
|
197 |
/**
|
198 |
* Obtiene el valor m?nimo del slider
|
199 |
* @return Entero con el valor m?nimo
|
200 |
*/
|
201 |
public int getMin() { |
202 |
return min;
|
203 |
} |
204 |
|
205 |
/**
|
206 |
* Asigna el valor m?nimo del slider
|
207 |
* @param Entero con el valor m?nimo
|
208 |
* @deprecated Usar setMinimum
|
209 |
*/
|
210 |
public void setMin(int min) { |
211 |
this.setMinimum(min);
|
212 |
} |
213 |
|
214 |
/**
|
215 |
* Asigna el valor m?nimo del slider
|
216 |
* @param Entero con el valor m?nimo
|
217 |
*/
|
218 |
public void setMinimum(int min) { |
219 |
this.min = min;
|
220 |
updateInterval(); |
221 |
} |
222 |
|
223 |
private void updateInterval() { |
224 |
int aux = this.getValue(); |
225 |
getSlider().setMinimum(min); |
226 |
getSlider().setMaximum(max); |
227 |
setValue(aux); |
228 |
} |
229 |
|
230 |
/**
|
231 |
* Especificar el color izquierdo del control
|
232 |
* @param color
|
233 |
*/
|
234 |
public void setColor1(Color color, boolean refresh) { |
235 |
slider.setColor1(color, refresh); |
236 |
} |
237 |
|
238 |
/**
|
239 |
* Especificar el color derecho del control
|
240 |
* @param color
|
241 |
*/
|
242 |
public void setColor2(Color color, boolean refresh) { |
243 |
slider.setColor2(color, refresh); |
244 |
} |
245 |
|
246 |
/**
|
247 |
* Controla cuando cambia el spinner
|
248 |
*/
|
249 |
public void stateChanged(ChangeEvent e) { |
250 |
int value = new Integer(getJSpinner().getValue().toString()).intValue(); |
251 |
if (value != getSlider().getValue())
|
252 |
getSlider().setValue(value); |
253 |
if (new Integer(getJSpinner().getValue().toString()).intValue() != getSlider().getValue()) |
254 |
getJSpinner().setValue(new Integer(getSlider().getValue())); |
255 |
|
256 |
if (spinnerEvent)
|
257 |
callChangeValue(); |
258 |
} |
259 |
|
260 |
/**
|
261 |
* Dispara el evento del cambio del control
|
262 |
*/
|
263 |
protected void callChangeValue() { |
264 |
Iterator<DoubleSliderListener> iterator = actionCommandListeners.iterator();
|
265 |
while (iterator.hasNext()) {
|
266 |
DoubleSliderListener listener = iterator.next(); |
267 |
listener.actionValueChanged(new DoubleSliderEvent(this)); |
268 |
} |
269 |
} |
270 |
|
271 |
/**
|
272 |
* Dispara el evento del cambio del control
|
273 |
*/
|
274 |
protected void callDraggedValue() { |
275 |
Iterator<DoubleSliderListener> iterator = actionCommandListeners.iterator();
|
276 |
while (iterator.hasNext()) {
|
277 |
DoubleSliderListener listener = iterator.next(); |
278 |
listener.actionValueDragged(new DoubleSliderEvent(this)); |
279 |
} |
280 |
} |
281 |
|
282 |
/**
|
283 |
* A?adir un listener a la lista de eventos
|
284 |
* @param listener
|
285 |
*/
|
286 |
public void addValueChangedListener(DoubleSliderListener listener) { |
287 |
if (!actionCommandListeners.contains(listener))
|
288 |
actionCommandListeners.add(listener); |
289 |
} |
290 |
|
291 |
/**
|
292 |
* Borrar un listener de la lista de eventos
|
293 |
* @param listener
|
294 |
*/
|
295 |
public void removeValueChangedListener(DoubleSliderListener listener) { |
296 |
actionCommandListeners.remove(listener); |
297 |
} |
298 |
|
299 |
/* (non-Javadoc)
|
300 |
* @see javax.swing.JComponent#setEnabled(boolean)
|
301 |
*/
|
302 |
public void setEnabled(boolean enabled) { |
303 |
super.setEnabled(enabled);
|
304 |
jSpinner.setEnabled(enabled); |
305 |
slider.setEnabled(enabled); |
306 |
} |
307 |
|
308 |
/**
|
309 |
* Controla cuando cambia el slider
|
310 |
*/
|
311 |
public void actionValueChanged(DoubleSliderEvent e) { |
312 |
int value = getSlider().getValue();
|
313 |
getJSpinner().setValue(new Integer(value)); |
314 |
|
315 |
if (isEnabled())
|
316 |
callChangeValue(); |
317 |
} |
318 |
|
319 |
/*
|
320 |
* (non-Javadoc)
|
321 |
* @see org.gvsig.gui.beans.doubleslider.DoubleSliderListener#actionValueDragged(java.util.EventObject)
|
322 |
*/
|
323 |
boolean spinnerEvent = true; |
324 |
int lastDragged = -1; |
325 |
public void actionValueDragged(DoubleSliderEvent e) { |
326 |
int value = getSlider().getValue();
|
327 |
if (lastDragged != value) {
|
328 |
lastDragged = value; |
329 |
spinnerEvent = false;
|
330 |
getJSpinner().setValue(new Integer(value)); |
331 |
spinnerEvent = true;
|
332 |
|
333 |
if (isEnabled())
|
334 |
callDraggedValue(); |
335 |
} |
336 |
} |
337 |
} |