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         
  54         public double position() {
  55             return textWrap.position();
  56         }
  57 
  58         public String text() {
  59             return textWrap.text();
  60         }
  61 
  62         public String tooltip() {
  63             return textWrap.tooltip();
  64         }
  65         
  66         public double anchor() {
  67             return textWrap.anchor();
  68         }
  69         
  70         public ClickFocus focuser() {
  71             if (focuser == null) {
  72                 focuser = new ClickFocus();
  73             }
  74             return focuser;
  75         }
  76     }
  77 
  78     FocusableSelectionText text = new FocusableSelectionText(this) ;
  79 
  80     public TextWrap(Environment env, T node) {
  81         super(env, node);
  82     }
  83 
  84     @Property(Wrap.TEXT_PROP_NAME)
  85     @Override
  86     public String text() {
  87         GetAction<String> action = new GetAction<String>() {
  88 
  89             @Override
  90             public void run(Object... parameters) {
  91                 setResult(getControl().getText());
  92             }
  93         };
  94         getEnvironment().getExecutor().execute(getEnvironment(), true, action);
  95         return action.getResult();
  96     }
  97 
  98     @Property(Wrap.TOOLTIP_PROP_NAME)
  99     public String tooltip() {
 100         GetAction<String> action = new GetAction<String>() {
 101 
 102             @Override
 103             public void run(Object... parameters) {
 104                 setResult(getControl().getToolTipText());
 105             }
 106         };
 107         getEnvironment().getExecutor().execute(getEnvironment(), true, action);
 108         return action.getResult();
 109     }   
 110     
 111     @Property(Wrap.POSITION_PROP_NAME)
 112     public Integer position() {
 113         GetAction<Integer> action = new GetAction<Integer>() {
 114 
 115             @Override
 116             public void run(Object... parameters) {
 117                 setResult(getControl().getCaretPosition());
 118             }
 119         };
 120         getEnvironment().getExecutor().execute(getEnvironment(), true, action);
 121         return action.getResult();
 122     }
 123 
 124     @Property(SelectionText.SELECTION_ANCHOR_PROP_NAME)
 125     public Integer anchor() {
 126         GetAction<Integer> action = new GetAction<Integer>() {
 127 
 128             @Override
 129             public void run(Object... parameters) {
 130                 Point selection = getControl().getSelection();
 131                 setResult((selection.x == getControl().getCaretPosition()) ?
 132                     selection.y : selection.x);
 133             }
 134         };
 135         getEnvironment().getExecutor().execute(getEnvironment(), true, action);
 136         return action.getResult();
 137     }
 138 
 139     @Override
 140     public <INTERFACE extends ControlInterface> boolean is(Class<INTERFACE> interfaceClass) {
 141         if(interfaceClass.isAssignableFrom(SelectionText.class)) {
 142             return true;
 143         }
 144         if(interfaceClass.equals(Focusable.class)) {
 145             return true;
 146         }
 147         return super.is(interfaceClass);
 148     }
 149 
 150     @Override
 151     public <INTERFACE extends ControlInterface> INTERFACE as(Class<INTERFACE> interfaceClass) {
 152         if(interfaceClass.isAssignableFrom(SelectionText.class)) {
 153             return (INTERFACE) text;
 154         }
 155         if(interfaceClass.isAssignableFrom(Focusable.class)) {
 156             return (INTERFACE) text;
 157         }
 158         return super.as(interfaceClass);
 159     }
 160 
 161 
 162 }