JScrollBarDriver.java revision 13978:1993af50385d
1/*
2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.netbeans.jemmy.drivers.scrolling;
24
25import java.awt.Point;
26
27import javax.swing.JScrollBar;
28
29import org.netbeans.jemmy.QueueTool;
30import org.netbeans.jemmy.Timeout;
31import org.netbeans.jemmy.drivers.DriverManager;
32import org.netbeans.jemmy.drivers.MouseDriver;
33import org.netbeans.jemmy.operators.ComponentOperator;
34import org.netbeans.jemmy.operators.JButtonOperator;
35import org.netbeans.jemmy.operators.JScrollBarOperator;
36import org.netbeans.jemmy.operators.Operator;
37
38/**
39 * ScrollDriver for javax.swing.JScrollBar component type.
40 *
41 * @author Alexandre Iline(alexandre.iline@oracle.com)
42 */
43public class JScrollBarDriver extends AbstractScrollDriver {
44
45    private final static int SMALL_INCREMENT = 1;
46    private final static int MINIMAL_DRAGGER_SIZE = 5;
47    private final static int RELATIVE_DRAG_STEP_LENGTH = 20;
48
49    private QueueTool queueTool;
50
51    /**
52     * Constructs a JScrollBarDriver.
53     */
54    public JScrollBarDriver() {
55        super(new String[]{"org.netbeans.jemmy.operators.JScrollBarOperator"});
56        queueTool = new QueueTool();
57    }
58
59    @Override
60    protected int position(ComponentOperator oper, int orientation) {
61        return ((JScrollBarOperator) oper).getValue();
62    }
63
64    @Override
65    public void scrollToMinimum(ComponentOperator oper, int orientation) {
66        startDragging(oper);
67        Point pnt = new Point(0, 0);
68        drag(oper, pnt);
69        Timeout sleepTime = oper.getTimeouts().create("Waiter.TimeDelta");
70        while (((JScrollBarOperator) oper).getValue()
71                > ((JScrollBarOperator) oper).getMinimum()) {
72            sleepTime.sleep();
73        }
74        drop(oper, pnt);
75    }
76
77    @Override
78    public void scrollToMaximum(ComponentOperator oper, int orientation) {
79        startDragging(oper);
80        Point pnt = new Point(oper.getWidth() - 1, oper.getHeight() - 1);
81        drag(oper, pnt);
82        Timeout sleepTime = oper.getTimeouts().create("Waiter.TimeDelta");
83        while (((JScrollBarOperator) oper).getValue()
84                > (((JScrollBarOperator) oper).getMaximum()
85                - ((JScrollBarOperator) oper).getVisibleAmount())) {
86            sleepTime.sleep();
87        }
88        drop(oper, pnt);
89    }
90
91    @Override
92    protected void step(ComponentOperator oper, ScrollAdjuster adj) {
93        JButtonOperator boper = findAButton(oper, adj.getScrollDirection());
94        DriverManager.getButtonDriver(boper).push(boper);
95    }
96
97    @Override
98    protected void jump(final ComponentOperator oper, final ScrollAdjuster adj) {
99        final JButtonOperator lessButton = findAButton(oper, ScrollAdjuster.DECREASE_SCROLL_DIRECTION);
100        final JButtonOperator moreButton = findAButton(oper, ScrollAdjuster.INCREASE_SCROLL_DIRECTION);
101        queueTool.invokeSmoothly(new QueueTool.QueueAction<Void>("Scrolling by clicking with the mouse") {
102            @Override
103            public Void launch() {
104                if (adj.getScrollDirection() != ScrollAdjuster.DO_NOT_TOUCH_SCROLL_DIRECTION) {
105                    int x, y;
106                    switch (((JScrollBarOperator) oper).getOrientation()) {
107                        case JScrollBar.HORIZONTAL:
108                            switch (adj.getScrollDirection()) {
109                                case ScrollAdjuster.INCREASE_SCROLL_DIRECTION:
110                                    x = moreButton.getX() - 1;
111                                    break;
112                                case ScrollAdjuster.DECREASE_SCROLL_DIRECTION:
113                                    x = lessButton.getX() + lessButton.getWidth();
114                                    break;
115                                default:
116                                    return null;
117                            }
118                            y = lessButton.getHeight() / 2;
119                            break;
120                        case JScrollBar.VERTICAL:
121                            switch (adj.getScrollDirection()) {
122                                case ScrollAdjuster.INCREASE_SCROLL_DIRECTION:
123                                    y = moreButton.getY() - 1;
124                                    break;
125                                case ScrollAdjuster.DECREASE_SCROLL_DIRECTION:
126                                    y = lessButton.getY() + lessButton.getHeight();
127                                    break;
128                                default:
129                                    return null;
130                            }
131                            x = lessButton.getWidth() / 2;
132                            break;
133                        default:
134                            return null;
135                    }
136                    DriverManager.getMouseDriver(oper).
137                            clickMouse(oper, x, y, 1, Operator.getDefaultMouseButton(), 0, new Timeout("", 0));
138                }
139                return null;
140            }
141        });
142    }
143
144    @Override
145    protected void startPushAndWait(ComponentOperator oper, int direction, int orientation) {
146        JButtonOperator boper = findAButton(oper, direction);
147        DriverManager.getButtonDriver(boper).press(boper);
148    }
149
150    @Override
151    protected void stopPushAndWait(ComponentOperator oper, int direction, int orientation) {
152        JButtonOperator boper = findAButton(oper, direction);
153        DriverManager.getButtonDriver(boper).release(boper);
154    }
155
156    @Override
157    protected Point startDragging(ComponentOperator oper) {
158        JButtonOperator lessButton = findAButton(oper, ScrollAdjuster.DECREASE_SCROLL_DIRECTION);
159        JButtonOperator moreButton = findAButton(oper, ScrollAdjuster.INCREASE_SCROLL_DIRECTION);
160        Point pnt = getClickPoint((JScrollBarOperator) oper, lessButton, moreButton, ((JScrollBarOperator) oper).getValue());
161        MouseDriver mdriver = DriverManager.getMouseDriver(oper);
162        mdriver.moveMouse(oper, pnt.x, pnt.y);
163        mdriver.pressMouse(oper, pnt.x, pnt.y, Operator.getDefaultMouseButton(), 0);
164        return pnt;
165    }
166
167    @Override
168    protected void drop(ComponentOperator oper, Point pnt) {
169        DriverManager.getMouseDriver(oper).
170                releaseMouse(oper, pnt.x, pnt.y, Operator.getDefaultMouseButton(), 0);
171    }
172
173    @Override
174    protected void drag(ComponentOperator oper, Point pnt) {
175        DriverManager.getMouseDriver(oper).
176                dragMouse(oper, pnt.x, pnt.y, Operator.getDefaultMouseButton(), 0);
177    }
178
179    @Override
180    protected Timeout getScrollDeltaTimeout(ComponentOperator oper) {
181        return (oper.getTimeouts().
182                create("ScrollbarOperator.DragAndDropScrollingDelta"));
183    }
184
185    @Override
186    protected boolean canDragAndDrop(ComponentOperator oper) {
187        if (!isSmallIncrement((JScrollBarOperator) oper)) {
188            return false;
189        }
190        boolean result = false;
191        MouseDriver mdriver = DriverManager.getMouseDriver(oper);
192        JButtonOperator less = findAButton(oper, ScrollAdjuster.DECREASE_SCROLL_DIRECTION);
193        JButtonOperator more = findAButton(oper, ScrollAdjuster.INCREASE_SCROLL_DIRECTION);
194        Point pnt = getClickPoint((JScrollBarOperator) oper, less, more, ((JScrollBarOperator) oper).getValue());
195        mdriver.moveMouse(oper, pnt.x, pnt.y);
196        mdriver.pressMouse(oper, pnt.x, pnt.y, Operator.getDefaultMouseButton(), 0);
197        result = ((JScrollBarOperator) oper).getValueIsAdjusting();
198        mdriver.releaseMouse(oper, pnt.x, pnt.y, Operator.getDefaultMouseButton(), 0);
199        return result && isSmallIncrement((JScrollBarOperator) oper);
200    }
201
202    @Override
203    protected boolean canJump(ComponentOperator oper) {
204        return isSmallIncrement((JScrollBarOperator) oper);
205    }
206
207    @Override
208    protected boolean canPushAndWait(ComponentOperator oper) {
209        return isSmallIncrement((JScrollBarOperator) oper);
210    }
211
212    @Override
213    protected int getDragAndDropStepLength(ComponentOperator oper) {
214        JButtonOperator less = findAButton(oper, ScrollAdjuster.DECREASE_SCROLL_DIRECTION);
215        JButtonOperator more = findAButton(oper, ScrollAdjuster.INCREASE_SCROLL_DIRECTION);
216        int width = oper.getWidth() - less.getWidth() - more.getWidth();
217        int height = oper.getHeight() - less.getHeight() - more.getHeight();
218        int max = (width > height) ? width : height;
219        if (max >= RELATIVE_DRAG_STEP_LENGTH * 2) {
220            return max / RELATIVE_DRAG_STEP_LENGTH;
221        } else {
222            return 1;
223        }
224    }
225
226    private boolean isSmallIncrement(JScrollBarOperator oper) {
227        return (oper.getUnitIncrement(-1) <= SMALL_INCREMENT
228                && oper.getUnitIncrement(1) <= SMALL_INCREMENT);
229    }
230
231    private Point getClickPoint(JScrollBarOperator oper, JButtonOperator lessButton, JButtonOperator moreButton, int value) {
232        int lenght = (oper.getOrientation() == JScrollBar.HORIZONTAL)
233                ? oper.getWidth() - lessButton.getWidth() - moreButton.getWidth()
234                : oper.getHeight() - lessButton.getHeight() - moreButton.getHeight();
235        int subpos = (int) (((float) lenght / (oper.getMaximum() - oper.getMinimum())) * value);
236        if (oper.getOrientation() == JScrollBar.HORIZONTAL) {
237            subpos = subpos + lessButton.getWidth();
238        } else {
239            subpos = subpos + lessButton.getHeight();
240        }
241        subpos = subpos + MINIMAL_DRAGGER_SIZE / 2 + 1;
242        return ((oper.getOrientation() == JScrollBar.HORIZONTAL)
243                ? new Point(subpos, oper.getHeight() / 2)
244                : new Point(oper.getWidth() / 2, subpos));
245    }
246
247    private JButtonOperator findAButton(ComponentOperator oper, int direction) {
248        return ((direction == ScrollAdjuster.DECREASE_SCROLL_DIRECTION)
249                ? ((JScrollBarOperator) oper).getDecreaseButton()
250                : ((JScrollBarOperator) oper).getIncreaseButton());
251    }
252}
253