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