001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package docrobot;
031:
032: import java.awt.Graphics;
033: import java.awt.event.ComponentAdapter;
034: import java.awt.event.ComponentEvent;
035: import java.awt.image.BufferedImage;
036: import java.io.File;
037: import java.io.IOException;
038:
039: import javax.imageio.ImageIO;
040: import javax.swing.*;
041:
042: import org.jvnet.substance.*;
043: import org.jvnet.substance.skin.SubstanceSkin;
044:
045: import test.check.SampleFrame;
046:
047: /**
048: * The base class for taking screenshots of skins for Substance documentation.
049: *
050: * @author Kirill Grouchnikov
051: */
052: public abstract class SkinRobot {
053: /**
054: * The associated Substance skin.
055: */
056: protected SubstanceSkin skin;
057:
058: /**
059: * The screenshot filename.
060: */
061: protected String screenshotFilename;
062:
063: /**
064: * The frame instance.
065: */
066: protected SampleFrame sf;
067:
068: /**
069: * Indicates whether the screenshot process is complete.
070: */
071: protected boolean done = false;
072:
073: /**
074: * Creates the new screenshot robot.
075: *
076: * @param skin
077: * Substance skin.
078: * @param screenshotFilename
079: * The screenshot filename.
080: */
081: public SkinRobot(SubstanceSkin skin, String screenshotFilename) {
082: this .skin = skin;
083: this .screenshotFilename = screenshotFilename;
084: }
085:
086: /**
087: * Runs the screenshot process.
088: */
089: public void run() {
090: long start = System.currentTimeMillis();
091: SwingUtilities.invokeLater(new Runnable() {
092: public void run() {
093: try {
094: UIManager
095: .setLookAndFeel(new SubstanceDefaultLookAndFeel());
096: SubstanceLookAndFeel.setSkin(skin);
097: } catch (Exception exc) {
098: exc.printStackTrace();
099: }
100: SwingUtilities.invokeLater(new Runnable() {
101: public void run() {
102: JFrame.setDefaultLookAndFeelDecorated(true);
103: sf = new SampleFrame(false);
104: sf
105: .setIconImage(SubstanceImageCreator
106: .getThemeImage(
107: null,
108: new ImageIcon(
109: SkinRobot.class
110: .getClassLoader()
111: .getResource(
112: "test/resource/image-x-generic.png")),
113: SubstanceLookAndFeel
114: .getTheme(),
115: false));
116: sf.addComponentListener(new ComponentAdapter() {
117: @Override
118: public void componentShown(ComponentEvent e) {
119: SwingUtilities
120: .invokeLater(new Runnable() {
121: public void run() {
122: makeScreenshot(1);
123: sf.switchToLastTab();
124: try {
125: Thread.sleep(2000);
126: } catch (Exception exc) {
127: }
128: makeScreenshot(2);
129: SwingUtilities
130: .invokeLater(new Runnable() {
131: public void run() {
132: sf
133: .dispose();
134: done = true;
135: }
136: });
137: }
138: });
139: }
140: });
141: sf.setSize(300, 225);
142: sf.setLocationRelativeTo(null);
143: sf
144: .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
145: sf.setVisible(true);
146: }
147: });
148: }
149: });
150: while (!done) {
151: try {
152: Thread.sleep(100);
153: } catch (InterruptedException ie) {
154: }
155: }
156: long end = System.currentTimeMillis();
157: System.out.println(this .getClass().getSimpleName() + " : "
158: + (end - start) + "ms");
159: }
160:
161: /**
162: * Creates the screenshot and saves it on the disk.
163: *
164: * @param count
165: * Sequence number for the screenshot.
166: */
167: public void makeScreenshot(int count) {
168: BufferedImage bi = new BufferedImage(sf.getWidth(), sf
169: .getHeight(), BufferedImage.TYPE_INT_ARGB);
170: Graphics g = bi.getGraphics();
171: sf.paint(g);
172: try {
173: ImageIO.write(bi, "png", new File(this .screenshotFilename
174: + count + ".png"));
175: } catch (IOException ioe) {
176: ioe.printStackTrace();
177: }
178: }
179: }
|