Debian Codegraph @

substance-swingx/src/tools/java/docrobot/LoginDialogRobot.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.util.HashMap;
26 import java.util.Map;
27
28 import javax.imageio.ImageIO;
29 import javax.swing.JButton;
30 import javax.swing.JDialog;
31
32 import org.fest.swing.core.Robot;
33 import org.fest.swing.edt.*;
34 import org.fest.swing.timing.Condition;
35 import org.fest.swing.timing.Pause;
36 import org.jdesktop.swingx.JXLoginPane;
37 import org.jdesktop.swingx.JXLoginPane.JXLoginDialog;
38 import org.pushingpixels.substance.api.SubstanceLookAndFeel;
39 import org.pushingpixels.substance.api.SubstanceSkin;
40
41 /**
42 * The base class for taking a single screenshot for Substance documentation.
43 *
44 * @author Kirill Grouchnikov
45 */
46 public class LoginDialogRobot {
47 /**
48 * The associated Substance skin.
49 */
50 protected SubstanceSkin skin;
51
52 /**
53 * The screenshot filename.
54 */
55 protected String screenshotFilename;
56
57 protected JXLoginDialog dialog;
58
59 private Robot robot;
60
61 /**
62 * Creates the new screenshot robot.
63 *
64 * @param skin
65 * Substance skin.
66 * @param screenshotFilename
67 * The screenshot filename.
68 */
69 public LoginDialogRobot(SubstanceSkin skin, String screenshotFilename,
70 Robot robot) {
71 this.skin = skin;
72 this.screenshotFilename = screenshotFilename;
73 this.robot = robot;
74 }
75
76 /**
77 * Runs the screenshot process.
78 */
79 public void run() {
80 long start = System.currentTimeMillis();
81
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 // create the dialog
93 GuiActionRunner.execute(new GuiTask() {
94 @Override
95 protected void executeInEDT() throws Throwable {
96 JXLoginPane loginPane = new JXLoginPane();
97 loginPane.setMessage("Sample login message");
98 loginPane.setBannerText("Sample banner");
99
100 dialog = new JXLoginDialog((Frame) null, loginPane);
101 dialog.setVisible(true);
102 }
103 });
104 robot.waitForIdle();
105
106 // wait one second
107 Pause.pause(1000);
108
109 // get the login button
110 final JButton loginButton = GuiActionRunner
111 .execute(new GuiQuery<JButton>() {
112 @Override
113 protected JButton executeInEDT() throws Throwable {
114 return getLoginButton(dialog.getContentPane());
115 }
116
117 private JButton getLoginButton(Component comp) {
118 if (comp instanceof JButton) {
119 JButton button = (JButton) comp;
120 if ("Login".equals(button.getText())) {
121 return button;
122 }
123 }
124 if (comp instanceof Container) {
125 Container cont = (Container) comp;
126 for (int i = 0; i < cont.getComponentCount(); i++) {
127 JButton result = getLoginButton(cont
128 .getComponent(i));
129 if (result != null)
130 return result;
131 }
132 }
133 return null;
134 }
135 });
136
137 // move the mouse to the login button
138 if (loginButton != null) {
139 robot.moveMouse(loginButton);
140 }
141
142 // make screenshot
143 GuiActionRunner.execute(new GuiTask() {
144 @Override
145 protected void executeInEDT() throws Throwable {
146 makeScreenshot();
147 }
148 });
149
150 // click the button
151 if (loginButton != null) {
152 GuiActionRunner.execute(new GuiTask() {
153 @Override
154 protected void executeInEDT() throws Throwable {
155 loginButton.doClick();
156 }
157 });
158 }
159
160 Pause.pause(new Condition("Wait for the login button click") {
161 @Override
162 public boolean test() {
163 return !dialog.isDisplayable();
164 }
165 });
166
167 long end = System.currentTimeMillis();
168 System.out.println(this.getClass().getSimpleName() + " ["
169 + this.skin.getDisplayName() + "] : " + (end - start) + "ms");
170 }
171
172 private boolean dismiss(Component comp) {
173 if (comp instanceof JButton) {
174 JButton button = (JButton) comp;
175 if ("Login".equals(button.getText())) {
176 button.doClick();
177 return true;
178 }
179 }
180 if (comp instanceof Container) {
181 Container cont = (Container) comp;
182 for (int i = 0; i < cont.getComponentCount(); i++) {
183 if (dismiss(cont.getComponent(i)))
184 return true;
185 }
186 }
187 return false;
188 }
189
190 /**
191 * Creates the screenshot and saves it on the disk.
192 */
193 public void makeScreenshot() {
194 BufferedImage bi = new BufferedImage(dialog.getWidth(), dialog
195 .getHeight(), BufferedImage.TYPE_INT_ARGB);
196 Map<Component, Boolean> map = new HashMap<Component, Boolean>();
197 RobotUtilities.makePreviewable(dialog.getRootPane(), map);
198 Graphics g = bi.getGraphics();
199 dialog.paint(g);
200 try {
201 ImageIO
202 .write(bi, "png",
203 new File(this.screenshotFilename + ".png"));
204 } catch (IOException ioe) {
205 ioe.printStackTrace();
206 }
207 }
208 }