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