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