svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / doubleslider / DoubleSlider.java @ 11004
History | View | Annotate | Download (5.81 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.doubleslider; |
20 |
|
21 |
import java.awt.Color; |
22 |
import java.awt.Dimension; |
23 |
import java.awt.Graphics; |
24 |
import java.awt.Image; |
25 |
import java.awt.event.MouseEvent; |
26 |
import java.awt.event.MouseListener; |
27 |
import java.awt.event.MouseMotionListener; |
28 |
import java.util.ArrayList; |
29 |
import java.util.Iterator; |
30 |
|
31 |
import javax.swing.JComponent; |
32 |
|
33 |
public class DoubleSlider extends JComponent implements MouseMotionListener, MouseListener { |
34 |
private static final long serialVersionUID = 663355422780987493L; |
35 |
|
36 |
Image bufferImage = null; |
37 |
int width, height = 0; |
38 |
Graphics bufferGraphics;
|
39 |
int x1 = 0; |
40 |
int x2 = 100; |
41 |
|
42 |
int minimum = 0; |
43 |
int maximum = 100; |
44 |
|
45 |
private ArrayList actionCommandListeners = new ArrayList(); |
46 |
private boolean bDoCallListeners = true; |
47 |
static private int eventId = Integer.MIN_VALUE; |
48 |
|
49 |
public DoubleSlider() {
|
50 |
this.setPreferredSize(new Dimension(100, 21)); |
51 |
} |
52 |
|
53 |
public void setMaximum(int value) { |
54 |
maximum = value; |
55 |
validateValues(); |
56 |
refreshImage(); |
57 |
} |
58 |
|
59 |
public void setMinimum(int value) { |
60 |
minimum = value; |
61 |
validateValues(); |
62 |
refreshImage(); |
63 |
} |
64 |
|
65 |
public void addNotify() { |
66 |
super.addNotify();
|
67 |
|
68 |
addMouseMotionListener(this);
|
69 |
addMouseListener(this);
|
70 |
|
71 |
refreshImage(); |
72 |
} |
73 |
|
74 |
private Graphics getBufferGraphics() { |
75 |
int width2=getBounds().width;
|
76 |
int height2=getBounds().height;
|
77 |
if (width2<=0) width2=1; |
78 |
if (height2<=0) height2=1; |
79 |
|
80 |
if ((width!=width2) || (height!=height2)) {
|
81 |
bufferImage = createImage(width2, height2); |
82 |
if (bufferImage == null) return null; |
83 |
bufferGraphics = bufferImage.getGraphics(); |
84 |
} |
85 |
|
86 |
width = width2; |
87 |
height = height2; |
88 |
|
89 |
return bufferGraphics;
|
90 |
} |
91 |
|
92 |
public void redrawBuffer() { |
93 |
if (getBufferGraphics() == null) return; |
94 |
getBufferGraphics().setColor(this.getBackground());
|
95 |
|
96 |
getBufferGraphics().fillRect(0,0,width,height); |
97 |
|
98 |
int color = 0; |
99 |
for (int i=1; i<=(width-1); i++) { |
100 |
color = (i*255)/width;
|
101 |
getBufferGraphics().setColor(new Color(color,color,color)); |
102 |
getBufferGraphics().drawLine(i,1,i,12); |
103 |
} |
104 |
|
105 |
drawTriangle(valuetopixel(x1), Color.BLACK);
|
106 |
drawTriangle(valuetopixel(x2), Color.WHITE);
|
107 |
} |
108 |
|
109 |
private void drawTriangle(int x, Color color) { |
110 |
getBufferGraphics().setColor(color); |
111 |
getBufferGraphics().drawLine(x, 14, x, 18); |
112 |
getBufferGraphics().drawLine(x-1, 16, x-1, 18); |
113 |
getBufferGraphics().drawLine(x+1, 16, x+1, 18); |
114 |
getBufferGraphics().drawLine(x-2, 18, x-2, 18); |
115 |
getBufferGraphics().drawLine(x+2, 18, x+2, 18); |
116 |
|
117 |
getBufferGraphics().setColor(Color.BLACK);
|
118 |
getBufferGraphics().drawLine(x, 12, x-3, 19); |
119 |
getBufferGraphics().drawLine(x, 12, x+3, 19); |
120 |
getBufferGraphics().drawLine(x-3, 19, x+3, 19); |
121 |
} |
122 |
|
123 |
public void refreshImage() { |
124 |
redrawBuffer(); |
125 |
if (bufferImage != null) |
126 |
getGraphics().drawImage(bufferImage, 0, 0, this); |
127 |
super.paint(getGraphics());
|
128 |
} |
129 |
|
130 |
private int valuetopixel(int value) { |
131 |
return ((value-minimum)*(width-3)/(maximum-minimum)) + 1; |
132 |
} |
133 |
|
134 |
private int pixeltovalue(int value) { |
135 |
return (((value-1)*(maximum-minimum))/(width-3))+minimum; |
136 |
} |
137 |
|
138 |
public void paint(Graphics g) { |
139 |
redrawBuffer(); |
140 |
g.drawImage(bufferImage, 0, 0, this); |
141 |
super.paint(g);
|
142 |
} |
143 |
|
144 |
int valuePressed = 0; |
145 |
|
146 |
public void mousePressed(MouseEvent e) { |
147 |
if (e.getButton() != MouseEvent.BUTTON1) return; |
148 |
|
149 |
int aux = pixeltovalue(e.getX()+1); |
150 |
int aux2 = aux - x1;
|
151 |
int aux3 = x2 - aux;
|
152 |
if (aux3 < aux2)
|
153 |
valuePressed = 2;
|
154 |
else
|
155 |
valuePressed = 1;
|
156 |
|
157 |
changeValue(e.getX()); |
158 |
} |
159 |
|
160 |
public void mouseReleased(MouseEvent e) { |
161 |
valuePressed = 0;
|
162 |
} |
163 |
|
164 |
private void validateValues() { |
165 |
if (x1 < minimum) x1 = minimum;
|
166 |
if (x1 > maximum) x1 = maximum;
|
167 |
if (x2 < minimum) x2 = minimum;
|
168 |
if (x2 > maximum) x2 = maximum;
|
169 |
} |
170 |
|
171 |
public void setX1(int value) { |
172 |
x1 = value; |
173 |
if (x1 > x2) x2 = x1;
|
174 |
validateValues(); |
175 |
refreshImage(); |
176 |
} |
177 |
|
178 |
public void setX2(int value) { |
179 |
x2 = value; |
180 |
if (x2 < x1) x1 = x2;
|
181 |
validateValues(); |
182 |
refreshImage(); |
183 |
} |
184 |
|
185 |
public int getX1() { |
186 |
return x1;
|
187 |
} |
188 |
|
189 |
public int getX2() { |
190 |
return x2;
|
191 |
} |
192 |
|
193 |
private void changeValue(int pos) { |
194 |
if (valuePressed == 0) return; |
195 |
|
196 |
int aux = pixeltovalue(pos + 1); |
197 |
|
198 |
if (valuePressed == 1) setX1(aux); |
199 |
if (valuePressed == 2) setX2(aux); |
200 |
callValueChangedListeners(); |
201 |
// System.out.println(x1 + ":" + x2);
|
202 |
} |
203 |
|
204 |
public void mouseDragged(MouseEvent arg0) { |
205 |
changeValue(arg0.getX()); |
206 |
} |
207 |
|
208 |
public void mouseMoved(MouseEvent e) { |
209 |
} |
210 |
public void mouseClicked(MouseEvent e) { |
211 |
} |
212 |
public void mouseEntered(MouseEvent e) { |
213 |
} |
214 |
public void mouseExited(MouseEvent e) { |
215 |
} |
216 |
|
217 |
public void addValueChangedListener(DoubleSliderListener listener) { |
218 |
if (!actionCommandListeners.contains(listener))
|
219 |
actionCommandListeners.add(listener); |
220 |
} |
221 |
|
222 |
public void removeValueChangedListener(DoubleSliderListener listener) { |
223 |
actionCommandListeners.remove(listener); |
224 |
} |
225 |
|
226 |
private void callValueChangedListeners() { |
227 |
if (!bDoCallListeners)
|
228 |
return;
|
229 |
Iterator acIterator = actionCommandListeners.iterator();
|
230 |
while (acIterator.hasNext()) {
|
231 |
DoubleSliderListener listener = (DoubleSliderListener) acIterator.next(); |
232 |
listener.actionValueChanged(new DoubleSliderEvent(this)); |
233 |
} |
234 |
eventId++; |
235 |
} |
236 |
|
237 |
} |