001: /*
002: * Copyright 2005-2008 Kirill Grouchnikov, based on work by
003: * Sun Microsystems, Inc. All rights reserved.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
018: */
019: package docrobot;
020:
021: import java.awt.Graphics;
022: import java.awt.event.ComponentAdapter;
023: import java.awt.event.ComponentEvent;
024: import java.awt.image.BufferedImage;
025: import java.io.File;
026: import java.io.IOException;
027:
028: import javax.imageio.ImageIO;
029: import javax.swing.*;
030:
031: import org.jvnet.substance.*;
032: import org.jvnet.substance.skin.SubstanceSkin;
033:
034: /**
035: * The base class for taking a single screenshot for Substance documentation.
036: *
037: * @author Kirill Grouchnikov
038: */
039: public class FrameRobot {
040: /**
041: * The associated Substance skin.
042: */
043: protected SubstanceSkin skin;
044:
045: /**
046: * The screenshot filename.
047: */
048: protected String screenshotFilename;
049:
050: /**
051: * The frame class name.
052: */
053: protected String frameClass;
054:
055: /**
056: * Indicates whether the screenshot process is complete.
057: */
058: protected boolean done = false;
059:
060: protected JFrame frame;
061:
062: /**
063: * Creates the new screenshot robot.
064: *
065: * @param skin
066: * Substance skin.
067: * @param screenshotFilename
068: * The screenshot filename.
069: */
070: public FrameRobot(String frameClass, SubstanceSkin skin,
071: String screenshotFilename) {
072: this .frameClass = frameClass;
073: this .skin = skin;
074: this .screenshotFilename = screenshotFilename;
075: }
076:
077: /**
078: * Runs the screenshot process.
079: */
080: public void run() {
081: long start = System.currentTimeMillis();
082: SwingUtilities.invokeLater(new Runnable() {
083: public void run() {
084: try {
085: UIManager
086: .setLookAndFeel(new SubstanceDefaultLookAndFeel());
087: SubstanceLookAndFeel.setSkin(skin);
088: } catch (Exception exc) {
089: exc.printStackTrace();
090: }
091: SwingUtilities.invokeLater(new Runnable() {
092: public void run() {
093: JFrame.setDefaultLookAndFeelDecorated(true);
094: // sf = new TaskPaneFrame();
095: try {
096: frame = (JFrame) Class.forName(frameClass)
097: .newInstance();
098: } catch (Exception exc) {
099: exc.printStackTrace();
100: }
101: frame
102: .addComponentListener(new ComponentAdapter() {
103: @Override
104: public void componentShown(
105: ComponentEvent e) {
106: SwingUtilities
107: .invokeLater(new Runnable() {
108: public void run() {
109: // for (Frame frame : Frame.getFrames())
110: // {
111: // SwingUtilities
112: // .updateComponentTreeUI(frame);
113: // }
114: makeScreenshot();
115: SwingUtilities
116: .invokeLater(new Runnable() {
117: public void run() {
118: frame
119: .dispose();
120: done = true;
121: }
122: });
123: }
124: });
125: }
126: });
127: // sf.setSize(300, 225);
128: // sf.setLocationRelativeTo(null);
129: // sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
130: frame
131: .setIconImage(SubstanceImageCreator
132: .getThemeImage(
133: null,
134: new ImageIcon(
135: FrameRobot.class
136: .getClassLoader()
137: .getResource(
138: "test/resource/image-x-generic.png")),
139: SubstanceLookAndFeel
140: .getTheme(),
141: false));
142:
143: frame.setVisible(true);
144: }
145: });
146: }
147: });
148: while (!done) {
149: try {
150: Thread.sleep(100);
151: } catch (InterruptedException ie) {
152: }
153: }
154: long end = System.currentTimeMillis();
155: System.out
156: .println(this .getClass().getSimpleName() + " ["
157: + skin.getDisplayName() + "] : "
158: + (end - start) + "ms");
159: }
160:
161: /**
162: * Creates the screenshot and saves it on the disk.
163: */
164: public void makeScreenshot() {
165: BufferedImage bi = new BufferedImage(frame.getWidth(), frame
166: .getHeight(), BufferedImage.TYPE_INT_ARGB);
167: Graphics g = bi.getGraphics();
168: frame.paint(g);
169: try {
170: ImageIO.write(bi, "png", new File(screenshotFilename
171: + ".png"));
172: } catch (IOException ioe) {
173: ioe.printStackTrace();
174: }
175: }
176: }
|