View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Michael Powers
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.ui.swing.util;
20  
21  import java.awt.Component;
22  import java.awt.Image;
23  import java.awt.Window;
24  import java.io.File;
25  import java.io.FileOutputStream;
26  import java.io.FilenameFilter;
27  import java.io.OutputStream;
28  
29  import javax.swing.JDialog;
30  import javax.swing.JFrame;
31  import javax.swing.JWindow;
32  
33  /***
34   * WindowGrabber is a collection of static utility methods
35   * for taking screen shots of lightweight containers.  All
36   * components that are not native peers will be drawn to a
37   * bitmap that will be run-length compressed (LZW encoding, GIF format)
38   * and written to the specified file or output stream. <br><br>
39   *
40   * Any Swing/JFC or IFC window is a good candidate for use with these
41   * methods. The component is not expected to contain more than 256 colors
42   * (the maximum that GIF allows).  While there is a color depth limitation,
43   * the compression is lossless, with no blurs or bleeding color.
44   *
45   * @author michael@mpowers.net
46   * @version $Revision: 904 $
47   * $Date: 2006-02-18 23:19:05 +0000 (Sat, 18 Feb 2006) $
48   */
49  public class WindowGrabber
50  {
51  /***
52   * Captures the screen contents of the specified component,
53   * and write it to a file with the specified name.
54   *
55   * @param aComponent a lightweight component or container.
56   * @param aFileName the name of the file to write, optionally preceded by path.
57   * @return True if the file was successfully written, false if there was an error.
58   * Errors are written to the standard error stream.
59   */
60  	static public boolean grab( Component aComponent, String aFileName )
61  	{
62  		OutputStream output = null;
63  		try
64  		{
65  			output = new FileOutputStream( aFileName );
66  		}
67  		catch ( Exception exc )
68  		{
69  			System.err.println( exc );
70  			return false;
71  		}
72  
73  		return grab( aComponent, output );
74  	}
75  
76  /***
77   * Captures the screen contents of the specified component,
78   * and write it to a file with the specified name.
79   *
80   * @param aComponent a lightweight component or container.
81   * @param anOutputStream an output stream to which the image will be written.
82   * @return True if the image was successfully written, false if there was an error.
83   * Errors are written to the standard error stream.
84   */
85  	static public boolean grab( Component aComponent, OutputStream anOutputStream )
86  	{
87  		Image img = aComponent.createImage(
88  			aComponent.getSize().width, aComponent.getSize().height );
89  		aComponent.paintAll( img.getGraphics() );
90  
91  		try {
92  			GIFEncoder encoder = new GIFEncoder( img );
93  			encoder.write( anOutputStream );
94  			anOutputStream.flush();
95  		} catch ( Exception exc ) {
96  			System.err.println( exc );
97  			return false;
98  		}
99  
100 		return true;
101 	}
102 
103 	protected static void processFilenames( String path, String[] filenames )
104 	{
105 		ClassLoader loader = new ClassGrabber();
106 
107 		File f;
108 		for ( int i = 0; i < filenames.length; i++ )
109 		{
110 			try
111 			{
112 				f = new File( path + filenames[i] );
113 				if ( f.isDirectory() )
114 				{
115 					processFilenames (
116 						path + filenames[i] + "/",
117 						f.list( new FilenameFilter()
118 						{
119 							public boolean accept(File dir, String name)
120 							{
121 								return name.endsWith( ".class" );
122 							}
123 						})
124 					);
125 				}
126 				else
127 				{
128 					System.out.println( "Loading " + filenames[i] + ": " );
129 					Class c = loader.loadClass( path + filenames[i] );
130 
131 					System.out.println( c );
132 
133                     if ( JWindow.class.isAssignableFrom( c ) ||
134                          JDialog.class.isAssignableFrom( c ) ||
135                          JFrame.class.isAssignableFrom( c ) )
136                     {
137                         try
138                         {
139                             Window w = (Window) c.newInstance();
140                             if ( w.getBounds().width * w.getBounds().height == 0 )
141                             { // if size not specified or set, pack it
142                                 w.pack();
143                             }
144                             String gifName = // replace .class with .gif
145                                 filenames[i].substring(
146                                     0, filenames[i].length() - 5 ) + "gif";
147                             w.addNotify();
148                             w.repaint();
149                             grab( w, gifName );
150                             System.out.println( "wrote: " + gifName );
151                         }
152                         catch ( Exception exc )
153                         {
154                             System.err.println( "WindowGrab failed for " + filenames[i] + ": " );
155                             exc.printStackTrace();
156                         }
157 
158                     }
159                     else
160                     {
161                         System.out.println( "not a JWindow, JDialog, or JFrame; ignored." );
162                     }
163 				}
164 			}
165 			catch ( Exception exc )
166 			{
167 				System.err.println( filenames[i] + ": " + exc );
168 			}
169 		}
170 	}
171 
172 /***
173  * Captures images of any Swing window component classes specified
174  * as parameters.  If created, image file will have the same name
175  * as the corresponding class file, but with a ".gif" extension.
176  *
177  * @param argv a list of filenames or directory names.
178  */
179 	public static void main( String[] argv )
180 	{
181 		processFilenames( "", argv );
182         System.exit( 0 );
183 	}
184 
185 }
186 
187 /*
188  * $Log$
189  * Revision 1.2  2006/02/18 23:19:05  cgruber
190  * Update imports and maven dependencies.
191  *
192  * Revision 1.1  2006/02/16 13:22:22  cgruber
193  * Check in all sources in eclipse-friendly maven-enabled packages.
194  *
195  * Revision 1.1.1.1  2000/12/21 15:51:49  mpowers
196  * Contributing wotonomy.
197  *
198  * Revision 1.2  2000/12/20 16:25:46  michael
199  * Added log to all files.
200  *
201  *
202  */
203