1 26 27 package net.sourceforge.groboutils.uicapture.v1; 28 29 import java.awt.Robot ; 30 import java.awt.Window ; 31 import java.awt.Dimension ; 32 import java.awt.Rectangle ; 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 ; 42 import java.io.IOException ; 43 44 45 55 public class VirtualWindowController 56 { 57 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 71 74 public VirtualWindowController() 75 { 76 this( new DefaultScreenScraper(), new DefaultFocusedWindowFinder() ); 77 } 78 79 80 83 public VirtualWindowController( IScreenScraper ss, 84 IFocusedWindowFinder fwf ) 85 { 86 this.ss = ss; 87 this.fwf = fwf; 88 } 89 90 91 92 95 96 102 public void begin() 103 throws java.awt.AWTException 104 { 105 assertInactive(); 106 this.window = new VirtualWindow( null, false ); 107 } 108 109 110 113 public void end() 114 { 115 assertActive(); 116 this.window.dispose(); 117 this.window = null; 118 } 119 120 121 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 ie) 144 { 145 } 147 } 148 } 149 150 151 154 public void waitForIdle() 155 { 156 assertActive(); 157 this.window.waitForIdle(); 158 } 159 160 161 166 public Rectangle getBounds() 167 { 168 assertActive(); 169 return this.window.getWindow().getCoveredScreen(); 170 } 171 172 173 178 public boolean compareFiles( File one, File two ) 179 throws IOException 180 { 181 throw new IllegalStateException ( "Not implemented yet." ); 182 } 183 184 185 public String getImageExtension() 186 { 187 return this.ss.getFileExtention(); 188 } 189 190 191 197 public void saveScreen( String filename ) 198 throws IOException 199 { 200 assertActive(); 201 saveScreenImage( filename, new Rectangle ( getBounds() ) ); 202 } 203 204 205 211 public void saveFocusedWindow( String filename ) 212 throws IOException 213 { 214 assertActive(); 215 Rectangle bounds = this.fwf.getFocusedWindowBounds(); 216 if (bounds == null) 217 { 218 saveScreen( filename ); 220 } 221 else 222 { 223 saveScreenImage( filename, bounds ); 224 } 225 } 226 227 228 238 public void saveScreenImage( String filename, int x, int y, int w, int h ) 239 throws IOException 240 { 241 saveScreenImage( filename, new Rectangle ( x, y, w, h ) ); 242 } 243 244 245 252 public void saveScreenImage( String filename, Rectangle bounds ) 253 throws IOException 254 { 255 assertActive(); 256 this.ss.writeImageToFile( this.window.createScreenScrape( bounds ), 257 new File ( filename ) ); 258 } 259 260 261 262 263 266 267 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 286 public void pressMouse( int modifiers ) 287 { 288 assertActive(); 289 MousePressedCaptureEvent ce = new MousePressedCaptureEvent( modifiers ); 290 this.window.simulateEvent( ce ); 291 } 292 293 294 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 313 public void rotateMouseWheel( int rotation ) 314 { 315 assertActive(); 316 MouseWheelCaptureEvent ce = new MouseWheelCaptureEvent( rotation ); 317 this.window.simulateEvent( ce ); 318 } 319 320 321 326 public void pressKey( int keyCode ) 327 { 328 assertActive(); 329 KeyPressedCaptureEvent ce = new KeyPressedCaptureEvent( keyCode ); 330 this.window.simulateEvent( ce ); 331 } 332 333 334 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 351 protected void assertActive() 352 { 353 if (this.window == null) 354 { 355 throw new IllegalStateException ("Window is not active."); 356 } 357 } 358 359 360 protected void assertInactive() 361 { 362 if (this.window != null) 363 { 364 throw new IllegalStateException ("Window is active."); 365 } 366 } 367 } 368 369 | Popular Tags |