JSpinnerDriver.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 javax.swing.SwingConstants;
26
27import org.netbeans.jemmy.drivers.LightSupportiveDriver;
28import org.netbeans.jemmy.drivers.ScrollDriver;
29import org.netbeans.jemmy.operators.ComponentOperator;
30import org.netbeans.jemmy.operators.JButtonOperator;
31import org.netbeans.jemmy.operators.JSpinnerOperator;
32
33/**
34 * A scroll driver serving JSpinner component.
35 *
36 * @author Alexandre Iline(alexandre.iline@oracle.com)
37 */
38public class JSpinnerDriver extends LightSupportiveDriver implements ScrollDriver {
39
40    /**
41     * Constructs a JSpinnerDriver object.
42     */
43    public JSpinnerDriver() {
44        super(new String[]{"org.netbeans.jemmy.operators.JSpinnerOperator"});
45    }
46
47    @Override
48    public void scrollToMinimum(final ComponentOperator oper, int orientation) {
49        Object minimum = ((JSpinnerOperator) oper).getMinimum();
50        if (minimum == null) {
51            throw (new JSpinnerOperator.SpinnerModelException("Impossible to get a minimum of JSpinner model.", oper.getSource()));
52        }
53        scroll(oper, new ScrollAdjuster() {
54            @Override
55            public int getScrollOrientation() {
56                return SwingConstants.VERTICAL;
57            }
58
59            @Override
60            public String getDescription() {
61                return "Spin to minimum";
62            }
63
64            @Override
65            public int getScrollDirection() {
66                if (((JSpinnerOperator) oper).getModel().getPreviousValue() != null) {
67                    return ScrollAdjuster.DECREASE_SCROLL_DIRECTION;
68                } else {
69                    return ScrollAdjuster.DO_NOT_TOUCH_SCROLL_DIRECTION;
70                }
71            }
72
73            @Override
74            public String toString() {
75                return "scrollToMinimum.ScrollAdjuster{description = " + getDescription() + '}';
76            }
77        });
78    }
79
80    @Override
81    public void scrollToMaximum(final ComponentOperator oper, int orientation) {
82        Object maximum = ((JSpinnerOperator) oper).getMaximum();
83        if (maximum == null) {
84            throw (new JSpinnerOperator.SpinnerModelException("Impossible to get a maximum of JSpinner model.", oper.getSource()));
85        }
86        scroll(oper, new ScrollAdjuster() {
87            @Override
88            public int getScrollOrientation() {
89                return SwingConstants.VERTICAL;
90            }
91
92            @Override
93            public String getDescription() {
94                return "Spin to maximum";
95            }
96
97            @Override
98            public int getScrollDirection() {
99                if (((JSpinnerOperator) oper).getModel().getNextValue() != null) {
100                    return ScrollAdjuster.INCREASE_SCROLL_DIRECTION;
101                } else {
102                    return ScrollAdjuster.DO_NOT_TOUCH_SCROLL_DIRECTION;
103                }
104            }
105
106            @Override
107            public String toString() {
108                return "scrollToMaximum.ScrollAdjuster{description = " + getDescription() + '}';
109            }
110        });
111    }
112
113    @Override
114    public void scroll(ComponentOperator oper, ScrollAdjuster adj) {
115        JButtonOperator increaseButton = ((JSpinnerOperator) oper).getIncreaseOperator();
116        JButtonOperator decreaseButton = ((JSpinnerOperator) oper).getDecreaseOperator();
117        if (adj.getScrollDirection() == ScrollAdjuster.DO_NOT_TOUCH_SCROLL_DIRECTION) {
118            return;
119        }
120        int originalDirection = adj.getScrollDirection();
121        while (adj.getScrollDirection() == originalDirection) {
122            if (originalDirection == ScrollAdjuster.INCREASE_SCROLL_DIRECTION) {
123                increaseButton.push();
124            } else {
125                decreaseButton.push();
126            }
127        }
128    }
129}
130