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