KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)VirtualWindowController.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.Robot JavaDoc;
30 import java.awt.Window JavaDoc;
31 import java.awt.Dimension JavaDoc;
32 import java.awt.Rectangle JavaDoc;
33
34 import net.sourceforge.groboutils.uicapture.v1.event.MouseMovedCaptureEvent;
35 import net.sourceforge.groboutils.uicapture.v1.event.MousePressedCaptureEvent;
36 import net.sourceforge.groboutils.uicapture.v1.event.MouseReleasedCaptureEvent;
37 import net.sourceforge.groboutils.uicapture.v1.event.MouseWheelCaptureEvent;
38 import net.sourceforge.groboutils.uicapture.v1.event.KeyPressedCaptureEvent;
39 import net.sourceforge.groboutils.uicapture.v1.event.KeyReleasedCaptureEvent;
40
41 import java.io.File JavaDoc;
42 import java.io.IOException JavaDoc;
43
44
45 /**
46  * A window which covers the whole screen, and does not paint in the background.
47  * It captures keyboard and mouse events, and sends them to both all registered
48  * listeners, and to the underlying GUI as well.
49  *
50  * WARNING: if the screen size is to resize, then this will not work correctly.
51  *
52  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
53  * @version Jan 4, 2002
54  */

55 public class VirtualWindowController
56 {
57     //-------------------------------------------------------------------------
58
// Private fields
59

60     private VirtualWindow window = null;
61     private IScreenScraper ss = null;
62     private IFocusedWindowFinder fwf = null;
63     
64     private static final long MAX_ROBOT_DELAY = 60000L;
65     
66     
67     
68     //-------------------------------------------------------------------------
69
// Constructors
70

71     /**
72      * Uses the Default version for the IScreenScraper and IFocusedWindowFinder.
73      */

74     public VirtualWindowController()
75     {
76         this( new DefaultScreenScraper(), new DefaultFocusedWindowFinder() );
77     }
78     
79     
80     /**
81      * Create using the given arguments to build the framework.
82      */

83     public VirtualWindowController( IScreenScraper ss,
84             IFocusedWindowFinder fwf )
85     {
86         this.ss = ss;
87         this.fwf = fwf;
88     }
89     
90
91
92     //-------------------------------------------------------------------------
93
// Public methods
94

95     
96     /**
97      * Begin the reinactment process.
98      *
99      * @exception java.awt.AWTException thrown if a Robot is not supported
100      * in the current JDK implementation.
101      */

102     public void begin()
103             throws java.awt.AWTException JavaDoc
104     {
105         assertInactive();
106         this.window = new VirtualWindow( null, false );
107     }
108     
109     
110     /**
111      * Complete the reinactment process.
112      */

113     public void end()
114     {
115         assertActive();
116         this.window.dispose();
117         this.window = null;
118     }
119     
120     
121     /**
122      * Pause for a period of milliseconds. Since Robot can only delay for
123      * 60 seconds at a time, this will first sleep using the Robot, then
124      * will use Thread for the remainder of the time.
125      *
126      * @param milliseconds Time in milliseconds to sleep.
127      */

128     public void sleep( long milliseconds )
129     {
130         assertActive();
131         
132         int ms = (milliseconds > MAX_ROBOT_DELAY ? (int)MAX_ROBOT_DELAY :
133             (int)milliseconds);
134         long remainder = milliseconds - ms;
135         
136         this.window.delay( ms );
137         if (remainder > 0)
138         {
139             try
140             {
141                 Thread.sleep( remainder );
142             }
143             catch (InterruptedException JavaDoc ie)
144             {
145                 // ignore
146
}
147         }
148     }
149     
150     
151     /**
152      * Waits until all events currently on the event queue have been processed.
153      */

154     public void waitForIdle()
155     {
156         assertActive();
157         this.window.waitForIdle();
158     }
159     
160     
161     /**
162      * Retrieves the size and position of the capture area of the screen.
163      *
164      * @return the screen size.
165      */

166     public Rectangle JavaDoc getBounds()
167     {
168         assertActive();
169         return this.window.getWindow().getCoveredScreen();
170     }
171     
172     
173     /**
174      * Compares the two files for identity.
175      *
176      * @return <tt>true</tt> if the files are identical, else <tt>false</tt>.
177      */

178     public boolean compareFiles( File JavaDoc one, File JavaDoc two )
179             throws IOException JavaDoc
180     {
181         throw new IllegalStateException JavaDoc( "Not implemented yet." );
182     }
183     
184     
185     public String JavaDoc getImageExtension()
186     {
187         return this.ss.getFileExtention();
188     }
189     
190     
191     /**
192      * Saves the entire screen to a file.
193      *
194      * @param filename the file to save the image as.
195      * @exception IOException thrown if there was a problem writing the file.
196      */

197     public void saveScreen( String JavaDoc filename )
198             throws IOException JavaDoc
199     {
200         assertActive();
201         saveScreenImage( filename, new Rectangle JavaDoc( getBounds() ) );
202     }
203     
204     
205     /**
206      * Saves a picture of the underlying UI's focused window to a file.
207      *
208      * @param filename the file to save the image as.
209      * @exception IOException thrown if there was a problem writing the file.
210      */

211     public void saveFocusedWindow( String JavaDoc filename )
212             throws IOException JavaDoc
213     {
214         assertActive();
215         Rectangle JavaDoc bounds = this.fwf.getFocusedWindowBounds();
216         if (bounds == null)
217         {
218             // save the entire screen
219
saveScreen( filename );
220         }
221         else
222         {
223             saveScreenImage( filename, bounds );
224         }
225     }
226     
227     
228     /**
229      * Saves the selected screen part to a file.
230      *
231      * @param filename the file to save the image as.
232      * @param x the x position of the part of the screen to grab.
233      * @param y the y position of the part of the screen to grab.
234      * @param w the width of the part of the screen to grab.
235      * @param h the height of the part of the screen to grab.
236      * @exception IOException thrown if there was a problem writing the file.
237      */

238     public void saveScreenImage( String JavaDoc filename, int x, int y, int w, int h )
239             throws IOException JavaDoc
240     {
241         saveScreenImage( filename, new Rectangle JavaDoc( x, y, w, h ) );
242     }
243     
244     
245     /**
246      * Saves the selected screen part to a file.
247      *
248      * @param filename the file to save the image as.
249      * @param bounds the part of the screen to grab.
250      * @exception IOException thrown if there was a problem writing the file.
251      */

252     public void saveScreenImage( String JavaDoc filename, Rectangle JavaDoc bounds )
253             throws IOException JavaDoc
254     {
255         assertActive();
256         this.ss.writeImageToFile( this.window.createScreenScrape( bounds ),
257             new File JavaDoc( filename ) );
258     }
259     
260     
261     
262     
263     //-------------------------------------------------------------------------
264
// Event methods
265

266     
267     /**
268      * Simulate a mouse movement.
269      *
270      * @param x the x position (aboslute) to move the mouse to.
271      * @param y the y position (aboslute) to move the mouse to.
272      */

273     public void moveMouse( int x, int y )
274     {
275         assertActive();
276         MouseMovedCaptureEvent ce = new MouseMovedCaptureEvent( x, y );
277         this.window.simulateEvent( ce );
278     }
279     
280     
281     /**
282      * Simulate a mouse button press.
283      *
284      * @param modifiers the mouse button modifiers.
285      */

286     public void pressMouse( int modifiers )
287     {
288         assertActive();
289         MousePressedCaptureEvent ce = new MousePressedCaptureEvent( modifiers );
290         this.window.simulateEvent( ce );
291     }
292     
293     
294     /**
295      * Simulate a mouse button release.
296      *
297      * @param modifiers the mouse button modifiers.
298      */

299     public void releaseMouse( int modifiers )
300     {
301         assertActive();
302         MouseReleasedCaptureEvent ce =
303             new MouseReleasedCaptureEvent( modifiers );
304         this.window.simulateEvent( ce );
305     }
306     
307     
308     /**
309      * Simulate a mouse wheel rotation.
310      *
311      * @param rotation the number of 'clicks' to rotate the mouse wheel.
312      */

313     public void rotateMouseWheel( int rotation )
314     {
315         assertActive();
316         MouseWheelCaptureEvent ce = new MouseWheelCaptureEvent( rotation );
317         this.window.simulateEvent( ce );
318     }
319     
320     
321     /**
322      * Simulate a key press.
323      *
324      * @param keyCode the key code of the key.
325      */

326     public void pressKey( int keyCode )
327     {
328         assertActive();
329         KeyPressedCaptureEvent ce = new KeyPressedCaptureEvent( keyCode );
330         this.window.simulateEvent( ce );
331     }
332     
333     
334     /**
335      * Simulate a key release.
336      *
337      * @param keyCode the key code of the key.
338      */

339     public void releaseKey( int keyCode )
340     {
341         assertActive();
342         KeyReleasedCaptureEvent ce = new KeyReleasedCaptureEvent( keyCode );
343         this.window.simulateEvent( ce );
344     }
345     
346     
347     
348     //-------------------------------------------------------------------------
349
// Protected methods
350

351     protected void assertActive()
352     {
353         if (this.window == null)
354         {
355             throw new IllegalStateException JavaDoc("Window is not active.");
356         }
357     }
358
359
360     protected void assertInactive()
361     {
362         if (this.window != null)
363         {
364             throw new IllegalStateException JavaDoc("Window is active.");
365         }
366     }
367 }
368
369
Popular Tags