1 18 package org.apache.batik.swing.svg; 19 20 import java.util.Collections ; 21 import java.util.LinkedList ; 22 import java.util.List ; 23 24 import org.apache.batik.bridge.BridgeContext; 25 import org.apache.batik.bridge.BridgeException; 26 import org.apache.batik.bridge.DynamicGVTBuilder; 27 import org.apache.batik.bridge.GVTBuilder; 28 import org.apache.batik.bridge.InterruptedBridgeException; 29 import org.apache.batik.gvt.GraphicsNode; 30 import org.apache.batik.util.EventDispatcher; 31 import org.apache.batik.util.EventDispatcher.Dispatcher; 32 import org.apache.batik.util.HaltingThread; 33 34 import org.w3c.dom.svg.SVGDocument; 35 36 37 44 public class GVTTreeBuilder extends HaltingThread { 45 46 49 protected SVGDocument svgDocument; 50 51 54 protected BridgeContext bridgeContext; 55 56 59 protected List listeners = Collections.synchronizedList(new LinkedList ()); 60 61 64 protected Exception exception; 65 66 69 public GVTTreeBuilder(SVGDocument doc, 70 BridgeContext bc) { 71 svgDocument = doc; 72 bridgeContext = bc; 73 } 74 75 78 public void run() { 79 GVTTreeBuilderEvent ev; 80 ev = new GVTTreeBuilderEvent(this, null); 81 try { 82 fireEvent(startedDispatcher, ev); 83 84 if (isHalted()) { 85 fireEvent(cancelledDispatcher, ev); 86 return; 87 } 88 GVTBuilder builder = null; 89 90 if (bridgeContext.isDynamic()) { 91 builder = new DynamicGVTBuilder(); 92 } else { 93 builder = new GVTBuilder(); 94 } 95 GraphicsNode gvtRoot = builder.build(bridgeContext, svgDocument); 96 97 if (isHalted()) { 98 fireEvent(cancelledDispatcher, ev); 99 return; 100 } 101 102 ev = new GVTTreeBuilderEvent(this, gvtRoot); 103 fireEvent(completedDispatcher, ev); 104 } catch (InterruptedBridgeException e) { 105 fireEvent(cancelledDispatcher, ev); 106 } catch (BridgeException e) { 107 exception = e; 108 ev = new GVTTreeBuilderEvent(this, e.getGraphicsNode()); 109 fireEvent(failedDispatcher, ev); 110 } catch (Exception e) { 111 exception = e; 112 fireEvent(failedDispatcher, ev); 113 } catch (ThreadDeath td) { 114 exception = new Exception (td.getMessage()); 115 fireEvent(failedDispatcher, ev); 116 throw td; 117 } catch (Throwable t) { 118 t.printStackTrace(); 119 exception = new Exception (t.getMessage()); 120 fireEvent(failedDispatcher, ev); 121 } finally { 122 } 124 } 125 126 129 public Exception getException() { 130 return exception; 131 } 132 133 136 public void addGVTTreeBuilderListener(GVTTreeBuilderListener l) { 137 listeners.add(l); 138 } 139 140 143 public void removeGVTTreeBuilderListener(GVTTreeBuilderListener l) { 144 listeners.remove(l); 145 } 146 147 public void fireEvent(Dispatcher dispatcher, Object event) { 148 EventDispatcher.fireEvent(dispatcher, listeners, event, true); 149 } 150 151 static Dispatcher startedDispatcher = new Dispatcher() { 152 public void dispatch(Object listener, 153 Object event) { 154 ((GVTTreeBuilderListener)listener).gvtBuildStarted 155 ((GVTTreeBuilderEvent)event); 156 } 157 }; 158 159 static Dispatcher completedDispatcher = new Dispatcher() { 160 public void dispatch(Object listener, 161 Object event) { 162 ((GVTTreeBuilderListener)listener).gvtBuildCompleted 163 ((GVTTreeBuilderEvent)event); 164 } 165 }; 166 167 static Dispatcher cancelledDispatcher = new Dispatcher() { 168 public void dispatch(Object listener, 169 Object event) { 170 ((GVTTreeBuilderListener)listener).gvtBuildCancelled 171 ((GVTTreeBuilderEvent)event); 172 } 173 }; 174 175 static Dispatcher failedDispatcher = new Dispatcher() { 176 public void dispatch(Object listener, 177 Object event) { 178 ((GVTTreeBuilderListener)listener).gvtBuildFailed 179 ((GVTTreeBuilderEvent)event); 180 } 181 }; 182 } 183 | Popular Tags |