KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.InterruptedIOException JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
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 /**
33  * This class represents an object which loads asynchroneaously a SVG document.
34  *
35  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
36  * @version $Id: SVGDocumentLoader.java,v 1.11 2004/10/23 17:11:03 deweese Exp $
37  */

38 public class SVGDocumentLoader extends HaltingThread {
39
40     /**
41      * The URL of the document,
42      */

43     protected String JavaDoc url;
44
45     /**
46      * The document loader.
47      */

48     protected DocumentLoader loader;
49
50     /**
51      * The exception thrown.
52      */

53     protected Exception JavaDoc exception;
54
55     /**
56      * The listeners.
57      */

58     protected List JavaDoc listeners = Collections.synchronizedList(new LinkedList JavaDoc());
59
60     /**
61      * Creates a new SVGDocumentLoader.
62      * @param u The URL of the document.
63      * @param l The document loader to use
64      */

65     public SVGDocumentLoader(String JavaDoc u, DocumentLoader l) {
66         url = u;
67         loader = l;
68     }
69
70     /**
71      * Runs this loader.
72      */

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 JavaDoc e) {
93             fireEvent(cancelledDispatcher, evt);
94         } catch (Exception JavaDoc e) {
95             exception = e;
96             fireEvent(failedDispatcher, evt);
97         } catch (ThreadDeath JavaDoc td) {
98             exception = new Exception JavaDoc(td.getMessage());
99             fireEvent(failedDispatcher, evt);
100             throw td;
101         } catch (Throwable JavaDoc t) {
102             t.printStackTrace();
103             exception = new Exception JavaDoc(t.getMessage());
104             fireEvent(failedDispatcher, evt);
105         }
106     }
107
108     /**
109      * Returns the exception, if any occured.
110      */

111     public Exception JavaDoc getException() {
112         return exception;
113     }
114
115     /**
116      * Adds a SVGDocumentLoaderListener to this SVGDocumentLoader.
117      */

118     public void addSVGDocumentLoaderListener(SVGDocumentLoaderListener l) {
119         listeners.add(l);
120     }
121
122     /**
123      * Removes a SVGDocumentLoaderListener from this SVGDocumentLoader.
124      */

125     public void removeSVGDocumentLoaderListener(SVGDocumentLoaderListener l) {
126         listeners.remove(l);
127     }
128
129     public void fireEvent(Dispatcher dispatcher, Object JavaDoc event) {
130         EventDispatcher.fireEvent(dispatcher, listeners, event, true);
131     }
132
133     static Dispatcher startedDispatcher = new Dispatcher() {
134             public void dispatch(Object JavaDoc listener,
135                                  Object JavaDoc event) {
136                 ((SVGDocumentLoaderListener)listener).documentLoadingStarted
137                     ((SVGDocumentLoaderEvent)event);
138             }
139         };
140             
141             static Dispatcher completedDispatcher = new Dispatcher() {
142             public void dispatch(Object JavaDoc listener,
143                                  Object JavaDoc event) {
144                 ((SVGDocumentLoaderListener)listener).documentLoadingCompleted
145                  ((SVGDocumentLoaderEvent)event);
146             }
147         };
148
149     static Dispatcher cancelledDispatcher = new Dispatcher() {
150             public void dispatch(Object JavaDoc listener,
151                                  Object JavaDoc event) {
152                 ((SVGDocumentLoaderListener)listener).documentLoadingCancelled
153                  ((SVGDocumentLoaderEvent)event);
154             }
155         };
156
157     static Dispatcher failedDispatcher = new Dispatcher() {
158             public void dispatch(Object JavaDoc listener,
159                                  Object JavaDoc event) {
160                 ((SVGDocumentLoaderListener)listener).documentLoadingFailed
161                  ((SVGDocumentLoaderEvent)event);
162             }
163         };
164 }
165
Popular Tags