1 package org.catacomb.druid.swing;
2
3 import org.catacomb.druid.event.LabelActor;
4 import org.catacomb.interlish.interact.DComponent;
5 import org.catacomb.interlish.structure.MouseActor;
6 import org.catacomb.interlish.structure.MouseSource;
7
8
9 import java.awt.Color;
10 import java.awt.Dimension;
11 import java.awt.Graphics;
12 import java.awt.event.InputEvent;
13 import java.awt.event.MouseEvent;
14 import java.awt.event.MouseListener;
15 import java.awt.event.MouseMotionListener;
16
17 import javax.swing.JPanel;
18
19
20
21
22
23 public class DSlider extends JPanel implements DComponent, MouseSource, MouseListener,
24 MouseMotionListener {
25
26 static final long serialVersionUID = 1001;
27
28 LabelActor labelActor;
29
30 int nmax;
31 int islider;
32
33 String descriptionText;
34
35 String[] values;
36
37 int xdown;
38 int ydown;
39 long downtime;
40
41
42 Color bgColor;
43 RolloverEffect rollover;
44
45
46 int state;
47 final static int NONE = 0;
48 final static int DRAG = 1;
49
50
51 public DSlider() {
52 this(1);
53 }
54
55
56 public DSlider(int n) {
57 nmax = n;
58 values = new String[n];
59 for (int i = 0; i < n; i++) {
60 values[i] = "" + i;
61 }
62
63 addMouseListener(this);
64 addMouseMotionListener(this);
65
66 attachRollover();
67 }
68
69 public void setTooltip(String s) {
70 setToolTipText(s);
71 }
72
73
74 public void setNPoint(int npt) {
75 nmax = npt;
76 values = new String[nmax];
77 for (int i = 0; i < nmax; i++) {
78 values[i] = "" + i;
79 }
80 repaint();
81 }
82
83
84 public void setMouseActor(MouseActor ma) {
85 addMouseListener(new DMouseRelay(ma));
86 }
87
88
89 public void setBg(Color col) {
90 setBackground(col);
91 bgColor = col;
92 rollover.setBg(col);
93 }
94
95
96 public void attachRollover() {
97 rollover = new RolloverEffect(this);
98 addMouseListener(rollover);
99 }
100
101
102 public void setLabelActor(LabelActor lact) {
103 labelActor = lact;
104 }
105
106
107 private void notifyChange() {
108 if (labelActor != null) {
109 labelActor.labelAction("change", true);
110 }
111 }
112
113
114 public void export() {
115 notifyChange();
116 }
117
118
119 public void setValues(String[] sa) {
120 values = sa;
121 nmax = sa.length;
122 showValue(0);
123 }
124
125
126 public int getValue() {
127 return islider;
128 }
129
130
131
132 public void showValue(int iv) {
133 if (iv != islider) {
134 islider = iv;
135 repaint();
136 }
137 }
138
139
140 public Dimension getMinimumSize() {
141 return new Dimension(80, 20);
142 }
143
144
145 public Dimension getPreferredSize() {
146 return new Dimension(140, 22);
147 }
148
149
150
151 public void paintComponent(Graphics g) {
152 realPaint(g);
153 }
154
155
156
157 public void realPaint(Graphics g) {
158 int w = getWidth();
159 int h = getHeight();
160 if (bgColor == null) {
161 bgColor = getBackground();
162 }
163 g.setColor(bgColor);
164 g.fillRect(0, 0, w, h);
165
166 paintArrows(g);
167
168 paintKnob(g);
169
170 if (descriptionText != null) {
171 g.setColor(Color.black);
172 g.drawString(descriptionText, 40, h-5);
173 }
174 }
175
176
177
178 private void paintArrows(Graphics g) {
179 int w = getWidth();
180 int h = getHeight();
181
182 Color cbg = bgColor;
183 Color cbr = cbg.brighter();
184 Color cdk = cbg.darker();
185
186 int hh = h / 2;
187 g.setColor(cbr);
188 g.drawLine(4, hh, 15, 4);
189
190 g.drawLine(w - 15, h - 4, w - 15, 4);
191
192
193
194
195 g.setColor(cdk);
196 g.drawLine(15, 4, 15, h - 4);
197
198
199
200 g.drawLine(4, hh, 15, h - 4);
201
202 g.drawLine(w - 15, h - 4, w - 4, hh);
203 g.drawLine(w - 15, 4, w - 4, hh);
204
205 }
206
207
208 private void paintKnob(Graphics g) {
209 int width = getWidth();
210 int height = getHeight();
211 int hh = height / 2;
212
213 if (nmax < 1) {
214 nmax = 1;
215 }
216
217 if (islider >= nmax) {
218 islider = nmax - 1;
219 }
220
221 double f = (islider + 0.5) / nmax;
222
223 int icen = (int)(25 + f * (width - 50));
224 if (icen < 25) {
225 icen = 25;
226 }
227 if (icen > width - 25) {
228 icen = width - 25;
229 }
230 drawUpButton(g, icen, hh, 5, 5);
231
232 if (islider >= 0 && values != null && islider < values.length && values[islider] != null) {
233 g.setColor(Color.black);
234 if (icen < 80) {
235 g.drawString(values[islider], 100, 18);
236 } else {
237 g.drawString(values[islider], 30, 18);
238 }
239 }
240
241 }
242
243
244 private void drawUpButton(Graphics g, int icx, int icy, int hw, int hh) {
245 Color c = getBackground();
246
247 g.setColor(c.darker());
248 g.drawLine(icx - hw - 1, icy + hh + 1, icx + hw + 1, icy + hh + 1);
249 g.drawLine(icx - hw, icy + hh, icx + hw, icy + hh);
250
251 g.drawLine(icx + hw + 1, icy - hh - 1, icx + hw + 1, icy + hh + 1);
252 g.drawLine(icx + hw, icy - hh, icx + hw, icy + hh);
253
254
255 g.setColor(c.brighter());
256 g.drawLine(icx - hw - 1, icy - hh - 1, icx + hw + 1, icy - hh - 1);
257 g.drawLine(icx - hw, icy - hh, icx + hw, icy - hh);
258
259 g.drawLine(icx - hw - 1, icy - hh - 1, icx - hw - 1, icy + hh + 1);
260 g.drawLine(icx - hw, icy - hh, icx - hw, icy + hh);
261
262
263
264 }
265
266
267 public void nudgeLeft() {
268 if (islider > 0) {
269 islider = islider - 1;
270 repaint();
271 export();
272 }
273 }
274
275
276 public void nudgeRight() {
277 if (islider < nmax - 1) {
278 islider = islider + 1;
279 repaint();
280 export();
281 }
282 }
283
284
285 public void rsfMouseDown(int x, int y, long when, int button) {
286
287 state = NONE;
288 if (x > 15 && x < getWidth() - 15) {
289 state = DRAG;
290 moveTo(x);
291
292 } else if (x < 15) {
293 nudgeLeft();
294
295 } else if (x > getWidth() - 15) {
296 nudgeRight();
297 }
298 }
299
300
301 public void rsfMouseUp(int x, int y) {
302 }
303
304
305 public void rsfMouseDrag(int x, int y) {
306 if (state == DRAG) {
307 moveTo(x);
308 }
309 }
310
311
312 private void moveTo(int x) {
313 int width = getWidth();
314
315
316 double frel = (x - 15.) / (width - 30.);
317 int oslider = islider;
318 islider = (int)(frel * nmax + 0.0);
319 if (islider < 0) {
320 islider = 0;
321 }
322 if (islider > nmax - 1) {
323 islider = nmax - 1;
324 }
325 if (islider != oslider) {
326 export();
327 repaint();
328 }
329 }
330
331
332
333
334
335 public void mouseDragged(MouseEvent e) {
336 int x = e.getX();
337 int y = e.getY();
338 rsfMouseDrag(x, y);
339 }
340
341
342 public void mouseMoved(MouseEvent e) {
343 }
344
345
346 public void mousePressed(MouseEvent e) {
347 int x = e.getX();
348 int y = e.getY();
349 long when = e.getWhen();
350 int modif = e.getModifiers();
351 int button = 0;
352 if (modif == InputEvent.BUTTON1_MASK) {
353 button = 1;
354 } else if (modif == InputEvent.BUTTON2_MASK) {
355 button = 2;
356 } else if (modif == InputEvent.BUTTON3_MASK) {
357 button = 3;
358 }
359 rsfMouseDown(x, y, when, button);
360 }
361
362
363
364 public void mouseReleased(MouseEvent e) {
365 int x = e.getX();
366 int y = e.getY();
367 rsfMouseUp(x, y);
368 }
369
370
371
372 public void mouseEntered(MouseEvent e) {
373 requestFocus();
374 }
375
376
377 public void mouseExited(MouseEvent e) {
378 }
379
380
381 public void mouseClicked(MouseEvent e) {
382 requestFocus();
383 }
384
385
386 public void pointShown(int ifr, String desc) {
387 descriptionText = desc;
388 showValue(ifr);
389 }
390
391 }