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