KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > swing > JSVGInterruptTest


1 /*
2
3    Copyright 2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.swing;
19
20 import java.awt.EventQueue JavaDoc;
21 import java.awt.geom.AffineTransform JavaDoc;
22 import java.io.File JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
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 /**
38  * One line Class Desc
39  *
40  * Complete Class Desc
41  *
42  * @author <a HREF="mailto:deweese@apache.org">l449433</a>
43  * @version $Id: JSVGInterruptTest.java,v 1.6 2005/03/27 08:58:37 cam Exp $
44  */

45 public class JSVGInterruptTest extends JSVGMemoryLeakTest {
46
47     public String JavaDoc getName() { return "JSVGInterruptTest."+getId(); }
48
49     public JSVGInterruptTest() {
50     }
51
52     /* JSVGCanvasHandler.Delegate Interface */
53     Runnable JavaDoc 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 JavaDoc 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 JavaDoc t) {
87                         t.printStackTrace();
88                     } finally {
89                         delegate.canvasDone(canvas);
90                         dispose();
91                     }
92                 }
93                 public void checkSomething(Object JavaDoc monitor, String JavaDoc errorCode) {
94                     synchronized (monitor) {
95                         try { monitor.wait(); }
96                         catch(InterruptedException JavaDoc ie) { /* nothing */ }
97                     }
98                 }
99             };
100     }
101
102
103     public boolean canvasInit(final JSVGCanvas canvas) {
104         // System.err.println("In Init");
105
theCanvas = canvas;
106         theFrame = handler.getFrame();
107         registerObjectDesc(canvas, "JSVGCanvas");
108         registerObjectDesc(handler.getFrame(), "JFrame");
109
110         stopRunnable = new StopRunnable(canvas);
111
112         File JavaDoc f = new File JavaDoc(getId());
113         String JavaDoc uri;
114         try {
115             uri = f.toURL().toString();
116         } catch (MalformedURLException JavaDoc mue) {
117             throw new IllegalArgumentException JavaDoc(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 JavaDoc uri) {
135         Thread JavaDoc t = new Thread JavaDoc() {
136                 public void run() {
137                     int state;
138                     Runnable JavaDoc setURI = new Runnable JavaDoc() {
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 JavaDoc setDoc = new Runnable JavaDoc() {
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 JavaDoc setTrans = new Runnable JavaDoc() {
172                             public void run() {
173                                 canvas.setRenderingTransform
174                                     (new AffineTransform JavaDoc(), 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 JavaDoc 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                     // No 'complete' event generated yet and
209
// Still willing to wait a bit...
210
try {
211                         JSVGInterruptTest.this.wait(end-curr);
212                     } catch(InterruptedException JavaDoc ie) {
213                     }
214                     curr = System.currentTimeMillis();
215                 }
216                 if (state == 0) {
217                     throw new IllegalArgumentException JavaDoc
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             // System.err.println("Build Start: " + e.getSource());
273
triggerStopProcessing(delay);
274         }
275         public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
276             stopStopper();
277             // System.err.println("Build Complete: " + e.getSource());
278
synchronized (JSVGInterruptTest.this) {
279                 state |= COMPLETE;
280                 JSVGInterruptTest.this.notifyAll();
281             }
282         }
283         public void gvtBuildCancelled(GVTTreeBuilderEvent e) {
284             // System.err.println("Build Cancelled");
285
synchronized (JSVGInterruptTest.this) {
286                 state |= CANCELLED;
287                 JSVGInterruptTest.this.notifyAll();
288             }
289         }
290         public void gvtBuildFailed(GVTTreeBuilderEvent e) {
291             // System.err.println("Build Failed");
292
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             // System.err.println("Onload Start: " + e.getSource());
306
triggerStopProcessing(delay);
307             }
308             public void svgLoadEventDispatchCompleted
309                 (SVGLoadEventDispatcherEvent e) {
310                 stopStopper();
311                 // System.err.println("Onload Complete: " + e.getSource());
312
synchronized (JSVGInterruptTest.this) {
313                     state |= COMPLETE;
314                     JSVGInterruptTest.this.notifyAll();
315                 }
316             }
317             public void svgLoadEventDispatchCancelled
318                 (SVGLoadEventDispatcherEvent e) {
319                 // System.err.println("Onload Cancelled");
320
synchronized (JSVGInterruptTest.this) {
321                     state |= CANCELLED;
322                     JSVGInterruptTest.this.notifyAll();
323                 }
324             }
325             public void svgLoadEventDispatchFailed
326                 (SVGLoadEventDispatcherEvent e) {
327                 // System.err.println("Onload Failed");
328
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                 // System.err.println("Render Prep");
340
triggerStopProcessing(delay);
341             }
342             public void gvtRenderingStarted(GVTTreeRendererEvent e) {
343                 // System.err.println("Render Start");
344
}
345             public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
346                 stopStopper();
347                 // System.err.println("Render Complete");
348
synchronized (JSVGInterruptTest.this) {
349                     state |= COMPLETE;
350                     JSVGInterruptTest.this.notifyAll();
351                 }
352             }
353             public void gvtRenderingCancelled(GVTTreeRendererEvent e) {
354                 // System.err.println("Render Cancelled");
355
synchronized (JSVGInterruptTest.this) {
356                     state |= CANCELLED;
357                     JSVGInterruptTest.this.notifyAll();
358                 }
359             }
360             public void gvtRenderingFailed(GVTTreeRendererEvent e) {
361                 // System.err.println("Render Failed");
362
synchronized (JSVGInterruptTest.this) {
363                     state |= FAILED;
364                     JSVGInterruptTest.this.notifyAll();
365                 }
366             }
367         }
368
369
370     static class StopRunnable implements Runnable JavaDoc {
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 JavaDoc {
386         int delay;
387         Runnable JavaDoc r;
388         boolean stop = false;
389         boolean complete = false;
390         public DelayRunnable(int delay, Runnable JavaDoc 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 JavaDoc 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