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;
  26 
  27 import java.lang.reflect.InvocationTargetException;
  28 import java.lang.reflect.Method;
  29 import org.eclipse.swt.widgets.Item;
  30 import org.jemmy.JemmyException;
  31 import org.jemmy.Rectangle;
  32 import org.jemmy.action.GetAction;
  33 import org.jemmy.control.Property;
  34 import org.jemmy.control.Wrap;
  35 import org.jemmy.env.Environment;
  36 
  37 /**
  38  *
  39  * @author shura
  40  * @author erikgreijus
  41  */
  42 public class ItemWrap<T extends Item> extends Wrap<T> {
  43 
  44     private final Wrap<?> parent;
  45 
  46     public ItemWrap(Wrap<?> parent, T node) {
  47         super(new Environment(parent.getEnvironment()), node);
  48         this.parent = parent;
  49     }
  50 
  51     @Property(Wrap.TEXT_PROP_NAME)
  52     public String getText() {
  53         return new GetAction<String>() {
  54 
  55             @Override
  56             public void run(Object... parameters) {
  57                 setResult(getControl().getText());
  58             }
  59         }.dispatch(getEnvironment());
  60     }
  61 
  62     /**
  63      * @return The visible part of this item's bounds (intersection of this and the parent's screen bounds)
  64      */
  65     @Override
  66     public Rectangle getScreenBounds() {
  67         try {
  68             final Method m = getControl().getClass().getMethod("getBounds");
  69             org.eclipse.swt.graphics.Rectangle bounds =
  70                     new GetAction<org.eclipse.swt.graphics.Rectangle>() {
  71 
  72                 @Override
  73                 public void run(Object... parameters) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  74                     setResult((org.eclipse.swt.graphics.Rectangle) m.invoke(getControl()));
  75                 }
  76             }.dispatch(getEnvironment());
  77             Rectangle parentBounds = parent.getScreenBounds();
  78             return parentBounds.intersection(new Rectangle(bounds.x + parentBounds.x, bounds.y + parentBounds.y, bounds.width, bounds.height));
  79         } catch (IllegalArgumentException | SecurityException ex) {
  80             throw new JemmyException("Unable to get bounds on " + getControl(), ex);
  81         } catch (NoSuchMethodException ex) {
  82             throw new JemmyException("Bounds are not supported by " + getControl().getClass().getName(), ex);
  83         }
  84     }
  85 }