001 /*
002 * Created on Feb 1, 2008
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 *
014 * Copyright @2008-2010 the original author or authors.
015 */
016 package org.fest.swing.driver;
017
018 import static org.fest.swing.core.MouseButton.LEFT_BUTTON;
019 import static org.fest.swing.exception.ActionFailedException.actionFailure;
020 import static org.fest.swing.timing.Pause.pause;
021 import static org.fest.swing.util.Platform.isMacintosh;
022 import static org.fest.swing.util.Platform.isWindows;
023 import static org.fest.swing.util.TimeoutWatch.startWatchWithTimeoutOf;
024
025 import java.awt.*;
026
027 import org.fest.swing.annotation.RunsInEDT;
028 import org.fest.swing.core.*;
029 import org.fest.swing.core.Robot;
030 import org.fest.swing.exception.ActionFailedException;
031 import org.fest.swing.util.TimeoutWatch;
032
033 /**
034 * Understands drag and drop.
035 *
036 * @author Alex Ruiz
037 *
038 * @deprecated use <code>{@link ComponentDragAndDrop}</code> instead. This class will be removed in version 2.0.
039 */
040 @Deprecated
041 public class DragAndDrop {
042
043 private final Robot robot;
044
045 /**
046 * Creates a new </code>{@link DragAndDrop}</code>.
047 * @param robot the robot to use to simulate user input.
048 * @deprecated use <code>{@link ComponentDragAndDrop}</code> instead.
049 */
050 @Deprecated
051 public DragAndDrop(Robot robot) {
052 this.robot = robot;
053 }
054
055 /** Number of pixels traversed before a drag starts. */
056 public static final int DRAG_THRESHOLD = isWindows() || isMacintosh() ? 10 : 16;
057
058 /**
059 * Performs a drag action at the given point.
060 * @param target the target component.
061 * @param where the point where to start the drag action.
062 * @deprecated use <code>{@link ComponentDragAndDrop}</code> instead.
063 */
064 @RunsInEDT
065 @Deprecated public void drag(Component target, Point where) {
066 robot.pressMouse(target, where, LEFT_BUTTON);
067 int dragDelay = settings().dragDelay();
068 if (dragDelay > delayBetweenEvents()) pause(dragDelay);
069 mouseMove(target, where.x, where.y);
070 robot.waitForIdle();
071 }
072
073 @Deprecated private void mouseMove(Component target, int x, int y) {
074 if (isWindows() || isMacintosh()) {
075 mouseMoveOnWindowsAndMacintosh(target, x, y);
076 return;
077 }
078 mouseMove(target,
079 point(x + DRAG_THRESHOLD / 2, y + DRAG_THRESHOLD / 2),
080 point(x + DRAG_THRESHOLD, y + DRAG_THRESHOLD),
081 point(x + DRAG_THRESHOLD / 2, y + DRAG_THRESHOLD / 2),
082 point(x, y)
083 );
084 }
085
086 @RunsInEDT
087 @Deprecated private void mouseMoveOnWindowsAndMacintosh(Component target, int x, int y) {
088 Dimension size = target.getSize();
089 int dx = distance(x, size.width);
090 int dy = distance(y, size.height);
091 if (dx == 0 && dy == 0) dx = DRAG_THRESHOLD;
092 mouseMove(target,
093 point(x + dx / 4, y + dy / 4),
094 point(x + dx / 2, y + dy / 2),
095 point(x + dx, y + dy),
096 point(x + dx + 1, y + dy)
097 );
098 }
099
100 @Deprecated private int distance(int coordinate, int dimension) {
101 return coordinate + DRAG_THRESHOLD < dimension ? DRAG_THRESHOLD : 0;
102 }
103
104 @Deprecated private Point point(int x, int y) { return new Point(x, y); }
105
106 /**
107 * Ends a drag operation, releasing the mouse button over the given target location.
108 * <p>
109 * This method is tuned for native drag/drop operations, so if you get odd behavior, you might try using a simple
110 * <code>{@link Robot#moveMouse(Component, int, int)}</code> and <code>{@link Robot#releaseMouseButtons()}</code>.
111 * @param target the target component.
112 * @param where the point where the drag operation ends.
113 * @throws ActionFailedException if there is no drag action in effect.
114 * @deprecated use <code>{@link ComponentDragAndDrop}</code> instead.
115 */
116 @RunsInEDT
117 @Deprecated public void drop(Component target, Point where) {
118 dragOver(target, where);
119 TimeoutWatch watch = startWatchWithTimeoutOf(settings().eventPostingDelay() * 4);
120 while (!robot.isDragging()) {
121 if (watch.isTimeOut()) throw actionFailure("There is no drag in effect");
122 pause();
123 }
124 int dropDelay = settings().dropDelay();
125 int delayBetweenEvents = delayBetweenEvents();
126 if (dropDelay > delayBetweenEvents) pause(dropDelay - delayBetweenEvents);
127 robot.releaseMouseButtons();
128 robot.waitForIdle();
129 }
130
131 @Deprecated private int delayBetweenEvents() {
132 return settings().delayBetweenEvents();
133 }
134
135 @Deprecated private Settings settings() {
136 return robot.settings();
137 }
138
139 /**
140 * Move the mouse appropriately to get from the source to the destination. Enter/exit events will be generated where
141 * appropriate.
142 * @param target the target component.
143 * @param where the point to drag over.
144 * @deprecated use <code>{@link ComponentDragAndDrop}</code> instead.
145 */
146 @Deprecated public void dragOver(Component target, Point where) {
147 dragOver(target, where.x, where.y);
148 }
149
150 @Deprecated private void dragOver(Component target, int x, int y) {
151 robot.moveMouse(target, x - 4, y);
152 robot.moveMouse(target, x, y);
153 }
154
155 @Deprecated private void mouseMove(Component target, Point...points) {
156 for (Point p : points) robot.moveMouse(target, p.x, p.y);
157 }
158 }