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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package org.netbeans.jemmy.drivers.text; 26 27 import java.awt.event.KeyEvent; 28 29 import org.netbeans.jemmy.Timeout; 30 import org.netbeans.jemmy.drivers.DriverManager; 31 import org.netbeans.jemmy.drivers.LightSupportiveDriver; 32 import org.netbeans.jemmy.drivers.TextDriver; 33 import org.netbeans.jemmy.operators.ComponentOperator; 34 import org.netbeans.jemmy.operators.JTextComponentOperator; 35 import org.netbeans.jemmy.operators.TextComponentOperator; 36 37 /** 38 * Superclass for all TextDrivers using API calls. 39 * 40 * @author Alexandre Iline(alexandre.iline@oracle.com) 41 */ 42 public abstract class TextAPIDriver extends LightSupportiveDriver implements TextDriver { 43 44 /** 45 * Constructs a ChoiceDriver. 46 * 47 * @param supported an array of supported class names 48 */ TextAPIDriver(String[] supported)49 public TextAPIDriver(String[] supported) { 50 super(supported); 51 } 52 53 @Override changeCaretPosition(ComponentOperator oper, int position)54 public void changeCaretPosition(ComponentOperator oper, int position) { 55 checkSupported(oper); 56 if (oper instanceof TextComponentOperator) { 57 ((TextComponentOperator) oper).setCaretPosition(position); 58 } else { 59 ((JTextComponentOperator) oper).setCaretPosition(position); 60 } 61 } 62 63 @Override selectText(ComponentOperator oper, int startPosition, int finalPosition)64 public void selectText(ComponentOperator oper, int startPosition, int finalPosition) { 65 checkSupported(oper); 66 int start = (startPosition < finalPosition) ? startPosition : finalPosition; 67 int end = (startPosition > finalPosition) ? startPosition : finalPosition; 68 if (oper instanceof TextComponentOperator) { 69 TextComponentOperator toper = ((TextComponentOperator) oper); 70 toper.setSelectionStart(start); 71 toper.setSelectionEnd(end); 72 } else { 73 JTextComponentOperator toper = ((JTextComponentOperator) oper); 74 toper.setSelectionStart(start); 75 toper.setSelectionEnd(end); 76 } 77 } 78 79 @Override clearText(ComponentOperator oper)80 public void clearText(ComponentOperator oper) { 81 if (oper instanceof TextComponentOperator) { 82 ((TextComponentOperator) oper).setText(""); 83 } else { 84 ((JTextComponentOperator) oper).setText(""); 85 } 86 } 87 88 @Override typeText(ComponentOperator oper, String text, int caretPosition)89 public void typeText(ComponentOperator oper, String text, int caretPosition) { 90 checkSupported(oper); 91 String curtext = getText(oper); 92 int realPos = caretPosition; 93 if (getSelectionStart(oper) == realPos 94 || getSelectionEnd(oper) == realPos) { 95 if (getSelectionEnd(oper) == realPos) { 96 realPos = realPos - (getSelectionEnd(oper) - getSelectionStart(oper)); 97 } 98 curtext 99 = curtext.substring(0, getSelectionStart(oper)) 100 + curtext.substring(getSelectionEnd(oper)); 101 } 102 changeText(oper, 103 curtext.substring(0, realPos) + text 104 + curtext.substring(realPos)); 105 } 106 107 @Override changeText(ComponentOperator oper, String text)108 public void changeText(ComponentOperator oper, String text) { 109 checkSupported(oper); 110 if (oper instanceof TextComponentOperator) { 111 ((TextComponentOperator) oper).setText(text); 112 } else { 113 ((JTextComponentOperator) oper).setText(text); 114 } 115 } 116 117 @Override enterText(ComponentOperator oper, String text)118 public void enterText(ComponentOperator oper, String text) { 119 changeText(oper, text); 120 DriverManager.getKeyDriver(oper). 121 pushKey(oper, KeyEvent.VK_ENTER, 0, 122 new Timeout("", 0)); 123 } 124 125 /** 126 * Returns operator's text. 127 * 128 * @param oper an operator. 129 * @return string representing component text. 130 */ getText(ComponentOperator oper)131 public abstract String getText(ComponentOperator oper); 132 133 /** 134 * Returns current caret position. 135 * 136 * @param oper an operator. 137 * @return int represnting current operator's caret position. 138 */ getCaretPosition(ComponentOperator oper)139 public abstract int getCaretPosition(ComponentOperator oper); 140 141 /** 142 * Returns a caret position of selection start. 143 * 144 * @param oper an operator. 145 * @return int represnting index of operator's selection start. 146 */ getSelectionStart(ComponentOperator oper)147 public abstract int getSelectionStart(ComponentOperator oper); 148 149 /** 150 * Returns a caret position of selection end. 151 * 152 * @param oper an operator. 153 * @return int represnting index of operator's selection end. 154 */ getSelectionEnd(ComponentOperator oper)155 public abstract int getSelectionEnd(ComponentOperator oper); 156 } 157