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.lookup; 26 27 import org.eclipse.swt.custom.CTabFolder; 28 import org.eclipse.swt.widgets.Combo; 29 import org.eclipse.swt.widgets.Composite; 30 import org.eclipse.swt.widgets.Item; 31 import org.eclipse.swt.widgets.TabFolder; 32 import org.eclipse.swt.widgets.Table; 33 import org.eclipse.swt.widgets.ToolBar; 34 import org.eclipse.swt.widgets.Tree; 35 import org.jemmy.resources.StringComparePolicy; 36 37 /** 38 * @author shura 39 * @author erikgreijus 40 */ 41 public class ByItemLookup<T extends Composite> extends QueueLookup<T> { 42 43 private final StringComparePolicy policy; 44 private final String text; 45 46 public ByItemLookup(String text) { 47 this(text, StringComparePolicy.SUBSTRING); 48 } 49 50 public ByItemLookup(String text, StringComparePolicy policy) { 51 this.policy = policy; 52 this.text = text; 53 } 54 55 @Override 56 public boolean doCheck(final T control) { 57 Object[] items = null; 58 if (Tree.class.isInstance(control)) { 59 items = Tree.class.cast(control).getItems(); 60 } else if (ToolBar.class.isInstance(control)) { 61 items = ToolBar.class.cast(control).getItems(); 62 } else if (CTabFolder.class.isInstance(control)) { 63 items = CTabFolder.class.cast(control).getItems(); 64 } else if (TabFolder.class.isInstance(control)) { 65 items = TabFolder.class.cast(control).getItems(); 66 } else if (Table.class.isInstance(control)) { 67 items = Table.class.cast(control).getItems(); 68 } else if (Combo.class.isInstance(control)) { 69 items = Combo.class.cast(control).getItems(); 70 } else { 71 System.err.println("Class " + control.getClass() + " does not match any supported class in ByItemLookup"); 72 return false; 73 } 74 if (items instanceof Item[]) { 75 for (Item item : Item[].class.cast(items)) { 76 if (policy.compare(text, item.getText())) { 77 return true; 78 } else { 79 try { 80 item.getClass().getMethod("getText", int.class); 81 return ByItemStringsLookup.getTexts(item).stream().map((textElement) -> policy.compare(text, textElement)).anyMatch((matches) -> (matches)); 82 } catch (NoSuchMethodException e) { 83 // silently ignore the error to not clutter the logs (not all objects implement the method) 84 } catch (SecurityException e) { 85 System.err.println("SecurityException when using reflection to get method 'getText(int)' from " + item); 86 e.printStackTrace(); 87 } 88 } 89 } 90 } else if (items instanceof String[]) { 91 for (String stringItem : String[].class.cast(items)) { 92 if (policy.compare(text, stringItem)) { 93 return true; 94 } 95 } 96 } 97 return false; 98 } 99 100 @Override 101 public String toString() { 102 return "Text = " + text; 103 } 104 }