1 /*
   2  * Copyright (c) 2007, 2018, 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 package org.jemmy.swt;
  26 
  27 import org.eclipse.swt.graphics.Point;
  28 import org.eclipse.swt.widgets.Text;
  29 import org.jemmy.action.GetAction;
  30 import org.jemmy.control.ControlType;
  31 import org.jemmy.control.Property;
  32 import org.jemmy.control.Wrap;
  33 import org.jemmy.env.Environment;
  34 import org.jemmy.input.SelectionText;
  35 import org.jemmy.interfaces.ControlInterface;
  36 import org.jemmy.interfaces.Focusable;
  37 
  38 /**
  39  *
  40  * @author shura
  41  * @author erikgreijus
  42  */
  43 @ControlType(Text.class)
  44 public class TextWrap<T extends Text> extends ControlWrap<T> implements Focusable {
  45 
  46     class FocusableSelectionText extends SelectionText implements Focusable {
  47         protected ClickFocus focuser;
  48         private final TextWrap textWrap;
  49 
  50         public FocusableSelectionText(TextWrap textWrap) {
  51             super(textWrap);
  52             this.textWrap = textWrap;
  53         }
  54 
  55         public double position() {
  56             return textWrap.position();
  57         }
  58 
  59         public String text() {
  60             return textWrap.text();
  61         }
  62 
  63         public String tooltip() {
  64             return textWrap.tooltip();
  65         }
  66 
  67         public double anchor() {
  68             return textWrap.anchor();
  69         }
  70 
  71         public ClickFocus focuser() {
  72             if (focuser == null) {
  73                 focuser = new ClickFocus();
  74             }
  75             return focuser;
  76         }
  77     }
  78 
  79     FocusableSelectionText text = new FocusableSelectionText(this) ;
  80 
  81     public TextWrap(Environment env, T node) {
  82         super(env, node);
  83     }
  84 
  85     @Property(Wrap.TEXT_PROP_NAME)
  86     @Override
  87     public String text() {
  88         GetAction<String> action = new GetAction<String>() {
  89 
  90             @Override
  91             public void run(Object... parameters) {
  92                 setResult(getControl().getText());
  93             }
  94         };
  95         getEnvironment().getExecutor().execute(getEnvironment(), true, action);
  96         return action.getResult();
  97     }
  98 
  99     @Property(Wrap.TOOLTIP_PROP_NAME)
 100     public String tooltip() {
 101         GetAction<String> action = new GetAction<String>() {
 102 
 103             @Override
 104             public void run(Object... parameters) {
 105                 setResult(getControl().getToolTipText());
 106             }
 107         };
 108         getEnvironment().getExecutor().execute(getEnvironment(), true, action);
 109         return action.getResult();
 110     }
 111 
 112     @Property(Wrap.POSITION_PROP_NAME)
 113     public Integer position() {
 114         GetAction<Integer> action = new GetAction<Integer>() {
 115 
 116             @Override
 117             public void run(Object... parameters) {
 118                 setResult(getControl().getCaretPosition());
 119             }
 120         };
 121         getEnvironment().getExecutor().execute(getEnvironment(), true, action);
 122         return action.getResult();
 123     }
 124 
 125     @Property(SelectionText.SELECTION_ANCHOR_PROP_NAME)
 126     public Integer anchor() {
 127         GetAction<Integer> action = new GetAction<Integer>() {
 128 
 129             @Override
 130             public void run(Object... parameters) {
 131                 Point selection = getControl().getSelection();
 132                 setResult((selection.x == getControl().getCaretPosition()) ?
 133                     selection.y : selection.x);
 134             }
 135         };
 136         getEnvironment().getExecutor().execute(getEnvironment(), true, action);
 137         return action.getResult();
 138     }
 139 
 140     @Override
 141     public <INTERFACE extends ControlInterface> boolean is(Class<INTERFACE> interfaceClass) {
 142         if(interfaceClass.isAssignableFrom(SelectionText.class)) {
 143             return true;
 144         }
 145         if(interfaceClass.equals(Focusable.class)) {
 146             return true;
 147         }
 148         return super.is(interfaceClass);
 149     }
 150 
 151     @Override
 152     public <INTERFACE extends ControlInterface> INTERFACE as(Class<INTERFACE> interfaceClass) {
 153         if(interfaceClass.isAssignableFrom(SelectionText.class)) {
 154             return (INTERFACE) text;
 155         }
 156         if(interfaceClass.isAssignableFrom(Focusable.class)) {
 157             return (INTERFACE) text;
 158         }
 159         return super.as(interfaceClass);
 160     }
 161 
 162 
 163 }