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. 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 
  26 package org.jemmy.input;
  27 
  28 import org.jemmy.control.Wrap;
  29 import org.jemmy.env.Environment;
  30 import org.jemmy.interfaces.Keyboard.KeyboardButton;
  31 import org.jemmy.interfaces.Keyboard.KeyboardModifier;
  32 import org.jemmy.interfaces.Text;
  33 
  34 /**
  35  *
  36  * @author shura
  37  */
  38 public abstract class CaretText extends AbstractCaretOwner implements Text {
  39 
  40     static {
  41         Environment.getEnvironment().setPropertyIfNotSet(RegexCaretDirection.REGEX_FLAGS, 0);
  42     }
  43 
  44     TextCaret caret;
  45     TextImpl text;
  46     Wrap<?> wrap;
  47 
  48     public CaretText(Wrap<?> wrap) {
  49         this.wrap = wrap;
  50         text = new TextImpl(wrap) {
  51             public String text() {
  52                 return CaretText.this.text();
  53             }
  54         };
  55     }
  56 
  57     public TextCaret caret() {
  58         if (caret == null) {
  59             initCaret();
  60         }
  61         return caret;
  62     }
  63 
  64     protected void initCaret() {
  65         caret = new TextCaret(wrap, this);
  66     }
  67 
  68     protected int getFlags() {
  69         return (Integer)wrap.getEnvironment().
  70                 getProperty(RegexCaretDirection.REGEX_FLAGS, 0);
  71     }
  72 
  73     public void type(String newText) {
  74         text.type(newText);
  75     }
  76 
  77     public void clear() {
  78         text.clear();
  79     }
  80 
  81     /**
  82      * Moves caret to a beginning/end of an <code>index</code>'th occurance of the regex.
  83      *
  84      * @param regex the regular expression to search for
  85      * @param front todo document
  86      * @param index todo document
  87      */
  88     public void to(String regex, boolean front, int index) {
  89         caret().to(new RegexCaretDirection(this, this, regex, getFlags(), front, index));
  90     }
  91 
  92     /**
  93      * Moves caret to a beginning/end of the first occurance of the regex.
  94      *
  95      * @param regex the regular expression to search for
  96      * @param front todo document
  97      */
  98     public void to(String regex, boolean front) {
  99         to(regex, front, 0);
 100     }
 101 
 102     /**
 103      * Moves caret to a beginning the first occurance of the regex.
 104      *
 105      * @param regex the regular expression to search for
 106      */
 107     public void to(String regex) {
 108         to(regex, true);
 109     }
 110 
 111     public void addNavKeys(KeyboardButton left, KeyboardModifier[] leftMods,
 112             KeyboardButton right, KeyboardModifier[] rightMods) {
 113         caret().addNavKeys(left, leftMods, right, rightMods);
 114     }
 115 
 116     public void addNavKeys(KeyboardButton left, KeyboardButton right) {
 117         addNavKeys(left, new KeyboardModifier[0], right, new KeyboardModifier[0]);
 118     }
 119 }