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.Method;
  26 
  27 import org.eclipse.swt.custom.CTabFolder;
  28 import org.eclipse.swt.widgets.Composite;
  29 import org.eclipse.swt.widgets.Item;
  30 import org.eclipse.swt.widgets.TabFolder;
  31 import org.eclipse.swt.widgets.ToolBar;
  32 import org.jemmy.resources.StringComparePolicy;
  33 
  34 /**
  35  * 
  36  * @param <T>
  37  * @author klara
  38  */
  39 public class ByItemToolTipLookup<T extends Composite> extends QueueLookup<T> {
  40 
  41         private StringComparePolicy policy;
  42         private String text;
  43 
  44         public ByItemToolTipLookup(String text) {
  45                 this(text, StringComparePolicy.SUBSTRING);
  46         }
  47 
  48         protected ByItemToolTipLookup(String text, StringComparePolicy policy) {
  49                 this.policy = policy;
  50                 this.text = text;
  51         }
  52 
  53         public boolean doCheck(final T control) {
  54                 Item[] items = null;
  55                 if (ToolBar.class.isInstance(control)) {
  56                         items = ToolBar.class.cast(control).getItems();
  57                 } else if (CTabFolder.class.isInstance(control)) {
  58                         items = CTabFolder.class.cast(control).getItems();
  59                 } else if (TabFolder.class.isInstance(control)) {
  60                         items = TabFolder.class.cast(control).getItems();
  61                 } else {
  62                         System.err.println("Class " + control.getClass() + " does not match any supported class in ByItemLookup");
  63                         return false;
  64                 }
  65                 try {
  66                         for (Item item : items) {
  67                                 Method mthd = item.getClass().getMethod("getToolTipText");
  68                                 if (policy.compare(text, (String) mthd.invoke(item))) {
  69                                         return true;
  70                                 }
  71                         }
  72                 } catch (Exception e) {
  73                         System.err.println("Exception when using reflection to get tooltip text from items of " + control);
  74                         e.printStackTrace();
  75                 }
  76                 return false;
  77         }
  78 }
  79