root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / colorslideredition / ColorSliderEdition.java @ 13022
History | View | Annotate | Download (18.7 KB)
1 | 12898 | bsanchez | /* 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.colorslideredition; |
||
20 | |||
21 | import java.awt.Color; |
||
22 | import java.awt.Cursor; |
||
23 | import java.awt.Dimension; |
||
24 | import java.awt.Graphics; |
||
25 | import java.awt.Image; |
||
26 | import java.awt.Polygon; |
||
27 | import java.awt.Shape; |
||
28 | import java.awt.event.MouseEvent; |
||
29 | import java.awt.event.MouseListener; |
||
30 | import java.awt.event.MouseMotionListener; |
||
31 | import java.util.ArrayList; |
||
32 | 12939 | bsanchez | import java.util.Iterator; |
33 | 12898 | bsanchez | |
34 | import javax.swing.JComponent; |
||
35 | /**
|
||
36 | * <code>DoubleSlider</code> representa un componente que tiene dos
|
||
37 | * deslizadores. Se puede definir un m?ximo y un m?nimo.
|
||
38 | *
|
||
39 | * @version 04/05/2007
|
||
40 | * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
||
41 | */
|
||
42 | public class ColorSliderEdition extends JComponent implements MouseListener, MouseMotionListener { |
||
43 | private static final long serialVersionUID = 663355422780987493L; |
||
44 | |||
45 | 12939 | bsanchez | private ArrayList actionCommandListeners = new ArrayList(); |
46 | private ArrayList actionSelectionListeners = new ArrayList(); |
||
47 | private final int LEFT_PAD = 3; |
||
48 | private final int RIGHT_PAD = 3; |
||
49 | 12898 | bsanchez | |
50 | 12939 | bsanchez | private Image bufferImage = null; |
51 | private int width = 0; |
||
52 | private int height = 0; |
||
53 | 12898 | bsanchez | private Graphics bufferGraphics; |
54 | 12939 | bsanchez | private ArrayList items = new ArrayList(); |
55 | private boolean interpolated = true; |
||
56 | 12898 | bsanchez | |
57 | /**
|
||
58 | * Crea un DoubleSlider con las opciones por defecto.
|
||
59 | */
|
||
60 | public ColorSliderEdition() {
|
||
61 | 12939 | bsanchez | this.setPreferredSize(new Dimension(100, 46)); |
62 | 12898 | bsanchez | addMouseListener(this);
|
63 | addMouseMotionListener(this);
|
||
64 | } |
||
65 | |||
66 | /*
|
||
67 | * (non-Javadoc)
|
||
68 | * @see javax.swing.JComponent#addNotify()
|
||
69 | */
|
||
70 | public void addNotify() { |
||
71 | super.addNotify();
|
||
72 | |||
73 | refreshImage(); |
||
74 | } |
||
75 | |||
76 | 12939 | bsanchez | public void addItem(ItemColorSlider value) { |
77 | addItem(value, true);
|
||
78 | } |
||
79 | 12898 | bsanchez | |
80 | 12939 | bsanchez | public void addItem(ItemColorSlider value, boolean repaint) { |
81 | items.add(value); |
||
82 | if (repaint)
|
||
83 | refreshImage(); |
||
84 | } |
||
85 | |||
86 | 12898 | bsanchez | /**
|
87 | * Devuelve un color de interpolacion entre dos colores
|
||
88 | * @param value
|
||
89 | * @param pos
|
||
90 | * @return
|
||
91 | */
|
||
92 | private Color interpolatedColor(ArrayList newItems, double value, int pos) { |
||
93 | if (newItems.size() <= 0) |
||
94 | return Color.black; |
||
95 | |||
96 | if ((pos + 1) == newItems.size()) |
||
97 | return ((ItemColorSlider) newItems.get(pos)).getColor();
|
||
98 | |||
99 | if (value <= ((ItemColorSlider) newItems.get(0)).getValue()) |
||
100 | return ((ItemColorSlider) newItems.get(0)).getColor(); |
||
101 | |||
102 | ItemColorSlider item1 = (ItemColorSlider) newItems.get(pos); |
||
103 | ItemColorSlider item2 = (ItemColorSlider) newItems.get(pos + 1);
|
||
104 | |||
105 | double percValue = ((value - item1.getValue()) * 100) / (item2.getValue() - item1.getValue()); |
||
106 | |||
107 | Color halfColor = new Color( |
||
108 | (item2.getColor().getRed() + item1.getColor().getRed()) >> 1,
|
||
109 | (item2.getColor().getGreen() + item1.getColor().getGreen()) >> 1,
|
||
110 | (item2.getColor().getBlue() + item1.getColor().getBlue()) >> 1,
|
||
111 | (item2.getColor().getAlpha() + item1.getColor().getAlpha()) >> 1);
|
||
112 | |||
113 | Color color1, color2;
|
||
114 | double perc1, perc2;
|
||
115 | |||
116 | if (percValue > item2.getInterpolated()) {
|
||
117 | color1 = halfColor; |
||
118 | color2 = item2.getColor(); |
||
119 | perc1 = item2.getInterpolated(); |
||
120 | perc2 = 100;
|
||
121 | } else {
|
||
122 | color1 = item1.getColor(); |
||
123 | color2 = halfColor; |
||
124 | perc1 = 0;
|
||
125 | perc2 = item2.getInterpolated(); |
||
126 | } |
||
127 | |||
128 | double percNew = (percValue - perc1) / (perc2 - perc1);
|
||
129 | |||
130 | Color newColor = new Color( |
||
131 | (int) (color1.getRed() + ((color2.getRed() - color1.getRed()) * percNew)) & 0xff, |
||
132 | (int) (color1.getGreen() + ((color2.getGreen() - color1.getGreen()) * percNew)) & 0xff, |
||
133 | (int) (color1.getBlue() + ((color2.getBlue() - color1.getBlue()) * percNew)) & 0xff, |
||
134 | (int) (color1.getAlpha() + ((color2.getAlpha() - color1.getAlpha()) * percNew)) & 0xff); |
||
135 | |||
136 | |||
137 | return newColor;
|
||
138 | } |
||
139 | |||
140 | /**
|
||
141 | * Crea un graphics con las dimensiones del componente si no estaba creado y
|
||
142 | * lo devuelve para poder usarlo para dibujar en ?l.
|
||
143 | * @return Graphics
|
||
144 | */
|
||
145 | private Graphics getBufferGraphics() { |
||
146 | int width2 = getBounds().width;
|
||
147 | int height2 = getBounds().height;
|
||
148 | if (width2 <= 0) |
||
149 | width2 = 1;
|
||
150 | if (height2 <= 0) |
||
151 | height2 = 1;
|
||
152 | |||
153 | 13022 | bsanchez | if ((width != width2) || (height != height2)) {
|
154 | 12898 | bsanchez | bufferImage = createImage(width2, height2); |
155 | 13022 | bsanchez | if (bufferImage == null) |
156 | return null; |
||
157 | 12898 | bsanchez | bufferGraphics = bufferImage.getGraphics(); |
158 | } |
||
159 | |||
160 | width = width2; |
||
161 | height = height2; |
||
162 | |||
163 | return bufferGraphics;
|
||
164 | } |
||
165 | |||
166 | private void sortItems() { |
||
167 | for (int i = 0; i < items.size(); i++) { |
||
168 | for (int j = i + 1; j < items.size(); j++) { |
||
169 | if (((ItemColorSlider) items.get(j)).getValue() < ((ItemColorSlider) items.get(i)).getValue()) {
|
||
170 | Object aux = items.get(i);
|
||
171 | items.set(i, items.get(j)); |
||
172 | items.set(j, aux); |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | |||
178 | private int getPosForValue(double value, ArrayList newArray) { |
||
179 | int pos = 0; |
||
180 | for (int j = 1; j <= newArray.size(); j++) { |
||
181 | if (j < newArray.size()) {
|
||
182 | if (value < ((ItemColorSlider) newArray.get(j)).getValue()) {
|
||
183 | pos = j - 1;
|
||
184 | break;
|
||
185 | } |
||
186 | } else {
|
||
187 | pos = j - 1;
|
||
188 | break;
|
||
189 | } |
||
190 | } |
||
191 | return pos;
|
||
192 | } |
||
193 | |||
194 | /**
|
||
195 | * Redibujar el componente en el graphics temporal
|
||
196 | */
|
||
197 | private void redrawBuffer() { |
||
198 | 13022 | bsanchez | if (getBufferGraphics() == null) |
199 | return;
|
||
200 | 12898 | bsanchez | |
201 | sortItems(); |
||
202 | |||
203 | getBufferGraphics().setColor(this.getBackground());
|
||
204 | |||
205 | 13022 | bsanchez | getBufferGraphics().fillRect(0, 0, width, height); |
206 | 12898 | bsanchez | |
207 | getBufferGraphics().setColor(Color.black);
|
||
208 | getBufferGraphics().drawRect(LEFT_PAD, 0, width - 1 - LEFT_PAD - RIGHT_PAD, height - 18); |
||
209 | |||
210 | Shape oldClip = getBufferGraphics().getClip();
|
||
211 | getBufferGraphics().setClip(LEFT_PAD + 2, 2, width - 4 - LEFT_PAD - RIGHT_PAD, height - 21); |
||
212 | for (int i = 0; (i * 4 + 2) <= (width - 3 - LEFT_PAD - RIGHT_PAD); i++) { |
||
213 | for (int j = 0; (j * 4 + 2) <= (height - 20); j++) { |
||
214 | if ((i + j) % 2 == 0) |
||
215 | getBufferGraphics().setColor(Color.white);
|
||
216 | else
|
||
217 | getBufferGraphics().setColor(new Color(204, 204, 204)); |
||
218 | getBufferGraphics().fillRect(i * 4 + 2 + LEFT_PAD, j * 4 + 2, 4, 4); |
||
219 | } |
||
220 | } |
||
221 | 13022 | bsanchez | Color newColor = Color.black; |
222 | 12898 | bsanchez | |
223 | 13022 | bsanchez | ArrayList newArray = getItemsShowed();
|
224 | |||
225 | 12898 | bsanchez | for (int i = LEFT_PAD + 2; i <= width - 2 - RIGHT_PAD; i++) { |
226 | int pos = getPosForValue(pixelToValue(i), newArray);
|
||
227 | |||
228 | if (isInterpolated()) {
|
||
229 | 13022 | bsanchez | newColor = interpolatedColor(newArray, pixelToValue(i), pos); |
230 | 12898 | bsanchez | } else {
|
231 | if ((pos + 1) < newArray.size()) { |
||
232 | double min = ((ItemColorSlider) newArray.get(pos)).getValue();
|
||
233 | double max = ((ItemColorSlider) newArray.get(pos + 1)).getValue(); |
||
234 | if ((min + ((max - min) * ((ItemColorSlider) newArray.get(pos + 1)).getInterpolated() / 100)) < pixelToValue(i)) |
||
235 | pos++; |
||
236 | } |
||
237 | 13022 | bsanchez | if (pos < newArray.size())
|
238 | newColor = ((ItemColorSlider) newArray.get(pos)).getColor(); |
||
239 | 12898 | bsanchez | } |
240 | 13022 | bsanchez | if (newColor != null) { |
241 | getBufferGraphics().setColor(newColor); |
||
242 | getBufferGraphics().drawLine(i, 2, i, height - 18); |
||
243 | } |
||
244 | 12898 | bsanchez | } |
245 | |||
246 | getBufferGraphics().setClip(oldClip); |
||
247 | |||
248 | boolean paintNext = false; |
||
249 | for (int i = 0; i < items.size(); i++) { |
||
250 | ItemColorSlider aux = (ItemColorSlider) items.get(i); |
||
251 | if (!aux.isVisible())
|
||
252 | continue;
|
||
253 | // Dibujar los deslizadores de la interpolacion
|
||
254 | if (aux.getSelected() != -1) |
||
255 | paintNext = true;
|
||
256 | if (paintNext) {
|
||
257 | if (i != 0) { |
||
258 | double value = ((ItemColorSlider) items.get(i - 1)).getValue() + ((((ItemColorSlider) items.get(i)).getValue() - ((ItemColorSlider) items.get(i - 1)).getValue()) * ((ItemColorSlider) items.get(i)).getInterpolated() / 100); |
||
259 | |||
260 | drawSliderInterpolation(valueToPixel(value), height - 17, aux.getColor(), (aux.getSelected() == 2)); |
||
261 | } |
||
262 | paintNext = false;
|
||
263 | } |
||
264 | if (aux.getSelected() == 1) |
||
265 | paintNext = true;
|
||
266 | } |
||
267 | |||
268 | 13022 | bsanchez | for (int i = items.size() - 1; i >= 0; i--) { |
269 | 12898 | bsanchez | ItemColorSlider aux = (ItemColorSlider) items.get(i); |
270 | if (!aux.isVisible())
|
||
271 | continue;
|
||
272 | drawSliderColor(valueToPixel(aux.getValue()), height - 17, aux.getColor(), (aux.getSelected() == 1)); |
||
273 | } |
||
274 | } |
||
275 | |||
276 | /**
|
||
277 | * Convierte un porcentaje al valor pixel en X
|
||
278 | * @param value
|
||
279 | * @return
|
||
280 | */
|
||
281 | private int valueToPixel(double value) { |
||
282 | if (value < 0) |
||
283 | value = 0;
|
||
284 | if (value > 100) |
||
285 | value = 100;
|
||
286 | return (int) (((width - 5 - LEFT_PAD - RIGHT_PAD) * value) / 100) + 2 + LEFT_PAD; |
||
287 | } |
||
288 | |||
289 | /**
|
||
290 | * Convierte un valor en X en porcentaje
|
||
291 | * @param value
|
||
292 | * @return
|
||
293 | */
|
||
294 | private double pixelToValue(int value) { |
||
295 | double aux = value - LEFT_PAD - 1; |
||
296 | return (aux * 100.0f) / (width - LEFT_PAD - RIGHT_PAD - 4.0f); |
||
297 | } |
||
298 | |||
299 | /**
|
||
300 | * Dibujar un triangulo, un triangulo es un deslizador del componente. Puedes
|
||
301 | * indicarle que color tendra y en que posici?n estar?.
|
||
302 | * @param x
|
||
303 | * @param color
|
||
304 | */
|
||
305 | private void drawSliderColor(int x, int y, Color color, boolean isSelected) { |
||
306 | 13022 | bsanchez | if (color == null) |
307 | return;
|
||
308 | 12898 | bsanchez | Polygon p = new Polygon(); |
309 | p.addPoint(x, y); |
||
310 | p.addPoint(x - 5, y + 6); |
||
311 | p.addPoint(x - 5, y + 16); |
||
312 | p.addPoint(x + 5, y + 16); |
||
313 | p.addPoint(x + 5, y + 6); |
||
314 | if (!isSelected) {
|
||
315 | getBufferGraphics().setColor(this.getBackground());
|
||
316 | getBufferGraphics().fillPolygon(p); |
||
317 | getBufferGraphics().setColor(new Color(172, 168, 153)); |
||
318 | getBufferGraphics().drawLine(x + 1, y + 2, x + 4, y + 6); |
||
319 | getBufferGraphics().drawLine(x - 4, y + 6, x + 4, y + 6); |
||
320 | } |
||
321 | else {
|
||
322 | getBufferGraphics().setColor(Color.black);
|
||
323 | getBufferGraphics().fillPolygon(p); |
||
324 | } |
||
325 | |||
326 | getBufferGraphics().setColor(Color.black);
|
||
327 | getBufferGraphics().drawPolygon(p); |
||
328 | |||
329 | getBufferGraphics().setColor(new Color(172, 168, 153)); |
||
330 | getBufferGraphics().drawLine(x - 3, y + 15, x + 4, y + 15); |
||
331 | getBufferGraphics().drawLine(x + 4, y + 8, x + 4, y + 15); |
||
332 | |||
333 | getBufferGraphics().setColor(Color.white);
|
||
334 | getBufferGraphics().drawLine(x - 4, y + 7, x - 4, y + 15); |
||
335 | getBufferGraphics().drawLine(x - 4, y + 7, x + 4, y + 7); |
||
336 | |||
337 | getBufferGraphics().setColor(new Color(color.getRed(), color.getGreen(), color.getBlue())); |
||
338 | getBufferGraphics().fillRect(x - 3, y + 8, 7, 7); |
||
339 | } |
||
340 | |||
341 | /**
|
||
342 | * Dibujar un triangulo, un triangulo es un deslizador del componente. Puedes
|
||
343 | * indicarle que color tendra y en que posici?n estar?.
|
||
344 | * @param x
|
||
345 | * @param color
|
||
346 | */
|
||
347 | private void drawSliderInterpolation(int x, int y, Color color, boolean isSelected) { |
||
348 | Polygon p = new Polygon(); |
||
349 | p.addPoint(x, y); |
||
350 | p.addPoint(x - 3, y + 3); |
||
351 | p.addPoint(x, y + 6);
|
||
352 | p.addPoint(x + 3, y + 3); |
||
353 | if (isSelected)
|
||
354 | getBufferGraphics().setColor(new Color(color.getRed(), color.getGreen(), color.getBlue())); |
||
355 | else
|
||
356 | getBufferGraphics().setColor(this.getBackground());
|
||
357 | |||
358 | getBufferGraphics().fillPolygon(p); |
||
359 | |||
360 | getBufferGraphics().setColor(Color.black);
|
||
361 | getBufferGraphics().drawPolygon(p); |
||
362 | } |
||
363 | |||
364 | /**
|
||
365 | * Redibujar el componente en el graphics temporal y representarlo en el
|
||
366 | * componente
|
||
367 | */
|
||
368 | private void refreshImage() { |
||
369 | redrawBuffer(); |
||
370 | if (bufferImage != null) |
||
371 | getGraphics().drawImage(bufferImage, 0, 0, this); |
||
372 | super.paint(getGraphics());
|
||
373 | } |
||
374 | |||
375 | /*
|
||
376 | * (non-Javadoc)
|
||
377 | * @see javax.swing.JComponent#paint(java.awt.Graphics)
|
||
378 | */
|
||
379 | public void paint(Graphics g) { |
||
380 | redrawBuffer(); |
||
381 | g.drawImage(bufferImage, 0, 0, this); |
||
382 | super.paint(g);
|
||
383 | } |
||
384 | |||
385 | /*
|
||
386 | * (non-Javadoc)
|
||
387 | * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
||
388 | */
|
||
389 | public void mousePressed(MouseEvent e) { |
||
390 | int onmask = MouseEvent.BUTTON1_DOWN_MASK; |
||
391 | if ((e.getModifiersEx() & onmask) != onmask)
|
||
392 | return;
|
||
393 | onmask = MouseEvent.ALT_DOWN_MASK;
|
||
394 | boolean newItem = false; |
||
395 | if ((e.getModifiersEx() & onmask) == onmask)
|
||
396 | newItem = true;
|
||
397 | |||
398 | if ((e.getY() > height) || (e.getY() < (height - 18))) { |
||
399 | clearSelected(); |
||
400 | refreshImage(); |
||
401 | return;
|
||
402 | } |
||
403 | |||
404 | int type = 1; |
||
405 | ItemColorSlider itemSelected = getItem(e.getX(), e.getY()); |
||
406 | if ((itemSelected == null) || (newItem)) { |
||
407 | itemSelected = getItemInterpolated(e.getX(), e.getY()); |
||
408 | if ((itemSelected == null) || (newItem)) { |
||
409 | int pos = getPosForValue(pixelToValue(e.getX()), items);
|
||
410 | |||
411 | Color newColor = interpolatedColor(items, pixelToValue(e.getX()), pos);
|
||
412 | |||
413 | itemSelected = new ItemColorSlider(pixelToValue(e.getX()), newColor);
|
||
414 | items.add(itemSelected); |
||
415 | } else {
|
||
416 | type = 2;
|
||
417 | } |
||
418 | } |
||
419 | setItemSelected(itemSelected, type); |
||
420 | |||
421 | 12939 | bsanchez | callSelectionChangedListeners(); |
422 | callValueChangedListeners(); |
||
423 | |||
424 | 12898 | bsanchez | refreshImage(); |
425 | } |
||
426 | |||
427 | private void clearSelected() { |
||
428 | for (int i = items.size() - 1; i >= 0; i--) |
||
429 | ((ItemColorSlider) items.get(i)).setSelected(-1);
|
||
430 | } |
||
431 | |||
432 | private void setItemSelected(ItemColorSlider aux, int type) { |
||
433 | clearSelected(); |
||
434 | aux.setSelected(type); |
||
435 | } |
||
436 | |||
437 | /*
|
||
438 | * (non-Javadoc)
|
||
439 | * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
||
440 | */
|
||
441 | public void mouseReleased(MouseEvent e) { |
||
442 | 13022 | bsanchez | try {
|
443 | for (int i = items.size() - 1; i >= 0; i--) { |
||
444 | if (items.size() <= 2) |
||
445 | return;
|
||
446 | if (((ItemColorSlider) items.get(i)).isVisible() == false) |
||
447 | items.remove(i); |
||
448 | } |
||
449 | } finally {
|
||
450 | callValueChangedListeners(); |
||
451 | 12898 | bsanchez | } |
452 | } |
||
453 | |||
454 | public ItemColorSlider getSelectedItem() {
|
||
455 | for (int i = 0; i < items.size(); i++) { |
||
456 | if (((ItemColorSlider) items.get(i)).getSelected() != -1) |
||
457 | return (ItemColorSlider) items.get(i);
|
||
458 | } |
||
459 | return null; |
||
460 | } |
||
461 | |||
462 | /*
|
||
463 | * (non-Javadoc)
|
||
464 | * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
|
||
465 | */
|
||
466 | public void mouseDragged(MouseEvent e) { |
||
467 | ItemColorSlider aux = getSelectedItem(); |
||
468 | if (aux != null) { |
||
469 | if (aux.getSelected() == 1) { |
||
470 | aux.setValue(pixelToValue(e.getX())); |
||
471 | refreshImage(); |
||
472 | setMouseCursor(e.getX(), e.getY()); |
||
473 | aux.setVisible((e.getY() <= height) && (e.getY() >= 0) || items.size() <= 2); |
||
474 | 12939 | bsanchez | callValueDraggedListeners(); |
475 | 12898 | bsanchez | return;
|
476 | } |
||
477 | if (aux.getSelected() == 2) { |
||
478 | int pos = -1; |
||
479 | for (int i = 0; i < items.size(); i++) { |
||
480 | if (items.get(i) == aux)
|
||
481 | pos = i; |
||
482 | } |
||
483 | if (pos > 0) { |
||
484 | double min = ((ItemColorSlider) items.get(pos - 1)).getValue(); |
||
485 | double max = ((ItemColorSlider) items.get(pos)).getValue();
|
||
486 | |||
487 | double newValue = 100 * (pixelToValue(e.getX()) - min) / (max - min); |
||
488 | |||
489 | aux.setInterpolated(newValue); |
||
490 | refreshImage(); |
||
491 | setMouseCursor(e.getX(), e.getY()); |
||
492 | 12939 | bsanchez | callValueDraggedListeners(); |
493 | 12898 | bsanchez | } |
494 | return;
|
||
495 | } |
||
496 | } |
||
497 | } |
||
498 | |||
499 | private ItemColorSlider getItem(int x, int y) { |
||
500 | for (int i = 0; i < items.size(); i++) { |
||
501 | if ((x >= (valueToPixel(((ItemColorSlider) items.get(i)).getValue()) - 5)) && |
||
502 | (x <= (valueToPixel(((ItemColorSlider) items.get(i)).getValue()) + 5)) &&
|
||
503 | (y <= height) && |
||
504 | (y >= (height - 18))) {
|
||
505 | return (ItemColorSlider) items.get(i);
|
||
506 | } |
||
507 | } |
||
508 | return null; |
||
509 | } |
||
510 | |||
511 | private ItemColorSlider getItemInterpolated(int x, int y) { |
||
512 | for (int i = 1; i < items.size(); i++) { |
||
513 | int value = valueToPixel(((ItemColorSlider) items.get(i - 1)).getValue() + ((((ItemColorSlider) items.get(i)).getValue() - ((ItemColorSlider) items.get(i - 1)).getValue()) * ((ItemColorSlider) items.get(i)).getInterpolated() / 100)); |
||
514 | |||
515 | if ((x >= value - 3) && |
||
516 | (x <= value + 3) &&
|
||
517 | (y <= (height - 11)) &&
|
||
518 | (y >= (height - 18))) {
|
||
519 | if (((ItemColorSlider) items.get(i)).getSelected() != -1) |
||
520 | return (ItemColorSlider) items.get(i);
|
||
521 | if (((ItemColorSlider) items.get(i - 1)).getSelected() == 1) |
||
522 | return (ItemColorSlider) items.get(i);
|
||
523 | } |
||
524 | } |
||
525 | return null; |
||
526 | } |
||
527 | |||
528 | private void setMouseCursor(int x, int y) { |
||
529 | if (getItem(x, y) != null) { |
||
530 | setCursor(new Cursor(Cursor.E_RESIZE_CURSOR)); |
||
531 | return;
|
||
532 | } |
||
533 | if (getItemInterpolated(x, y) != null) { |
||
534 | setCursor(new Cursor(Cursor.E_RESIZE_CURSOR)); |
||
535 | return;
|
||
536 | } |
||
537 | if ((y <= height) && (y >= (height - 18))) { |
||
538 | setCursor(new Cursor(Cursor.HAND_CURSOR)); |
||
539 | return;
|
||
540 | } |
||
541 | setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); |
||
542 | return;
|
||
543 | } |
||
544 | |||
545 | /*
|
||
546 | * (non-Javadoc)
|
||
547 | * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
||
548 | */
|
||
549 | public void mouseMoved(MouseEvent e) { |
||
550 | setMouseCursor(e.getX(), e.getY()); |
||
551 | } |
||
552 | |||
553 | /**
|
||
554 | * @return the interpolated
|
||
555 | */
|
||
556 | public boolean isInterpolated() { |
||
557 | return interpolated;
|
||
558 | } |
||
559 | |||
560 | /**
|
||
561 | * @param interpolated the interpolated to set
|
||
562 | */
|
||
563 | public void setInterpolated(boolean interpolated) { |
||
564 | this.interpolated = interpolated;
|
||
565 | refreshImage(); |
||
566 | } |
||
567 | |||
568 | 12939 | bsanchez | /**
|
569 | * A?adir un listener a la lista de eventos
|
||
570 | * @param listener
|
||
571 | */
|
||
572 | public void addValueChangedListener(ColorSliderListener listener) { |
||
573 | if (!actionCommandListeners.contains(listener))
|
||
574 | actionCommandListeners.add(listener); |
||
575 | } |
||
576 | |||
577 | /**
|
||
578 | * Borrar un listener de la lista de eventos
|
||
579 | * @param listener
|
||
580 | */
|
||
581 | public void removeValueChangedListener(ColorSliderListener listener) { |
||
582 | actionCommandListeners.remove(listener); |
||
583 | } |
||
584 | |||
585 | /**
|
||
586 | * A?adir un listener a la lista de eventos
|
||
587 | * @param listener
|
||
588 | */
|
||
589 | public void addSelectionChangedListener(ColorSliderListener listener) { |
||
590 | if (!actionSelectionListeners.contains(listener))
|
||
591 | actionSelectionListeners.add(listener); |
||
592 | } |
||
593 | |||
594 | /**
|
||
595 | * Borrar un listener de la lista de eventos
|
||
596 | * @param listener
|
||
597 | */
|
||
598 | public void removeSelectionChangedListener(ColorSliderListener listener) { |
||
599 | actionSelectionListeners.remove(listener); |
||
600 | } |
||
601 | |||
602 | /**
|
||
603 | * Invocar a los eventos asociados al componente
|
||
604 | */
|
||
605 | private void callSelectionChangedListeners() { |
||
606 | Iterator acIterator = actionSelectionListeners.iterator();
|
||
607 | while (acIterator.hasNext()) {
|
||
608 | ColorSliderListener listener = (ColorSliderListener) acIterator.next(); |
||
609 | listener.actionSelectionChanged(new ColorSliderEvent(this)); |
||
610 | } |
||
611 | } |
||
612 | |||
613 | /**
|
||
614 | * Invocar a los eventos asociados al componente
|
||
615 | */
|
||
616 | private void callValueChangedListeners() { |
||
617 | Iterator acIterator = actionCommandListeners.iterator();
|
||
618 | while (acIterator.hasNext()) {
|
||
619 | ColorSliderListener listener = (ColorSliderListener) acIterator.next(); |
||
620 | listener.actionValueChanged(new ColorSliderEvent(this)); |
||
621 | } |
||
622 | } |
||
623 | |||
624 | /**
|
||
625 | * Invocar a los eventos asociados al componente
|
||
626 | */
|
||
627 | private void callValueDraggedListeners() { |
||
628 | Iterator acIterator = actionCommandListeners.iterator();
|
||
629 | while (acIterator.hasNext()) {
|
||
630 | ColorSliderListener listener = (ColorSliderListener) acIterator.next(); |
||
631 | listener.actionValueDragged(new ColorSliderEvent(this)); |
||
632 | } |
||
633 | } |
||
634 | |||
635 | public void removeAllItems() { |
||
636 | items.clear(); |
||
637 | } |
||
638 | |||
639 | 13022 | bsanchez | /**
|
640 | * @return the items
|
||
641 | */
|
||
642 | public ArrayList getItems() { |
||
643 | return items;
|
||
644 | } |
||
645 | |||
646 | /**
|
||
647 | * Devuelve los items que estan visibles en el componente
|
||
648 | * @return the items
|
||
649 | */
|
||
650 | public ArrayList getItemsShowed() { |
||
651 | ArrayList newArray = (ArrayList) items.clone(); |
||
652 | for (int i = newArray.size() - 1; i >= 0; i--) { |
||
653 | if (((ItemColorSlider) newArray.get(i)).isVisible() == false) |
||
654 | newArray.remove(i); |
||
655 | } |
||
656 | |||
657 | return newArray;
|
||
658 | } |
||
659 | |||
660 | 12898 | bsanchez | public void mouseClicked(MouseEvent e) {} |
661 | public void mouseEntered(MouseEvent e) {} |
||
662 | public void mouseExited(MouseEvent e) {} |
||
663 | } |