~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to substance/src/tools/java/tools/docrobot/SkinRobot.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2005-2010 Substance Kirill Grouchnikov. All Rights Reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 *
 
7
 *  o Redistributions of source code must retain the above copyright notice,
 
8
 *    this list of conditions and the following disclaimer.
 
9
 *
 
10
 *  o Redistributions in binary form must reproduce the above copyright notice,
 
11
 *    this list of conditions and the following disclaimer in the documentation
 
12
 *    and/or other materials provided with the distribution.
 
13
 *
 
14
 *  o Neither the name of Substance Kirill Grouchnikov nor the names of
 
15
 *    its contributors may be used to endorse or promote products derived
 
16
 *    from this software without specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 
20
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
21
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 
22
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
23
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
24
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
25
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
26
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 
27
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 
28
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 */
 
30
package tools.docrobot;
 
31
 
 
32
import java.awt.Graphics;
 
33
import java.awt.Point;
 
34
import java.awt.image.BufferedImage;
 
35
import java.io.File;
 
36
import java.io.IOException;
 
37
 
 
38
import javax.imageio.ImageIO;
 
39
import javax.swing.ImageIcon;
 
40
import javax.swing.JButton;
 
41
import javax.swing.JFrame;
 
42
 
 
43
import org.fest.swing.core.BasicRobot;
 
44
import org.fest.swing.core.Robot;
 
45
import org.fest.swing.edt.GuiActionRunner;
 
46
import org.fest.swing.edt.GuiQuery;
 
47
import org.fest.swing.edt.GuiTask;
 
48
import org.fest.swing.timing.Pause;
 
49
import org.pushingpixels.substance.api.DecorationAreaType;
 
50
import org.pushingpixels.substance.api.SubstanceLookAndFeel;
 
51
import org.pushingpixels.substance.api.SubstanceSkin;
 
52
import org.pushingpixels.substance.internal.utils.SubstanceImageCreator;
 
53
 
 
54
import test.check.SampleFrame;
 
55
 
 
56
/**
 
57
 * The base class for taking screenshots of skins for Substance documentation.
 
58
 * 
 
59
 * @author Kirill Grouchnikov
 
60
 */
 
