KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > swing > gvt > GVTTreeRenderer


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.gvt;
19
20 import java.awt.Shape JavaDoc;
21 import java.awt.geom.AffineTransform JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.batik.bridge.InterruptedBridgeException;
27 import org.apache.batik.gvt.renderer.ImageRenderer;
28 import org.apache.batik.util.EventDispatcher;
29 import org.apache.batik.util.EventDispatcher.Dispatcher;
30 import org.apache.batik.util.HaltingThread;
31
32 /**
33  * This class represents an object which renders asynchroneaously
34  * a GVT tree.
35  *
36  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
37  * @version $Id: GVTTreeRenderer.java,v 1.15 2005/03/27 08:58:36 cam Exp $
38  */

39 public class GVTTreeRenderer extends HaltingThread {
40
41     /**
42      * The renderer used to paint.
43      */

44     protected ImageRenderer renderer;
45
46     /**
47      * The area of interest.
48      */

49     protected Shape JavaDoc areaOfInterest;
50
51     /**
52      * The buffer width.
53      */

54     protected int width;
55
56     /**
57      * The buffer height.
58      */

59     protected int height;
60
61     /**
62      * The user to device transform.
63      */

64     protected AffineTransform JavaDoc user2DeviceTransform;
65
66     /**
67      * Whether to enable the double buffering.
68      */

69     protected boolean doubleBuffering;
70
71     /**
72      * The listeners.
73      */

74     protected List JavaDoc listeners = Collections.synchronizedList(new LinkedList JavaDoc());
75
76     /**
77      * Creates a new GVTTreeRenderer.
78      * @param r The renderer to use to paint.
79      * @param usr2dev The user to device transform.
80      * @param dbuffer Whether the double buffering should be enabled.
81      * @param aoi The area of interest in the renderer space units.
82      * @param width The offscreen buffer width.
83      * @param height The offscreen buffer height.
84      */

85     public GVTTreeRenderer(ImageRenderer r, AffineTransform JavaDoc usr2dev,
86                            boolean dbuffer,
87                            Shape JavaDoc aoi, int width, int height) {
88         renderer = r;
89         areaOfInterest = aoi;
90         user2DeviceTransform = usr2dev;
91         doubleBuffering = dbuffer;
92         this.width = width;
93         this.height = height;
94     }
95
96     /**
97      * Runs this renderer.
98      */

99     public void run() {
100         GVTTreeRendererEvent ev = new GVTTreeRendererEvent(this, null);
101         try {
102             fireEvent(prepareDispatcher, ev);
103
104             renderer.setTransform(user2DeviceTransform);
105             renderer.setDoubleBuffered(doubleBuffering);
106             renderer.updateOffScreen(width, height);
107             renderer.clearOffScreen();
108
109             if (isHalted()) {
110                 fireEvent(cancelledDispatcher, ev);
111                 return;
112             }
113
114             ev = new GVTTreeRendererEvent(this, renderer.getOffScreen());
115             fireEvent(startedDispatcher, ev);
116
117             if (isHalted()) {
118                 fireEvent(cancelledDispatcher, ev);
119                 return;
120             }
121
122             renderer.repaint(areaOfInterest);
123
124             if (isHalted()) {
125                 fireEvent(cancelledDispatcher, ev);
126                 return;
127             }
128
129             ev = new GVTTreeRendererEvent(this, renderer.getOffScreen());
130             fireEvent(completedDispatcher, ev);
131         } catch (NoClassDefFoundError JavaDoc e) {
132             // This error was reported to happen when the rendering
133
// is interrupted with JDK1.3.0rc1 Solaris.
134
} catch (InterruptedBridgeException e) {
135             // this sometimes happens with SVG Fonts since the glyphs are
136
// not built till the rendering stage
137
fireEvent(cancelledDispatcher, ev);
138         } catch (ThreadDeath JavaDoc td) {
139             fireEvent(failedDispatcher, ev);
140             throw td;
141         } catch (Throwable JavaDoc t) {
142             t.printStackTrace();
143             fireEvent(failedDispatcher, ev);
144         }
145     }
146
147     public void fireEvent(Dispatcher dispatcher, Object JavaDoc event) {
148         EventDispatcher.fireEvent(dispatcher, listeners, event, true);
149     }
150
151     /**
152      * Adds a GVTTreeRendererListener to this GVTTreeRenderer.
153      */

154     public void addGVTTreeRendererListener(GVTTreeRendererListener l) {
155         listeners.add(l);
156     }
157
158     /**
159      * Removes a GVTTreeRendererListener from this GVTTreeRenderer.
160      */

161     public void removeGVTTreeRendererListener(GVTTreeRendererListener l) {
162         listeners.remove(l);
163     }
164
165     static Dispatcher prepareDispatcher = new Dispatcher() {
166             public void dispatch(Object JavaDoc listener,
167                                  Object JavaDoc event) {
168                 ((GVTTreeRendererListener)listener).gvtRenderingPrepare
169                     ((GVTTreeRendererEvent)event);
170             }
171         };
172             
173     static Dispatcher startedDispatcher = new Dispatcher() {
174             public void dispatch(Object JavaDoc listener,
175                                  Object JavaDoc event) {
176                 ((GVTTreeRendererListener)listener).gvtRenderingStarted
177                     ((GVTTreeRendererEvent)event);
178             }
179         };
180             
181     static Dispatcher cancelledDispatcher = new Dispatcher() {
182             public void dispatch(Object JavaDoc listener,
183                                  Object JavaDoc event) {
184                 ((GVTTreeRendererListener)listener).gvtRenderingCancelled
185                     ((GVTTreeRendererEvent)event);
186             }
187         };
188             
189     static Dispatcher completedDispatcher = new Dispatcher() {
190             public void dispatch(Object JavaDoc listener,
191                                  Object JavaDoc event) {
192                 ((GVTTreeRendererListener)listener).gvtRenderingCompleted
193                     ((GVTTreeRendererEvent)event);
194             }
195         };
196             
197     static Dispatcher failedDispatcher = new Dispatcher() {
198             public void dispatch(Object JavaDoc listener,
199                                  Object JavaDoc event) {
200                 ((GVTTreeRendererListener)listener).gvtRenderingFailed
201                     ((GVTTreeRendererEvent)event);
202             }
203         };
204             
205
206 }
207
Popular Tags