1 26 27 package net.sourceforge.groboutils.uicapture.v1; 28 29 import java.awt.Robot ; 30 import java.awt.Frame ; 31 import java.awt.Rectangle ; 32 import java.awt.image.BufferedImage ; 33 import java.awt.event.KeyListener ; 34 import java.awt.event.MouseListener ; 35 import java.awt.event.MouseMotionListener ; 36 import java.awt.event.MouseWheelListener ; 37 import java.awt.event.MouseEvent ; 38 import java.awt.event.KeyEvent ; 39 import java.awt.event.MouseWheelEvent ; 40 41 import java.util.LinkedList ; 42 import java.util.Iterator ; 43 44 import net.sourceforge.groboutils.uicapture.v1.event.ICaptureListener; 45 import net.sourceforge.groboutils.uicapture.v1.event.CaptureEvent; 46 import net.sourceforge.groboutils.uicapture.v1.event.KeyPressedCaptureEvent; 47 import net.sourceforge.groboutils.uicapture.v1.event.KeyReleasedCaptureEvent; 48 import net.sourceforge.groboutils.uicapture.v1.event.MousePressedCaptureEvent; 49 import net.sourceforge.groboutils.uicapture.v1.event.MouseReleasedCaptureEvent; 50 import net.sourceforge.groboutils.uicapture.v1.event.MouseMovedCaptureEvent; 51 import net.sourceforge.groboutils.uicapture.v1.event.MouseWheelCaptureEvent; 52 import net.sourceforge.groboutils.uicapture.v1.event.IAllowCapturePassThroughListener; 53 54 69 public class VirtualWindow 70 implements KeyListener , MouseListener , MouseMotionListener , 71 MouseWheelListener 72 { 73 76 private LinkedList captureListeners = new LinkedList (); 77 private Robot robot = null; 78 private VirtualWindowUI window = null; 79 private boolean enableGlass = true; 80 private Frame owningFrame = null; 81 82 private static final String DEFAULT_TITLE = "UI Capture"; 83 84 87 88 94 public VirtualWindow() 95 throws java.awt.AWTException 96 { 97 this( null, true ); 98 } 99 100 101 108 public VirtualWindow( String title, boolean enable ) 109 throws java.awt.AWTException 110 { 111 if (title == null) 112 { 113 title = DEFAULT_TITLE; 114 } 115 this.owningFrame = new Frame ( title ); 116 this.owningFrame.setSize( 0, 0 ); 117 this.window = new VirtualWindowUI( this.owningFrame ); 120 this.robot = new Robot (); 121 122 this.window.addKeyListener( this ); 123 this.window.addMouseListener( this ); 124 this.window.addMouseMotionListener( this ); 125 this.window.addMouseWheelListener( this ); 126 127 update(); 128 129 setGlassEnabled( enable ); 130 } 131 132 133 134 137 138 141 public void dispose() 142 { 143 this.window.removeKeyListener( this ); 144 this.window.removeMouseListener( this ); 145 this.window.removeMouseMotionListener( this ); 146 this.window.removeMouseWheelListener( this ); 147 148 this.window.dispose(); 149 this.owningFrame.dispose(); 150 this.captureListeners.clear(); 151 this.robot = null; 152 this.window = null; 153 } 154 155 156 160 public VirtualWindowUI getWindow() 161 { 162 return this.window; 163 } 164 165 166 174 public synchronized void setGlassEnabled( boolean on ) 175 { 176 this.enableGlass = on; 177 this.window.setGlassEnabled( on ); 178 } 179 180 181 187 public boolean isGlassEnabled() 188 { 189 return this.enableGlass; 190 } 191 192 193 200 public synchronized void simulateEvent( CaptureEvent ce ) 201 { 202 hide(); 203 204 try 206 { 207 ce.performEvent( this.robot ); 208 } 209 finally 210 { 211 show(); 212 } 213 } 214 215 216 224 public void delay( int ms ) 225 { 226 this.robot.delay( ms ); 227 } 228 229 230 234 public void waitForIdle() 235 { 236 this.robot.waitForIdle(); 237 } 238 239 240 246 public BufferedImage createScreenScrape() 247 { 248 return createScreenScrape( this.window.getCoveredScreen() ); 249 } 250 251 252 258 public BufferedImage createScreenScrape( Rectangle bounds ) 259 { 260 if (bounds == null || 262 !this.window.getCoveredScreen().contains( bounds )) 263 { 264 throw new IllegalArgumentException ("Bounds "+bounds+ 265 " is not within the screen dimension."); 266 } 267 268 hide(); 271 272 try 273 { 274 return this.robot.createScreenCapture( bounds ); 275 } 276 finally 277 { 278 show(); 279 } 280 } 281 282 283 290 public void addCaptureListener( ICaptureListener cl ) 291 { 292 if (cl != null) 293 { 294 this.captureListeners.add( cl ); 295 } 296 } 297 298 299 306 public void removeCaptureListener( ICaptureListener cl ) 307 { 308 if (cl != null) 309 { 310 this.captureListeners.remove( cl ); 311 } 312 } 313 314 315 320 public void hide() 321 { 322 if (this.enableGlass) 323 { 324 this.window.hide(); 325 } 326 } 327 328 329 334 public void show() 335 { 336 if (this.enableGlass) 337 { 338 this.window.show(); 339 } 340 } 341 342 343 346 public void update() 347 { 348 this.window.setBackground( createScreenScrape() ); 349 } 350 351 352 355 356 357 360 public void mouseWheelMoved( MouseWheelEvent me ) 361 { 362 MouseWheelCaptureEvent ce = new MouseWheelCaptureEvent( me ); 363 Iterator iter = getCaptureListeners(); 364 boolean allow = true; 365 while (iter.hasNext()) 366 { 367 ICaptureListener icl = (ICaptureListener)iter.next(); 368 if (icl instanceof IAllowCapturePassThroughListener) 369 { 370 if (!((IAllowCapturePassThroughListener)icl). 371 allowMouseWheelMoved( ce )) 372 { 373 allow = false; 374 } 375 } 376 icl.mouseWheelMoved( ce ); 377 } 378 if (allow) 379 { 380 simulateEvent( ce ); 381 } 382 } 383 384 385 388 public void mouseDragged( MouseEvent me ) 389 { 390 } 392 393 394 397 public void mouseMoved( MouseEvent me ) 398 { 399 MouseMovedCaptureEvent ce = new MouseMovedCaptureEvent( me ); 400 401 404 Iterator iter = getCaptureListeners(); 405 while (iter.hasNext()) 406 { 407 ((ICaptureListener)iter.next()).mouseMoved( ce ); 408 } 409 } 410 411 412 415 public void mousePressed( MouseEvent me ) 416 { 417 MousePressedCaptureEvent ce = new MousePressedCaptureEvent( me ); 418 Iterator iter = getCaptureListeners(); 419 boolean allow = true; 420 while (iter.hasNext()) 421 { 422 ICaptureListener icl = (ICaptureListener)iter.next(); 423 if (icl instanceof IAllowCapturePassThroughListener) 424 { 425 if (!((IAllowCapturePassThroughListener)icl). 426 allowMousePressed( ce )) 427 { 428 allow = false; 429 } 430 } 431 icl.mousePressed( ce ); 432 } 433 if (allow) 434 { 435 simulateEvent( ce ); 436 } 437 } 438 439 440 443 public void mouseReleased( MouseEvent me ) 444 { 445 MouseReleasedCaptureEvent ce = new MouseReleasedCaptureEvent( me ); 446 Iterator iter = getCaptureListeners(); 447 boolean allow = true; 448 while (iter.hasNext()) 449 { 450 ICaptureListener icl = (ICaptureListener)iter.next(); 451 if (icl instanceof IAllowCapturePassThroughListener) 452 { 453 if (!((IAllowCapturePassThroughListener)icl). 454 allowMouseReleased( ce )) 455 { 456 allow = false; 457 } 458 } 459 icl.mouseReleased( ce ); 460 } 461 if (allow) 462 { 463 simulateEvent( ce ); 464 } 465 } 466 467 468 469 472 public void mouseClicked( MouseEvent me ) 473 { 474 } 476 477 478 479 482 public void mouseEntered( MouseEvent me ) 483 { 484 } 486 487 488 489 492 public void mouseExited( MouseEvent me ) 493 { 494 } 496 497 498 499 502 public void keyTyped( KeyEvent me ) 503 { 504 } 506 507 508 511 public void keyPressed( KeyEvent ke ) 512 { 513 KeyPressedCaptureEvent ce = new KeyPressedCaptureEvent( ke ); 514 Iterator iter = getCaptureListeners(); 515 boolean allow = true; 516 while (iter.hasNext()) 517 { 518 ICaptureListener icl = (ICaptureListener)iter.next(); 519 if (icl instanceof IAllowCapturePassThroughListener) 520 { 521 if (!((IAllowCapturePassThroughListener)icl). 522 allowKeyPressed( ce )) 523 { 524 allow = false; 525 } 526 } 527 icl.keyPressed( ce ); 528 } 529 if (allow) 530 { 531 simulateEvent( ce ); 532 } 533 } 534 535 536 539 public void keyReleased( KeyEvent ke ) 540 { 541 KeyReleasedCaptureEvent ce = new KeyReleasedCaptureEvent( ke ); 542 Iterator iter = getCaptureListeners(); 543 boolean allow = true; 544 while (iter.hasNext()) 545 { 546 ICaptureListener icl = (ICaptureListener)iter.next(); 547 if (icl instanceof IAllowCapturePassThroughListener) 548 { 549 if (!((IAllowCapturePassThroughListener)icl). 550 allowKeyReleased( ce )) 551 { 552 allow = false; 553 } 554 } 555 icl.keyReleased( ce ); 556 } 557 if (allow) 558 { 559 simulateEvent( ce ); 560 } 561 } 562 563 564 565 568 569 574 protected Iterator getCaptureListeners() 575 { 576 return this.captureListeners.iterator(); 577 } 578 } 579 580 | Popular Tags |