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