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.input;
  24 
  25 
  26 import org.jemmy.Point;
  27 import org.jemmy.action.Action;
  28 import org.jemmy.control.Wrap;
  29 import org.jemmy.env.Environment;
  30 import org.jemmy.interfaces.Drag;
  31 import org.jemmy.interfaces.Modifier;
  32 import org.jemmy.interfaces.Mouse.MouseButton;
  33 import org.jemmy.interfaces.Mouse.MouseButtons;
  34 import org.jemmy.interfaces.Showable;
  35 
  36 
  37 /**
  38  *
  39  * @author shura
  40  */
  41 public class DragImpl implements Drag {
  42 
  43     /**
  44      *
  45      */
  46     public static final int DND_POINTS = 10;
  47 
  48     static {
  49         Environment.getEnvironment().setTimeout(BEFORE_DRAG_TIMEOUT);
  50         Environment.getEnvironment().setTimeout(BEFORE_DROP_TIMEOUT);
  51         Environment.getEnvironment().setTimeout(IN_DRAG_TIMEOUT);
  52     }
  53 
  54     private Wrap<?> source;
  55 
  56     /**
  57      *
  58      * @param source
  59      */
  60     public DragImpl(Wrap<?> source) {
  61         this.source = source;
  62     }
  63 
  64     /**
  65      *
  66      * @param targetPoint
  67      */
  68     public void dnd(Point targetPoint) {
  69         dnd(source, targetPoint);
  70     }
  71 
  72     /**
  73      *
  74      * @param target
  75      * @param targetPoint
  76      */
  77     public void dnd(Wrap target, Point targetPoint) {
  78         dnd(source.getClickPoint(), target, targetPoint);
  79     }
  80 
  81     /**
  82      *
  83      * @param point
  84      * @param target
  85      * @param targetPoint
  86      */
  87     public void dnd(Point point, Wrap target, Point targetPoint) {
  88         dnd(point, target, targetPoint, MouseButtons.BUTTON1);
  89     }
  90 
  91     /**
  92      *
  93      * @param point
  94      * @param target
  95      * @param targetPoint
  96      * @param button
  97      */
  98     public void dnd(Point point, Wrap target, Point targetPoint, MouseButton button) {
  99         dnd(point, target, targetPoint, button, new Modifier[]{});
 100     }
 101 
 102     /**
 103      *
 104      * @param point
 105      * @param target
 106      * @param targetPoint
 107      * @param button
 108      * @param modifiers
 109      */
 110     public void dnd(Point pointParam, final Wrap target, final Point targetPoint, final MouseButton button, final Modifier... modifiers) {
 111         final Point point = pointParam == null ? source.getClickPoint() : pointParam;
 112         source.getEnvironment().getExecutor().execute(target.getEnvironment(), false, new Action() {
 113             public void run(Object... parameters) {
 114                 if(source.is(Showable.class)) ((Showable)source.as(Showable.class)).shower().show();
 115                 source.mouse().move(point);
 116                 source.mouse().press(button, modifiers);
 117                 source.getEnvironment().getTimeout(BEFORE_DRAG_TIMEOUT.getName()).sleep();
 118                 Point intermediatePoint = new Point();
 119                 int xDistance = target.getScreenBounds().x + targetPoint.x - source.getScreenBounds().x - point.x;
 120                 int yDistance = target.getScreenBounds().y + targetPoint.y - source.getScreenBounds().y - point.y;
 121                 int startX = point.x + source.getScreenBounds().x;
 122                 int startY = point.y + source.getScreenBounds().y;
 123                 int endX = startX + xDistance;
 124                 int endY = startY + yDistance;
 125                 for(int i = 0; i < DND_POINTS + 1; i++) {
 126                     intermediatePoint.x = startX + xDistance * i / DND_POINTS - source.getScreenBounds().x;
 127                     intermediatePoint.y = startY + yDistance * i / DND_POINTS - source.getScreenBounds().y;
 128                     source.mouse().move(intermediatePoint);
 129                     source.getEnvironment().getTimeout(IN_DRAG_TIMEOUT.getName()).sleep();
 130                 }
 131                 source.mouse().move(new Point(endX - source.getScreenBounds().x, endY - source.getScreenBounds().y));
 132                 //target.mouse().move(targetPoint);
 133                 source.getEnvironment().getTimeout(BEFORE_DROP_TIMEOUT.getName()).sleep();
 134                 target.mouse().release(button, modifiers);
 135             }
 136 
 137             @Override
 138             public String toString() {
 139                 return "grag'n'drop from " + point + " to " + targetPoint + " of " + target.getClass() + " with mouse button " + button + " with " + modifiers + " modifiers";
 140             }
 141 
 142         }, button, modifiers);
 143     }
 144 }