KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 2002-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.InterruptedBridgeException;
26 import org.apache.batik.bridge.UpdateManager;
27 import org.apache.batik.gvt.GraphicsNode;
28 import org.apache.batik.util.EventDispatcher;
29 import org.apache.batik.util.EventDispatcher.Dispatcher;
30 import org.apache.batik.util.HaltingThread;
31
32 import org.w3c.dom.svg.SVGDocument;
33
34 /**
35  * This class dispatches the SVGLoadEvent event on a SVG document.
36  *
37  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
38  * @version $Id: SVGLoadEventDispatcher.java,v 1.10 2004/10/23 17:11:03 deweese Exp $
39  */

40 public class SVGLoadEventDispatcher extends HaltingThread {
41
42     /**
43      * The SVG document to give to the bridge.
44      */

45     protected SVGDocument svgDocument;
46
47     /**
48      * The root graphics node.
49      */

50     protected GraphicsNode root;
51
52     /**
53      * The bridge context to use.
54      */

55     protected BridgeContext bridgeContext;
56
57     /**
58      * The update manager.
59      */

60     protected UpdateManager updateManager;
61
62     /**
63      * The listeners.
64      */

65     protected List JavaDoc listeners = Collections.synchronizedList(new LinkedList JavaDoc());
66
67     /**
68      * The exception thrown.
69      */

70     protected Exception JavaDoc exception;
71
72     /**
73      * Creates a new SVGLoadEventDispatcher.
74      */

75     public SVGLoadEventDispatcher(GraphicsNode gn,
76                                   SVGDocument doc,
77                                   BridgeContext bc,
78                                   UpdateManager um) {
79         svgDocument = doc;
80         root = gn;
81         bridgeContext = bc;
82         updateManager = um;
83     }
84
85     /**
86      * Runs the dispatcher.
87      */

88     public void run() {
89         SVGLoadEventDispatcherEvent ev;
90         ev = new SVGLoadEventDispatcherEvent(this, root);
91         try {
92             fireEvent(startedDispatcher, ev);
93
94             if (isHalted()) {
95                 fireEvent(cancelledDispatcher, ev);
96                 return;
97             }
98
99             updateManager.dispatchSVGLoadEvent();
100
101             if (isHalted()) {
102                 fireEvent(cancelledDispatcher, ev);
103                 return;
104             }
105
106             fireEvent(completedDispatcher, ev);
107         } catch (InterruptedException JavaDoc e) {
108             fireEvent(cancelledDispatcher, ev);
109         } catch (InterruptedBridgeException e) {
110             fireEvent(cancelledDispatcher, ev);
111         } catch (Exception JavaDoc e) {
112             exception = e;
113             fireEvent(failedDispatcher, ev);
114         } catch (ThreadDeath JavaDoc td) {
115             exception = new Exception JavaDoc(td.getMessage());
116             fireEvent(failedDispatcher, ev);
117             throw td;
118         } catch (Throwable JavaDoc t) {
119             t.printStackTrace();
120             exception = new Exception JavaDoc(t.getMessage());
121             fireEvent(failedDispatcher, ev);
122         }
123     }
124
125     /**
126      * Returns the update manager.
127      */

128     public UpdateManager getUpdateManager() {
129         return updateManager;
130     }
131
132     /**
133      * Returns the exception, if any occured.
134      */

135     public Exception JavaDoc getException() {
136         return exception;
137     }
138
139     /**
140      * Adds a SVGLoadEventDispatcherListener to this SVGLoadEventDispatcher.
141      */

142     public void addSVGLoadEventDispatcherListener
143         (SVGLoadEventDispatcherListener l) {
144         listeners.add(l);
145     }
146
147     /**
148      * Removes a SVGLoadEventDispatcherListener from this
149      * SVGLoadEventDispatcher.
150      */

151     public void removeSVGLoadEventDispatcherListener
152         (SVGLoadEventDispatcherListener l) {
153         listeners.remove(l);
154     }
155
156     public void fireEvent(Dispatcher dispatcher, Object JavaDoc event) {
157         EventDispatcher.fireEvent(dispatcher, listeners, event, true);
158     }
159
160     static Dispatcher startedDispatcher = new Dispatcher() {
161             public void dispatch(Object JavaDoc listener,
162                                  Object JavaDoc event) {
163                 ((SVGLoadEventDispatcherListener)listener).
164                     svgLoadEventDispatchStarted
165                     ((SVGLoadEventDispatcherEvent)event);
166             }
167         };
168
169     static Dispatcher completedDispatcher = new Dispatcher() {
170             public void dispatch(Object JavaDoc listener,
171                                  Object JavaDoc event) {
172                 ((SVGLoadEventDispatcherListener)listener).
173                     svgLoadEventDispatchCompleted
174                     ((SVGLoadEventDispatcherEvent)event);
175             }
176         };
177
178     static Dispatcher cancelledDispatcher = new Dispatcher() {
179             public void dispatch(Object JavaDoc listener,
180                                  Object JavaDoc event) {
181                 ((SVGLoadEventDispatcherListener)listener).
182                     svgLoadEventDispatchCancelled
183                     ((SVGLoadEventDispatcherEvent)event);
184             }
185         };
186
187     static Dispatcher failedDispatcher = new Dispatcher() {
188             public void dispatch(Object JavaDoc listener,
189                                  Object JavaDoc event) {
190                 ((SVGLoadEventDispatcherListener)listener).
191                     svgLoadEventDispatchFailed
192                     ((SVGLoadEventDispatcherEvent)event);
193             }
194         };
195 }
196
Popular Tags