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 java.util.Arrays;
  26 
  27 import org.eclipse.swt.custom.CCombo;
  28 import org.jemmy.action.GetAction;
  29 import org.jemmy.control.ControlType;
  30 import org.jemmy.control.Property;
  31 import org.jemmy.control.Wrap;
  32 import org.jemmy.env.Environment;
  33 import org.jemmy.input.KeyboardSelectable;
  34 import org.jemmy.input.KeyboardSelector;
  35 import org.jemmy.input.SelectionText;
  36 import org.jemmy.interfaces.ControlInterface;
  37 import org.jemmy.interfaces.Focusable;
  38 import org.jemmy.interfaces.Selector;
  39 
  40 /**
  41  * @author Erik Greijus
  42  */
  43 @ControlType(CCombo.class)
  44 public class CComboWrap<T extends CCombo> extends ControlWrap<T> implements KeyboardSelectable<String>, Focusable {
  45 
  46     class FocusableSelectionText extends SelectionText implements Focusable {
  47 
  48         protected ClickFocus focuser;
  49         private final CComboWrap cComboWrap;
  50 
  51         public FocusableSelectionText(CComboWrap textWrap) {
  52             super(textWrap);
  53             this.cComboWrap = textWrap;
  54         }
  55 
  56         @Override
  57         public double position() {
  58             return cComboWrap.position();
  59         }
  60 
  61         @Override
  62         public String text() {
  63             return cComboWrap.text();
  64         }
  65 
  66         @Override
  67         public double anchor() {
  68             return cComboWrap.anchor();
  69         }
  70 
  71         @Override
  72         public ClickFocus focuser() {
  73             if (focuser == null) {
  74                 focuser = new ClickFocus();
  75             }
  76             return focuser;
  77         }
  78     }
  79 
  80     FocusableSelectionText text = new FocusableSelectionText(this);
  81     private KeyboardSelector<String> selector = null;
  82 
  83     public CComboWrap(Environment env, T node) {
  84         super(env, node);
  85     }
  86 
  87     @Override
  88     public java.util.List<String> getStates() {
  89         return new GetAction<java.util.List<String>>() {
  90 
  91             @Override
  92             public void run(Object... parameters) throws Exception {
  93                 setResult(Arrays.asList(getControl().getItems()));
  94             }
  95         }.dispatch(getEnvironment());
  96     }
  97 
  98     @Override
  99     public String getState() {
 100         return new GetAction<String>() {
 101 
 102             @Override
 103             public void run(Object... parameters) throws Exception {
 104                 if (getControl().getSelectionIndex() >= 0) {
 105                     setResult(getControl().getItem(getControl().getSelectionIndex()));
 106                 } else {
 107                     setResult(null);
 108                 }
 109             }
 110         }.dispatch(getEnvironment());
 111     }
 112 
 113     @Override
 114     public int selection() {
 115         return new GetAction<Integer>() {
 116 
 117             @Override
 118             public void run(Object... parameters) throws Exception {
 119                 setResult(getControl().getSelectionIndex());
 120             }
 121         }.dispatch(getEnvironment());
 122     }
 123 
 124     @Override
 125     public Selector<String> selector() {
 126         if (selector == null) {
 127             selector = new KeyboardSelector<String>(this, this);
 128         }
 129         return selector;
 130     }
 131 
 132     @Override
 133     public Class<String> getType() {
 134         return String.class;
 135     }
 136 
 137     @Override
 138     public boolean isVertical() {
 139         return true;
 140     }
 141 
 142     @Override
 143     public int index(String item) {
 144         return getStates().indexOf(item);
 145     }
 146 
 147     @Override
 148     public ClickFocus focuser() {
 149         if (focuser == null) {
 150             super.focuser().clickCount = 2;
 151         }
 152         return focuser;
 153     }
 154 
 155     @Property(Wrap.POSITION_PROP_NAME)
 156     public Integer position() {
 157         GetAction<Integer> action = new GetAction<Integer>() {
 158 
 159             @Override
 160             public void run(Object... parameters) {
 161                 setResult(getControl().getSelection().x);
 162             }
 163         };
 164         getEnvironment().getExecutor().execute(getEnvironment(), true, action);
 165         return action.getResult();
 166     }
 167 
 168     @Property(SelectionText.SELECTION_ANCHOR_PROP_NAME)
 169     public Integer anchor() {
 170         return position();
 171     }
 172 
 173     @Property(Wrap.TEXT_PROP_NAME)
 174     @Override
 175     public String text() {
 176         GetAction<String> action = new GetAction<String>() {
 177 
 178             @Override
 179             public void run(Object... parameters) {
 180                 setResult(getControl().getText());
 181             }
 182         };
 183         getEnvironment().getExecutor().execute(getEnvironment(), true, action);
 184         return action.getResult();
 185     }
 186 
 187     @Override
 188     public <INTERFACE extends ControlInterface> boolean is(Class<INTERFACE> interfaceClass) {
 189         if (interfaceClass.isAssignableFrom(SelectionText.class)) {
 190             return true;
 191         }
 192         if (interfaceClass.equals(Focusable.class)) {
 193             return true;
 194         }
 195         return super.is(interfaceClass);
 196     }
 197 
 198     @Override
 199     public <INTERFACE extends ControlInterface> INTERFACE as(Class<INTERFACE> interfaceClass) {
 200         if (interfaceClass.isAssignableFrom(SelectionText.class)) {
 201             return (INTERFACE) text;
 202         }
 203         if (interfaceClass.isAssignableFrom(Focusable.class)) {
 204             return (INTERFACE) text;
 205         }
 206         return super.as(interfaceClass);
 207     }
 208 }