1 /*
   2  * Copyright (c) 2007, 2017 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.lookup;
  24 
  25 import org.jemmy.Rectangle;
  26 import org.jemmy.control.Wrap;
  27 
  28 /**
  29  *
  30  * @param <CONTROL>
  31  * @author shura
  32  */
  33 public abstract class RelativeCoordinateLookup<CONTROL> extends CoordinateLookup<CONTROL> {
  34 
  35     private static final int MAX_SCREEN_SIZE = 100000;
  36 
  37     private Wrap wrap;
  38     private boolean includeControl;
  39     private int hr;
  40     private int vr;
  41 
  42     /**
  43      *
  44      * @param wrap
  45      * @param includeControl
  46      * @param hr
  47      * @param vr
  48      */
  49     public RelativeCoordinateLookup(Wrap wrap, boolean includeControl, int hr, int vr) {
  50         super(wrap.getScreenBounds());
  51         this.wrap = wrap;
  52         this.includeControl = includeControl;
  53         this.hr = hr;
  54         this.vr = vr;
  55     }
  56 
  57     /**
  58      *
  59      * @return
  60      */
  61     @Override
  62     protected Rectangle getArea() {
  63         return constructArea(wrap, includeControl, hr, vr);
  64     }
  65 
  66     private static Rectangle constructArea(Wrap wrap, boolean includeControl, int hr, int vr) {
  67         Rectangle res = new Rectangle();
  68         res.width = MAX_SCREEN_SIZE;
  69         res.height = MAX_SCREEN_SIZE;
  70         Rectangle bounds = wrap.getScreenBounds();
  71         if (hr != 0) {
  72             if (hr < 0) {
  73                 res.x = -MAX_SCREEN_SIZE + bounds.x + (includeControl ? bounds.width : 0);
  74             } else {
  75                 res.x = bounds.x + (includeControl ? 0 : bounds.width);
  76             }
  77         } else {
  78             res.x = -MAX_SCREEN_SIZE / 2 + bounds.x + (includeControl ? bounds.width / 2 : 0);
  79         }
  80         if (vr != 0) {
  81             if (vr < 0) {
  82                 res.y = -MAX_SCREEN_SIZE + bounds.y + (includeControl ? bounds.height : 0);
  83             } else {
  84                 res.y = bounds.y + (includeControl ? 0 : bounds.height);
  85             }
  86         } else {
  87             res.y = -MAX_SCREEN_SIZE / 2 + bounds.y + (includeControl ? bounds.height / 2 : 0);
  88         }
  89         return res;
  90     }
  91 
  92 }