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.lookup; 24 25 import java.lang.reflect.InvocationTargetException; 26 import java.lang.reflect.Method; 27 import java.util.ArrayList; 28 import java.util.List; 29 import org.eclipse.swt.widgets.Item; 30 import org.jemmy.resources.StringComparePolicy; 31 32 /** 33 * 34 * @author erikgreijus 35 * @param <T> 36 */ 37 public class ByItemStringsLookup<T extends Item> extends QueueLookup<T> { 38 39 private final StringComparePolicy policy; 40 private final String text; 41 42 /** 43 * 44 * @param text The text to use for matching 45 */ 46 public ByItemStringsLookup(String text) { 47 this(text, StringComparePolicy.SUBSTRING); 48 } 49 50 /** 51 * 52 * @param text The text to use for matching 53 * @param policy The policy to use when matching the text 54 */ 55 public ByItemStringsLookup(String text, StringComparePolicy policy) { 56 this.policy = policy; 57 this.text = text; 58 } 59 60 public static <V extends Item> List<String> getTexts(V item) { 61 List<String> result = new ArrayList<>(); 62 result.add(item.getText()); 63 try { 64 Method getText = item.getClass().getMethod("getText", int.class); 65 Method getParent = item.getClass().getMethod("getParent"); 66 Object parent = getParent.invoke(item); 67 Method getColumnCount = parent.getClass().getMethod("getColumnCount"); 68 int columnCount = (int) getColumnCount.invoke(parent); 69 for (int i = 0; i < columnCount; i++) { 70 result.add((String) getText.invoke(item, i)); 71 } 72 } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { 73 System.err.println("Exception when using reflection to get additional text elements: " + ex.getMessage()); 74 ex.printStackTrace(); 75 } 76 return result; 77 } 78 79 @Override 80 public boolean doCheck(T control) { 81 // this lookup only supports objects with a getText(int) method 82 try { 83 control.getClass().getMethod("getText", int.class); 84 return getTexts(control).stream().map((textElement) -> policy.compare(text, textElement)).anyMatch((matches) -> (matches)); 85 } catch (NoSuchMethodException e) { 86 System.err.println("Class " + control.getClass() + " isn't supported by " + this.getClass().getSimpleName() + " (i.e has no getText(int) method"); 87 return false; 88 } 89 } 90 91 @Override 92 public String toString() { 93 return "Text = " + text; 94 } 95 }