/[schmitzm]/branches/2.0-RC2/src/skrueger/geotools/RenderingExecutor.java
ViewVC logotype

Annotation of /branches/2.0-RC2/src/skrueger/geotools/RenderingExecutor.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 533 - (hide annotations)
Thu Nov 19 17:27:01 2009 UTC (15 years, 3 months ago) by alfonx
Original Path: branches/1.0-gt2-2.6/src/skrueger/geotools/RenderingExecutor.java
File size: 4954 byte(s)
* XMapPane now prints erros nicely
1 alfonx 510 package skrueger.geotools;
2    
3     import java.awt.Graphics2D;
4     import java.awt.Rectangle;
5 alfonx 529 import java.awt.geom.AffineTransform;
6 alfonx 510
7     import org.geotools.geometry.jts.ReferencedEnvelope;
8     import org.geotools.renderer.GTRenderer;
9     import org.geotools.renderer.RenderListener;
10     import org.opengis.feature.simple.SimpleFeature;
11    
12 alfonx 532 /**
13     * This class is used by {@link XMapPane} to start and stop the rendering a
14     * {@link Thread} for rendering.
15     */
16     class RenderingExecutor {
17    
18     /**
19     * Instance to a {@link RenderThread} doing any work. It's volatile so the
20     * correct value will always be visible to any {@link Thread}
21     **/
22 alfonx 530 private volatile RenderThread renderThread;
23 alfonx 532
24 alfonx 530 private final XMapPane mapPane;
25    
26 alfonx 529 public RenderingExecutor(XMapPane mapPane) {
27     this.mapPane = mapPane;
28 alfonx 530 }
29 alfonx 510
30 alfonx 530 /**
31     * Submit a new rendering task. If no rendering task is presently running
32 alfonx 532 * this new job will be accepted; otherwise it will be rejected and it
33     * returns <code>false</code>.
34 alfonx 530 *
35     * @param envelope
36 alfonx 532 * the map area (world coordinates) to be rendered.
37 alfonx 530 * @param graphics
38 alfonx 532 * the graphics object to draw on.
39     * @param paintArea
40     * size of the area to paint the world into.
41     * @param worldToScreen
42     * the {@link AffineTransform} from world coordinates to screen
43     * coordinates.
44     * @param renderer
45     * the {@link GTRenderer} to use.
46 alfonx 530 *
47     * @return true if the rendering task was accepted; false if it was rejected
48     */
49     public synchronized boolean submit(ReferencedEnvelope envelope,
50     Rectangle paintArea, Graphics2D graphics,
51     final GTRenderer renderer, AffineTransform worldToScreen) {
52     if (renderThread == null || !renderThread.isAlive()) {
53 alfonx 532 // System.out.println("is vacant... starting thread!");
54     renderThread = null;
55 alfonx 510
56 alfonx 530 renderThread = new RenderThread(paintArea, graphics, renderer,
57 alfonx 533 worldToScreen, envelope);
58 alfonx 530 renderThread.start();
59 alfonx 510
60 alfonx 530 return true;
61     } else {
62 alfonx 532 // System.out.println("is busy... requesting stop!");
63 alfonx 530 renderThread.getRenderer().stopRendering();
64 alfonx 532 return false;
65 alfonx 530 }
66     }
67    
68 alfonx 532 /**
69     * For every new rendering job submitted and accepted, an instance of this
70     * {@link Thread} will be started.
71     *
72     */
73 alfonx 530 class RenderThread extends Thread {
74    
75     private final GTRenderer renderer;
76    
77     public RenderThread(final Rectangle paintArea,
78     final Graphics2D graphics, GTRenderer renderer,
79 alfonx 533 AffineTransform worldToScreen, ReferencedEnvelope mapEnv) {
80     super(new RenderRun(paintArea, graphics, renderer, mapEnv, worldToScreen));
81 alfonx 529 this.renderer = renderer;
82 alfonx 530
83 alfonx 532 setName("Render" + getName());
84    
85     // System.out.println("starting render thread " + getName());
86 alfonx 529 }
87 alfonx 530
88     public GTRenderer getRenderer() {
89 alfonx 529 return renderer;
90     }
91    
92 alfonx 530 }
93    
94 alfonx 532 /**
95     * This {@link Runnable} will actually start the rendering
96     */
97 alfonx 530 class RenderRun implements Runnable, RenderListener {
98     private final Rectangle paintArea;
99 alfonx 529 private final Graphics2D graphics;
100     private final AffineTransform worldToScreen;
101     private final GTRenderer renderer;
102 alfonx 533 private final ReferencedEnvelope mapEnv;
103 alfonx 510
104 alfonx 529 public RenderRun(Rectangle paintArea, Graphics2D graphics,
105 alfonx 533 GTRenderer renderer, ReferencedEnvelope mapEnv, AffineTransform worldToScreen) {
106 alfonx 530 this.paintArea = paintArea;
107     this.graphics = graphics;
108     this.renderer = renderer;
109 alfonx 533 this.mapEnv = mapEnv;
110 alfonx 530 this.worldToScreen = worldToScreen;
111 alfonx 529 }
112 alfonx 530
113 alfonx 529 @Override
114     public void run() {
115     try {
116 alfonx 530 renderer.addRenderListener(this);
117     System.out.println("start rendering...");
118 alfonx 532 // try {
119     // Thread.sleep(1000);
120     // } catch (InterruptedException e) {
121     // e.printStackTrace();
122     // }
123    
124     // Clear the graphics context
125     graphics.setBackground(mapPane.getMapBackgroundColor());
126     graphics.clearRect(paintArea.x, paintArea.y, paintArea.width,
127     paintArea.height);
128    
129 alfonx 533 renderer.paint(graphics, paintArea, mapEnv, worldToScreen);
130 alfonx 530
131 alfonx 533 // Kill the reference to this Thread to #isRunning will def. say false
132     renderThread = null;
133     mapPane.onRenderingCompleted();
134    
135 alfonx 530 } finally {
136     renderer.removeRenderListener(this);
137     }
138 alfonx 529 }
139 alfonx 530
140 alfonx 529 @Override
141     public void errorOccurred(Exception e) {
142     mapPane.onRenderingFailed(e);
143     }
144 alfonx 510
145 alfonx 529 @Override
146     public void featureRenderer(SimpleFeature feature) {
147     }
148 alfonx 510
149 alfonx 530 }
150    
151 alfonx 532 /**
152     * Ask to stop the rendering. May be called often.
153     */
154 alfonx 529 public void cancelTask() {
155 alfonx 530 if (renderThread != null && renderThread.isAlive()) {
156     // System.out.println("request stop for thread " +task.getName());
157     renderThread.getRenderer().stopRendering();
158 alfonx 529 }
159     }
160 alfonx 510
161 alfonx 532 /**
162     * @return <code>true</code> if the {@link Thread} is busy rendering.
163     */
164 alfonx 529 public boolean isRunning() {
165 alfonx 530 // if (task != null)
166     // System.out.println("is running "+task.getName()+" = true");
167     return (renderThread != null && renderThread.isAlive());
168 alfonx 529 }
169 alfonx 510
170 alfonx 532 /**
171     * Will stop rendering and remove the reference to the {@link Thread}.
172     */
173 alfonx 529 public void dispose() {
174 alfonx 530 if (renderThread != null) {
175     renderThread.renderer.stopRendering();
176     renderThread = null;
177 alfonx 529 }
178     }
179 alfonx 510
180     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26