ChoiceDriver.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.lists; 24 25import java.awt.Point; 26import java.awt.event.KeyEvent; 27 28import org.netbeans.jemmy.Timeout; 29import org.netbeans.jemmy.drivers.DriverManager; 30import org.netbeans.jemmy.drivers.KeyDriver; 31import org.netbeans.jemmy.drivers.LightSupportiveDriver; 32import org.netbeans.jemmy.drivers.ListDriver; 33import org.netbeans.jemmy.operators.ChoiceOperator; 34import org.netbeans.jemmy.operators.ComponentOperator; 35import org.netbeans.jemmy.operators.Operator; 36 37/** 38 * List driver for java.awt.Choice component type. 39 * 40 * @author Alexandre Iline(alexandre.iline@oracle.com) 41 */ 42public class ChoiceDriver extends LightSupportiveDriver implements ListDriver { 43 44 private final static int RIGHT_INDENT = 10; 45 46 /** 47 * Constructs a ChoiceDriver. 48 */ 49 public ChoiceDriver() { 50 super(new String[]{"org.netbeans.jemmy.operators.ChoiceOperator"}); 51 } 52 53 @Override 54 public void selectItem(ComponentOperator oper, int index) { 55 ChoiceOperator coper = (ChoiceOperator) oper; 56 Point pointToClick = getClickPoint(oper); 57 DriverManager.getMouseDriver(oper). 58 clickMouse(oper, pointToClick.x, pointToClick.y, 59 1, Operator.getDefaultMouseButton(), 0, 60 oper.getTimeouts().create("ComponentOperator.MouseClickTimeout")); 61 KeyDriver kdriver = DriverManager.getKeyDriver(oper); 62 Timeout pushTimeout = oper.getTimeouts().create("ComponentOperator.PushKeyTimeout"); 63 if (System.getProperty("java.specification.version").compareTo("1.3") > 0) { 64 while (coper.getSelectedIndex() != index) { 65 kdriver.pushKey(oper, (index > coper.getSelectedIndex()) ? KeyEvent.VK_DOWN : KeyEvent.VK_UP, 0, pushTimeout); 66 } 67 } else { 68 int current = ((ChoiceOperator) oper).getSelectedIndex(); 69 int diff = 0; 70 int key = 0; 71 if (index > current) { 72 diff = index - current; 73 key = KeyEvent.VK_DOWN; 74 } else { 75 diff = current - index; 76 key = KeyEvent.VK_UP; 77 } 78 for (int i = 0; i < diff; i++) { 79 kdriver.pushKey(oper, key, 0, pushTimeout); 80 } 81 } 82 kdriver.pushKey(oper, KeyEvent.VK_ENTER, 0, pushTimeout); 83 } 84 85 private Point getClickPoint(ComponentOperator oper) { 86 return new Point(oper.getWidth() - RIGHT_INDENT, oper.getHeight() / 2); 87 } 88} 89