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