1 26 27 package net.sourceforge.groboutils.uicapture.v1; 28 29 import java.awt.Rectangle ; 30 31 import java.io.File ; 32 import java.io.IOException ; 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 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 baseImageName = null; 66 private int filenameIndex = 0; 67 private boolean isRunning = false; 68 69 70 73 76 public ScriptGenerator( IScriptMaker maker, String baseImageName ) 77 { 78 this( new DefaultScreenScraper(), new DefaultFocusedWindowFinder(), 79 maker, baseImageName ); 80 } 81 82 83 86 public ScriptGenerator( IScreenScraper ss, 87 IFocusedWindowFinder fwf, IScriptMaker maker, String baseImageName ) 88 { 89 this.ss = ss; 90 this.fwf = fwf; 91 this.maker = maker; 92 this.baseImageName = baseImageName; 93 } 94 95 96 99 100 107 108 112 public void begin() 113 throws java.awt.AWTException 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 140 143 public boolean isInMetaMode() 144 { 145 return this.metaMode; 146 } 147 148 149 152 public void setInMetaMode( boolean set ) 153 { 154 this.metaMode = set; 155 } 156 157 158 162 protected void metaMouseWheelMoved( MouseWheelCaptureEvent ce ) 163 { 164 } 166 167 168 172 protected void metaMousePressed( MousePressedCaptureEvent ce ) 173 { 174 } 176 177 178 182 protected void metaMouseReleased( MouseReleasedCaptureEvent ce ) 183 { 184 } 186 187 188 192 protected void metaKeyPressed( KeyPressedCaptureEvent ce ) 193 { 194 } 196 197 198 202 protected void metaKeyReleased( KeyReleasedCaptureEvent ce ) 203 { 204 } 206 207 208 212 protected void metaMouseMoved( MouseMovedCaptureEvent ce ) 213 { 214 } 216 217 218 221 222 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 247 protected synchronized void fireSaveScreen() 248 { 249 if (isRunning()) 250 { 251 fireSaveScreenImage( new Rectangle ( 252 this.window.getWindow().getCoveredScreen() ) ); 253 } 254 } 255 256 257 262 protected synchronized void fireSaveFocusedWindow() 263 { 264 if (isRunning()) 265 { 266 Rectangle bounds = this.fwf.getFocusedWindowBounds(); 267 if (bounds == null) 268 { 269 fireSaveScreen(); 271 } 272 else 273 { 274 fireSaveScreenImage( bounds ); 275 } 276 } 277 } 278 279 280 289 protected synchronized void fireSaveScreenImage( int x, int y, int w,int h ) 290 { 291 if (isRunning()) 292 { 293 fireSaveScreenImage( new Rectangle ( x, y, w, h ) ); 294 } 295 } 296 297 298 304 protected synchronized void fireSaveScreenImage( Rectangle bounds ) 305 { 306 if (isRunning()) 307 { 308 File 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 ioe) 318 { 319 encounteredError( ioe ); 320 } 321 } 322 } 323 324 325 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 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 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 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 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 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 406 407 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 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 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 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 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 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 523 524 530 public final boolean allowMouseWheelMoved( MouseWheelCaptureEvent ce ) 531 { 532 return !isInMetaMode(); 533 } 534 535 536 542 public final boolean allowMousePressed( MousePressedCaptureEvent ce ) 543 { 544 return !isInMetaMode(); 545 } 546 547 548 554 public final boolean allowMouseReleased( MouseReleasedCaptureEvent ce ) 555 { 556 return !isInMetaMode(); 557 } 558 559 560 566 public final boolean allowKeyPressed( KeyPressedCaptureEvent ce ) 567 { 568 return !isInMetaMode(); 569 } 570 571 572 578 public final boolean allowKeyReleased( KeyReleasedCaptureEvent ce ) 579 { 580 return !isInMetaMode(); 581 } 582 583 584 590 public final boolean allowMouseMoved( MouseMovedCaptureEvent ce ) 591 { 592 return !isInMetaMode(); 593 } 594 595 596 597 600 601 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 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 641 protected synchronized File createFile() 642 { 643 File f = new File ( this.baseImageName + this.filenameIndex + '.' + 644 this.ss.getFileExtention() ); 645 ++this.filenameIndex; 646 647 return f; 648 } 649 650 651 654 protected void encounteredError( Throwable t ) 655 { 656 t.printStackTrace(); 657 } 658 } 659 660 | Popular Tags |