KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ScriptGenerator.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.Rectangle JavaDoc;
30
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 import net.sourceforge.groboutils.uicapture.v1.event.ICaptureListener;
35 import net.sourceforge.groboutils.uicapture.v1.event.CaptureEvent;
36 import net.sourceforge.groboutils.uicapture.v1.event.MouseWheelCaptureEvent;
37 import net.sourceforge.groboutils.uicapture.v1.event.MouseMovedCaptureEvent;
38 import net.sourceforge.groboutils.uicapture.v1.event.MouseButtonCaptureEvent;
39 import net.sourceforge.groboutils.uicapture.v1.event.MousePressedCaptureEvent;
40 import net.sourceforge.groboutils.uicapture.v1.event.MouseReleasedCaptureEvent;
41 import net.sourceforge.groboutils.uicapture.v1.event.KeyCaptureEvent;
42 import net.sourceforge.groboutils.uicapture.v1.event.KeyPressedCaptureEvent;
43 import net.sourceforge.groboutils.uicapture.v1.event.KeyReleasedCaptureEvent;
44 import net.sourceforge.groboutils.uicapture.v1.event.IAllowCapturePassThroughListener;
45
46
47 /**
48  * Listens to CaptureEvents, and converts these into script commands to the
49  * IScriptMaker. It allows for easy subclassing through having a "Meta-Mode".
50  * All events will be swallowed (not passed to the UI or IScriptMaker)
51  * while in this meta-mode.
52  *
53  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
54  * @version Jan 7, 2002
55  */

