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