1 26 27 package net.sourceforge.groboutils.uicapture.v1.javamaker; 28 29 import net.sourceforge.groboutils.uicapture.v1.VirtualWindowController; 30 import net.sourceforge.groboutils.uicapture.v1.IScriptMaker; 31 32 import java.io.Writer ; 33 import java.io.FileWriter ; 34 import java.io.PrintWriter ; 35 import java.io.IOException ; 36 import java.io.File ; 37 38 39 46 public class JavaMaker implements IScriptMaker 47 { 48 private Writer outF = null; 49 private PrintWriter out = null; 50 private String packageName = null; 51 private String className = null; 52 private int step = 0; 53 54 55 public JavaMaker( File outFile, String packageName, String className ) 56 throws IOException 57 { 58 if (outFile == null || packageName == null || className == null) 59 { 60 throw new IllegalArgumentException ( "no null args" ); 61 } 62 this.outF = new FileWriter ( outFile ); 63 this.out = new PrintWriter ( outF ); 64 65 this.packageName = packageName; 66 this.className = className; 67 } 68 69 70 public JavaMaker( Writer w, String packageName, String className ) 71 { 72 if (w == null || packageName == null || className == null) 73 { 74 throw new IllegalArgumentException ( "no null args" ); 75 } 76 this.outF = w; 77 this.out = new PrintWriter ( w ); 78 79 this.packageName = packageName; 80 this.className = className; 81 } 82 83 84 87 90 public void start() 91 { 92 if (this.step > 0) 93 { 94 throw new IllegalStateException ("Already started generation."); 95 } 96 97 wrln( "/"+"*" ); 98 wrln( " * Pregenerated script file." ); 99 wrln( " * Created "+(new java.util.Date ()).toString() ); 100 wrln( " *"+"/" ); 101 wrln(); 102 wrln( "package "+this.packageName+";" ); 103 wrln(); 104 wrln( "import "+VirtualWindowController.class.getName()+";" ); 105 wrln( "import "+File .class.getName()+";" ); 106 wrln(); 107 wrln( "public class "+this.className ); 108 wrln( "{" ); 109 wrln( " public static void main( String[] args )" ); 110 wrln( " throws Exception" ); 111 wrln( " {" ); 112 step( "Setup." ); 113 wrln( " VirtualWindowController vwc = new " ); 114 wrln( " VirtualWindowController();" ); 115 wrln( " vwc.begin();" ); 116 wrln( " try {" ); 117 wrln( " vwc.sleep( 2000 );" ); 118 119 this.step = 1; 121 } 122 123 124 127 public void end() 128 { 129 assertActive(); 130 131 wrln( " /"+"/ end of script" ); 132 wrln( " System.out.println( \"No errors found in playback.\" ):"); 133 wrln( " System.exit( 0 );" ); 134 wrln( " } finally {" ); 135 wrln( " vwc.end();" ); 136 wrln( " vwc = null;" ); 137 wrln( " }" ); 138 wrln( " }" ); 139 wrln( "}" ); 140 141 this.step = 2; 143 144 try 145 { 146 this.out.close(); 147 this.outF.close(); 148 } 149 catch (IOException ioe) 150 { 151 handleException( ioe ); 152 } 153 finally 154 { 155 this.out = null; 156 this.outF = null; 157 } 158 } 159 160 161 164 public void generateDelay( long milliseconds ) 165 { 166 assertActive(); 167 168 step( "sleep for "+milliseconds+" milliseconds." ); 169 wrln( " vwc.sleep( "+milliseconds+" );" ); 170 } 171 172 173 176 public void generateMoveMouseWheel( int rotate ) 177 { 178 assertActive(); 179 180 step( "rotate mouse wheel "+rotate+" clicks." ); 181 wrln( " vwc.moveMouseWheel( "+rotate+" );" ); 182 } 183 184 185 188 public void generateMoveMouse( int x, int y ) 189 { 190 assertActive(); 191 192 step( "move mouse to ("+x+", "+y+")." ); 193 wrln( " vwc.moveMouse( "+x+", "+y+" );" ); 194 } 195 196 197 200 public void generatePressMouse( int modifier ) 201 { 202 assertActive(); 203 204 step( "press mouse with "+modifier+"." ); 205 wrln( " vwc.pressMouse( "+modifier+" );" ); 206 } 207 208 209 212 public void generateReleaseMouse( int modifier ) 213 { 214 assertActive(); 215 216 step( "release mouse with "+modifier+"." ); 217 wrln( " vwc.releaseMouse( "+modifier+" );" ); 218 } 219 220 221 224 public void generatePressKey( int keyCode ) 225 { 226 assertActive(); 227 228 step( "press key with "+keyCode+"." ); 229 wrln( " vwc.pressKey( "+keyCode+" );" ); 230 } 231 232 233 236 public void generateReleaseKey( int keyCode ) 237 { 238 assertActive(); 239 240 step( "release key with "+keyCode+"." ); 241 wrln( " vwc.releaseKey( "+keyCode+" );" ); 242 } 243 244 245 254 public void generateScreenCapture( File originalImage, 255 int x, int y, int width, int height ) 256 { 257 assertActive(); 258 259 step( "capture and compare screen in ("+x+", "+y+", "+width+", "+ 260 height+" ) against "+originalImage+"." ); 261 wrln( " File f"+this.step+" = new File( \""+ 262 className + "-" + this.step + ".\" + vwc.getImageExtension() );" ); 263 wrln( " vwc.saveScreenImage( f"+this.step+".toString(), "+x+ 264 ", "+y+", "+width+", "+height+" );" ); 265 wrln( " if (!vwc.compareFiles( f"+step+", new File( \"" + 266 originalImage + "\" ))" ); 267 wrln( " {" ); 268 wrln( " System.out.println( \"Images did not match on step "+ 269 this.step+".\" );" ); 270 wrln( " System.out.println( \"Test Failed.\" );" ); 271 wrln( " vwc.end();" ); 272 wrln( " vwc = null;" ); 273 wrln( " Thread.sleep( 1000 );" ); 274 wrln( " System.exit(1);" ); 275 wrln( " }" ); 276 wrln( " System.out.println( \"** Images match!\" );"); 277 } 278 279 280 282 protected void handleException( Throwable t ) 283 { 284 t.printStackTrace(); 285 this.out = null; 286 this.outF = null; 287 throw new IllegalStateException ("Encountered exception "+t); 288 } 289 290 protected void assertActive() 291 { 292 if (this.out == null) 293 { 294 throw new IllegalStateException ("Not open for writing."); 295 } 296 if (this.step <= 0) 297 { 298 throw new IllegalStateException ("Never started writing."); 299 } 300 } 301 302 303 protected void wr( String text ) 304 { 305 this.out.print( text ); 306 } 307 protected void wrln( String text ) 308 { 309 this.out.println( text ); 310 } 311 protected void wrln() 312 { 313 this.out.println( "" ); 314 } 315 316 317 protected void step( String msg ) 318 { 319 ++this.step; 320 wrln( " System.out.println( \"Step "+this.step+": "+ 321 msg +"\" );" ); 322 } 323 } 324 325 | Popular Tags |