Debian Codegraph @

substance-swingx/src/tools/java/docrobot/ErrorPaneRobot.java

raw symbols SCIP
1 /*
2 * Copyright 2005-2008 Kirill Grouchnikov, based on work by
3 * Sun Microsystems, Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 * The base class for taking a single screenshot for Substance documentation.
44 *
45 * @author Kirill Grouchnikov
46 */
47 public class ErrorPaneRobot {
48 /**
49 * The associated Substance skin.
50 */
51 protected SubstanceSkin skin;
52
53 /**
54 * The screenshot filename.
55 */
56 protected String screenshotFilename;
57
58 protected JDialog dialog;
59
60 protected Robot robot;
61
62 /**
63 * Creates the new screenshot robot.
64 *
65 * @param skin
66 * Substance skin.
67 * @param screenshotFilename
68 * The screenshot filename.
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 * Runs the screenshot process.
79 */
80 public void run() {
81 long start = System.currentTimeMillis();
82 // set the skin
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 // show dialog
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 // get dialog
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 // get the close button
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 // move the mouse to the close button
162 if (closeButton != null) {
163 robot.moveMouse(closeButton);
164 }
165
166 // make screenshot
167 GuiActionRunner.execute(new GuiTask() {
168 @Override
169 protected void executeInEDT() throws Throwable {
170 makeScreenshot();
171 }
172 });
173
174 // click the button
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 * Creates the screenshot and saves it on the disk.
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 }