61
public abstract class SkinRobot {
 
62
        /**
 
63
         * The associated Substance skin.
 
64
         */
 
65
        protected SubstanceSkin skin;
 
66
 
 
67
        /**
 
68
         * The screenshot filename.
 
69
         */
 
70
        protected String screenshotFilename;
 
71
 
 
72
        /**
 
73
         * The frame instance.
 
74
         */
 
75
        protected SampleFrame sf;
 
76
 
 
77
        /**
 
78
         * Creates the new screenshot robot.
 
79
         * 
 
80
         * @param skin
 
81
         *            Substance skin.
 
82
         * @param screenshotFilename
 
83
         *            The screenshot filename.
 
84
         */
 
85
        public SkinRobot(SubstanceSkin skin, String screenshotFilename) {
 
86
                this.skin = skin;
 
87
                this.screenshotFilename = screenshotFilename;
 
88
        }
 
89
 
 
90
        /**
 
91
         * Runs the screenshot process.
 
92
         */
 
93
        public void run() {
 
94
                long start = System.currentTimeMillis();
 
95
 
 
96
                Robot robot = BasicRobot.robotWithNewAwtHierarchy();
 
97
 
 
98
                // set skin
 
99
                GuiActionRunner.execute(new GuiTask() {
 
100
                        @Override
 
101
                        protected void executeInEDT() throws Throwable {
 
102
                                SubstanceLookAndFeel.setSkin(skin);
 
103
                                JFrame.setDefaultLookAndFeelDecorated(true);
 
104
                        }
 
105
                });
 
106
                robot.waitForIdle();
 
107
 
 
108
                // create the frame and set the icon image
 
109
                GuiActionRunner.execute(new GuiTask() {
 
110
                        @Override
 
111
                        protected void executeInEDT() throws Throwable {
 
112
                                sf = new SampleFrame();
 
113
                                sf.setIconImage(SubstanceImageCreator.getColorSchemeImage(null,
 
114
                                                new ImageIcon(SkinRobot.class.getClassLoader()
 
115
                                                                .getResource(
 
116
                                                                                "test/resource/image-x-generic.png")),
 
117
                                                SubstanceLookAndFeel.getCurrentSkin(sf.getRootPane())
 
118
                                                                .getActiveColorScheme(
 
119
                                                                                DecorationAreaType.PRIMARY_TITLE_PANE),
 
120
                                                0.0f));
 
121
                                sf.setSize(338, 245);
 
122
                                sf.setLocationRelativeTo(null);
 
123
                                sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
124
                                sf.setVisible(true);
 
125
                        }
 
126
                });
 
127
                robot.waitForIdle();
 
128
 
 
129
                // get the default button
 
130
                JButton defaultButton = GuiActionRunner
 
131
                                .execute(new GuiQuery<JButton>() {
 
132
                                        @Override
 
133
                                        protected JButton executeInEDT() throws Throwable {
 
134
                                                return sf.getRootPane().getDefaultButton();
 
135
                                        }
 
136
                                });
 
137
                // and move the mouse to it
 
138
                robot.moveMouse(defaultButton);
 
139
                robot.waitForIdle();
 
140
 
 
141
                // wait for a second
 
142
                Pause.pause(1000);
 
143
 
 
144
                // make the first screenshot
 
145
                GuiActionRunner.execute(new GuiTask() {
 
146
                        @Override
 
147
                        protected void executeInEDT() throws Throwable {
 
148
                                makeScreenshot(1);
 
149
                        }
 
150
                });
 
151
                robot.waitForIdle();
 
152
 
 
153
                // switch to the last tab
 
154
                GuiActionRunner.execute(new GuiTask() {
 
155
                        @Override
 
156
                        protected void executeInEDT() throws Throwable {
 
157
                                sf.switchToLastTab();
 
158
                        }
 
159
                });
 
160
                robot.waitForIdle();
 
161
 
 
162
                // move the mouse away from the frame
 
163
                robot.moveMouse(new Point(0, 0));
 
164
                robot.waitForIdle();
 
165
 
 
166
                // wait for two seconds
 
167
                Pause.pause(1000);
 
168
 
 
169
                // make the second screenshot
 
170
                GuiActionRunner.execute(new GuiTask() {
 
171
                        @Override
 
172
                        protected void executeInEDT() throws Throwable {
 
173
                                makeScreenshot(2);
 
174
                        }
 
175
                });
 
176
                robot.waitForIdle();
 
177
 
 
178
                // dispose the frame
 
179
                GuiActionRunner.execute(new GuiTask() {
 
180
                        @Override
 
181
                        protected void executeInEDT() throws Throwable {
 
182
                                sf.dispose();
 
183
                        }
 
184
                });
 
185
                robot.waitForIdle();
 
186
 
 
187
                long end = System.currentTimeMillis();
 
188
                System.out.println(this.getClass().getSimpleName() + " : "
 
189
                                + (end - start) + "ms");
 
190
        }
 
191
 
 
192
        /**
 
193
         * Creates the screenshot and saves it on the disk.
 
194
         * 
 
195
         * @param count
 
196
         *            Sequence number for the screenshot.
 
197
         */
 
198
        public void makeScreenshot(int count) {
 
199
                BufferedImage bi = new BufferedImage(sf.getWidth(), sf.getHeight(),
 
200
                                BufferedImage.TYPE_INT_ARGB);
 
201
                Graphics g = bi.getGraphics();
 
202
                sf.paint(g);
 
203
                try {
 
204
                        File output = new File(this.screenshotFilename + count + ".png");
 
205
                        output.getParentFile().mkdirs();
 
206
                        ImageIO.write(bi, "png", output);
 
207
                } catch (IOException ioe) {
 
208
                        ioe.printStackTrace();
 
209
                }
 
210
        }
 
211
}