| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
package docrobot; |
| 20 |
|
| 21 |
import java.awt.*; |
| 22 |
import java.awt.image.BufferedImage; |
| 23 |
import java.io.File; |
| 24 |
import java.io.IOException; |
| 25 |
import java.net.MalformedURLException; |
| 26 |
import java.net.URL; |
| 27 |
import java.util.HashMap; |
| 28 |
import java.util.Map; |
| 29 |
|
| 30 |
import javax.imageio.ImageIO; |
| 31 |
import javax.swing.*; |
| 32 |
|
| 33 |
import org.fest.swing.core.Robot; |
| 34 |
import org.fest.swing.edt.*; |
| 35 |
import org.fest.swing.timing.Condition; |
| 36 |
import org.fest.swing.timing.Pause; |
| 37 |
import org.jdesktop.swingx.JXErrorPane; |
| 38 |
import org.jdesktop.swingx.error.ErrorInfo; |
| 39 |
import org.pushingpixels.substance.api.SubstanceLookAndFeel; |
| 40 |
import org.pushingpixels.substance.api.SubstanceSkin; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public class ErrorPaneRobot { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
protected SubstanceSkin skin; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
protected String screenshotFilename; |
| 57 |
|
| 58 |
protected JDialog dialog; |
| 59 |
|
| 60 |
protected Robot robot; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public ErrorPaneRobot(SubstanceSkin skin, String screenshotFilename, |
| 71 |
Robot robot) { |
| 72 |
this.skin = skin; |
| 73 |
this.screenshotFilename = screenshotFilename; |
| 74 |
this.robot = robot; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public void run() { |
| 81 |
long start = System.currentTimeMillis(); |
| 82 |
|
| 83 |
GuiActionRunner.execute(new GuiTask() { |
| 84 |
@Override |
| 85 |
protected void executeInEDT() throws Throwable { |
| 86 |
SubstanceLookAndFeel.setSkin(skin); |
| 87 |
JDialog.setDefaultLookAndFeelDecorated(true); |
| 88 |
} |
| 89 |
}); |
| 90 |
robot.waitForIdle(); |
| 91 |
|
| 92 |
final Frame owner = GuiActionRunner.execute(new GuiQuery<Frame>() { |
| 93 |
@Override |
| 94 |
protected Frame executeInEDT() throws Throwable { |
| 95 |
return JOptionPane.getRootFrame(); |
| 96 |
} |
| 97 |
}); |
| 98 |
|
| 99 |
|
| 100 |
GuiActionRunner.execute(new GuiTask() { |
| 101 |
@Override |
| 102 |
protected void executeInEDT() throws Throwable { |
| 103 |
try { |
| 104 |
URL url = new URL("some wrong URL string"); |
| 105 |
} catch (MalformedURLException murle) { |
| 106 |
String msg = "<html>An error just happened. Possible reasons:" |
| 107 |
+ "<ol><li>Development team hoped nobody would notice." |
| 108 |
+ "<li>The testers missed this scenario. Wait, we don't have testers." |
| 109 |
+ "<li>Didn't your momma teach you not to use Linux?" |
| 110 |
+ "</ol>" |
| 111 |
+ "In any case, it's all open source so it's all good. Fix it yourself."; |
| 112 |
String details = "<html>Web resources should begin with \"http://\"" |
| 113 |
+ " and cannot contain any spaces. Below are a few" |
| 114 |
+ " more guidelines.<ul></ul></html>"; |
| 115 |
JXErrorPane.showDialog(owner, new ErrorInfo( |
| 116 |
"JXErrorPane example", msg, details, null, murle, |
| 117 |
null, null)); |
| 118 |
} |
| 119 |
} |
| 120 |
}); |
| 121 |
robot.waitForIdle(); |
| 122 |
|
| 123 |
|
| 124 |
this.dialog = GuiActionRunner.execute(new GuiQuery<JDialog>() { |
| 125 |
@Override |
| 126 |
protected JDialog executeInEDT() throws Throwable { |
| 127 |
return (JDialog) owner.getOwnedWindows()[owner |
| 128 |
.getOwnedWindows().length - 1]; |
| 129 |
} |
| 130 |
}); |
| 131 |
robot.waitForIdle(); |
| 132 |
|
| 133 |
|
| 134 |
final JButton closeButton = GuiActionRunner |
| 135 |
.execute(new GuiQuery<JButton>() { |
| 136 |
@Override |
| 137 |
protected JButton executeInEDT() throws Throwable { |
| 138 |
return getLoginButton(dialog.getContentPane()); |
| 139 |
} |
| 140 |
|
| 141 |
private JButton getLoginButton(Component comp) { |
| 142 |
if (comp instanceof JButton) { |
| 143 |
JButton button = (JButton) comp; |
| 144 |
if ("Close".equals(button.getText())) { |
| 145 |
return button; |
| 146 |
} |
| 147 |
} |
| 148 |
if (comp instanceof Container) { |
| 149 |
Container cont = (Container) comp; |
| 150 |
for (int i = 0; i < cont.getComponentCount(); i++) { |
| 151 |
JButton result = getLoginButton(cont |
| 152 |
.getComponent(i)); |
| 153 |
if (result != null) |
| 154 |
return result; |
| 155 |
} |
| 156 |
} |
| 157 |
return null; |
| 158 |
} |
| 159 |
}); |
| 160 |
|
| 161 |
|
| 162 |
if (closeButton != null) { |
| 163 |
robot.moveMouse(closeButton); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
GuiActionRunner.execute(new GuiTask() { |
| 168 |
@Override |
| 169 |
protected void executeInEDT() throws Throwable { |
| 170 |
makeScreenshot(); |
| 171 |
} |
| 172 |
}); |
| 173 |
|
| 174 |
|
| 175 |
if (closeButton != null) { |
| 176 |
GuiActionRunner.execute(new GuiTask() { |
| 177 |
@Override |
| 178 |
protected void executeInEDT() throws Throwable { |
| 179 |
closeButton.doClick(); |
| 180 |
} |
| 181 |
}); |
| 182 |
} |
| 183 |
|
| 184 |
Pause.pause(new Condition("Wait for the close button click") { |
| 185 |
@Override |
| 186 |
public boolean test() { |
| 187 |
return !dialog.isDisplayable(); |
| 188 |
} |
| 189 |
}); |
| 190 |
|
| 191 |
long end = System.currentTimeMillis(); |
| 192 |
System.out.println(this.getClass().getSimpleName() + " [" |
| 193 |
+ this.skin.getDisplayName() + "] : " + (end - start) + "ms"); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public void makeScreenshot() { |
| 200 |
BufferedImage bi = new BufferedImage(dialog.getWidth(), dialog |
| 201 |
.getHeight(), BufferedImage.TYPE_INT_ARGB); |
| 202 |
Map<Component, Boolean> map = new HashMap<Component, Boolean>(); |
| 203 |
RobotUtilities.makePreviewable(dialog, map); |
| 204 |
Graphics g = bi.getGraphics(); |
| 205 |
dialog.paint(g); |
| 206 |
try { |
| 207 |
ImageIO |
| 208 |
.write(bi, "png", |
| 209 |
new File(this.screenshotFilename + ".png")); |
| 210 |
} catch (IOException ioe) { |
| 211 |
ioe.printStackTrace(); |
| 212 |
} |
| 213 |
} |
| 214 |
} |