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;
26import java.awt.Scrollbar;
27
28import org.netbeans.jemmy.operators.ComponentOperator;
29import org.netbeans.jemmy.operators.ScrollbarOperator;
30
31/**
32 * ScrollDriver for java.awt.Scrollbar component type.
33 *
34 * @author Alexandre Iline(alexandre.iline@oracle.com)
35 */
36public class ScrollbarDriver extends AWTScrollDriver {
37
38    private static final int CLICK_OFFSET = 5;
39
40    /**
41     * Constructs a ScrollbarDriver.
42     */
43    public ScrollbarDriver() {
44        super(new String[]{"org.netbeans.jemmy.operators.ScrollbarOperator"});
45    }
46
47    @Override
48    protected int position(ComponentOperator oper, int orientation) {
49        return ((ScrollbarOperator) oper).getValue();
50    }
51
52    @Override
53    public void scrollToMinimum(final ComponentOperator oper, final int orientation) {
54        scroll(oper,
55                new ScrollAdjuster() {
56            @Override
57            public int getScrollDirection() {
58                return ((((ScrollbarOperator) oper).getMinimum()
59                        < ((ScrollbarOperator) oper).getValue())
60                                ? DECREASE_SCROLL_DIRECTION
61                                : DO_NOT_TOUCH_SCROLL_DIRECTION);
62            }
63
64            @Override
65            public int getScrollOrientation() {
66                return ((ScrollbarOperator) oper).getOrientation();
67            }
68
69            @Override
70            public String getDescription() {
71                return "Scroll to minimum";
72            }
73
74            @Override
75            public String toString() {
76                return "scrollToMinimum.ScrollAdjuster{description = " + getDescription() + '}';
77            }
78        });
79    }
80
81    @Override
82    public void scrollToMaximum(final ComponentOperator oper, final int orientation) {
83        scroll(oper,
84                new ScrollAdjuster() {
85            @Override
86            public int getScrollDirection() {
87                return (((((ScrollbarOperator) oper).getMaximum()
88                        - ((ScrollbarOperator) oper).getVisibleAmount())
89                        > ((ScrollbarOperator) oper).getValue())
90                                ? INCREASE_SCROLL_DIRECTION
91                                : DO_NOT_TOUCH_SCROLL_DIRECTION);
92            }
93
94            @Override
95            public int getScrollOrientation() {
96                return ((ScrollbarOperator) oper).getOrientation();
97            }
98
99            @Override
100            public String getDescription() {
101                return "Scroll to maximum";
102            }
103
104            @Override
105            public String toString() {
106                return "scrollToMaximum.ScrollAdjuster{description = " + getDescription() + '}';
107            }
108        });
109    }
110
111    @Override
112    protected Point getClickPoint(ComponentOperator oper, int direction, int orientation) {
113        int x, y;
114        if (orientation == Scrollbar.HORIZONTAL) {
115            if (direction == ScrollAdjuster.INCREASE_SCROLL_DIRECTION) {
116                x = oper.getWidth() - 1 - CLICK_OFFSET;
117            } else if (direction == ScrollAdjuster.DECREASE_SCROLL_DIRECTION) {
118                x = CLICK_OFFSET;
119            } else {
120                return null;
121            }
122            y = oper.getHeight() / 2;
123        } else if (orientation == Scrollbar.VERTICAL) {
124            if (direction == ScrollAdjuster.INCREASE_SCROLL_DIRECTION) {
125                y = oper.getHeight() - 1 - CLICK_OFFSET;
126            } else if (direction == ScrollAdjuster.DECREASE_SCROLL_DIRECTION) {
127                y = CLICK_OFFSET;
128            } else {
129                return null;
130            }
131            x = oper.getWidth() / 2;
132        } else {
133            return null;
134        }
135        return new Point(x, y);
136    }
137}
138