JTreeMouseDriver.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.trees; 24 25import java.awt.Point; 26import java.awt.event.InputEvent; 27import java.awt.event.KeyEvent; 28 29import javax.swing.text.JTextComponent; 30 31import org.netbeans.jemmy.QueueTool; 32import org.netbeans.jemmy.Timeout; 33import org.netbeans.jemmy.drivers.DriverManager; 34import org.netbeans.jemmy.drivers.LightSupportiveDriver; 35import org.netbeans.jemmy.drivers.MouseDriver; 36import org.netbeans.jemmy.drivers.TextDriver; 37import org.netbeans.jemmy.drivers.TreeDriver; 38import org.netbeans.jemmy.operators.ComponentOperator; 39import org.netbeans.jemmy.operators.JTextComponentOperator; 40import org.netbeans.jemmy.operators.JTreeOperator; 41import org.netbeans.jemmy.operators.Operator; 42 43/** 44 * TreeDriver for javax.swing.JTree component type. Uses mouse operations. 45 * 46 * @author Alexandre Iline(alexandre.iline@oracle.com) 47 */ 48public class JTreeMouseDriver extends LightSupportiveDriver implements TreeDriver { 49 50 QueueTool queueTool; 51 52 /** 53 * Constructs a JTreeMouseDriver. 54 */ 55 public JTreeMouseDriver() { 56 super(new String[]{"org.netbeans.jemmy.operators.JTreeOperator"}); 57 queueTool = new QueueTool(); 58 } 59 60 @Override 61 public void selectItem(ComponentOperator oper, int index) { 62 selectItems(oper, new int[]{index}); 63 } 64 65 @Override 66 public void selectItems(final ComponentOperator oper, int[] indices) { 67 ((JTreeOperator) oper).clearSelection(); 68 checkSupported(oper); 69 final MouseDriver mdriver = DriverManager.getMouseDriver(oper); 70 final JTreeOperator toper = (JTreeOperator) oper; 71 final Timeout clickTime = oper.getTimeouts().create("ComponentOperator.MouseClickTimeout"); 72 for (int i = 0; i < indices.length; i++) { 73 final int index = i; 74 if (!QueueTool.isDispatchThread()) { 75 toper.scrollToRow(indices[i]); 76 } 77 final Point p = toper.getPointToClick(indices[index]); 78 queueTool.invokeSmoothly(new QueueTool.QueueAction<Void>("Path selecting") { 79 @Override 80 public Void launch() { 81 mdriver.clickMouse(oper, p.x, p.y, 1, Operator.getDefaultMouseButton(), 82 (index == 0) ? 0 : InputEvent.CTRL_MASK, clickTime); 83 return null; 84 } 85 }); 86 } 87 //1.5 workaround 88 if (System.getProperty("java.specification.version").compareTo("1.4") > 0) { 89 if (!QueueTool.isDispatchThread()) { 90 queueTool.setOutput(oper.getOutput().createErrorOutput()); 91 queueTool.waitEmpty(10); 92 queueTool.waitEmpty(10); 93 queueTool.waitEmpty(10); 94 } 95 } 96 //end of 1.5 workaround 97 } 98 99 @Override 100 public void expandItem(ComponentOperator oper, final int index) { 101 checkSupported(oper); 102 final JTreeOperator toper = (JTreeOperator) oper; 103 final MouseDriver mdriver = DriverManager.getMouseDriver(oper); 104 if (!toper.isExpanded(index)) { 105 queueTool.invokeSmoothly(new QueueTool.QueueAction<Void>("Path selecting") { 106 @Override 107 public Void launch() { 108 Point p = toper.getPointToClick(index); 109 mdriver.clickMouse(toper, p.x, p.y, 2, Operator.getDefaultMouseButton(), 110 0, toper.getTimeouts(). 111 create("ComponentOperator.MouseClickTimeout")); 112 return null; 113 } 114 }); 115 } 116 } 117 118 @Override 119 public void collapseItem(ComponentOperator oper, final int index) { 120 checkSupported(oper); 121 final JTreeOperator toper = (JTreeOperator) oper; 122 final MouseDriver mdriver = DriverManager.getMouseDriver(oper); 123 if (toper.isExpanded(index)) { 124 queueTool.invokeSmoothly(new QueueTool.QueueAction<Void>("Path selecting") { 125 @Override 126 public Void launch() { 127 Point p = toper.getPointToClick(index); 128 mdriver.clickMouse(toper, p.x, p.y, 2, Operator.getDefaultMouseButton(), 129 0, toper.getTimeouts(). 130 create("ComponentOperator.MouseClickTimeout")); 131 return null; 132 } 133 }); 134 } 135 } 136 137 @Override 138 public void editItem(ComponentOperator oper, int index, Object newValue, Timeout waitEditorTime) { 139 JTextComponentOperator textoper = startEditingAndReturnEditor(oper, index, waitEditorTime); 140 final TextDriver text = DriverManager.getTextDriver(JTextComponentOperator.class); 141 text.clearText(textoper); 142 text.typeText(textoper, newValue.toString(), 0); 143 DriverManager.getKeyDriver(oper). 144 pushKey(textoper, KeyEvent.VK_ENTER, 0, 145 oper.getTimeouts(). 146 create("ComponentOperator.PushKeyTimeout")); 147 } 148 149 @Override 150 public void startEditing(ComponentOperator oper, int index, Timeout waitEditorTime) { 151 startEditingAndReturnEditor(oper, index, waitEditorTime); 152 } 153 154 private JTextComponentOperator startEditingAndReturnEditor(ComponentOperator oper, final int index, Timeout waitEditorTime) { 155 checkSupported(oper); 156 final JTreeOperator toper = (JTreeOperator) oper; 157 final MouseDriver mdriver = DriverManager.getMouseDriver(oper); 158 queueTool.invokeSmoothly(new QueueTool.QueueAction<Void>("Path selecting") { 159 @Override 160 public Void launch() { 161 Point p = toper.getPointToClick(index); 162 mdriver.clickMouse(toper, p.x, p.y, 1, Operator.getDefaultMouseButton(), 163 0, toper.getTimeouts(). 164 create("ComponentOperator.MouseClickTimeout")); 165 return null; 166 } 167 }); 168 oper.getTimeouts().sleep("JTreeOperator.BeforeEditTimeout"); 169 queueTool.invokeSmoothly(new QueueTool.QueueAction<Void>("Path selecting") { 170 @Override 171 public Void launch() { 172 Point p = toper.getPointToClick(index); 173 mdriver.clickMouse(toper, p.x, p.y, 1, Operator.getDefaultMouseButton(), 174 0, toper.getTimeouts(). 175 create("ComponentOperator.MouseClickTimeout")); 176 return null; 177 } 178 }); 179 toper.getTimeouts(). 180 setTimeout("ComponentOperator.WaitComponentTimeout", waitEditorTime.getValue()); 181 return (new JTextComponentOperator((JTextComponent) toper. 182 waitSubComponent(new JTextComponentOperator.JTextComponentFinder()))); 183 } 184} 185