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. Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package org.netbeans.jemmy.drivers.scrolling;
26 
27 import java.awt.Point;
28 
29 import org.netbeans.jemmy.Timeout;
30 import org.netbeans.jemmy.operators.ComponentOperator;
31 import org.netbeans.jemmy.operators.JScrollBarOperator;
32 
33 /**
34  * ScrollDriver for javax.swing.JScrollBar component type.
35  *
36  * @author Alexandre Iline(alexandre.iline@oracle.com)
37  */
38 public class JScrollBarAPIDriver extends AbstractScrollDriver {
39 
40     private final static int SMALL_INCREMENT = 1;
41 
42     /**
43      * Constructs a JScrollBarDriver.
44      */
JScrollBarAPIDriver()45     public JScrollBarAPIDriver() {
46         super(new String[]{"org.netbeans.jemmy.operators.JScrollBarOperator"});
47     }
48 
49     @Override
position(ComponentOperator oper, int orientation)50     protected int position(ComponentOperator oper, int orientation) {
51         return ((JScrollBarOperator) oper).getValue();
52     }
53 
54     @Override
scrollToMinimum(ComponentOperator oper, int orientation)55     public void scrollToMinimum(ComponentOperator oper, int orientation) {
56         JScrollBarOperator scroll = (JScrollBarOperator) oper;
57         setValue(oper, scroll.getMinimum());
58     }
59 
60     @Override
scrollToMaximum(ComponentOperator oper, int orientation)61     public void scrollToMaximum(ComponentOperator oper, int orientation) {
62         JScrollBarOperator scroll = (JScrollBarOperator) oper;
63         setValue(oper, scroll.getMaximum() - scroll.getVisibleAmount());
64     }
65 
66     @Override
step(ComponentOperator oper, ScrollAdjuster adj)67     protected void step(ComponentOperator oper, ScrollAdjuster adj) {
68         JScrollBarOperator scroll = (JScrollBarOperator) oper;
69         int newValue = -1;
70         if (adj.getScrollDirection() == ScrollAdjuster.DECREASE_SCROLL_DIRECTION) {
71             newValue = (scroll.getValue() > scroll.getMinimum()
72                     + scroll.getUnitIncrement())
73                             ? scroll.getValue() - scroll.getUnitIncrement()
74                             : scroll.getMinimum();
75         } else if (adj.getScrollDirection() == ScrollAdjuster.INCREASE_SCROLL_DIRECTION) {
76             newValue = (scroll.getValue() < scroll.getMaximum()
77                     - scroll.getVisibleAmount() - scroll.getUnitIncrement())
78                             ? scroll.getValue() + scroll.getUnitIncrement()
79                             : scroll.getMaximum();
80         }
81         setValue(oper, newValue);
82     }
83 
setValue(ComponentOperator oper, int value)84     private void setValue(ComponentOperator oper, int value) {
85         if (value != -1) {
86             ((JScrollBarOperator) oper).setValue(value);
87         }
88     }
89 
90     @Override
getScrollDeltaTimeout(ComponentOperator oper)91     protected Timeout getScrollDeltaTimeout(ComponentOperator oper) {
92         return (oper.getTimeouts().
93                 create("JScrollBarOperator.DragAndDropScrollingDelta"));
94     }
95 
96     @Override
jump(final ComponentOperator oper, final ScrollAdjuster adj)97     protected void jump(final ComponentOperator oper, final ScrollAdjuster adj) {
98         JScrollBarOperator scroll = (JScrollBarOperator) oper;
99         int newValue = -1;
100         if (adj.getScrollDirection() == ScrollAdjuster.DECREASE_SCROLL_DIRECTION) {
101             newValue = (scroll.getValue() > scroll.getMinimum()
102                     + scroll.getBlockIncrement())
103                             ? scroll.getValue() - scroll.getBlockIncrement()
104                             : scroll.getMinimum();
105         } else if (adj.getScrollDirection() == ScrollAdjuster.INCREASE_SCROLL_DIRECTION) {
106             newValue = (scroll.getValue() < scroll.getMaximum()
107                     - scroll.getVisibleAmount() - scroll.getBlockIncrement())
108                             ? scroll.getValue() + scroll.getBlockIncrement()
109                             : scroll.getMaximum();
110         }
111         setValue(oper, newValue);
112     }
113 
114     @Override
startPushAndWait(ComponentOperator oper, int direction, int orientation)115     protected void startPushAndWait(ComponentOperator oper, int direction, int orientation) {
116     }
117 
118     @Override
stopPushAndWait(ComponentOperator oper, int direction, int orientation)119     protected void stopPushAndWait(ComponentOperator oper, int direction, int orientation) {
120     }
121 
122     @Override
startDragging(ComponentOperator oper)123     protected Point startDragging(ComponentOperator oper) {
124         return null;
125     }
126 
127     @Override
drop(ComponentOperator oper, Point pnt)128     protected void drop(ComponentOperator oper, Point pnt) {
129     }
130 
131     @Override
drag(ComponentOperator oper, Point pnt)132     protected void drag(ComponentOperator oper, Point pnt) {
133     }
134 
135     @Override
canDragAndDrop(ComponentOperator oper)136     protected boolean canDragAndDrop(ComponentOperator oper) {
137         return false;
138     }
139 
140     @Override
canJump(ComponentOperator oper)141     protected boolean canJump(ComponentOperator oper) {
142         return isSmallIncrement((JScrollBarOperator) oper);
143     }
144 
145     @Override
canPushAndWait(ComponentOperator oper)146     protected boolean canPushAndWait(ComponentOperator oper) {
147         return false;
148     }
149 
150     @Override
getDragAndDropStepLength(ComponentOperator oper)151     protected int getDragAndDropStepLength(ComponentOperator oper) {
152         return 1;
153     }
154 
isSmallIncrement(JScrollBarOperator oper)155     private boolean isSmallIncrement(JScrollBarOperator oper) {
156         return (oper.getUnitIncrement(-1) <= SMALL_INCREMENT
157                 && oper.getUnitIncrement(1) <= SMALL_INCREMENT);
158     }
159 
160 }
161