Clover Coverage Report - FEST Swing 1.2
Coverage timestamp: Tue Jun 1 2010 15:19:25 PDT
../../../../img/srcFileCovDistChart8.png 93% of files have more coverage
38   149   21   3.17
14   85   0.55   12
12     1.75  
1    
 
  ComponentDragAndDrop       Line # 37 38 0% 21 13 79.7% 0.796875
 
No Tests
 
1    /*
2    * Created on Feb 1, 2008
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10    * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11    * or implied. See the License for the specific language governing permissions and limitations under
12    * the License.
13    *
14    * Copyright @2008-2010 the original author or authors.
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    * Understands <code>{@link Component}</code>-based drag and drop.
33    * @since 1.1
34    *
35    * @author Alex Ruiz
36    */
 
37    public class ComponentDragAndDrop {
38   
39    private final Robot robot;
40   
41    /**
42    * Creates a new </code>{@link ComponentDragAndDrop}</code>.
43    * @param robot the robot to use to simulate user input.
44    */
 
45  2890 toggle public ComponentDragAndDrop(Robot robot) {
46  2890 this.robot = robot;
47    }
48   
49    /** Number of pixels traversed before a drag starts. */
50    public static final int DRAG_THRESHOLD = isWindows() || isMacintosh() ? 10 : 16;
51   
52    /**
53    * Performs a drag action at the given point.
54    * @param target the target component.
55    * @param where the point where to start the drag action.
56    */
 
57  21 toggle @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  21 toggle 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   
 
79  21 toggle @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   
 
93  42 toggle private int distance(int coordinate, int dimension) {
94  42 return coordinate + DRAG_THRESHOLD < dimension ? DRAG_THRESHOLD : 0;
95    }
96   
 
97  84 toggle private Point point(int x, int y) { return new Point(x, y); }
98   
99    /**
100    * Ends a drag operation, releasing the mouse button over the given target location.
101    * <p>
102    * This method is tuned for native drag/drop operations, so if you get odd behavior, you might try using a simple
103    * <code>{@link Robot#moveMouse(Component, int, int)}</code> and <code>{@link Robot#releaseMouseButtons()}</code>.
104    * @param target the target component.
105    * @param where the point where the drag operation ends.
106    * @throws ActionFailedException if there is no drag action in effect.
107    */
 
108  21 toggle @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   
 
123  42 toggle private int delayBetweenEvents() {
124  42 return settings().delayBetweenEvents();
125    }
126   
 
127  105 toggle private Settings settings() {
128  105 return robot.settings();
129    }
130   
131    /**
132    * Move the mouse appropriately to get from the source to the destination. Enter/exit events will be generated where
133    * appropriate.
134    * @param target the target component.
135    * @param where the point to drag over.
136    */
 
137  21 toggle public void dragOver(Component target, Point where) {
138  21 dragOver(target, where.x, where.y);
139    }
140   
 
141  21 toggle 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   
 
146  21 toggle private void mouseMove(Component target, Point...points) {
147  84 for (Point p : points) robot.moveMouse(target, p.x, p.y);
148    }
149    }