56 public class ScriptGenerator implements ICaptureListener,
57         IAllowCapturePassThroughListener
58 {
59     private IScriptMaker maker = null;
60     private boolean metaMode = false;
61     private long lastEventTime = -1;
62     private VirtualWindow window = null;
63     private IScreenScraper ss = null;
64     private IFocusedWindowFinder fwf = null;
65     private String JavaDoc baseImageName = null;
66     private int filenameIndex = 0;
67     private boolean isRunning = false;
68     
69     
70     //-------------------------------------------------------------------------
71
// Constructors
72

73     /**
74      * Uses the Default version for the IScreenScraper and IFocusedWindowFinder.
75      */

76     public ScriptGenerator( IScriptMaker maker, String JavaDoc baseImageName )
77     {
78         this( new DefaultScreenScraper(), new DefaultFocusedWindowFinder(),
79             maker, baseImageName );
80     }
81     
82     
83     /**
84      * Create using the given arguments to build the framework.
85      */

86     public ScriptGenerator( IScreenScraper ss,
87             IFocusedWindowFinder fwf, IScriptMaker maker, String JavaDoc baseImageName )
88     {
89         this.ss = ss;
90         this.fwf = fwf;
91         this.maker = maker;
92         this.baseImageName = baseImageName;
93     }
94
95     
96     //-------------------------------------------------------------------------
97
// Public Methods
98

99     
100     /*
101      * Should this be here ????
102     public VirtualWindow getVirtualWindow()
103     {
104         return this.window;
105     }
106      */

107
108     /**
109      *
110      * @exception java.awt.AWTException Robot isn't supported.
111      */

112     public void begin()
113             throws java.awt.AWTException JavaDoc
114     {
115         this.window = new VirtualWindow( null, true );
116         this.window.addCaptureListener( this );
117         this.maker.start();
118         this.isRunning = true;
119     }
120     
121     
122     public void end()
123     {
124         this.isRunning = false;
125         fireStop();
126     }
127     
128     
129     public boolean isRunning()
130     {
131         return this.isRunning;
132     }
133      
134      
135
136
137     //-------------------------------------------------------------------------
138
// Meta Methods
139

140     /**
141      *
142      */

143     public boolean isInMetaMode()
144     {
145         return this.metaMode;
146     }
147     
148     
149     /**
150      *
151      */

152     public void setInMetaMode( boolean set )
153     {
154         this.metaMode = set;
155     }
156     
157     
158     /**
159      * Performs a meta-mode action when this event is caught and the
160      * generator is in meta-mode. Default action does nothing.
161      */

162     protected void metaMouseWheelMoved( MouseWheelCaptureEvent ce )
163     {
164         // do nothing
165
}
166     
167     
168     /**
169      * Performs a meta-mode action when this event is caught and the
170      * generator is in meta-mode. Default action does nothing.
171      */

172     protected void metaMousePressed( MousePressedCaptureEvent ce )
173     {
174         // do nothing
175
}
176     
177     
178     /**
179      * Performs a meta-mode action when this event is caught and the
180      * generator is in meta-mode. Default action does nothing.
181      */

182     protected void metaMouseReleased( MouseReleasedCaptureEvent ce )
183     {
184         // do nothing
185
}
186     
187     
188     /**
189      * Performs a meta-mode action when this event is caught and the
190      * generator is in meta-mode. Default action does nothing.
191      */

192     protected void metaKeyPressed( KeyPressedCaptureEvent ce )
193     {
194         // do nothing
195
}
196     
197     
198     /**
199      * Performs a meta-mode action when this event is caught and the
200      * generator is in meta-mode. Default action does nothing.
201      */

202     protected void metaKeyReleased( KeyReleasedCaptureEvent ce )
203     {
204         // do nothing
205
}
206     
207     
208     /**
209      * Performs a meta-mode action when this event is caught and the
210      * generator is in meta-mode. Default action does nothing.
211      */

212     protected void metaMouseMoved( MouseMovedCaptureEvent ce )
213     {
214         // do nothing
215
}
216
217     
218     //-------------------------------------------------------------------------
219
// Fire ScriptMaker Event Methods
220

221     
222     /**
223      *
224      */

225     protected synchronized void fireStop()
226     {
227         if (this.window != null)
228         {
229             this.window.removeCaptureListener( this );
230             
231             if (isRunning())
232             {
233                 this.maker.end();
234             }
235             
236             this.window.dispose();
237             this.window = null;
238         }
239     }
240     
241     
242     /**
243      * Saves the entire screen to a file.
244      *
245      * @param filename the file to save the image as.
246      */

247     protected synchronized void fireSaveScreen()
248     {
249         if (isRunning())
250         {
251             fireSaveScreenImage( new Rectangle JavaDoc(
252                 this.window.getWindow().getCoveredScreen() ) );
253         }
254     }
255     
256     
257     /**
258      * Saves a picture of the underlying UI's focused window to a file.
259      *
260      * @param filename the file to save the image as.
261      */

262     protected synchronized void fireSaveFocusedWindow()
263     {
264         if (isRunning())
265         {
266             Rectangle JavaDoc bounds = this.fwf.getFocusedWindowBounds();
267             if (bounds == null)
268             {
269                 // save the entire screen
270
fireSaveScreen();
271             }
272             else
273             {
274                 fireSaveScreenImage( bounds );
275             }
276         }
277     }
278     
279     
280     /**
281      * Saves the selected screen part to a file.
282      *
283      * @param filename the file to save the image as.
284      * @param x the x position of the part of the screen to grab.
285      * @param y the y position of the part of the screen to grab.
286      * @param w the width of the part of the screen to grab.
287      * @param h the height of the part of the screen to grab.
288      */

289     protected synchronized void fireSaveScreenImage( int x, int y, int w,int h )
290     {
291         if (isRunning())
292         {
293             fireSaveScreenImage( new Rectangle JavaDoc( x, y, w, h ) );
294         }
295     }
296     
297     
298     /**
299      * Saves the selected screen part to a file.
300      *
301      * @param filename the file to save the image as.
302      * @param bounds the part of the screen to grab.
303      */

304     protected synchronized void fireSaveScreenImage( Rectangle JavaDoc bounds )
305     {
306         if (isRunning())
307         {
308             File JavaDoc f = createFile();
309             try
310             {
311                 this.ss.writeImageToFile(
312                     this.window.createScreenScrape(bounds ),
313                     f );
314                 this.maker.generateScreenCapture( f,
315                     bounds.x, bounds.y, bounds.width, bounds.height );
316             }
317             catch (IOException JavaDoc ioe)
318             {
319                 encounteredError( ioe );
320             }
321         }
322     }
323     
324     
325     /**
326      *
327      */

328     protected synchronized void fireMouseWheelMoved( MouseWheelCaptureEvent ce )
329     {
330         if (isRunning())
331         {
332             generateDelay( ce );
333             this.maker.generateMoveMouseWheel( ce.getWheelRotation() );
334         }
335     }
336
337     
338     /**
339      *
340      */

341     protected synchronized void fireMouseMoved( MouseMovedCaptureEvent ce )
342     {
343         if (isRunning())
344         {
345             generateDelay( ce );
346             this.maker.generateMoveMouse( ce.getX(), ce.getY() );
347         }
348     }
349
350     
351     /**
352      *
353      */

354     protected synchronized void fireMousePressed( MouseButtonCaptureEvent ce )
355     {
356         if (isRunning())
357         {
358             generateDelay( ce );
359             this.maker.generatePressMouse( ce.getModifiers() );
360         }
361     }
362
363     
364     /**
365      *
366      */

367     protected synchronized void fireMouseReleased( MouseButtonCaptureEvent ce )
368     {
369         if (isRunning())
370         {
371             generateDelay( ce );
372             this.maker.generateReleaseMouse( ce.getModifiers() );
373         }
374     }
375
376     
377     /**
378      *
379      */

380     protected synchronized void fireKeyPressed( KeyCaptureEvent ce )
381     {
382         if (isRunning())
383         {
384             generateDelay( ce );
385             this.maker.generatePressKey( ce.getKeyCode() );
386         }
387     }
388
389     
390     /**
391      *
392      */

393     protected synchronized void fireKeyReleased( KeyCaptureEvent ce )
394     {
395         if (isRunning())
396         {
397             generateDelay( ce );
398             this.maker.generateReleaseKey( ce.getKeyCode() );
399         }
400     }
401
402     
403     //-------------------------------------------------------------------------
404
// Event Methods
405

406     
407     /**
408      * If a subclass overrides this method, it <b>must</b> first check
409      * if meta-mode is enabled, and invoke
410      * the super's version of this method at the very end (right before
411      * leaving the method).
412      */

413     public void mouseWheelMoved( MouseWheelCaptureEvent ce )
414     {
415         if (allowMouseWheelMoved( ce ))
416         {
417             fireMouseWheelMoved( ce );
418         }
419         else
420         {
421             metaMouseWheelMoved( ce );
422         }
423     }
424     
425     
426     /**
427      * If a subclass overrides this method, it <b>must</b> first check
428      * if meta-mode is enabled, and invoke
429      * the super's version of this method at the very end (right before
430      * leaving the method).
431      */

432     public void mouseMoved( MouseMovedCaptureEvent ce )
433     {
434         if (allowMouseMoved( ce ))
435         {
436             fireMouseMoved( ce );
437         }
438         else
439         {
440             metaMouseMoved( ce );
441         }
442     }
443     
444     
445     /**
446      * If a subclass overrides this method, it <b>must</b> first check
447      * if meta-mode is enabled, and invoke
448      * the super's version of this method at the very end (right before
449      * leaving the method).
450      */

451     public void mousePressed( MousePressedCaptureEvent ce )
452     {
453         if (allowMousePressed( ce ))
454         {
455             fireMousePressed( ce );
456         }
457         else
458         {
459             metaMousePressed( ce );
460         }
461     }
462     
463     
464     /**
465      * If a subclass overrides this method, it <b>must</b> first check
466      * if meta-mode is enabled, and invoke
467      * the super's version of this method at the very end (right before
468      * leaving the method).
469      */

470     public void mouseReleased( MouseReleasedCaptureEvent ce )
471     {
472         if (allowMouseReleased( ce ))
473         {
474             fireMouseReleased( ce );
475         }
476         else
477         {
478             metaMouseReleased( ce );
479         }
480     }
481     
482     
483     /**
484      * If a subclass overrides this method, it <b>must</b> first check
485      * if meta-mode is enabled, and invoke
486      * the super's version of this method at the very end (right before
487      * leaving the method).
488      */

489     public void keyPressed( KeyPressedCaptureEvent ce )
490     {
491         if (allowKeyPressed( ce ))
492         {
493             fireKeyPressed( ce );
494         }
495         else
496         {
497             metaKeyPressed( ce );
498         }
499     }
500     
501     
502     /**
503      * If a subclass overrides this method, it <b>must</b> first check
504      * if meta-mode is enabled, and invoke
505      * the super's version of this method at the very end (right before
506      * leaving the method).
507      */

508     public void keyReleased( KeyReleasedCaptureEvent ce )
509     {
510         if (allowKeyReleased( ce ))
511         {
512             fireKeyReleased( ce );
513         }
514         else
515         {
516             metaKeyReleased( ce );
517         }
518     }
519     
520     
521     //-------------
522

523     
524     /**
525      * @see java.awt.event.MouseWheelListener
526      *
527      * @return <tt>true</tt> to allow the event to passed to the underlying
528      * UI, or <tt>false</tt> to 'swallow' the event.
529      */

530     public final boolean allowMouseWheelMoved( MouseWheelCaptureEvent ce )
531     {
532         return !isInMetaMode();
533     }
534     
535     
536     /**
537      * @see java.awt.event.MouseListener
538      *
539      * @return <tt>true</tt> to allow the event to passed to the underlying
540      * UI, or <tt>false</tt> to 'swallow' the event.
541      */

542     public final boolean allowMousePressed( MousePressedCaptureEvent ce )
543     {
544         return !isInMetaMode();
545     }
546     
547     
548     /**
549      * @see java.awt.event.MouseListener
550      *
551      * @return <tt>true</tt> to allow the event to passed to the underlying
552      * UI, or <tt>false</tt> to 'swallow' the event.
553      */

554     public final boolean allowMouseReleased( MouseReleasedCaptureEvent ce )
555     {
556         return !isInMetaMode();
557     }
558     
559     
560     /**
561      * @see java.awt.event.KeyListener
562      *
563      * @return <tt>true</tt> to allow the event to passed to the underlying
564      * UI, or <tt>false</tt> to 'swallow' the event.
565      */

566     public final boolean allowKeyPressed( KeyPressedCaptureEvent ce )
567     {
568         return !isInMetaMode();
569     }
570     
571     
572     /**
573      * @see java.awt.event.KeyListener
574      *
575      * @return <tt>true</tt> to allow the event to passed to the underlying
576      * UI, or <tt>false</tt> to 'swallow' the event.
577      */

578     public final boolean allowKeyReleased( KeyReleasedCaptureEvent ce )
579     {
580         return !isInMetaMode();
581     }
582     
583     
584     /**
585      * Not part of the allow interface.
586      *
587      * @return <tt>true</tt> to allow the event to passed to the underlying
588      * UI, or <tt>false</tt> to 'swallow' the event.
589      */

590     public final boolean allowMouseMoved( MouseMovedCaptureEvent ce )
591     {
592         return !isInMetaMode();
593     }
594     
595     
596     
597     //-------------------------------------------------------------------------
598
// Misc Protected Methods
599

600     
601     /**
602      * Create a delay event in the script maker.
603      */

604     protected void generateDelay( CaptureEvent ce )
605     {
606         long delay = getDelayTime( ce );
607         if (delay > 0)
608         {
609             this.maker.generateDelay( delay );
610         }
611     }
612     
613     
614     /**
615      * Calculate the amount of time between the last event and the given
616      * event.
617      *
618      * @return the delay time in milliseconds.
619      */

620     protected synchronized long getDelayTime( CaptureEvent ce )
621     {
622         long waitTime = ce.getTimeOfEvent();
623         if (this.lastEventTime < 0)
624         {
625             this.lastEventTime = waitTime;
626             return 0;
627         }
628         if (waitTime < 0 || waitTime < this.lastEventTime)
629         {
630             return 0;
631         }
632         long diff = waitTime - this.lastEventTime;
633         this.lastEventTime = waitTime;
634         return diff;
635     }
636     
637     
638     /**
639      *
640      */

641     protected synchronized File JavaDoc createFile()
642     {
643         File JavaDoc f = new File JavaDoc( this.baseImageName + this.filenameIndex + '.' +
644             this.ss.getFileExtention() );
645         ++this.filenameIndex;
646         
647         return f;
648     }
649     
650     
651     /**
652      *
653      */

654     protected void encounteredError( Throwable JavaDoc t )
655     {
656         t.printStackTrace();
657     }
658 }
659
660
Popular Tags