| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
package org.fest.swing.monitor; |
| 17 |
|
|
| 18 |
|
import static java.lang.Math.max; |
| 19 |
|
import static java.util.logging.Level.WARNING; |
| 20 |
|
import static org.fest.swing.edt.GuiActionRunner.execute; |
| 21 |
|
import static org.fest.swing.query.ComponentSizeQuery.sizeOf; |
| 22 |
|
|
| 23 |
|
import java.awt.*; |
| 24 |
|
import java.util.logging.Logger; |
| 25 |
|
|
| 26 |
|
import org.fest.swing.annotation.RunsInCurrentThread; |
| 27 |
|
import org.fest.swing.annotation.RunsInEDT; |
| 28 |
|
import org.fest.swing.edt.GuiQuery; |
| 29 |
|
import org.fest.swing.edt.GuiTask; |
| 30 |
|
import org.fest.swing.util.Pair; |
| 31 |
|
import org.fest.swing.util.RobotFactory; |
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
@author |
| 37 |
|
|
|
|
|
| 95.5% |
Uncovered Elements: 3 (67) |
Complexity: 22 |
Complexity Density: 0.51 |
|
| 38 |
|
class WindowStatus { |
| 39 |
|
|
| 40 |
|
private static final Logger LOGGER = Logger.getAnonymousLogger(); |
| 41 |
|
|
| 42 |
|
private static final int ARBITRARY_EXTRA_VALUE = 20; |
| 43 |
|
|
| 44 |
|
private static int sign = 1; |
| 45 |
|
|
| 46 |
|
private final Windows windows; |
| 47 |
|
|
| 48 |
|
final Robot robot; |
| 49 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 50 |
625
|
WindowStatus(Windows windows) {... |
| 51 |
625
|
this(windows, new RobotFactory()); |
| 52 |
|
} |
| 53 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 54 |
628
|
WindowStatus(Windows windows, RobotFactory robotFactory) {... |
| 55 |
628
|
this.windows = windows; |
| 56 |
628
|
Robot r = null; |
| 57 |
628
|
try { |
| 58 |
628
|
r = robotFactory.newRobotInPrimaryScreen(); |
| 59 |
|
} catch (AWTException ignored) { |
| 60 |
1
|
LOGGER.log(WARNING, "Error ocurred when creating a new Robot", ignored); |
| 61 |
|
} |
| 62 |
628
|
robot = r; |
| 63 |
|
} |
| 64 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 65 |
621
|
Windows windows() { return windows; }... |
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
@param |
| 70 |
|
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 71 |
2692
|
@RunsInEDT... |
| 72 |
|
void checkIfReady(Window w) { |
| 73 |
2
|
if (robot == null) return; |
| 74 |
2690
|
try { |
| 75 |
2690
|
checkSafelyIfReady(w); |
| 76 |
|
} catch (Exception ignored) { |
| 77 |
|
|
| 78 |
|
|
| 79 |
0
|
LOGGER.log(WARNING, "Error ocurred when checking if window is ready", ignored); |
| 80 |
|
} |
| 81 |
|
} |
| 82 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 83 |
2690
|
private void checkSafelyIfReady(final Window w) {... |
| 84 |
|
|
| 85 |
2690
|
Pair<WindowMetrics, Point> metricsAndCenter = metricsAndCenter(w); |
| 86 |
2690
|
final WindowMetrics metrics = metricsAndCenter.i; |
| 87 |
2690
|
mouseMove(w, metricsAndCenter.ii); |
| 88 |
2375
|
if (!windows.isShowingButNotReady(w)) return; |
| 89 |
315
|
execute(new GuiTask() { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 90 |
315
|
protected void executeInEDT() {... |
| 91 |
315
|
makeLargeEnoughToReceiveEvents(w, metrics); |
| 92 |
|
} |
| 93 |
|
}); |
| 94 |
|
} |
| 95 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 96 |
2690
|
@RunsInEDT... |
| 97 |
|
private static Pair<WindowMetrics, Point> metricsAndCenter(final Window w) { |
| 98 |
2690
|
return execute(new GuiQuery<Pair<WindowMetrics, Point>>() { |
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 99 |
2690
|
protected Pair<WindowMetrics, Point> executeInEDT() {... |
| 100 |
2690
|
WindowMetrics metrics = new WindowMetrics(w); |
| 101 |
2690
|
return new Pair<WindowMetrics, Point>(metrics, metrics.center()); |
| 102 |
|
} |
| 103 |
|
}); |
| 104 |
|
} |
| 105 |
|
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
| 106 |
2690
|
@RunsInEDT... |
| 107 |
|
private void mouseMove(Window w, Point point) { |
| 108 |
2690
|
int x = point.x; |
| 109 |
2690
|
int y = point.y; |
| 110 |
2690
|
if (x == 0 || y == 0) return; |
| 111 |
2690
|
robot.mouseMove(x, y); |
| 112 |
2690
|
Dimension windowSize = sizeOf(w); |
| 113 |
2528
|
if (windowSize.width > windowSize.height) robot.mouseMove(x + sign, y); |
| 114 |
162
|
else robot.mouseMove(x, y + sign); |
| 115 |
2690
|
sign = -sign; |
| 116 |
|
} |
| 117 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 118 |
315
|
@RunsInCurrentThread... |
| 119 |
|
private void makeLargeEnoughToReceiveEvents(Window window, WindowMetrics metrics) { |
| 120 |
312
|
if (!isEmptyFrame(window)) return; |
| 121 |
3
|
int w = max(window.getWidth(), proposedWidth(metrics)); |
| 122 |
3
|
int h = max(window.getHeight(), proposedHeight(metrics)); |
| 123 |
3
|
window.setSize(new Dimension(w, h)); |
| 124 |
|
} |
| 125 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 126 |
315
|
@RunsInCurrentThread... |
| 127 |
|
private boolean isEmptyFrame(Window w) { |
| 128 |
315
|
Insets insets = w.getInsets(); |
| 129 |
315
|
return insets.top + insets.bottom == w.getHeight() || insets.left + insets.right == w.getWidth(); |
| 130 |
|
} |
| 131 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 132 |
3
|
@RunsInCurrentThread... |
| 133 |
|
private int proposedWidth(WindowMetrics metrics) { |
| 134 |
3
|
return metrics.leftAndRightInsets() + ARBITRARY_EXTRA_VALUE; |
| 135 |
|
} |
| 136 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 137 |
3
|
@RunsInCurrentThread... |
| 138 |
|
private int proposedHeight(WindowMetrics metrics) { |
| 139 |
3
|
return metrics.topAndBottomInsets() + ARBITRARY_EXTRA_VALUE; |
| 140 |
|
} |
| 141 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 142 |
2
|
static int sign() { return sign; }... |
| 143 |
|
} |