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