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