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