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