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.Frame;
033: import java.awt.Graphics;
034: import java.awt.event.ComponentAdapter;
035: import java.awt.event.ComponentEvent;
036: import java.awt.image.BufferedImage;
037: import java.io.File;
038: import java.io.IOException;
039:
040: import javax.imageio.ImageIO;
041: import javax.swing.*;
042:
043: import org.jvnet.substance.*;
044: import org.jvnet.substance.button.ClassicButtonShaper;
045: import org.jvnet.substance.painter.decoration.ArcDecorationPainter;
046: import org.jvnet.substance.painter.decoration.MarbleNoiseDecorationPainter;
047: import org.jvnet.substance.watermark.SubstanceNullWatermark;
048:
049: import test.check.SampleFrame;
050:
051: /**
052: * The base class for taking a single screenshot for Substance documentation.
053: *
054: * @author Kirill Grouchnikov
055: */
056: public abstract class BaseRobot {
057: /**
058: * The screenshot filename.
059: */
060: protected String screenshotFilename;
061:
062: /**
063: * The frame instance.
064: */
065: protected SampleFrame sf;
066:
067: /**
068: * Indicates whether the screenshot process is complete.
069: */
070: protected boolean done = false;
071:
072: /**
073: * Creates the new screenshot robot.
074: *
075: * @param screenshotFilename
076: * The screenshot filename.
077: */
078: public BaseRobot(String screenshotFilename) {
079: this .screenshotFilename = screenshotFilename;
080: }
081:
082: /**
083: * Runs the screenshot process.
084: */
085: public void run() {
086: long start = System.currentTimeMillis();
087: SwingUtilities.invokeLater(new Runnable() {
088: public void run() {
089: try {
090: UIManager
091: .setLookAndFeel(new SubstanceDefaultLookAndFeel());
092: SubstanceLookAndFeel
093: .setCurrentButtonShaper(new ClassicButtonShaper());
094: MarbleNoiseDecorationPainter decorationPainter = new MarbleNoiseDecorationPainter();
095: // TODO:
096: // decorationPainter.setPaintingToolbarDropShadows(true);
097: decorationPainter.setPaintingSeparators(true);
098: decorationPainter
099: .setBaseDecorationPainter(new ArcDecorationPainter());
100: decorationPainter.setTextureAlpha(0.3f);
101: SubstanceLookAndFeel
102: .setCurrentDecorationPainter(decorationPainter);
103: SubstanceLookAndFeel
104: .setCurrentWatermark(new SubstanceNullWatermark());
105: } catch (Exception exc) {
106: exc.printStackTrace();
107: }
108: SwingUtilities.invokeLater(new Runnable() {
109: public void run() {
110: JFrame.setDefaultLookAndFeelDecorated(true);
111: sf = new SampleFrame(false);
112: sf.addComponentListener(new ComponentAdapter() {
113: @Override
114: public void componentShown(ComponentEvent e) {
115: SwingUtilities
116: .invokeLater(new Runnable() {
117: public void run() {
118: apply();
119: sf
120: .setIconImage(SubstanceImageCreator
121: .getThemeImage(
122: null,
123: new ImageIcon(
124: BaseRobot.class
125: .getClassLoader()
126: .getResource(
127: "test/resource/image-x-generic.png")),
128: SubstanceLookAndFeel
129: .getTheme(),
130: false));
131: try {
132: UIManager
133: .setLookAndFeel(new SubstanceLookAndFeel());
134: } catch (Exception exc) {
135: }
136: for (Frame frame : Frame
137: .getFrames()) {
138: SwingUtilities
139: .updateComponentTreeUI(frame);
140: }
141:
142: makeScreenshot();
143: SwingUtilities
144: .invokeLater(new Runnable() {
145: public void run() {
146: sf
147: .dispose();
148: done = true;
149: }
150: });
151: }
152: });
153: }
154: });
155: sf.setSize(300, 225);
156: sf.setLocationRelativeTo(null);
157: sf
158: .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
159: sf.setVisible(true);
160: }
161: });
162: }
163: });
164: while (!done) {
165: try {
166: Thread.sleep(100);
167: } catch (InterruptedException ie) {
168: }
169: }
170: long end = System.currentTimeMillis();
171: System.out.println(this .getClass().getSimpleName() + " : "
172: + (end - start) + "ms");
173: }
174:
175: /**
176: * Applies instance-specific Substance settings before taking the
177: * screenshot.
178: */
179: protected abstract void apply();
180:
181: /**
182: * Creates the screenshot and saves it on the disk.
183: */
184: public void makeScreenshot() {
185: BufferedImage bi = new BufferedImage(sf.getWidth(), sf
186: .getHeight(), BufferedImage.TYPE_INT_ARGB);
187: Graphics g = bi.getGraphics();
188: sf.paint(g);
189: try {
190: ImageIO.write(bi, "png", new File(this .screenshotFilename));
191: } catch (IOException ioe) {
192: ioe.printStackTrace();
193: }
194: }
195: }
|