KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > swing > svg > GVTTreeBuilder


1 /*
2
3    Copyright 2001-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.svg;
19
20 import java.util.Collections JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
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 /**
38  * This class represents an object which builds asynchroneaously
39  * a GVT tree.
40  *
41  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
42  * @version $Id: GVTTreeBuilder.java,v 1.14 2004/10/23 17:11:03 deweese Exp $
43  */

44 public class GVTTreeBuilder extends HaltingThread {
45
46     /**
47      * The SVG document to give to the bridge.
48      */

49     protected SVGDocument svgDocument;
50
51     /**
52      * The bridge context to use.
53      */

54     protected BridgeContext bridgeContext;
55
56     /**
57      * The listeners.
58      */

59     protected List JavaDoc listeners = Collections.synchronizedList(new LinkedList JavaDoc());
60
61     /**
62      * The exception thrown.
63      */

64     protected Exception JavaDoc exception;
65
66     /**
67      * Creates a new GVTTreeBuilder.
68      */

69     public GVTTreeBuilder(SVGDocument doc,
70                           BridgeContext bc) {
71         svgDocument = doc;
72         bridgeContext = bc;
73     }
74
75     /**
76      * Runs this builder.
77      */

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 JavaDoc e) {
111             exception = e;
112             fireEvent(failedDispatcher, ev);
113         } catch (ThreadDeath JavaDoc td) {
114             exception = new Exception JavaDoc(td.getMessage());
115             fireEvent(failedDispatcher, ev);
116             throw td;
117         } catch (Throwable JavaDoc t) {
118             t.printStackTrace();
119             exception = new Exception JavaDoc(t.getMessage());
120             fireEvent(failedDispatcher, ev);
121         } finally {
122             // bridgeContext.getDocumentLoader().dispose();
123
}
124     }
125
126     /**
127      * Returns the exception, if any occured.
128      */

129     public Exception JavaDoc getException() {
130         return exception;
131     }
132
133     /**
134      * Adds a GVTTreeBuilderListener to this GVTTreeBuilder.
135      */

136     public void addGVTTreeBuilderListener(GVTTreeBuilderListener l) {
137         listeners.add(l);
138     }
139
140     /**
141      * Removes a GVTTreeBuilderListener from this GVTTreeBuilder.
142      */

143     public void removeGVTTreeBuilderListener(GVTTreeBuilderListener l) {
144         listeners.remove(l);
145     }
146
147     public void fireEvent(Dispatcher dispatcher, Object JavaDoc event) {
148         EventDispatcher.fireEvent(dispatcher, listeners, event, true);
149     }
150
151     static Dispatcher startedDispatcher = new Dispatcher() {
152             public void dispatch(Object JavaDoc listener,
153                                  Object JavaDoc event) {
154                 ((GVTTreeBuilderListener)listener).gvtBuildStarted
155                  ((GVTTreeBuilderEvent)event);
156             }
157         };
158
159     static Dispatcher completedDispatcher = new Dispatcher() {
160             public void dispatch(Object JavaDoc listener,
161                                  Object JavaDoc event) {
162                 ((GVTTreeBuilderListener)listener).gvtBuildCompleted
163                  ((GVTTreeBuilderEvent)event);
164             }
165         };
166
167     static Dispatcher cancelledDispatcher = new Dispatcher() {
168             public void dispatch(Object JavaDoc listener,
169                                  Object JavaDoc event) {
170                 ((GVTTreeBuilderListener)listener).gvtBuildCancelled
171                  ((GVTTreeBuilderEvent)event);
172             }
173         };
174
175     static Dispatcher failedDispatcher = new Dispatcher() {
176             public void dispatch(Object JavaDoc listener,
177                                  Object JavaDoc event) {
178                 ((GVTTreeBuilderListener)listener).gvtBuildFailed
179                  ((GVTTreeBuilderEvent)event);
180             }
181         };
182 }
183
Popular Tags