01: package abbot.tester;
02:
03: import java.awt.Component;
04: import java.awt.Insets;
05: import java.awt.Point;
06: import javax.swing.*;
07:
08: /** Provides access to all user actions on a JSlider. */
09: public class JSliderTester extends JComponentTester {
10:
11: private ComponentLocation valueToLocation(JSlider s, int value) {
12: int range = s.getMaximum() - s.getMinimum();
13: int x = s.getWidth() / 2;
14: int y = s.getHeight() / 2;
15: Insets insets = s.getInsets();
16: float percent = (float) (value - s.getMinimum()) / range;
17: if (s.getOrientation() == JSlider.VERTICAL) {
18: int max = s.getHeight() - insets.top - insets.bottom - 1;
19: y = (int) (percent * max);
20: if (!s.getInverted()) {
21: y = max - y;
22: }
23: } else {
24: int max = s.getWidth() - insets.left - insets.right - 1;
25: x = (int) (percent * max);
26: if (s.getInverted()) {
27: x = max - x;
28: }
29: }
30: return new ComponentLocation(new Point(x, y));
31: }
32:
33: /** Click at the maximum end of the slider. */
34: public void actionIncrement(Component c) {
35: JSlider s = (JSlider) c;
36: actionClick(c, valueToLocation(s, s.getMaximum()));
37: }
38:
39: /** Click at the minimum end of the slider. */
40: public void actionDecrement(Component c) {
41: JSlider s = (JSlider) c;
42: actionClick(c, valueToLocation(s, s.getMinimum()));
43: }
44:
45: /** Slide the knob to the requested value. */
46: public void actionSlide(Component c, final int value) {
47: final JSlider s = (JSlider) c;
48: // can't do drag actions in AWT mode
49: if (getEventMode() == EM_ROBOT) {
50: actionDrag(c, valueToLocation(s, s.getValue()));
51: actionDrop(c, valueToLocation(s, value));
52: }
53: // the drag is only approximate, so set the value directly
54: invokeAndWait(new Runnable() {
55: public void run() {
56: s.setValue(value);
57: }
58: });
59: }
60:
61: /** Slide the knob to its maximum. */
62: public void actionSlideMaximum(Component c) {
63: actionSlide(c, ((JSlider) c).getMaximum());
64: }
65:
66: /** Slide the knob to its minimum. */
67: public void actionSlideMinimum(Component c) {
68: actionSlide(c, ((JSlider) c).getMinimum());
69: }
70: }
|