1 18 package org.apache.batik.swing.svg; 19 20 import java.io.InterruptedIOException ; 21 import java.util.Collections ; 22 import java.util.LinkedList ; 23 import java.util.List ; 24 25 import org.apache.batik.bridge.DocumentLoader; 26 import org.apache.batik.util.EventDispatcher; 27 import org.apache.batik.util.EventDispatcher.Dispatcher; 28 import org.apache.batik.util.HaltingThread; 29 30 import org.w3c.dom.svg.SVGDocument; 31 32 38 public class SVGDocumentLoader extends HaltingThread { 39 40 43 protected String url; 44 45 48 protected DocumentLoader loader; 49 50 53 protected Exception exception; 54 55 58 protected List listeners = Collections.synchronizedList(new LinkedList ()); 59 60 65 public SVGDocumentLoader(String u, DocumentLoader l) { 66 url = u; 67 loader = l; 68 } 69 70 73 public void run() { 74 SVGDocumentLoaderEvent evt; 75 evt = new SVGDocumentLoaderEvent(this, null); 76 try { 77 fireEvent(startedDispatcher, evt); 78 if (isHalted()) { 79 fireEvent(cancelledDispatcher, evt); 80 return; 81 } 82 83 SVGDocument svgDocument = (SVGDocument)loader.loadDocument(url); 84 85 if (isHalted()) { 86 fireEvent(cancelledDispatcher, evt); 87 return; 88 } 89 90 evt = new SVGDocumentLoaderEvent(this, svgDocument); 91 fireEvent(completedDispatcher, evt); 92 } catch (InterruptedIOException e) { 93 fireEvent(cancelledDispatcher, evt); 94 } catch (Exception e) { 95 exception = e; 96 fireEvent(failedDispatcher, evt); 97 } catch (ThreadDeath td) { 98 exception = new Exception (td.getMessage()); 99 fireEvent(failedDispatcher, evt); 100 throw td; 101 } catch (Throwable t) { 102 t.printStackTrace(); 103 exception = new Exception (t.getMessage()); 104 fireEvent(failedDispatcher, evt); 105 } 106 } 107 108 111 public Exception getException() { 112 return exception; 113 } 114 115 118 public void addSVGDocumentLoaderListener(SVGDocumentLoaderListener l) { 119 listeners.add(l); 120 } 121 122 125 public void removeSVGDocumentLoaderListener(SVGDocumentLoaderListener l) { 126 listeners.remove(l); 127 } 128 129 public void fireEvent(Dispatcher dispatcher, Object event) { 130 EventDispatcher.fireEvent(dispatcher, listeners, event, true); 131 } 132 133 static Dispatcher startedDispatcher = new Dispatcher() { 134 public void dispatch(Object listener, 135 Object event) { 136 ((SVGDocumentLoaderListener)listener).documentLoadingStarted 137 ((SVGDocumentLoaderEvent)event); 138 } 139 }; 140 141 static Dispatcher completedDispatcher = new Dispatcher() { 142 public void dispatch(Object listener, 143 Object event) { 144 ((SVGDocumentLoaderListener)listener).documentLoadingCompleted 145 ((SVGDocumentLoaderEvent)event); 146 } 147 }; 148 149 static Dispatcher cancelledDispatcher = new Dispatcher() { 150 public void dispatch(Object listener, 151 Object event) { 152 ((SVGDocumentLoaderListener)listener).documentLoadingCancelled 153 ((SVGDocumentLoaderEvent)event); 154 } 155 }; 156 157 static Dispatcher failedDispatcher = new Dispatcher() { 158 public void dispatch(Object listener, 159 Object event) { 160 ((SVGDocumentLoaderListener)listener).documentLoadingFailed 161 ((SVGDocumentLoaderEvent)event); 162 } 163 }; 164 } 165 | Popular Tags |