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.*;
022: import java.awt.image.BufferedImage;
023: import java.io.File;
024: import java.io.IOException;
025: import java.util.*;
026:
027: import javax.imageio.ImageIO;
028: import javax.swing.*;
029:
030: import org.jdesktop.swingx.JXTipOfTheDay;
031: import org.jdesktop.swingx.tips.TipLoader;
032: import org.jdesktop.swingx.tips.TipOfTheDayModel;
033: import org.jvnet.substance.SubstanceDefaultLookAndFeel;
034: import org.jvnet.substance.SubstanceLookAndFeel;
035: import org.jvnet.substance.skin.SubstanceSkin;
036:
037: import test.TestSwingXFrame;
038:
039: /**
040: * The base class for taking a single screenshot for Substance documentation.
041: *
042: * @author Kirill Grouchnikov
043: */
044: public class TipOfTheDayRobot {
045: /**
046: * The associated Substance skin.
047: */
048: protected SubstanceSkin skin;
049:
050: /**
051: * The screenshot filename.
052: */
053: protected String screenshotFilename;
054:
055: /**
056: * Indicates whether the screenshot process is complete.
057: */
058: protected boolean done = false;
059:
060: protected JDialog dialog;
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 TipOfTheDayRobot(SubstanceSkin skin,
071: String screenshotFilename) {
072: this .skin = skin;
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.setSkin(skin);
087: } catch (Exception exc) {
088: exc.printStackTrace();
089: }
090: SwingUtilities.invokeLater(new Runnable() {
091: public void run() {
092: JDialog.setDefaultLookAndFeelDecorated(true);
093: // sf = new TaskPaneFrame();
094: final Frame owner = JOptionPane.getRootFrame();
095: Properties tips = new Properties();
096: try {
097: tips
098: .load(TestSwingXFrame.class
099: .getClassLoader()
100: .getResourceAsStream(
101: "docrobot/tips.properties"));
102: } catch (Exception exc) {
103: }
104:
105: TipOfTheDayModel model = TipLoader.load(tips);
106: JXTipOfTheDay totd = new JXTipOfTheDay(model);
107:
108: Thread tracking = new Thread() {
109: @Override
110: public void run() {
111: while (true) {
112: while (owner.getOwnedWindows().length > 1) {
113: System.out.println("["
114: + skin.getDisplayName()
115: + "] GCing");
116: System.gc();
117: }
118:
119: System.out
120: .println("["
121: + skin
122: .getDisplayName()
123: + "] Windows : "
124: + owner
125: .getOwnedWindows().length);
126:
127: if (owner.getOwnedWindows().length == 1) {
128: try {
129: Thread.sleep(1500);
130: } catch (InterruptedException ie) {
131: }
132: break;
133: }
134: try {
135: Thread.sleep(500);
136: } catch (InterruptedException ie) {
137: break;
138: }
139: }
140: dialog = (JDialog) owner
141: .getOwnedWindows()[owner
142: .getOwnedWindows().length - 1];
143: makeScreenshot();
144: SwingUtilities
145: .invokeLater(new Runnable() {
146: public void run() {
147: dialog.dispose();
148: done = true;
149: }
150: });
151: }
152: };
153: // tracking.setPriority(Thread.MIN_PRIORITY);
154: tracking.start();
155:
156: totd.showDialog(owner);
157: }
158: });
159: }
160: });
161: while (!done) {
162: try {
163: Thread.sleep(300);
164: } catch (InterruptedException ie) {
165: }
166: }
167: long end = System.currentTimeMillis();
168: System.out.println(this .getClass().getSimpleName() + " ["
169: + this .skin.getDisplayName() + "] : " + (end - start)
170: + "ms");
171: }
172:
173: /**
174: * Creates the screenshot and saves it on the disk.
175: */
176: public void makeScreenshot() {
177: BufferedImage bi = new BufferedImage(dialog.getWidth(), dialog
178: .getHeight(), BufferedImage.TYPE_INT_ARGB);
179: Map<Component, Boolean> map = new HashMap<Component, Boolean>();
180: RobotUtilities.makePreviewable(dialog, map);
181: Graphics g = bi.getGraphics();
182: dialog.paint(g);
183: try {
184: ImageIO.write(bi, "png", new File(this .screenshotFilename
185: + ".png"));
186: } catch (IOException ioe) {
187: ioe.printStackTrace();
188: }
189: }
190: }
|