JScrollBarAPIDriver.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 org.netbeans.jemmy.Timeout;
28import org.netbeans.jemmy.operators.ComponentOperator;
29import org.netbeans.jemmy.operators.JScrollBarOperator;
30
31/**
32 * ScrollDriver for javax.swing.JScrollBar component type.
33 *
34 * @author Alexandre Iline(alexandre.iline@oracle.com)
35 */
36public class JScrollBarAPIDriver extends AbstractScrollDriver {
37
38    private final static int SMALL_INCREMENT = 1;
39
40    /**
41     * Constructs a JScrollBarDriver.
42     */
43    public JScrollBarAPIDriver() {
44        super(new String[]{"org.netbeans.jemmy.operators.JScrollBarOperator"});
45    }
46
47    @Override
48    protected int position(ComponentOperator oper, int orientation) {
49        return ((JScrollBarOperator) oper).getValue();
50    }
51
52    @Override
53    public void scrollToMinimum(ComponentOperator oper, int orientation) {
54        JScrollBarOperator scroll = (JScrollBarOperator) oper;
55        setValue(oper, scroll.getMinimum());
56    }
57
58    @Override
59    public void scrollToMaximum(ComponentOperator oper, int orientation) {
60        JScrollBarOperator scroll = (JScrollBarOperator) oper;
61        setValue(oper, scroll.getMaximum() - scroll.getVisibleAmount());
62    }
63
64    @Override
65    protected void step(ComponentOperator oper, ScrollAdjuster adj) {
66        JScrollBarOperator scroll = (JScrollBarOperator) oper;
67        int newValue = -1;
68        if (adj.getScrollDirection() == ScrollAdjuster.DECREASE_SCROLL_DIRECTION) {
69            newValue = (scroll.getValue() > scroll.getMinimum()
70                    + scroll.getUnitIncrement())
71                            ? scroll.getValue() - scroll.getUnitIncrement()
72                            : scroll.getMinimum();
73        } else if (adj.getScrollDirection() == ScrollAdjuster.INCREASE_SCROLL_DIRECTION) {
74            newValue = (scroll.getValue() < scroll.getMaximum()
75                    - scroll.getVisibleAmount() - scroll.getUnitIncrement())
76                            ? scroll.getValue() + scroll.getUnitIncrement()
77                            : scroll.getMaximum();
78        }
79        setValue(oper, newValue);
80    }
81
82    private void setValue(ComponentOperator oper, int value) {
83        if (value != -1) {
84            ((JScrollBarOperator) oper).setValue(value);
85        }
86    }
87
88    @Override
89    protected Timeout getScrollDeltaTimeout(ComponentOperator oper) {
90        return (oper.getTimeouts().
91                create("JScrollBarOperator.DragAndDropScrollingDelta"));
92    }
93
94    @Override
95    protected void jump(final ComponentOperator oper, final ScrollAdjuster adj) {
96        JScrollBarOperator scroll = (JScrollBarOperator) oper;
97        int newValue = -1;
98        if (adj.getScrollDirection() == ScrollAdjuster.DECREASE_SCROLL_DIRECTION) {
99            newValue = (scroll.getValue() > scroll.getMinimum()
100                    + scroll.getBlockIncrement())
101                            ? scroll.getValue() - scroll.getBlockIncrement()
102                            : scroll.getMinimum();
103        } else if (adj.getScrollDirection() == ScrollAdjuster.INCREASE_SCROLL_DIRECTION) {
104            newValue = (scroll.getValue() < scroll.getMaximum()
105                    - scroll.getVisibleAmount() - scroll.getBlockIncrement())
106                            ? scroll.getValue() + scroll.getBlockIncrement()
107                            : scroll.getMaximum();
108        }
109        setValue(oper, newValue);
110    }
111
112    @Override
113    protected void startPushAndWait(ComponentOperator oper, int direction, int orientation) {
114    }
115
116    @Override
117    protected void stopPushAndWait(ComponentOperator oper, int direction, int orientation) {
118    }
119
120    @Override
121    protected Point startDragging(ComponentOperator oper) {
122        return null;
123    }
124
125    @Override
126    protected void drop(ComponentOperator oper, Point pnt) {
127    }
128
129    @Override
130    protected void drag(ComponentOperator oper, Point pnt) {
131    }
132
133    @Override
134    protected boolean canDragAndDrop(ComponentOperator oper) {
135        return false;
136    }
137
138    @Override
139    protected boolean canJump(ComponentOperator oper) {
140        return isSmallIncrement((JScrollBarOperator) oper);
141    }
142
143    @Override
144    protected boolean canPushAndWait(ComponentOperator oper) {
145        return false;
146    }
147
148    @Override
149    protected int getDragAndDropStepLength(ComponentOperator oper) {
150        return 1;
151    }
152
153    private boolean isSmallIncrement(JScrollBarOperator oper) {
154        return (oper.getUnitIncrement(-1) <= SMALL_INCREMENT
155                && oper.getUnitIncrement(1) <= SMALL_INCREMENT);
156    }
157
158}
159