Debian Codegraph @

substance-swingx/src/tools/java/docrobot/FrameRobot.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.Graphics;
22 import java.awt.KeyboardFocusManager;
23 import java.awt.image.BufferedImage;
24 import java.io.File;
25 import java.io.IOException;
26
27 import javax.imageio.ImageIO;
28 import javax.swing.ImageIcon;
29 import javax.swing.JFrame;
30
31 import org.fest.swing.core.Robot;
32 import org.fest.swing.edt.GuiActionRunner;
33 import org.fest.swing.edt.GuiTask;
34 import org.fest.swing.timing.Condition;
35 import org.fest.swing.timing.Pause;
36 import org.pushingpixels.substance.api.*;
37 import org.pushingpixels.substance.internal.utils.SubstanceImageCreator;
38
39 /**
40 * The base class for taking a single screenshot for Substance documentation.
41 *
42 * @author Kirill Grouchnikov
43 */
44 public class FrameRobot {
45 /**
46 * The associated Substance skin.
47 */
48 protected SubstanceSkin skin;
49
50 /**
51 * The screenshot filename.
52 */
53 protected String screenshotFilename;
54
55 /**
56 * The frame class name.
57 */
58 protected String frameClass;
59
60 /**
61 * Indicates whether the screenshot process is complete.
62 */
63 protected boolean done = false;
64
65 protected JFrame frame;
66
67 private Robot robot;
68
69 /**
70 * Creates the new screenshot robot.
71 *
72 * @param skin
73 * Substance skin.
74 * @param screenshotFilename
75 * The screenshot filename.
76 */
77 public FrameRobot(String frameClass, SubstanceSkin skin,
78 String screenshotFilename, Robot robot) {
79 this.frameClass = frameClass;
80 this.skin = skin;
81 this.screenshotFilename = screenshotFilename;
82 this.robot = robot;
83 }
84
85 /**
86 * Runs the screenshot process.
87 */
88 public void run() {
89 long start = System.currentTimeMillis();
90
91 // set the skin
92 GuiActionRunner.execute(new GuiTask() {
93 @Override
94 protected void executeInEDT() throws Throwable {
95 SubstanceLookAndFeel.setSkin(skin);
96 JFrame.setDefaultLookAndFeelDecorated(true);
97 }
98 });
99 robot.waitForIdle();
100
101 // create the frame
102 GuiActionRunner.execute(new GuiTask() {
103 @Override
104 protected void executeInEDT() throws Throwable {
105 try {
106 frame = (JFrame) Class.forName(frameClass).newInstance();
107 } catch (Exception exc) {
108 exc.printStackTrace();
109 }
110 frame.setIconImage(SubstanceImageCreator.getColorSchemeImage(
111 null, new ImageIcon(FrameRobot.class.getClassLoader()
112 .getResource(
113 "test/resource/image-x-generic.png")),
114 SubstanceLookAndFeel
115 .getCurrentSkin(frame.getRootPane())
116 .getActiveColorScheme(
117 DecorationAreaType.NONE), 0.0f));
118
119 frame.setVisible(true);
120 }
121 });
122 robot.waitForIdle();
123
124 // remove the focus ring
125 GuiActionRunner.execute(new GuiTask() {
126 @Override
127 protected void executeInEDT() throws Throwable {
128 KeyboardFocusManager.getCurrentKeyboardFocusManager()
129 .clearGlobalFocusOwner();
130 }
131 });
132
133 // make the screenshot
134 GuiActionRunner.execute(new GuiTask() {
135 @Override
136 protected void executeInEDT() throws Throwable {
137 makeScreenshot();
138 }
139 });
140 robot.waitForIdle();
141
142 // dispose the frame
143 GuiActionRunner.execute(new GuiTask() {
144 @Override
145 protected void executeInEDT() throws Throwable {
146 frame.dispose();
147 }
148 });
149 robot.waitForIdle();
150
151 // wait for the frame to become disposed
152 Pause.pause(new Condition("Wait for the frame to become disposed") {
153 @Override
154 public boolean test() {
155 return !frame.isDisplayable();
156 }
157 });
158
159 long end = System.currentTimeMillis();
160 System.out.println(this.getClass().getSimpleName() + " ["
161 + skin.getDisplayName() + "] : " + (end - start) + "ms");
162 }
163
164 /**
165 * Creates the screenshot and saves it on the disk.
166 */
167 public void makeScreenshot() {
168 BufferedImage bi = new BufferedImage(frame.getWidth(), frame
169 .getHeight(), BufferedImage.TYPE_INT_ARGB);
170 Graphics g = bi.getGraphics();
171 frame.paint(g);
172 try {
173 ImageIO.write(bi, "png", new File(screenshotFilename + ".png"));
174 } catch (IOException ioe) {
175 ioe.printStackTrace();
176 }
177 }
178 }