KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > uicapture > v1 > javamaker > JavaMaker


1 /*
2  * @(#)JavaScriptMaker.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.javamaker;
28
29 import net.sourceforge.groboutils.uicapture.v1.VirtualWindowController;
30 import net.sourceforge.groboutils.uicapture.v1.IScriptMaker;
31
32 import java.io.Writer JavaDoc;
33 import java.io.FileWriter JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.File JavaDoc;
37
38
39 /**
40  * Creates a Java playback sourcefile. The Java file will quit with an error
41  * code of 1 if the playback did not go perfectly.
42  *
43  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
44  * @version Jan 7, 2002
45  */

46 public class JavaMaker implements IScriptMaker
47 {
48     private Writer JavaDoc outF = null;
49     private PrintWriter JavaDoc out = null;
50     private String JavaDoc packageName = null;
51     private String JavaDoc className = null;
52     private int step = 0;
53     
54     
55     public JavaMaker( File JavaDoc outFile, String JavaDoc packageName, String JavaDoc className )
56             throws IOException JavaDoc
57     {
58         if (outFile == null || packageName == null || className == null)
59         {
60             throw new IllegalArgumentException JavaDoc( "no null args" );
61         }
62         this.outF = new FileWriter JavaDoc( outFile );
63         this.out = new PrintWriter JavaDoc( outF );
64         
65         this.packageName = packageName;
66         this.className = className;
67     }
68     
69     
70     public JavaMaker( Writer JavaDoc w, String JavaDoc packageName, String JavaDoc className )
71     {
72         if (w == null || packageName == null || className == null)
73         {
74             throw new IllegalArgumentException JavaDoc( "no null args" );
75         }
76         this.outF = w;
77         this.out = new PrintWriter JavaDoc( w );
78         
79         this.packageName = packageName;
80         this.className = className;
81     }
82     
83     
84     //-------------------------------------------------------------------------
85
// Public methods
86

87     /**
88      * Starts the script generation.
89      */

90     public void start()
91     {
92         if (this.step > 0)
93         {
94             throw new IllegalStateException JavaDoc("Already started generation.");
95         }
96         
97         wrln( "/"+"*" );
98         wrln( " * Pregenerated script file." );
99         wrln( " * Created "+(new java.util.Date JavaDoc()).toString() );
100         wrln( " *"+"/" );
101         wrln();
102         wrln( "package "+this.packageName+";" );
103         wrln();
104         wrln( "import "+VirtualWindowController.class.getName()+";" );
105         wrln( "import "+File JavaDoc.class.getName()+";" );
106         wrln();
107         wrln( "public class "+this.className );
108         wrln( "{" );
109         wrln( " public static void main( String[] args )" );
110         wrln( " throws Exception" );
111         wrln( " {" );
112         step( "Setup." );
113         wrln( " VirtualWindowController vwc = new " );
114         wrln( " VirtualWindowController();" );
115         wrln( " vwc.begin();" );
116         wrln( " try {" );
117         wrln( " vwc.sleep( 2000 );" );
118         
119         // bug 618295
120
this.step = 1;
121     }
122     
123     
124     /**
125      * Terminates the script generation.
126      */

127     public void end()
128     {
129         assertActive();
130         
131         wrln( " /"+"/ end of script" );
132         wrln( " System.out.println( \"No errors found in playback.\" ):");
133         wrln( " System.exit( 0 );" );
134         wrln( " } finally {" );
135         wrln( " vwc.end();" );
136         wrln( " vwc = null;" );
137         wrln( " }" );
138         wrln( " }" );
139         wrln( "}" );
140         
141         // bug 618295
142
this.step = 2;
143         
144         try
145         {
146             this.out.close();
147             this.outF.close();
148         }
149         catch (IOException JavaDoc ioe)
150         {
151             handleException( ioe );
152         }
153         finally
154         {
155             this.out = null;
156             this.outF = null;
157         }
158     }
159     
160     
161     /**
162      * Cause the script to delay for the given number of milliseconds.
163      */

164     public void generateDelay( long milliseconds )
165     {
166         assertActive();
167         
168         step( "sleep for "+milliseconds+" milliseconds." );
169         wrln( " vwc.sleep( "+milliseconds+" );" );
170     }
171
172     
173     /**
174      * Cause the script to rotate the mouse wheel.
175      */

176     public void generateMoveMouseWheel( int rotate )
177     {
178         assertActive();
179         
180         step( "rotate mouse wheel "+rotate+" clicks." );
181         wrln( " vwc.moveMouseWheel( "+rotate+" );" );
182     }
183
184     
185     /**
186      * Cause the script to move the mouse.
187      */

188     public void generateMoveMouse( int x, int y )
189     {
190         assertActive();
191         
192         step( "move mouse to ("+x+", "+y+")." );
193         wrln( " vwc.moveMouse( "+x+", "+y+" );" );
194     }
195
196     
197     /**
198      * Cause the script to press a mouse button.
199      */

200     public void generatePressMouse( int modifier )
201     {
202         assertActive();
203         
204         step( "press mouse with "+modifier+"." );
205         wrln( " vwc.pressMouse( "+modifier+" );" );
206     }
207
208     
209     /**
210      * Cause the script to release a mouse button.
211      */

212     public void generateReleaseMouse( int modifier )
213     {
214         assertActive();
215         
216         step( "release mouse with "+modifier+"." );
217         wrln( " vwc.releaseMouse( "+modifier+" );" );
218     }
219
220     
221     /**
222      * Cause the script to press a key.
223      */

224     public void generatePressKey( int keyCode )
225     {
226         assertActive();
227         
228         step( "press key with "+keyCode+"." );
229         wrln( " vwc.pressKey( "+keyCode+" );" );
230     }
231
232     
233     /**
234      * Cause the script to release a key.
235      */

236     public void generateReleaseKey( int keyCode )
237     {
238         assertActive();
239         
240         step( "release key with "+keyCode+"." );
241         wrln( " vwc.releaseKey( "+keyCode+" );" );
242     }
243
244     
245     /**
246      * Cause the script to capture a screen shot. Probably, it should compare
247      * the captured image against the original saved image.
248      *
249      * @param x screen image bounds.
250      * @param y screen image bounds.
251      * @param width screen image bounds.
252      * @param height screen image bounds.
253      */

254     public void generateScreenCapture( File JavaDoc originalImage,
255             int x, int y, int width, int height )
256     {
257         assertActive();
258         
259         step( "capture and compare screen in ("+x+", "+y+", "+width+", "+
260             height+" ) against "+originalImage+"." );
261         wrln( " File f"+this.step+" = new File( \""+
262             className + "-" + this.step + ".\" + vwc.getImageExtension() );" );
263         wrln( " vwc.saveScreenImage( f"+this.step+".toString(), "+x+
264             ", "+y+", "+width+", "+height+" );" );
265         wrln( " if (!vwc.compareFiles( f"+step+", new File( \"" +
266             originalImage + "\" ))" );
267         wrln( " {" );
268         wrln( " System.out.println( \"Images did not match on step "+
269             this.step+".\" );" );
270         wrln( " System.out.println( \"Test Failed.\" );" );
271         wrln( " vwc.end();" );
272         wrln( " vwc = null;" );
273         wrln( " Thread.sleep( 1000 );" );
274         wrln( " System.exit(1);" );
275         wrln( " }" );
276         wrln( " System.out.println( \"** Images match!\" );");
277     }
278     
279     
280     //-------------------------------------------------------------------------
281

282     protected void handleException( Throwable JavaDoc t )
283     {
284         t.printStackTrace();
285         this.out = null;
286         this.outF = null;
287         throw new IllegalStateException JavaDoc("Encountered exception "+t);
288     }
289     
290     protected void assertActive()
291     {
292         if (this.out == null)
293         {
294             throw new IllegalStateException JavaDoc("Not open for writing.");
295         }
296         if (this.step <= 0)
297         {
298             throw new IllegalStateException JavaDoc("Never started writing.");
299         }
300     }
301     
302     
303     protected void wr( String JavaDoc text )
304     {
305         this.out.print( text );
306     }
307     protected void wrln( String JavaDoc text )
308     {
309         this.out.println( text );
310     }
311     protected void wrln()
312     {
313         this.out.println( "" );
314     }
315     
316     
317     protected void step( String JavaDoc msg )
318     {
319         ++this.step;
320         wrln( " System.out.println( \"Step "+this.step+": "+
321             msg +"\" );" );
322     }
323 }
324
325
Popular Tags