KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > uicapture > v1 > DefaultScreenScraper


1 /*
2  * @(#)DefaultScreenScraper.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.uicapture.v1;
28
29 import java.awt.image.BufferedImage JavaDoc;
30
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 import java.util.Iterator JavaDoc;
35
36 import javax.imageio.ImageIO JavaDoc;
37
38 import org.apache.log4j.Logger;
39
40
41 /**
42  * Writes images to files using the <tt>javax.imageio</tt> package.
43  *
44  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
45  * @version Jan 7, 2002
46  */

47 public class DefaultScreenScraper implements IScreenScraper
48 {
49     private static final Logger LOG = Logger.getLogger( DefaultScreenScraper.class );
50     private String JavaDoc writerFormatName = null;
51     
52     private static final boolean DEBUG = true;
53     
54     
55     //-------------------------------------------------------------------------
56
// Constructors
57

58     
59     /**
60      * Find the first writer format name we come across.
61      *
62      * @exception IllegalArgumentException thrown if there are no formats
63      * supported.
64      */

65     public DefaultScreenScraper()
66     {
67         String JavaDoc[] names = ImageIO.getWriterFormatNames();
68         if (names == null || names.length <= 0)
69         {
70             throw new IllegalArgumentException JavaDoc(
71                 "No writer format names supported." );
72         }
73         
74         this.writerFormatName = names[0];
75         
76         for (int i = 0; i < names.length; ++i)
77         {
78             LOG.debug( "Writer format found: "+names[i] );
79         }
80         
81         assertFormatName();
82     }
83     
84     
85     /**
86      * Use the given writerFormatName to write images to disk.
87      *
88      * @exception IllegalArgumentException thrown if the given name is not
89      * supported.
90      */

91     public DefaultScreenScraper( String JavaDoc writerFormatName )
92     {
93         this.writerFormatName = writerFormatName;
94         
95         assertFormatName();
96     }
97     
98     
99     //-------------------------------------------------------------------------
100
// Public methods
101

102     
103
104     /**
105      * Write the given image to the given file.
106      *
107      * @param image The screen image to write to disk.
108      * @param file The File to save the image as.
109      * @exception IOException thrown if there was a problem saving the image to
110      * the file.
111      * @exception IllegalArgumentException if any parameter is <tt>null</tt>.
112      * @exception IllegalStateException if no appropriate writer could be found.
113      */

114     public void writeImageToFile( BufferedImage JavaDoc image, File JavaDoc file )
115             throws IOException JavaDoc
116     {
117         boolean result = ImageIO.write( image, this.writerFormatName, file );
118         if (!result)
119         {
120             throw new IllegalStateException JavaDoc(
121                 "No appropriate writer was found." );
122         }
123     }
124     
125     
126     
127     /**
128      * Discover the file extention for images created by this scraper.
129      * This does not include a '.'.
130      *
131      * @return the extention for files this scraper writes.
132      */

133     public String JavaDoc getFileExtention()
134     {
135         return this.writerFormatName;
136     }
137     
138     
139     
140     //-------------------------------------------------------------------------
141
// Protected methods
142

143     
144     /**
145      * Discover if the current format name is supported.
146      *
147      * @exception IllegalArgumentException thrown if the current name is not
148      * supported.
149      */

150     protected void assertFormatName()
151     {
152         Iterator JavaDoc iter = ImageIO.getImageWritersByFormatName(
153             this.writerFormatName );
154         if (iter == null || !iter.hasNext())
155         {
156             throw new IllegalArgumentException JavaDoc(
157                 "Image Format " + this.writerFormatName +
158                 " is not supported in the current runtime system." );
159         }
160     }
161 }
162
163
Popular Tags