| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
package org.fest.swing.image; |
| 17 |
|
|
| 18 |
|
import static org.fest.swing.core.FocusOwnerFinder.focusOwner; |
| 19 |
|
import static org.fest.swing.edt.GuiActionRunner.execute; |
| 20 |
|
import static org.fest.swing.image.ImageFileExtensions.PNG; |
| 21 |
|
import static org.fest.swing.query.ComponentLocationOnScreenQuery.locationOnScreen; |
| 22 |
|
import static org.fest.swing.query.ComponentSizeQuery.sizeOf; |
| 23 |
|
import static org.fest.util.Strings.*; |
| 24 |
|
|
| 25 |
|
import java.awt.*; |
| 26 |
|
import java.awt.image.BufferedImage; |
| 27 |
|
import java.util.Locale; |
| 28 |
|
|
| 29 |
|
import javax.swing.text.Caret; |
| 30 |
|
import javax.swing.text.JTextComponent; |
| 31 |
|
|
| 32 |
|
import org.fest.swing.annotation.RunsInEDT; |
| 33 |
|
import org.fest.swing.edt.GuiQuery; |
| 34 |
|
import org.fest.swing.edt.GuiTask; |
| 35 |
|
import org.fest.swing.util.RobotFactory; |
| 36 |
|
import org.fest.util.VisibleForTesting; |
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
@author |
| 42 |
|
@author |
| 43 |
|
|
|
|
|
| 95.9% |
Uncovered Elements: 3 (74) |
Complexity: 25 |
Complexity Density: 0.54 |
|
| 44 |
|
public class ScreenshotTaker { |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
@deprecated@link |
| 49 |
|
|
| 50 |
|
@Deprecated public static final String PNG_EXTENSION = "png"; |
| 51 |
|
|
| 52 |
|
private final Robot robot; |
| 53 |
|
private final ImageFileWriter writer; |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
@link |
| 57 |
|
@throws |
| 58 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 59 |
7
|
public ScreenshotTaker() {... |
| 60 |
7
|
this(new ImageFileWriter(), new RobotFactory()); |
| 61 |
|
} |
| 62 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 63 |
9
|
@VisibleForTesting... |
| 64 |
|
ScreenshotTaker(ImageFileWriter writer, RobotFactory robotFactory) { |
| 65 |
9
|
this.writer = writer; |
| 66 |
9
|
try { |
| 67 |
9
|
robot = robotFactory.newRobotInPrimaryScreen(); |
| 68 |
|
} catch (AWTException e) { |
| 69 |
1
|
throw new ImageException("Unable to create AWT Robot", e); |
| 70 |
|
} |
| 71 |
|
} |
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
@param |
| 76 |
|
@throws |
| 77 |
|
@throws |
| 78 |
|
@throws |
| 79 |
|
@throws |
| 80 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 81 |
1
|
public void saveDesktopAsPng(String imageFilePath) {... |
| 82 |
1
|
saveImage(takeDesktopScreenshot(), imageFilePath); |
| 83 |
|
} |
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
@return |
| 88 |
|
@throws |
| 89 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 90 |
1
|
public BufferedImage takeDesktopScreenshot() {... |
| 91 |
1
|
Rectangle r = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); |
| 92 |
1
|
return takeScreenshot(r); |
| 93 |
|
} |
| 94 |
|
|
| 95 |
|
|
| 96 |
|
@link |
| 97 |
|
@param |
| 98 |
|
@param |
| 99 |
|
@throws |
| 100 |
|
@throws |
| 101 |
|
@throws |
| 102 |
|
@throws |
| 103 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 104 |
2
|
public void saveComponentAsPng(Component c, String imageFilePath) {... |
| 105 |
2
|
saveImage(takeScreenshotOf(c), imageFilePath); |
| 106 |
|
} |
| 107 |
|
|
| 108 |
|
|
| 109 |
|
@link |
| 110 |
|
@param |
| 111 |
|
@return |
| 112 |
|
@throws |
| 113 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 114 |
103
|
public BufferedImage takeScreenshotOf(Component c) {... |
| 115 |
103
|
Point locationOnScreen = locationOnScreen(c); |
| 116 |
103
|
Dimension size = sizeOf(c); |
| 117 |
103
|
Rectangle r = new Rectangle(locationOnScreen.x, locationOnScreen.y, size.width, size.height); |
| 118 |
103
|
return takeScreenshot(r); |
| 119 |
|
} |
| 120 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 121 |
104
|
private BufferedImage takeScreenshot(Rectangle r) {... |
| 122 |
104
|
JTextComponent textComponent = findFocusOwnerAndHideItsCaret(); |
| 123 |
104
|
robot.waitForIdle(); |
| 124 |
104
|
try { |
| 125 |
104
|
return takeScreenshot(robot, r); |
| 126 |
|
} finally { |
| 127 |
104
|
showCaretIfPossible(textComponent); |
| 128 |
|
} |
| 129 |
|
} |
| 130 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 131 |
104
|
@RunsInEDT... |
| 132 |
|
private static JTextComponent findFocusOwnerAndHideItsCaret() { |
| 133 |
104
|
return execute(new GuiQuery<JTextComponent>() { |
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 4 |
Complexity Density: 0.44 |
|
| 134 |
104
|
protected JTextComponent executeInEDT() {... |
| 135 |
104
|
Component focusOwner = focusOwner(); |
| 136 |
1
|
if (!(focusOwner instanceof JTextComponent)) return null; |
| 137 |
103
|
JTextComponent textComponent = (JTextComponent)focusOwner; |
| 138 |
103
|
Caret caret = textComponent.getCaret(); |
| 139 |
103
|
if (caret == null || !caret.isVisible()) return null; |
| 140 |
103
|
caret.setVisible(false); |
| 141 |
103
|
return textComponent; |
| 142 |
|
} |
| 143 |
|
}); |
| 144 |
|
} |
| 145 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 146 |
104
|
private static BufferedImage takeScreenshot(final Robot robot, final Rectangle r) {... |
| 147 |
104
|
return execute(new GuiQuery<BufferedImage>() { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 148 |
104
|
protected BufferedImage executeInEDT() {... |
| 149 |
104
|
return robot.createScreenCapture(r); |
| 150 |
|
} |
| 151 |
|
}); |
| 152 |
|
} |
| 153 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 154 |
104
|
private void showCaretIfPossible(JTextComponent textComponent) {... |
| 155 |
1
|
if (textComponent == null) return; |
| 156 |
103
|
showCaretOf(textComponent); |
| 157 |
103
|
robot.waitForIdle(); |
| 158 |
|
} |
| 159 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 160 |
103
|
@RunsInEDT... |
| 161 |
|
private static void showCaretOf(final JTextComponent textComponent) { |
| 162 |
103
|
execute(new GuiTask() { |
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 163 |
103
|
protected void executeInEDT() {... |
| 164 |
103
|
Caret caret = textComponent.getCaret(); |
| 165 |
103
|
if (caret != null) caret.setVisible(true); |
| 166 |
|
} |
| 167 |
|
}); |
| 168 |
|
} |
| 169 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
|
@param |
| 173 |
|
@param |
| 174 |
|
@throws |
| 175 |
|
@throws |
| 176 |
|
@throws |
| 177 |
|
@throws |
| 178 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 179 |
7
|
public void saveImage(BufferedImage image, String filePath) {... |
| 180 |
7
|
validate(filePath); |
| 181 |
4
|
try { |
| 182 |
4
|
writer.writeAsPng(image, filePath); |
| 183 |
|
} catch (Exception e) { |
| 184 |
1
|
throw new ImageException(concat("Unable to save image as ", quote(filePath)), e); |
| 185 |
|
} |
| 186 |
|
} |
| 187 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 188 |
7
|
private void validate(String imageFilePath) {... |
| 189 |
2
|
if (isEmpty(imageFilePath)) throw new ImageException("The image path cannot be empty"); |
| 190 |
5
|
if (!imageFilePath.endsWith(PNG)) |
| 191 |
1
|
throw new ImageException(concat("The image file should be a ", PNG.toUpperCase(Locale.getDefault()))); |
| 192 |
|
} |
| 193 |
|
} |