1 /* 2 * Copyright (c) 2007, 2017 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 */ 23 24 package org.jemmy.input; 25 26 import org.jemmy.control.Wrap; 27 import org.jemmy.interfaces.Caret; 28 import org.jemmy.interfaces.IntervalSelectable; 29 30 /** 31 * 32 * @author shura 33 */ 34 public abstract class SelectionText extends CaretText implements IntervalSelectable { 35 36 /** 37 * 38 * @param wrap 39 */ 40 public SelectionText(Wrap<?> wrap) { 41 super(wrap); 42 } 43 44 /** 45 * Selects <code>index</code>'th occurance of the regex. 46 * @param regex 47 * @param index 48 */ 49 public void select(String regex, int index) { 50 to(regex, true, index); 51 caret().selectTo(new RegexCaretDirection(this, this, regex, getFlags(), false, index)); 52 } 53 54 /** 55 * Selects first occurance of the regex. 56 * @param regex 57 */ 58 public void select(String regex) { 59 select(regex, 0); 60 } 61 62 /** 63 * Retuns the selection portion of the text. 64 * @return 65 */ 66 public String selection() { 67 int a = (int) anchor(); int p = (int) position(); 68 int start = (a < p) ? a : p; 69 int end = (a < p) ? p : a; 70 return text().substring(start, end); 71 } 72 73 @Override 74 protected void initCaret() { 75 caret = new TextCaret(wrap, this) { 76 @Override 77 public void to(Caret.Direction direction) { 78 int orig = direction.to(); 79 if (orig == 0) { 80 return; 81 } 82 if (anchor() - position() != 0) { 83 super.to(direction); // clear selection 84 } 85 super.to(direction); 86 } 87 }; 88 } 89 }