| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
package org.fest.swing.core; |
| 17 |
|
|
| 18 |
|
import static org.fest.swing.core.MouseButton.LEFT_BUTTON; |
| 19 |
|
import static org.fest.swing.exception.ActionFailedException.actionFailure; |
| 20 |
|
import static org.fest.swing.timing.Pause.pause; |
| 21 |
|
import static org.fest.swing.util.Platform.isMacintosh; |
| 22 |
|
import static org.fest.swing.util.Platform.isWindows; |
| 23 |
|
import static org.fest.swing.util.TimeoutWatch.startWatchWithTimeoutOf; |
| 24 |
|
|
| 25 |
|
import java.awt.*; |
| 26 |
|
|
| 27 |
|
import org.fest.swing.annotation.RunsInEDT; |
| 28 |
|
import org.fest.swing.exception.ActionFailedException; |
| 29 |
|
import org.fest.swing.util.TimeoutWatch; |
| 30 |
|
|
| 31 |
|
|
| 32 |
|
@link |
| 33 |
|
@since |
| 34 |
|
|
| 35 |
|
@author |
| 36 |
|
|
|
|
|
| 79.7% |
Uncovered Elements: 13 (64) |
Complexity: 21 |
Complexity Density: 0.55 |
|
| 37 |
|
public class ComponentDragAndDrop { |
| 38 |
|
|
| 39 |
|
private final Robot robot; |
| 40 |
|
|
| 41 |
|
|
| 42 |
|
@link |
| 43 |
|
@param |
| 44 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 45 |
2890
|
public ComponentDragAndDrop(Robot robot) {... |
| 46 |
2890
|
this.robot = robot; |
| 47 |
|
} |
| 48 |
|
|
| 49 |
|
|
| 50 |
|
public static final int DRAG_THRESHOLD = isWindows() || isMacintosh() ? 10 : 16; |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
@param |
| 55 |
|
@param |
| 56 |
|
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 57 |
21
|
@RunsInEDT... |
| 58 |
|
public void drag(Component target, Point where) { |
| 59 |
21
|
robot.pressMouse(target, where, LEFT_BUTTON); |
| 60 |
21
|
int dragDelay = settings().dragDelay(); |
| 61 |
21
|
if (dragDelay > delayBetweenEvents()) pause(dragDelay); |
| 62 |
21
|
mouseMove(target, where.x, where.y); |
| 63 |
21
|
robot.waitForIdle(); |
| 64 |
|
} |
| 65 |
|
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 66 |
21
|
private void mouseMove(Component target, int x, int y) {... |
| 67 |
21
|
if (isWindows() || isMacintosh()) { |
| 68 |
21
|
mouseMoveOnWindowsAndMacintosh(target, x, y); |
| 69 |
21
|
return; |
| 70 |
|
} |
| 71 |
0
|
mouseMove(target, |
| 72 |
|
point(x + DRAG_THRESHOLD / 2, y + DRAG_THRESHOLD / 2), |
| 73 |
|
point(x + DRAG_THRESHOLD, y + DRAG_THRESHOLD), |
| 74 |
|
point(x + DRAG_THRESHOLD / 2, y + DRAG_THRESHOLD / 2), |
| 75 |
|
point(x, y) |
| 76 |
|
); |
| 77 |
|
} |
| 78 |
|
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 79 |
21
|
@RunsInEDT... |
| 80 |
|
private void mouseMoveOnWindowsAndMacintosh(Component target, int x, int y) { |
| 81 |
21
|
Dimension size = target.getSize(); |
| 82 |
21
|
int dx = distance(x, size.width); |
| 83 |
21
|
int dy = distance(y, size.height); |
| 84 |
21
|
if (dx == 0 && dy == 0) dx = DRAG_THRESHOLD; |
| 85 |
21
|
mouseMove(target, |
| 86 |
|
point(x + dx / 4, y + dy / 4), |
| 87 |
|
point(x + dx / 2, y + dy / 2), |
| 88 |
|
point(x + dx, y + dy), |
| 89 |
|
point(x + dx + 1, y + dy) |
| 90 |
|
); |
| 91 |
|
} |
| 92 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
| 93 |
42
|
private int distance(int coordinate, int dimension) {... |
| 94 |
42
|
return coordinate + DRAG_THRESHOLD < dimension ? DRAG_THRESHOLD : 0; |
| 95 |
|
} |
| 96 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 97 |
84
|
private Point point(int x, int y) { return new Point(x, y); }... |
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
@link@link |
| 104 |
|
@param |
| 105 |
|
@param |
| 106 |
|
@throws |
| 107 |
|
|
|
|
|
| 61.1% |
Uncovered Elements: 7 (18) |
Complexity: 4 |
Complexity Density: 0.33 |
|
| 108 |
21
|
@RunsInEDT... |
| 109 |
|
public void drop(Component target, Point where) { |
| 110 |
21
|
dragOver(target, where); |
| 111 |
21
|
TimeoutWatch watch = startWatchWithTimeoutOf(settings().eventPostingDelay() * 4); |
| 112 |
21
|
while (!robot.isDragging()) { |
| 113 |
0
|
if (watch.isTimeOut()) throw actionFailure("There is no drag in effect"); |
| 114 |
0
|
pause(); |
| 115 |
|
} |
| 116 |
21
|
int dropDelay = settings().dropDelay(); |
| 117 |
21
|
int delayBetweenEvents = delayBetweenEvents(); |
| 118 |
21
|
if (dropDelay > delayBetweenEvents) pause(dropDelay - delayBetweenEvents); |
| 119 |
21
|
robot.releaseMouseButtons(); |
| 120 |
21
|
robot.waitForIdle(); |
| 121 |
|
} |
| 122 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 123 |
42
|
private int delayBetweenEvents() {... |
| 124 |
42
|
return settings().delayBetweenEvents(); |
| 125 |
|
} |
| 126 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 127 |
105
|
private Settings settings() {... |
| 128 |
105
|
return robot.settings(); |
| 129 |
|
} |
| 130 |
|
|
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
@param |
| 135 |
|
@param |
| 136 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 137 |
21
|
public void dragOver(Component target, Point where) {... |
| 138 |
21
|
dragOver(target, where.x, where.y); |
| 139 |
|
} |
| 140 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 141 |
21
|
private void dragOver(Component target, int x, int y) {... |
| 142 |
21
|
robot.moveMouse(target, x - 4, y); |
| 143 |
21
|
robot.moveMouse(target, x, y); |
| 144 |
|
} |
| 145 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 146 |
21
|
private void mouseMove(Component target, Point...points) {... |
| 147 |
84
|
for (Point p : points) robot.moveMouse(target, p.x, p.y); |
| 148 |
|
} |
| 149 |
|
} |