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.lang.reflect.InvocationTargetException; 28 import java.lang.reflect.Method; 29 import org.eclipse.swt.widgets.Composite; 30 import org.eclipse.swt.widgets.Control; 31 import org.eclipse.swt.widgets.Display; 32 import org.eclipse.swt.widgets.Menu; 33 import org.eclipse.swt.widgets.Shell; 34 import org.jemmy.Point; 35 import org.jemmy.Rectangle; 36 import org.jemmy.action.GetAction; 37 import org.jemmy.control.Wrap; 38 import org.jemmy.control.ControlType; 39 import org.jemmy.control.Property; 40 import org.jemmy.env.Environment; 41 import org.jemmy.interfaces.ControlInterface; 42 import org.jemmy.interfaces.Focus; 43 import org.jemmy.interfaces.Focusable; 44 import org.jemmy.interfaces.PopupOwner; 45 46 /** 47 * 48 * @author shura 49 * @author erikgreijus 50 */ 51 @ControlType(org.eclipse.swt.widgets.Control.class) 52 public class ControlWrap<T extends org.eclipse.swt.widgets.Control> extends Wrap<T> implements Focusable { 53 54 protected ClickFocus focuser; 55 protected PopupOwner popup = null; 56 57 public ControlWrap(Environment env, T node) { 58 super(env, node); 59 } 60 61 @Override 62 public Rectangle getScreenBounds() { 63 return getScreenBounds(getControl(), getEnvironment()); 64 } 65 66 public static Rectangle getScreenBounds(Control control, Environment env) { 67 Rectangle res = getBounds(control, env); 68 org.eclipse.swt.graphics.Point loc = getScreenLocation(control, env); 69 res.x = loc.x; 70 res.y = loc.y; 71 return res; 72 } 73 74 public static org.eclipse.swt.graphics.Point getScreenLocation(final org.eclipse.swt.widgets.Control control, Environment env) { 75 GetAction<org.eclipse.swt.graphics.Point> action = new GetAction<org.eclipse.swt.graphics.Point>() { 76 77 public void run(Object... parameters) { 78 setResult(Display.getDefault().map(control, null, new org.eclipse.swt.graphics.Point(0, 0))); 79 } 80 81 @Override 82 public String toString() { 83 return "Getting parent for " + control; 84 } 85 }; 86 env.getExecutor().execute(env, true, action); 87 return action.getResult(); 88 } 89 90 public Shell getShell() { 91 GetAction<Shell> action = new GetAction<Shell>() { 92 93 public void run(Object... parameters) { 94 setResult(getControl().getShell()); 95 } 96 97 @Override 98 public String toString() { 99 return "Getting shell for " + getControl(); 100 } 101 }; 102 getEnvironment().getExecutor().execute(getEnvironment(), true, action); 103 return action.getResult(); 104 } 105 106 public boolean hasFocus() { 107 GetAction<Boolean> action = new GetAction<Boolean>() { 108 109 public void run(Object... parameters) { 110 setResult(getControl().isFocusControl()); 111 } 112 113 @Override 114 public String toString() { 115 return "Getting shell for " + getControl(); 116 } 117 }; 118 getEnvironment().getExecutor().execute(getEnvironment(), true, action); 119 return action.getResult(); 120 } 121 122 public Menu menu() { 123 GetAction<Menu> action = new GetAction<Menu>() { 124 125 public void run(Object... parameters) { 126 setResult(getControl().getMenu()); 127 } 128 129 @Override 130 public String toString() { 131 return "Getting shell for " + getControl(); 132 } 133 }; 134 getEnvironment().getExecutor().execute(getEnvironment(), true, action); 135 return action.getResult(); 136 } 137 138 public static Composite getParent(final org.eclipse.swt.widgets.Control control, Environment env) { 139 GetAction<Composite> action = new GetAction<Composite>() { 140 141 public void run(Object... parameters) { 142 setResult(control.getParent()); 143 } 144 145 @Override 146 public String toString() { 147 return "Getting parent for " + control; 148 } 149 }; 150 env.getExecutor().execute(env, true, action); 151 return action.getResult(); 152 } 153 154 public ClickFocus focuser() { 155 if (focuser == null) { 156 focuser = new ClickFocus(); 157 } 158 return focuser; 159 } 160 161 public static Rectangle getBounds(final org.eclipse.swt.widgets.Control c, Environment env) { 162 GetAction<org.eclipse.swt.graphics.Rectangle> action = new GetAction<org.eclipse.swt.graphics.Rectangle>() { 163 164 public void run(Object... parameters) { 165 setResult(c.getBounds()); 166 } 167 168 @Override 169 public String toString() { 170 return "Getting bounds for " + c; 171 } 172 }; 173 env.getExecutor().execute(env, true, action); 174 org.eclipse.swt.graphics.Rectangle res = action.getResult(); 175 return new Rectangle(res.x, res.y, res.width, res.height); 176 } 177 178 @Override 179 public <INTERFACE extends ControlInterface> boolean is(Class<INTERFACE> interfaceClass) { 180 if (PopupOwner.class.isAssignableFrom(interfaceClass)) { 181 return menu() != null; 182 } 183 //disable focusable for now 184 //TODO fix 185 if (interfaceClass.equals(Focusable.class)) { 186 return false; 187 } 188 return super.is(interfaceClass); 189 } 190 191 @Override 192 public <INTERFACE extends ControlInterface> INTERFACE as(Class<INTERFACE> interfaceClass) { 193 if (PopupOwner.class.isAssignableFrom(interfaceClass)) { 194 if (popup == null) { 195 popup = new SWTPopupOwner(this); 196 } 197 return (INTERFACE) popup; 198 } 199 return super.as(interfaceClass); 200 } 201 202 protected class ClickFocus implements Focus { 203 204 Point pnt = null; 205 int clickCount = 1; 206 207 public void focus() { 208 if (!hasFocus()) { 209 mouse().click(clickCount, pnt); 210 } 211 } 212 } 213 214 @Property(Wrap.NAME_PROP_NAME) 215 public String name() { 216 GetAction<String> action = new GetAction<String>() { 217 @Override 218 public void run(Object... parameters) { 219 setResult((String) getControl().getData("name")); 220 } 221 }; 222 getEnvironment().getExecutor().execute(getEnvironment(), true, action); 223 return action.getResult(); 224 } 225 226 @Property(Wrap.TEXT_PROP_NAME) 227 public String text() { 228 GetAction<String> action = new GetAction<String>() { 229 @Override 230 public void run(Object... parameters) { 231 String result = ""; 232 try { 233 Method mthd = getControl().getClass().getMethod("getText"); 234 result = (String) mthd.invoke(getControl()); 235 } catch (NoSuchMethodException e) { 236 System.err.println("Exception when using reflection to get text from " + getControl()); 237 e.printStackTrace(); 238 } catch (SecurityException e) { 239 System.err.println("Exception when using reflection to get text from " + getControl()); 240 e.printStackTrace(); 241 } catch (IllegalAccessException e) { 242 System.err.println("Exception when using reflection to get text from " + getControl()); 243 e.printStackTrace(); 244 } catch (IllegalArgumentException e) { 245 System.err.println("Exception when using reflection to get text from " + getControl()); 246 e.printStackTrace(); 247 } catch (InvocationTargetException e) { 248 System.err.println("Exception when using reflection to get text from " + getControl()); 249 e.printStackTrace(); 250 } 251 setResult(result); 252 } 253 }; 254 getEnvironment().getExecutor().execute(getEnvironment(), true, action); 255 return action.getResult(); 256 } 257 }