JSliderDriver.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 javax.swing.JSlider; 28 29import org.netbeans.jemmy.QueueTool; 30import org.netbeans.jemmy.Timeout; 31import org.netbeans.jemmy.drivers.DriverManager; 32import org.netbeans.jemmy.drivers.MouseDriver; 33import org.netbeans.jemmy.operators.ComponentOperator; 34import org.netbeans.jemmy.operators.JSliderOperator; 35import org.netbeans.jemmy.operators.Operator; 36 37/** 38 * A scroll driver serving JSlider component. 39 * 40 * @author Alexandre Iline(alexandre.iline@oracle.com) 41 */ 42public class JSliderDriver extends AbstractScrollDriver { 43 44 private QueueTool queueTool; 45 46 /** 47 * Constructs a JSliderDriver object. 48 */ 49 public JSliderDriver() { 50 super(new String[]{"org.netbeans.jemmy.operators.JSliderOperator"}); 51 queueTool = new QueueTool(); 52 } 53 54 @Override 55 protected int position(ComponentOperator oper, int orientation) { 56 return ((JSliderOperator) oper).getValue(); 57 } 58 59 @Override 60 public void scrollToMinimum(final ComponentOperator oper, int orientation) { 61 checkSupported(oper); 62 scroll(oper, 63 new ScrollAdjuster() { 64 @Override 65 public int getScrollDirection() { 66 return ((((JSliderOperator) oper).getMinimum() 67 < ((JSliderOperator) oper).getValue()) 68 ? DECREASE_SCROLL_DIRECTION 69 : DO_NOT_TOUCH_SCROLL_DIRECTION); 70 } 71 72 @Override 73 public int getScrollOrientation() { 74 return ((JSliderOperator) oper).getOrientation(); 75 } 76 77 @Override 78 public String getDescription() { 79 return "Scroll to minimum"; 80 } 81 82 @Override 83 public String toString() { 84 return "scrollToMinimum.ScrollAdjuster{description = " + getDescription() + '}'; 85 } 86 }); 87 } 88 89 @Override 90 public void scrollToMaximum(final ComponentOperator oper, int orientation) { 91 checkSupported(oper); 92 scroll(oper, 93 new ScrollAdjuster() { 94 @Override 95 public int getScrollDirection() { 96 return ((((JSliderOperator) oper).getMaximum() 97 > ((JSliderOperator) oper).getValue()) 98 ? INCREASE_SCROLL_DIRECTION 99 : DO_NOT_TOUCH_SCROLL_DIRECTION); 100 } 101 102 @Override 103 public int getScrollOrientation() { 104 return ((JSliderOperator) oper).getOrientation(); 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 void step(final ComponentOperator oper, final ScrollAdjuster adj) { 121 if (adj.getScrollDirection() != ScrollAdjuster.DO_NOT_TOUCH_SCROLL_DIRECTION) { 122 queueTool.invokeSmoothly(new QueueTool.QueueAction<Void>("Scrolling by clicking with the mouse") { 123 @Override 124 public Void launch() { 125 Point clickPoint = getClickPoint(oper, adj.getScrollDirection(), adj.getScrollOrientation()); 126 if (clickPoint != null) { 127 DriverManager.getMouseDriver(oper). 128 clickMouse(oper, clickPoint.x, clickPoint.y, 1, 129 Operator.getDefaultMouseButton(), 130 0, 131 oper.getTimeouts(). 132 create("ComponentOperator.MouseClickTimeout")); 133 } 134 return null; 135 } 136 }); 137 } 138 } 139 140 @Override 141 protected void jump(ComponentOperator oper, ScrollAdjuster adj) { 142 //cannot 143 } 144 145 @Override 146 protected void startPushAndWait(final ComponentOperator oper, final int direction, final int orientation) { 147 queueTool.invokeSmoothly(new QueueTool.QueueAction<Void>("Start scrolling") { 148 @Override 149 public Void launch() { 150 Point clickPoint = getClickPoint(oper, direction, orientation); 151 if (clickPoint != null) { 152 MouseDriver mdriver = DriverManager.getMouseDriver(oper); 153 mdriver.moveMouse(oper, clickPoint.x, clickPoint.y); 154 mdriver.pressMouse(oper, clickPoint.x, clickPoint.y, 155 Operator.getDefaultMouseButton(), 156 0); 157 } 158 return null; 159 } 160 }); 161 } 162 163 @Override 164 protected void stopPushAndWait(final ComponentOperator oper, final int direction, final int orientation) { 165 queueTool.invokeSmoothly(new QueueTool.QueueAction<Void>("Stop scrolling") { 166 @Override 167 public Void launch() { 168 Point clickPoint = getClickPoint(oper, direction, orientation); 169 if (clickPoint != null) { 170 MouseDriver mdriver = DriverManager.getMouseDriver(oper); 171 mdriver.releaseMouse(oper, clickPoint.x, clickPoint.y, 172 Operator.getDefaultMouseButton(), 173 0); 174 } 175 return null; 176 } 177 }); 178 } 179 180 @Override 181 protected Point startDragging(ComponentOperator oper) { 182 //cannot 183 return null; 184 } 185 186 @Override 187 protected void drop(ComponentOperator oper, Point pnt) { 188 //cannot 189 } 190 191 @Override 192 protected void drag(ComponentOperator oper, Point pnt) { 193 //cannot 194 } 195 196 @Override 197 protected Timeout getScrollDeltaTimeout(ComponentOperator oper) { 198 return oper.getTimeouts().create("JSliderOperator.ScrollingDelta"); 199 } 200 201 @Override 202 protected boolean canDragAndDrop(ComponentOperator oper) { 203 return false; 204 } 205 206 @Override 207 protected boolean canJump(ComponentOperator oper) { 208 return false; 209 } 210 211 @Override 212 protected boolean canPushAndWait(ComponentOperator oper) { 213 return true; 214 } 215 216 @Override 217 protected int getDragAndDropStepLength(ComponentOperator oper) { 218 return 0; 219 } 220 221 private Point getClickPoint(ComponentOperator oper, int direction, int orientation) { 222 int x, y; 223 boolean inverted = ((JSliderOperator) oper).getInverted(); 224 int realDirection = ScrollAdjuster.DO_NOT_TOUCH_SCROLL_DIRECTION; 225 if (inverted) { 226 if (direction == ScrollAdjuster.INCREASE_SCROLL_DIRECTION) { 227 realDirection = ScrollAdjuster.DECREASE_SCROLL_DIRECTION; 228 } else if (direction == ScrollAdjuster.DECREASE_SCROLL_DIRECTION) { 229 realDirection = ScrollAdjuster.INCREASE_SCROLL_DIRECTION; 230 } else { 231 return null; 232 } 233 } else { 234 realDirection = direction; 235 } 236 if (orientation == JSlider.HORIZONTAL) { 237 if (realDirection == ScrollAdjuster.INCREASE_SCROLL_DIRECTION) { 238 x = oper.getWidth() - 1; 239 } else if (realDirection == ScrollAdjuster.DECREASE_SCROLL_DIRECTION) { 240 x = 0; 241 } else { 242 return null; 243 } 244 y = oper.getHeight() / 2; 245 } else if (orientation == JSlider.VERTICAL) { 246 if (realDirection == ScrollAdjuster.INCREASE_SCROLL_DIRECTION) { 247 y = 0; 248 } else if (realDirection == ScrollAdjuster.DECREASE_SCROLL_DIRECTION) { 249 y = oper.getHeight() - 1; 250 } else { 251 return null; 252 } 253 x = oper.getWidth() / 2; 254 } else { 255 return null; 256 } 257 return new Point(x, y); 258 } 259} 260