Coverage Report - net.wotonomy.ui.swing.util.WindowGrabber
 
Classes in this File Line Coverage Branch Coverage Complexity
WindowGrabber
0% 
0% 
3
 
 1  0
 /*
 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  0
 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  0
                 OutputStream output = null;
 63  
                 try
 64  
                 {
 65  0
                         output = new FileOutputStream( aFileName );
 66  
                 }
 67  0
                 catch ( Exception exc )
 68  
                 {
 69  0
                         System.err.println( exc );
 70  0
                         return false;
 71  0
                 }
 72  
 
 73  0
                 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  0
                 Image img = aComponent.createImage(
 88  0
                         aComponent.getSize().width, aComponent.getSize().height );
 89  0
                 aComponent.paintAll( img.getGraphics() );
 90  
 
 91  
                 try {
 92  0
                         GIFEncoder encoder = new GIFEncoder( img );
 93  0
                         encoder.write( anOutputStream );
 94  0
                         anOutputStream.flush();
 95  0
                 } catch ( Exception exc ) {
 96  0
                         System.err.println( exc );
 97  0
                         return false;
 98  0
                 }
 99  
 
 100  0
                 return true;
 101  
         }
 102  
 
 103  
         protected static void processFilenames( String path, String[] filenames )
 104  
         {
 105  0
                 ClassLoader loader = new ClassGrabber();
 106  
 
 107  
                 File f;
 108  0
                 for ( int i = 0; i < filenames.length; i++ )
 109  
                 {
 110  
                         try
 111  
                         {
 112  0
                                 f = new File( path + filenames[i] );
 113  0
                                 if ( f.isDirectory() )
 114  
                                 {
 115  0
                                         processFilenames (
 116  0
                                                 path + filenames[i] + "/",
 117  0
                                                 f.list( new FilenameFilter()
 118  
                                                 {
 119  0
                                                         public boolean accept(File dir, String name)
 120  
                                                         {
 121  0
                                                                 return name.endsWith( ".class" );
 122  
                                                         }
 123  
                                                 })
 124  
                                         );
 125  0
                                 }
 126  
                                 else
 127  
                                 {
 128  0
                                         System.out.println( "Loading " + filenames[i] + ": " );
 129  0
                                         Class c = loader.loadClass( path + filenames[i] );
 130  
 
 131  0
                                         System.out.println( c );
 132  
 
 133  0
                     if ( JWindow.class.isAssignableFrom( c ) ||
 134  0
                          JDialog.class.isAssignableFrom( c ) ||
 135  0
                          JFrame.class.isAssignableFrom( c ) )
 136  
                     {
 137  
                         try
 138  
                         {
 139  0
                             Window w = (Window) c.newInstance();
 140  0
                             if ( w.getBounds().width * w.getBounds().height == 0 )
 141  
                             { // if size not specified or set, pack it
 142  0
                                 w.pack();
 143  
                             }
 144  0
                             String gifName = // replace .class with .gif
 145  0
                                 filenames[i].substring(
 146  0
                                     0, filenames[i].length() - 5 ) + "gif";
 147  0
                             w.addNotify();
 148  0
                             w.repaint();
 149  0
                             grab( w, gifName );
 150  0
                             System.out.println( "wrote: " + gifName );
 151  
                         }
 152  0
                         catch ( Exception exc )
 153  
                         {
 154  0
                             System.err.println( "WindowGrab failed for " + filenames[i] + ": " );
 155  0
                             exc.printStackTrace();
 156  0
                         }
 157  
 
 158  0
                     }
 159  
                     else
 160  
                     {
 161  0
                         System.out.println( "not a JWindow, JDialog, or JFrame; ignored." );
 162  
                     }
 163  
                                 }
 164  
                         }
 165  0
                         catch ( Exception exc )
 166  
                         {
 167  0
                                 System.err.println( filenames[i] + ": " + exc );
 168  0
                         }
 169  
                 }
 170  0
         }
 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  0
                 processFilenames( "", argv );
 182  0
         System.exit( 0 );
 183  0
         }
 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