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.net.MalformedURLException;
026: import java.net.URL;
027: import java.util.HashMap;
028: import java.util.Map;
029:
030: import javax.imageio.ImageIO;
031: import javax.swing.*;
032:
033: import org.jdesktop.swingx.JXErrorPane;
034: import org.jdesktop.swingx.error.ErrorInfo;
035: import org.jvnet.substance.SubstanceDefaultLookAndFeel;
036: import org.jvnet.substance.SubstanceLookAndFeel;
037: import org.jvnet.substance.skin.SubstanceSkin;
038:
039: /**
040: * The base class for taking a single screenshot for Substance documentation.
041: *
042: * @author Kirill Grouchnikov
043: */
044: public class ErrorPaneRobot {
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 ErrorPaneRobot(SubstanceSkin skin, String screenshotFilename) {
071: this .skin = skin;
072: this .screenshotFilename = screenshotFilename;
073: }
074:
075: /**
076: * Runs the screenshot process.
077: */
078: public void run() {
079: long start = System.currentTimeMillis();
080: SwingUtilities.invokeLater(new Runnable() {
081: public void run() {
082: try {
083: UIManager
084: .setLookAndFeel(new SubstanceDefaultLookAndFeel());
085: SubstanceLookAndFeel.setSkin(skin);
086: } catch (Exception exc) {
087: exc.printStackTrace();
088: }
089: SwingUtilities.invokeLater(new Runnable() {
090: public void run() {
091: JDialog.setDefaultLookAndFeelDecorated(true);
092: // sf = new TaskPaneFrame();
093: final Frame owner = JOptionPane.getRootFrame();
094: try {
095: URL url = new URL("some wrong URL string");
096: } catch (MalformedURLException murle) {
097: String msg = "<html>An error just happened. Possible reasons:"
098: + "<ol><li>Development team hoped nobody would notice."
099: + "<li>The testers missed this scenario. Wait, we don't have testers."
100: + "<li>Didn't your momma teach you not to use Linux?"
101: + "</ol>"
102: + "In any case, it's all open source so it's all good. Fix it yourself.";
103: String details = "<html>Web resources should begin with \"http://\""
104: + " and cannot contain any spaces. Below are a few"
105: + " more guidelines.<ul></ul></html>";
106: Thread tracking = new Thread() {
107: @Override
108: public void run() {
109: while (true) {
110: while (owner.getOwnedWindows().length > 1) {
111: System.out
112: .println("["
113: + skin
114: .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: JXErrorPane.showDialog(owner,
156: new ErrorInfo(
157: "JXErrorPane example", msg,
158: details, null, murle, null,
159: null));
160: }
161: }
162: });
163: }
164: });
165: while (!done) {
166: try {
167: Thread.sleep(300);
168: } catch (InterruptedException ie) {
169: }
170: }
171: long end = System.currentTimeMillis();
172: System.out.println(this .getClass().getSimpleName() + " ["
173: + this .skin.getDisplayName() + "] : " + (end - start)
174: + "ms");
175: }
176:
177: /**
178: * Creates the screenshot and saves it on the disk.
179: */
180: public void makeScreenshot() {
181: BufferedImage bi = new BufferedImage(dialog.getWidth(), dialog
182: .getHeight(), BufferedImage.TYPE_INT_ARGB);
183: Map<Component, Boolean> map = new HashMap<Component, Boolean>();
184: RobotUtilities.makePreviewable(dialog, map);
185: Graphics g = bi.getGraphics();
186: dialog.paint(g);
187: try {
188: ImageIO.write(bi, "png", new File(this .screenshotFilename
189: + ".png"));
190: } catch (IOException ioe) {
191: ioe.printStackTrace();
192: }
193: }
194: }
|