1 18 package org.apache.batik.swing; 19 20 import java.awt.Dimension ; 21 import java.awt.EventQueue ; 22 import java.awt.event.WindowAdapter ; 23 import java.awt.event.WindowEvent ; 24 import java.awt.event.WindowListener ; 25 26 import javax.swing.JFrame ; 27 28 import org.apache.batik.test.DefaultTestReport; 29 import org.apache.batik.test.Test; 30 import org.apache.batik.test.TestReport; 31 32 33 import org.apache.batik.bridge.ScriptingEnvironment; 34 import org.apache.batik.bridge.UpdateManager; 35 import org.apache.batik.bridge.UpdateManagerEvent; 36 import org.apache.batik.bridge.UpdateManagerListener; 37 import org.apache.batik.script.Interpreter; 38 import org.apache.batik.script.InterpreterException; 39 import org.apache.batik.util.RunnableQueue; 40 41 import org.apache.batik.swing.gvt.GVTTreeRendererAdapter; 42 import org.apache.batik.swing.gvt.GVTTreeRendererEvent; 43 import org.apache.batik.swing.JSVGCanvas; 44 import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter; 45 import org.apache.batik.swing.svg.SVGDocumentLoaderEvent; 46 47 55 public class JSVGCanvasHandler { 56 57 public interface Delegate { 58 public String getName(); 59 public boolean canvasInit(JSVGCanvas canvas); 62 public void canvasLoaded(JSVGCanvas canvas); 63 public void canvasRendered(JSVGCanvas canvas); 64 public boolean canvasUpdated(JSVGCanvas canvas); 65 public void canvasDone(JSVGCanvas canvas); 66 public void failure(TestReport report); 67 } 68 69 public static final String REGARD_TEST_INSTANCE = "regardTestInstance"; 70 public static final String REGARD_START_SCRIPT = 71 "try { regardStart(); } catch (er) {}"; 72 73 77 public static final String ERROR_CANNOT_LOAD_SVG = 78 "JSVGCanvasHandler.message.error.could.not.load.svg"; 79 80 84 public static final String ERROR_SVG_RENDER_FAILED = 85 "JSVGCanvasHandler.message.error.svg.render.failed"; 86 87 91 public static final String ERROR_SVG_UPDATE_FAILED = 92 "JSVGCanvasHandler.message.error.svg.update.failed"; 93 94 97 public static final String ENTRY_KEY_ERROR_DESCRIPTION 98 = "JSVGCanvasHandler.entry.key.error.description"; 99 100 public static String fmt(String key, Object []args) { 101 return TestMessages.formatMessage(key, args); 102 } 103 104 JFrame frame = null; 105 JSVGCanvas canvas = null; 106 UpdateManager updateManager = null; 107 WindowListener wl = null; 108 InitialRenderListener irl = null; 109 LoadListener ll = null; 110 UpdateRenderListener url = null; 111 112 boolean failed; 113 boolean abort = false; 114 boolean done = false; 115 Object loadMonitor = new Object (); 116 Object renderMonitor = new Object (); 117 118 Delegate delegate; 119 Test host; 120 String desc; 121 122 public JSVGCanvasHandler(Test host, Delegate delegate) { 123 this.host = host; 124 this.delegate = delegate; 125 } 126 127 public JFrame getFrame() { return frame; } 128 public JSVGCanvas getCanvas() { return canvas; } 129 130 public JSVGCanvas createCanvas() { return new JSVGCanvas(); } 131 132 public void runCanvas(String desc) { 133 this.desc = desc; 134 135 setupCanvas(); 136 137 if ( abort) return; 138 139 try { 140 synchronized (renderMonitor) { 141 synchronized (loadMonitor) { 142 if (delegate.canvasInit(canvas)) { 143 checkLoad(); 144 } 145 } 146 147 if ( abort) return; 148 149 checkRender(); 150 if ( abort) return; 151 152 try { 153 EventQueue.invokeAndWait(new Runnable () { 154 public void run() { 155 updateManager = canvas.getUpdateManager(); 156 if (updateManager == null) 157 return; 158 url = new UpdateRenderListener(); 159 updateManager.addUpdateManagerListener(url); 160 }}); 161 } catch (Throwable t) { t.printStackTrace(); } 162 163 if ( abort) return; 164 165 if (updateManager == null) 166 return; 167 168 while (!updateManager.isRunning()); 170 171 bindHost(); 172 173 if ( abort) return; 174 175 while (!done) { 176 checkUpdate(); 177 if ( abort) return; 178 } 179 } 180 } catch (Throwable t) { 181 t.printStackTrace(); 182 } finally { 183 delegate.canvasDone(canvas); 184 dispose(); 185 } 186 } 187 188 public void setupCanvas() { 189 try { 190 EventQueue.invokeAndWait(new Runnable () { 191 public void run() { 192 frame = new JFrame (delegate.getName()); 193 canvas = createCanvas(); 194 canvas.setPreferredSize(new Dimension (450, 500)); 195 frame.getContentPane().add(canvas); 196 frame.pack(); 197 wl = new WindowAdapter () { 198 public void windowClosing(WindowEvent e) { 199 synchronized (loadMonitor) { 200 abort = true; 201 loadMonitor.notifyAll(); 202 } 203 synchronized (renderMonitor) { 204 abort = true; 205 renderMonitor.notifyAll(); 206 } 207 } 208 }; 209 frame.addWindowListener(wl); 210 frame.setVisible(true); 211 212 irl = new InitialRenderListener(); 213 canvas.addGVTTreeRendererListener(irl); 214 ll = new LoadListener(); 215 canvas.addSVGDocumentLoaderListener(ll); 216 }}); 217 } catch (Throwable t) { 218 t.printStackTrace(); 219 } 220 } 221 222 223 public void scriptDone() { 224 Runnable r = new Runnable () { 225 public void run() { 226 synchronized(renderMonitor) { 227 done = true; 228 failed = false; 229 renderMonitor.notifyAll(); 230 } 231 } 232 }; 233 if ((updateManager == null) || 234 (!updateManager.isRunning())){ 235 Thread t = new Thread (r); 237 t.start(); 238 } else { 239 updateManager.getUpdateRunnableQueue().invokeLater(r); 240 } 241 } 242 243 public void dispose() { 244 if (frame != null) { 245 frame.removeWindowListener(wl); 246 frame.setVisible(false); 247 } 248 wl = null; 249 if (canvas != null) { 250 canvas.removeGVTTreeRendererListener(irl); irl=null; 251 canvas.removeSVGDocumentLoaderListener(ll); ll=null; 252 canvas.removeUpdateManagerListener(url); url=null; 253 } 254 updateManager = null; 255 canvas = null; 256 frame = null; 257 } 258 259 public void checkSomething(Object monitor, String errorCode) { 260 synchronized (monitor) { 261 failed = true; 262 try { monitor.wait(); } 263 catch(InterruptedException ie) { } 264 if (abort || failed) { 265 DefaultTestReport report = new DefaultTestReport(host); 266 report.setErrorCode(errorCode); 267 report.setDescription(new TestReport.Entry[] { 268 new TestReport.Entry 269 (fmt(ENTRY_KEY_ERROR_DESCRIPTION, null), 270 fmt(errorCode, new Object []{desc})) 271 }); 272 report.setPassed(false); 273 delegate.failure(report); 274 done = true; 275 return; 276 } 277 } 278 } 279 280 public void checkLoad() { 281 checkSomething(loadMonitor, ERROR_CANNOT_LOAD_SVG); 282 delegate.canvasLoaded(canvas); 283 } 284 285 public void checkRender() { 286 checkSomething(renderMonitor, ERROR_SVG_RENDER_FAILED); 287 delegate.canvasRendered(canvas); 288 } 289 290 public void checkUpdate() { 291 checkSomething(renderMonitor, ERROR_SVG_UPDATE_FAILED); 292 if (!done) 293 done = delegate.canvasUpdated(canvas); 294 } 295 296 public void bindHost() { 297 RunnableQueue rq; 298 rq = updateManager.getUpdateRunnableQueue(); 299 rq.invokeLater(new Runnable () { 300 UpdateManager um = updateManager; 301 public void run() { 302 ScriptingEnvironment scriptEnv; 303 scriptEnv = um.getScriptingEnvironment(); 304 Interpreter interp; 305 interp = scriptEnv.getInterpreter(); 306 interp.bindObject(REGARD_TEST_INSTANCE, 307 host); 308 try { 309 interp.evaluate(REGARD_START_SCRIPT); 310 } catch (InterpreterException ie) { 311 } 313 } 314 }); 315 } 316 317 class UpdateRenderListener implements UpdateManagerListener { 318 public void updateCompleted(UpdateManagerEvent e) { 319 synchronized(renderMonitor){ 320 failed = false; 321 renderMonitor.notifyAll(); 322 } 323 } 324 public void updateFailed(UpdateManagerEvent e) { 325 synchronized(renderMonitor){ 326 renderMonitor.notifyAll(); 327 } 328 } 329 public void managerStarted(UpdateManagerEvent e) { } 330 public void managerSuspended(UpdateManagerEvent e) { } 331 public void managerResumed(UpdateManagerEvent e) { } 332 public void managerStopped(UpdateManagerEvent e) { } 333 public void updateStarted(UpdateManagerEvent e) { } 334 } 335 336 class InitialRenderListener extends GVTTreeRendererAdapter { 337 public void gvtRenderingCompleted(GVTTreeRendererEvent e) { 338 synchronized(renderMonitor){ 339 failed = false; 340 renderMonitor.notifyAll(); 341 } 342 } 343 344 345 public void gvtRenderingCancelled(GVTTreeRendererEvent e) { 346 synchronized(renderMonitor){ 347 renderMonitor.notifyAll(); 348 } 349 } 350 351 public void gvtRenderingFailed(GVTTreeRendererEvent e) { 352 synchronized(renderMonitor){ 353 renderMonitor.notifyAll(); 354 } 355 } 356 } 357 358 class LoadListener extends SVGDocumentLoaderAdapter { 359 public void documentLoadingCompleted(SVGDocumentLoaderEvent e) { 360 synchronized(loadMonitor){ 361 failed = false; 362 loadMonitor.notifyAll(); 363 } 364 } 365 366 public void documentLoadingFailed(SVGDocumentLoaderEvent e) { 367 synchronized(loadMonitor){ 368 loadMonitor.notifyAll(); 369 } 370 } 371 372 public void documentLoadingCancelled(SVGDocumentLoaderEvent e) { 373 synchronized(loadMonitor){ 374 loadMonitor.notifyAll(); 375 } 376 } 377 } 378 } 379 | Popular Tags |