TextAPIDriver.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.text; 24 25import java.awt.event.KeyEvent; 26 27import org.netbeans.jemmy.Timeout; 28import org.netbeans.jemmy.drivers.DriverManager; 29import org.netbeans.jemmy.drivers.LightSupportiveDriver; 30import org.netbeans.jemmy.drivers.TextDriver; 31import org.netbeans.jemmy.operators.ComponentOperator; 32import org.netbeans.jemmy.operators.JTextComponentOperator; 33import org.netbeans.jemmy.operators.TextComponentOperator; 34 35/** 36 * Superclass for all TextDrivers using API calls. 37 * 38 * @author Alexandre Iline(alexandre.iline@oracle.com) 39 */ 40public abstract class TextAPIDriver extends LightSupportiveDriver implements TextDriver { 41 42 /** 43 * Constructs a ChoiceDriver. 44 * 45 * @param supported an array of supported class names 46 */ 47 public TextAPIDriver(String[] supported) { 48 super(supported); 49 } 50 51 @Override 52 public void changeCaretPosition(ComponentOperator oper, int position) { 53 checkSupported(oper); 54 if (oper instanceof TextComponentOperator) { 55 ((TextComponentOperator) oper).setCaretPosition(position); 56 } else { 57 ((JTextComponentOperator) oper).setCaretPosition(position); 58 } 59 } 60 61 @Override 62 public void selectText(ComponentOperator oper, int startPosition, int finalPosition) { 63 checkSupported(oper); 64 int start = (startPosition < finalPosition) ? startPosition : finalPosition; 65 int end = (startPosition > finalPosition) ? startPosition : finalPosition; 66 if (oper instanceof TextComponentOperator) { 67 TextComponentOperator toper = ((TextComponentOperator) oper); 68 toper.setSelectionStart(start); 69 toper.setSelectionEnd(end); 70 } else { 71 JTextComponentOperator toper = ((JTextComponentOperator) oper); 72 toper.setSelectionStart(start); 73 toper.setSelectionEnd(end); 74 } 75 } 76 77 @Override 78 public void clearText(ComponentOperator oper) { 79 if (oper instanceof TextComponentOperator) { 80 ((TextComponentOperator) oper).setText(""); 81 } else { 82 ((JTextComponentOperator) oper).setText(""); 83 } 84 } 85 86 @Override 87 public void typeText(ComponentOperator oper, String text, int caretPosition) { 88 checkSupported(oper); 89 String curtext = getText(oper); 90 int realPos = caretPosition; 91 if (getSelectionStart(oper) == realPos 92 || getSelectionEnd(oper) == realPos) { 93 if (getSelectionEnd(oper) == realPos) { 94 realPos = realPos - (getSelectionEnd(oper) - getSelectionStart(oper)); 95 } 96 curtext 97 = curtext.substring(0, getSelectionStart(oper)) 98 + curtext.substring(getSelectionEnd(oper)); 99 } 100 changeText(oper, 101 curtext.substring(0, realPos) + text 102 + curtext.substring(realPos)); 103 } 104 105 @Override 106 public void changeText(ComponentOperator oper, String text) { 107 checkSupported(oper); 108 if (oper instanceof TextComponentOperator) { 109 ((TextComponentOperator) oper).setText(text); 110 } else { 111 ((JTextComponentOperator) oper).setText(text); 112 } 113 } 114 115 @Override 116 public void enterText(ComponentOperator oper, String text) { 117 changeText(oper, text); 118 DriverManager.getKeyDriver(oper). 119 pushKey(oper, KeyEvent.VK_ENTER, 0, 120 new Timeout("", 0)); 121 } 122 123 /** 124 * Returns operator's text. 125 * 126 * @param oper an operator. 127 * @return string representing component text. 128 */ 129 public abstract String getText(ComponentOperator oper); 130 131 /** 132 * Returns current caret position. 133 * 134 * @param oper an operator. 135 * @return int represnting current operator's caret position. 136 */ 137 public abstract int getCaretPosition(ComponentOperator oper); 138 139 /** 140 * Returns a caret position of selection start. 141 * 142 * @param oper an operator. 143 * @return int represnting index of operator's selection start. 144 */ 145 public abstract int getSelectionStart(ComponentOperator oper); 146 147 /** 148 * Returns a caret position of selection end. 149 * 150 * @param oper an operator. 151 * @return int represnting index of operator's selection end. 152 */ 153 public abstract int getSelectionEnd(ComponentOperator oper); 154} 155