1 18 package org.apache.batik.swing; 19 20 import java.awt.EventQueue ; 21 import java.awt.geom.AffineTransform ; 22 import java.io.File ; 23 import java.net.MalformedURLException ; 24 25 import org.apache.batik.swing.JSVGCanvas; 26 import org.apache.batik.swing.gvt.GVTTreeRendererListener; 27 import org.apache.batik.swing.gvt.GVTTreeRendererEvent; 28 import org.apache.batik.swing.svg.SVGDocumentLoaderListener; 29 import org.apache.batik.swing.svg.SVGLoadEventDispatcherListener; 30 import org.apache.batik.swing.svg.SVGLoadEventDispatcherEvent; 31 import org.apache.batik.swing.svg.SVGDocumentLoaderEvent; 32 import org.apache.batik.swing.svg.GVTTreeBuilderListener; 33 import org.apache.batik.swing.svg.GVTTreeBuilderEvent; 34 35 import org.w3c.dom.svg.SVGDocument; 36 37 45 public class JSVGInterruptTest extends JSVGMemoryLeakTest { 46 47 public String getName() { return "JSVGInterruptTest."+getId(); } 48 49 public JSVGInterruptTest() { 50 } 51 52 53 Runnable stopRunnable; 54 55 int state = 0; 56 MyLoaderListener loadListener = new MyLoaderListener(); 57 MyBuildListener buildListener = new MyBuildListener(); 58 MyOnloadListener onloadListener = new MyOnloadListener(); 59 MyRenderListener renderListener = new MyRenderListener(); 60 61 DelayRunnable stopper = null; 62 63 64 final static int COMPLETE = 1; 65 final static int CANCELLED = 2; 66 final static int FAILED = 4; 67 final static int MAX_WAIT = 40000; 68 69 public JSVGCanvasHandler createHandler() { 70 return new JSVGCanvasHandler(this, this) { 71 public void runCanvas(String desc) { 72 this.desc = desc; 73 setupCanvas(); 74 75 if ( abort) return; 76 try { 77 synchronized (renderMonitor) { 78 delegate.canvasInit(canvas); 79 if ( abort) return; 80 81 while (!done) { 82 checkRender(); 83 if ( abort) return; 84 } 85 } 86 } catch (Throwable t) { 87 t.printStackTrace(); 88 } finally { 89 delegate.canvasDone(canvas); 90 dispose(); 91 } 92 } 93 public void checkSomething(Object monitor, String errorCode) { 94 synchronized (monitor) { 95 try { monitor.wait(); } 96 catch(InterruptedException ie) { } 97 } 98 } 99 }; 100 } 101 102 103 public boolean canvasInit(final JSVGCanvas canvas) { 104 theCanvas = canvas; 106 theFrame = handler.getFrame(); 107 registerObjectDesc(canvas, "JSVGCanvas"); 108 registerObjectDesc(handler.getFrame(), "JFrame"); 109 110 stopRunnable = new StopRunnable(canvas); 111 112 File f = new File (getId()); 113 String uri; 114 try { 115 uri = f.toURL().toString(); 116 } catch (MalformedURLException mue) { 117 throw new IllegalArgumentException (mue.getMessage()); 118 } 119 120 tweakIt(canvas, uri); 121 122 return false; 123 } 124 125 public void canvasDone(JSVGCanvas canvas) { 126 loadListener = null; 127 buildListener = null; 128 renderListener = null; 129 onloadListener = null; 130 stopper = null; 131 stopRunnable = null; 132 } 133 134 public void tweakIt(final JSVGCanvas canvas, final String uri) { 135 Thread t = new Thread () { 136 public void run() { 137 int state; 138 Runnable setURI = new Runnable () { 139 public void run() { 140 canvas.setURI(uri); 141 } 142 }; 143 System.err.println("Starting Load Tweak"); 144 canvas.addSVGDocumentLoaderListener(loadListener); 145 state = doTweak(setURI, loadListener); 146 canvas.removeSVGDocumentLoaderListener(loadListener); 147 System.err.println("Finished Load Tweak: " + state); 148 149 final SVGDocument doc = canvas.getSVGDocument(); 150 Runnable setDoc = new Runnable () { 151 public void run() { 152 canvas.setSVGDocument(doc); 153 } 154 }; 155 System.err.println("Starting setDoc Tweak"); 156 canvas.addGVTTreeBuilderListener(buildListener); 157 state = doTweak(setDoc, buildListener); 158 canvas.removeGVTTreeBuilderListener(buildListener); 159 System.err.println("Finished setDoc Tweak: " + state); 160 161 if (canvas.isDynamic()) { 162 System.err.println("Starting onload Tweak"); 163 canvas.addSVGLoadEventDispatcherListener 164 (onloadListener); 165 state = doTweak(setDoc, onloadListener); 166 canvas.removeSVGLoadEventDispatcherListener 167 (onloadListener); 168 System.err.println("Finished onload Tweak: " + state); 169 } 170 171 Runnable setTrans = new Runnable () { 172 public void run() { 173 canvas.setRenderingTransform 174 (new AffineTransform (), true); 175 } 176 }; 177 System.err.println("Starting render Tweak"); 178 canvas.addGVTTreeRendererListener(renderListener); 179 state = doTweak(setTrans, renderListener); 180 canvas.removeGVTTreeRendererListener(renderListener); 181 System.err.println("Finished render Tweak: " + state); 182 183 handler.scriptDone(); 184 } 185 }; 186 t.setDaemon(true); 187 t.start(); 188 } 189 190 public int doTweak(Runnable r, SetDelayable delayable) { 191 synchronized (JSVGInterruptTest.this) { 192 int delay = 0; 193 int delayInc = 3; 194 int delayIncInc = 4; 195 int ret = 0; 196 state = 0; 197 while ((state & (COMPLETE | FAILED)) == 0) { 198 ret |= state; 199 state = 0; 200 System.err.println("Tweaking: " + delay); 201 delayable.setDelay(delay); 202 EventQueue.invokeLater(r); 203 204 long start = System.currentTimeMillis(); 205 long end = start + MAX_WAIT; 206 long curr = start; 207 while ((state == 0) && (curr < end)) { 208 try { 211 JSVGInterruptTest.this.wait(end-curr); 212 } catch(InterruptedException ie) { 213 } 214 curr = System.currentTimeMillis(); 215 } 216 if (state == 0) { 217 throw new IllegalArgumentException 218 ("Timed out - proabably indicates failure"); 219 } 220 delay += delayInc + (curr-start-delay)/8; 221 delayInc += delayIncInc; 222 } 223 ret |= state; 224 return ret; 225 } 226 } 227 228 229 public void triggerStopProcessing(int delay) { 230 stopper = new DelayRunnable(delay, stopRunnable); 231 stopper.start(); 232 } 233 public boolean stopStopper() { 234 return stopper.abort(); 235 } 236 237 interface SetDelayable { 238 public void setDelay(int delay); 239 } 240 241 class MyLoaderListener implements SVGDocumentLoaderListener, SetDelayable { 242 int delay = 0; 243 public void setDelay(int delay) { this.delay = delay; } 244 public void documentLoadingStarted(SVGDocumentLoaderEvent e) { 245 triggerStopProcessing(delay); 246 } 247 public void documentLoadingCompleted(SVGDocumentLoaderEvent e) { 248 stopStopper(); 249 synchronized (JSVGInterruptTest.this) { 250 state |= COMPLETE; 251 JSVGInterruptTest.this.notifyAll(); 252 } 253 } 254 public void documentLoadingCancelled(SVGDocumentLoaderEvent e) { 255 synchronized (JSVGInterruptTest.this) { 256 state |= CANCELLED; 257 JSVGInterruptTest.this.notifyAll(); 258 } 259 } 260 public void documentLoadingFailed(SVGDocumentLoaderEvent e) { 261 synchronized (JSVGInterruptTest.this) { 262 state |= FAILED; 263 JSVGInterruptTest.this.notifyAll(); 264 } 265 } 266 } 267 268 class MyBuildListener implements GVTTreeBuilderListener, SetDelayable { 269 int delay = 0; 270 public void setDelay(int delay) { this.delay = delay; } 271 public void gvtBuildStarted(GVTTreeBuilderEvent e) { 272 triggerStopProcessing(delay); 274 } 275 public void gvtBuildCompleted(GVTTreeBuilderEvent e) { 276 stopStopper(); 277 synchronized (JSVGInterruptTest.this) { 279 state |= COMPLETE; 280 JSVGInterruptTest.this.notifyAll(); 281 } 282 } 283 public void gvtBuildCancelled(GVTTreeBuilderEvent e) { 284 synchronized (JSVGInterruptTest.this) { 286 state |= CANCELLED; 287 JSVGInterruptTest.this.notifyAll(); 288 } 289 } 290 public void gvtBuildFailed(GVTTreeBuilderEvent e) { 291 synchronized (JSVGInterruptTest.this) { 293 state |= FAILED; 294 JSVGInterruptTest.this.notifyAll(); 295 } 296 } 297 } 298 299 class MyOnloadListener 300 implements SVGLoadEventDispatcherListener, SetDelayable { 301 int delay = 0; 302 public void setDelay(int delay) { this.delay = delay; } 303 public void svgLoadEventDispatchStarted 304 (SVGLoadEventDispatcherEvent e) { 305 triggerStopProcessing(delay); 307 } 308 public void svgLoadEventDispatchCompleted 309 (SVGLoadEventDispatcherEvent e) { 310 stopStopper(); 311 synchronized (JSVGInterruptTest.this) { 313 state |= COMPLETE; 314 JSVGInterruptTest.this.notifyAll(); 315 } 316 } 317 public void svgLoadEventDispatchCancelled 318 (SVGLoadEventDispatcherEvent e) { 319 synchronized (JSVGInterruptTest.this) { 321 state |= CANCELLED; 322 JSVGInterruptTest.this.notifyAll(); 323 } 324 } 325 public void svgLoadEventDispatchFailed 326 (SVGLoadEventDispatcherEvent e) { 327 synchronized (JSVGInterruptTest.this) { 329 state |= FAILED; 330 JSVGInterruptTest.this.notifyAll(); 331 } 332 } 333 } 334 335 class MyRenderListener implements GVTTreeRendererListener, SetDelayable { 336 int delay = 0; 337 public void setDelay(int delay) { this.delay = delay; } 338 public void gvtRenderingPrepare(GVTTreeRendererEvent e) { 339 triggerStopProcessing(delay); 341 } 342 public void gvtRenderingStarted(GVTTreeRendererEvent e) { 343 } 345 public void gvtRenderingCompleted(GVTTreeRendererEvent e) { 346 stopStopper(); 347 synchronized (JSVGInterruptTest.this) { 349 state |= COMPLETE; 350 JSVGInterruptTest.this.notifyAll(); 351 } 352 } 353 public void gvtRenderingCancelled(GVTTreeRendererEvent e) { 354 synchronized (JSVGInterruptTest.this) { 356 state |= CANCELLED; 357 JSVGInterruptTest.this.notifyAll(); 358 } 359 } 360 public void gvtRenderingFailed(GVTTreeRendererEvent e) { 361 synchronized (JSVGInterruptTest.this) { 363 state |= FAILED; 364 JSVGInterruptTest.this.notifyAll(); 365 } 366 } 367 } 368 369 370 static class StopRunnable implements Runnable { 371 JSVGCanvas canvas; 372 public StopRunnable(JSVGCanvas canvas) { 373 this.canvas = canvas; 374 } 375 376 public void run() { 377 if (EventQueue.isDispatchThread()) 378 canvas.stopProcessing(); 379 else 380 EventQueue.invokeLater(this); 381 } 382 } 383 384 385 class DelayRunnable extends Thread { 386 int delay; 387 Runnable r; 388 boolean stop = false; 389 boolean complete = false; 390 public DelayRunnable(int delay, Runnable r) { 391 this.delay = delay; 392 this.r = r; 393 setDaemon(true); 394 } 395 public boolean getComplete() { return complete; } 396 397 public boolean abort() { 398 synchronized (this) { 399 if (complete) return false; 400 stop = true; return true; 401 } 402 } 403 public void run() { 404 long start = System.currentTimeMillis(); 405 long end = start + delay; 406 long curr = start; 407 while (curr < end) { 408 try { 409 Thread.sleep(end-curr); 410 } catch(InterruptedException ie) { 411 } 412 curr = System.currentTimeMillis(); 413 } 414 synchronized (this) { 415 if (stop) return; 416 r.run(); 417 complete = true; 418 } 419 } 420 } 421 422 } 423 | Popular Tags |