001: package abbot.tester;
002:
003: import java.awt.*;
004: import javax.swing.*;
005:
006: import abbot.i18n.*;
007:
008: /** Provides user actions on a JScrollPane. */
009: // TODO: ScrollBarLocation, which provides locations for thumb, arrows, etc
010: // TODO: multi-scroll when arrows pressed and held
011: // TODO: multi-scroll when block (outside thumb) pressed and held
012: public class JScrollBarTester extends JComponentTester {
013:
014: private static final int BLOCK_OFFSET = 4;
015:
016: protected Point getThumbLocation(JScrollBar bar, int value) {
017: boolean horizontal = bar.getOrientation() == JScrollBar.HORIZONTAL;
018: double fraction = (double) value
019: / (bar.getMaximum() - bar.getMinimum());
020: if (horizontal) {
021: int arrow = bar.getHeight();
022: return new Point(arrow
023: + (int) (fraction * (bar.getWidth() - 2 * arrow)),
024: arrow / 2);
025: }
026: int arrow = bar.getWidth();
027: return new Point(arrow / 2, arrow
028: + (int) (fraction * (bar.getHeight() - 2 * arrow)));
029: }
030:
031: protected Point getUnitLocation(JScrollBar bar, boolean up) {
032: boolean horizontal = bar.getOrientation() == JScrollBar.HORIZONTAL;
033: int arrow = horizontal ? bar.getHeight() : bar.getWidth();
034: if (up) {
035: return horizontal ? new Point(bar.getWidth() - arrow / 2,
036: arrow / 2) : new Point(arrow / 2, bar.getHeight()
037: - arrow / 2);
038: }
039: return new Point(arrow / 2, arrow / 2);
040: }
041:
042: protected Point getBlockLocation(JScrollBar bar, boolean up) {
043: boolean horizontal = bar.getOrientation() == JScrollBar.HORIZONTAL;
044: Point p = getUnitLocation(bar, up);
045: int offset = up ? BLOCK_OFFSET : -BLOCK_OFFSET;
046: if (horizontal)
047: p.x += offset;
048: else
049: p.y += offset;
050: return p;
051: }
052:
053: protected void scroll(final JScrollBar bar, final int value) {
054: invokeLater(bar, new Runnable() {
055: public void run() {
056: bar.setValue(value);
057: }
058: });
059: }
060:
061: protected void scroll(JScrollBar bar, int count, boolean block) {
062: // For now, do it programmatically, faking the mouse movement and
063: // clicking
064: Point where = block ? getBlockLocation(bar, count >= 0)
065: : getUnitLocation(bar, count >= 0);
066: // Don't really know if this is where the UI puts them
067: // mouseClick(bar, where.x, where.y);
068: mouseMove(bar, where.x, where.y);
069: int value = bar.getValue()
070: + count
071: * (block ? bar.getBlockIncrement() : bar
072: .getUnitIncrement());
073: scroll(bar, value);
074: }
075:
076: /** Scroll up (or right) one unit (usually a line). */
077: public void actionScrollUnitUp(Component c) {
078: scroll((JScrollBar) c, 1, false);
079: waitForIdle();
080: }
081:
082: /** Scroll down (or left) one unit (usually a line). */
083: public void actionScrollUnitDown(Component c) {
084: scroll((JScrollBar) c, -1, false);
085: waitForIdle();
086: }
087:
088: /** Scroll up (or right) one block (usually a page). */
089: public void actionScrollBlockUp(Component c) {
090: scroll((JScrollBar) c, 1, true);
091: waitForIdle();
092: }
093:
094: /** Scroll down (or left) one block (usually a page). */
095: public void actionScrollBlockDown(Component c) {
096: scroll((JScrollBar) c, -1, true);
097: waitForIdle();
098: }
099:
100: /** Scroll to the given scroll position. */
101: public void actionScrollTo(Component c, final int position) {
102: JScrollBar bar = (JScrollBar) c;
103: int min = bar.getMinimum();
104: int max = bar.getMaximum();
105: if (position < min || position > max) {
106: String msg = Strings.get("tester.JScrollBar.out_of_range",
107: new Object[] { new Integer(position),
108: new Integer(min), new Integer(max), });
109: throw new ActionFailedException(msg);
110: }
111:
112: Point thumb = getThumbLocation(bar, bar.getValue());
113: mouseMove(bar, thumb.x, thumb.y);
114:
115: thumb = getThumbLocation(bar, position);
116: mouseMove(bar, thumb.x, thumb.y);
117:
118: scroll(bar, position);
119: waitForIdle();
120: }
121: }
|