1 package com.google.gwt.widgetideas.client;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import com.google.gwt.core.client.GWT;
7 import com.google.gwt.dom.client.StyleInjector;
8 import com.google.gwt.user.client.DOM;
9 import com.google.gwt.user.client.Element;
10 import com.google.gwt.user.client.Event;
11 import com.google.gwt.user.client.Timer;
12 import com.google.gwt.user.client.ui.AbstractImagePrototype;
13 import com.google.gwt.user.client.ui.FocusPanel;
14 import com.google.gwt.user.client.ui.Image;
15
16 /**
17 * Abstract base class for slider bars.
18 */
19 public abstract class SliderBar extends FocusPanel implements ResizableWidget, SourcesSliderEvents {
20 static {
21 // Inject CSS in the document headers
22 StyleInjector.inject(Resources.INSTANCE.getHSliderCss().getText());
23 StyleInjector.inject(Resources.INSTANCE.getVSliderCss().getText());
24 }
25 /**
26 * The timer used to continue to shift the knob as the user holds down one of
27 * the left/right arrow keys. Only IE auto-repeats, so we just keep catching
28 * the events.
29 */
30 protected class KeyTimer extends Timer {
31 /**
32 * A bit indicating that this is the first run.
33 */
34 private boolean firstRun = true;
35
36 /**
37 * The delay between shifts, which shortens as the user holds down the
38 * button.
39 */
40 private int repeatDelay = 30;
41
42 /**
43 * A bit indicating whether we are shifting to a higher or lower value.
44 */
45 private boolean shiftBottom = false;
46
47 /**
48 * The number of steps to shift with each press.
49 */
50 private int multiplier = 1;
51
52 /**
53 * This method will be called when a timer fires. Override it to implement
54 * the timer's logic.
55 */
56 public void run() {
57 // Highlight the knob on first run
58 if (firstRun) {
59 firstRun = false;
60 startSliding(true, false);
61 }
62
63 // Slide the slider bar
64 if (shiftBottom) {
65 setCurrentValue(curValue + multiplier * stepSize);
66 } else {
67 setCurrentValue(curValue - multiplier * stepSize);
68 }
69
70 // Repeat this timer until cancelled by keyup event
71 schedule(repeatDelay);
72 }
73
74 /**
75 * Schedules a timer to elapse in the future.
76 *
77 * @param delayMillis how long to wait before the timer elapses, in
78 * milliseconds
79 * @param shiftRight whether to shift up or not
80 * @param multiplier the number of steps to shift
81 */
82 public void schedule(int delayMillis, boolean shiftRight, int multiplier) {
83 firstRun = true;
84 this.shiftBottom = shiftRight;
85 this.multiplier = multiplier;
86 super.schedule(delayMillis);
87 }
88 }
89
90 /**
91 * A formatter used to format the labels displayed in the widget.
92 */
93 public static class LabelFormatter {
94 /**
95 * Generate the text to display in each label based on the label's value.
96 *
97 * Override this method to change the text displayed within the SliderBar.
98 *
99 * @param slider the Slider bar
100 * @param value the value the label displays
101 * @return the text to display for the label
102 */
103 protected String formatLabel(SliderBar slider, double value) {
104 return (int) (10 * value) / 10.0 + "";
105 }
106 }
107 /**
108 * The current value.
109 */
110 protected double curValue;
111
112 /**
113 * The images used with the sliding bar.
114 */
115 protected AbstractImagePrototype[] images;
116
117
118 /**
119 * The knob that slides across the line.
120 */
121 protected Image knobImage = new Image();
122
123 /**
124 * The timer used to continue to shift the knob if the user holds down a key.
125 */
126 protected KeyTimer keyTimer = new KeyTimer();
127
128 /**
129 * The elements used to display labels above the ticks.
130 */
131 protected List/* <Element> */labelElements = new ArrayList/* <Element> */();
132
133 /**
134 * The formatter used to generate label text.
135 */
136 protected LabelFormatter labelFormatter;
137
138 /**
139 * The line that the knob moves over.
140 */
141 protected Element lineElement;
142
143 /**
144 * The offset between the edge of the shell and the line.
145 */
146 protected int lineOffset;
147
148 /**
149 * The maximum slider value.
150 */
151 protected double maxValue;
152
153 /**
154 * The minimum slider value.
155 */
156 protected double minValue;
157
158 /**
159 * The number of labels to show.
160 */
161 protected int numLabels;
162
163 /**
164 * The number of tick marks to show.
165 */
166 protected int numTicks;
167
168 /**
169 * A bit indicating whether or not we are currently sliding the slider bar due
170 * to keyboard events.
171 */
172 protected boolean slidingKeyboard = false;
173
174 /**
175 * A collection of widgets to notify on slider events.
176 */
177 protected SliderListenerCollection sliderListeners;
178
179 /**
180 * A bit indicating whether or not we are currently sliding the slider bar due
181 * to mouse events.
182 */
183 protected boolean slidingMouse = false;
184
185 /**
186 * The size of the increments between knob positions.
187 */
188 protected double stepSize;
189
190 /**
191 * The base name from which CSS style names for the slider are derived.
192 */
193 protected String styleBaseName;
194
195 /**
196 * The elements used to display tick marks, which are the vertical lines along
197 * the slider bar.
198 */
199 protected List/* <Element> */tickElements = new ArrayList/* <Element> */();
200
201 public SliderBar() {
202 super();
203 sinkEvents(Event.FOCUSEVENTS | Event.KEYEVENTS | Event.ONCLICK
204 | Event.MOUSEEVENTS | Event.ONMOUSEWHEEL);
205 }
206
207 public void addSliderListener(SliderListener listener) {
208 if (sliderListeners == null) {
209 sliderListeners = new SliderListenerCollection();
210 }
211 sliderListeners.add(listener);
212 }
213
214 /**
215 * Return the current value.
216 *
217 * @return the current value
218 */
219 public double getCurrentValue() {
220 return curValue;
221 }
222
223 /**
224 * Return the label formatter.
225 *
226 * @return the label formatter
227 */
228 public LabelFormatter getLabelFormatter() {
229 return labelFormatter;
230 }
231
232 /**
233 * Return the max value.
234 *
235 * @return the max value
236 */
237 public double getMaxValue() {
238 return maxValue;
239 }
240
241 /**
242 * Return the minimum value.
243 *
244 * @return the minimum value
245 */
246 public double getMinValue() {
247 return minValue;
248 }
249
250 /**
251 * Return the number of labels.
252 *
253 * @return the number of labels
254 */
255 public int getNumLabels() {
256 return numLabels;
257 }
258
259 /**
260 * Return the number of ticks.
261 *
262 * @return the number of ticks
263 */
264 public int getNumTicks() {
265 return numTicks;
266 }
267
268 /**
269 * Return the step size.
270 *
271 * @return the step size
272 */
273 public double getStepSize() {
274 return stepSize;
275 }
276
277 /**
278 * Return the total range between the minimum and maximum values.
279 *
280 * @return the total range
281 */
282 public double getTotalRange() {
283 if (minValue > maxValue) {
284 return 0;
285 } else {
286 return maxValue - minValue;
287 }
288 }
289
290 /**
291 * Redraw the progress bar when something changes the layout.
292 */
293 public void redraw() {
294 if (isAttached()) {
295 int width = DOM.getElementPropertyInt(getElement(), "clientWidth");
296 int height = DOM.getElementPropertyInt(getElement(), "clientHeight");
297 onResize(width, height);
298 }
299 }
300
301 public void removeSliderListener(SliderListener listener) {
302 if (sliderListeners != null) {
303 sliderListeners.remove(listener);
304 }
305 }
306
307 /**
308 * Set the current value and fire the onValueChange event.
309 *
310 * @param curValue the current value
311 */
312 public void setCurrentValue(double curValue) {
313 setCurrentValue(curValue, true);
314 }
315
316 /**
317 * Set the current value and optionally fire the onValueChange event.
318 *
319 * @param curValue the current value
320 * @param fireEvent fire the onValue change event if true
321 */
322 public void setCurrentValue(double curValue, boolean fireEvent) {
323 // Confine the value to the range
324 this.curValue = Math.max(minValue, Math.min(maxValue, curValue));
325 double remainder = (this.curValue - minValue) % stepSize;
326 this.curValue -= remainder;
327
328 // Go to next step if more than halfway there
329 if ((remainder > (stepSize / 2))
330 && ((this.curValue + stepSize) <= maxValue)) {
331 this.curValue += stepSize;
332 }
333
334 // Redraw the knob
335 drawKnob();
336
337 // Fire the onValueChange event
338 if (fireEvent && (sliderListeners != null)) {
339 sliderListeners.fireValueChanged(this, getCurrentValue());
340 }
341 }
342
343 /**
344 * Set the label formatter.
345 *
346 * @param labelFormatter the label formatter
347 */
348 public void setLabelFormatter(LabelFormatter labelFormatter) {
349 this.labelFormatter = labelFormatter;
350 }
351
352 /**
353 * Set the max value.
354 *
355 * @param maxValue the current value
356 */
357 public void setMaxValue(double maxValue) {
358 this.maxValue = maxValue;
359 drawLabels();
360 resetCurrentValue();
361 }
362
363 /**
364 * Set the minimum value.
365 *
366 * @param minValue the current value
367 */
368 public void setMinValue(double minValue) {
369 this.minValue = minValue;
370 drawLabels();
371 resetCurrentValue();
372 }
373
374 /**
375 * Set the number of labels to show on the line. Labels indicate the value of
376 * the slider at that point. Use this method to enable labels.
377 *
378 * If you set the number of labels equal to the total range divided by the
379 * step size, you will get a properly aligned "jumping" effect where the knob
380 * jumps between labels.
381 *
382 * Note that the number of labels displayed will be one more than the number
383 * you specify, so specify 1 labels to show labels on either end of the line.
384 * In other words, numLabels is really the number of slots between the labels.
385 *
386 * setNumLabels(0) will disable labels.
387 *
388 * @param numLabels the number of labels to show
389 */
390 public void setNumLabels(int numLabels) {
391 this.numLabels = numLabels;
392 drawLabels();
393 }
394
395 /**
396 * Set the number of ticks to show on the line. A tick is a vertical line that
397 * represents a division of the overall line. Use this method to enable ticks.
398 *
399 * If you set the number of ticks equal to the total range divided by the step
400 * size, you will get a properly aligned "jumping" effect where the knob jumps
401 * between ticks.
402 *
403 * Note that the number of ticks displayed will be one more than the number
404 * you specify, so specify 1 tick to show ticks on either end of the line. In
405 * other words, numTicks is really the number of slots between the ticks.
406 *
407 * setNumTicks(0) will disable ticks.
408 *
409 * @param numTicks the number of ticks to show
410 */
411 public void setNumTicks(int numTicks) {
412 this.numTicks = numTicks;
413 drawTicks();
414 }
415
416 /**
417 * Set the step size.
418 *
419 * @param stepSize the current value
420 */
421 public void setStepSize(double stepSize) {
422 this.stepSize = stepSize;
423 resetCurrentValue();
424 }
425
426 /**
427 * Draw the knob where it is supposed to be relative to the line.
428 */
429 protected abstract void drawKnob();
430
431 /**
432 * Draw the labels along the line.
433 */
434 protected abstract void drawLabels();
435
436 /**
437 * Draw the tick along the line.
438 */
439 protected abstract void drawTicks();
440
441 /**
442 * Format the label to display above the ticks
443 *
444 * Override this method in a subclass to customize the format. By default,
445 * this method returns the integer portion of the value.
446 *
447 * @param value the value at the label
448 * @return the text to put in the label
449 */
450 protected String formatLabel(double value) {
451 if (labelFormatter != null) {
452 return labelFormatter.formatLabel(this, value);
453 } else {
454 return (int) (10 * value) / 10.0 + "";
455 }
456 }
457
458 /**
459 * Get the percentage of the knob's position relative to the size of the line.
460 * The return value will be between 0.0 and 1.0.
461 *
462 * @return the current percent complete
463 */
464 protected double getKnobPercent() {
465 // If we have no range
466 if (maxValue <= minValue) {
467 return 0;
468 }
469
470 // Calculate the relative progress
471 double percent = (curValue - minValue) / (maxValue - minValue);
472 return Math.max(0.0, Math.min(1.0, percent));
473 }
474
475 /**
476 * This method is called immediately after a widget becomes attached to the
477 * browser's document.
478 */
479 protected void onLoad() {
480 // Reset the position attribute of the parent element
481 DOM.setStyleAttribute(getElement(), "position", "relative");
482 redraw();
483 }
484
485 /**
486 * Highlight this widget.
487 */
488 protected void highlight() {
489 String styleName = getStylePrimaryName();
490 DOM.setElementProperty(getElement(), "className", styleName + " "
491 + styleName + "-focused");
492 }
493
494 /**
495 * Reset the progress to constrain the progress to the current range and
496 * redraw the knob as needed.
497 */
498 protected void resetCurrentValue() {
499 setCurrentValue(getCurrentValue());
500 }
501
502 /**
503 * Start sliding the knob.
504 *
505 * @param highlight true to change the style
506 * @param fireEvent true to fire the event
507 */
508 protected abstract void startSliding(boolean highlight, boolean fireEvent);
509
510 /**
511 * Stop sliding the knob.
512 *
513 * @param unhighlight true to change the style
514 * @param fireEvent true to fire the event
515 */
516 protected abstract void stopSliding(boolean unhighlight, boolean fireEvent);
517
518 /**
519 * Unhighlight this widget.
520 */
521 protected void unhighlight() {
522 DOM.setElementProperty(getElement(), "className", getStylePrimaryName());
523 }
524 }