1 |
package skrueger.geotools; |
2 |
|
3 |
import java.awt.Graphics2D; |
4 |
import java.awt.Rectangle; |
5 |
import java.awt.geom.AffineTransform; |
6 |
|
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 |
/** |
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 |
private volatile RenderThread renderThread; |
23 |
|
24 |
private final XMapPane mapPane; |
25 |
|
26 |
public RenderingExecutor(XMapPane mapPane) { |
27 |
this.mapPane = mapPane; |
28 |
} |
29 |
|
30 |
/** |
31 |
* Submit a new rendering task. If no rendering task is presently running |
32 |
* this new job will be accepted; otherwise it will be rejected and it |
33 |
* returns <code>false</code>. |
34 |
* |
35 |
* @param envelope |
36 |
* the map area (world coordinates) to be rendered. |
37 |
* @param graphics |
38 |
* 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 |
* |
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 |
52 |
// , AffineTransform worldToScreen |
53 |
) { |
54 |
if (renderThread == null || !renderThread.isAlive()) { |
55 |
// System.out.println("is vacant... starting thread!"); |
56 |
renderThread = null; |
57 |
|
58 |
renderThread = new RenderThread(paintArea, graphics, renderer, |
59 |
// worldToScreen, |
60 |
envelope); |
61 |
renderThread.start(); |
62 |
|
63 |
return true; |
64 |
} else { |
65 |
// System.out.println("is busy... requesting stop!"); |
66 |
renderThread.getRenderer().stopRendering(); |
67 |
return false; |
68 |
} |
69 |
} |
70 |
|
71 |
/** |
72 |
* For every new rendering job submitted and accepted, an instance of this |
73 |
* {@link Thread} will be started. |
74 |
* |
75 |
*/ |
76 |
class RenderThread extends Thread { |
77 |
|
78 |
private final GTRenderer renderer; |
79 |
|
80 |
public RenderThread(final Rectangle paintArea, |
81 |
final Graphics2D graphics, GTRenderer renderer, |
82 |
// AffineTransform worldToScreen, |
83 |
ReferencedEnvelope mapEnv) { |
84 |
super(new RenderRun(paintArea, graphics, renderer, |
85 |
mapEnv |
86 |
// , worldToScreen |
87 |
)); |
88 |
this.renderer = renderer; |
89 |
|
90 |
setName("Render" + getName()); |
91 |
|
92 |
// System.out.println("starting render thread " + getName()); |
93 |
} |
94 |
|
95 |
public GTRenderer getRenderer() { |
96 |
return renderer; |
97 |
} |
98 |
|
99 |
} |
100 |
|
101 |
/** |
102 |
* This {@link Runnable} will actually start the rendering |
103 |
*/ |
104 |
class RenderRun implements Runnable, RenderListener { |
105 |
private final Rectangle paintArea; |
106 |
private final Graphics2D graphics; |
107 |
// private final AffineTransform worldToScreen; |
108 |
private final GTRenderer renderer; |
109 |
private final ReferencedEnvelope mapEnv; |
110 |
|
111 |
public RenderRun(Rectangle paintArea, Graphics2D graphics, |
112 |
GTRenderer renderer, ReferencedEnvelope mapEnv |
113 |
// , |
114 |
// AffineTransform worldToScreen |
115 |
) { |
116 |
this.paintArea = paintArea; |
117 |
this.graphics = graphics; |
118 |
this.renderer = renderer; |
119 |
this.mapEnv = mapEnv; |
120 |
// this.worldToScreen = worldToScreen; |
121 |
} |
122 |
|
123 |
@Override |
124 |
public void run() { |
125 |
try { |
126 |
renderer.addRenderListener(this); |
127 |
System.out.println("start rendering..."); |
128 |
// try { |
129 |
// Thread.sleep(1000); |
130 |
// } catch (InterruptedException e) { |
131 |
// e.printStackTrace(); |
132 |
// } |
133 |
|
134 |
// Clear the graphics context |
135 |
graphics.setBackground(mapPane.getMapBackgroundColor()); |
136 |
graphics.clearRect(paintArea.x, paintArea.y, paintArea.width, |
137 |
paintArea.height); |
138 |
|
139 |
renderer.paint(graphics, paintArea, mapEnv); |
140 |
|
141 |
// Kill the reference to this Thread so #isRunning will say |
142 |
// false directly |
143 |
renderThread = null; |
144 |
mapPane.onRenderingCompleted(); |
145 |
} catch (Exception e) { |
146 |
mapPane.onRenderingFailed(e); |
147 |
} finally { |
148 |
renderer.removeRenderListener(this); |
149 |
} |
150 |
} |
151 |
|
152 |
@Override |
153 |
public void errorOccurred(Exception e) { |
154 |
mapPane.onRenderingFailed(e); |
155 |
} |
156 |
|
157 |
@Override |
158 |
public void featureRenderer(SimpleFeature feature) { |
159 |
} |
160 |
|
161 |
} |
162 |
|
163 |
/** |
164 |
* Ask to stop the rendering. May be called often. |
165 |
*/ |
166 |
public void cancelTask() { |
167 |
if (renderThread != null && renderThread.isAlive()) { |
168 |
// System.out.println("request stop for thread " +task.getName()); |
169 |
renderThread.getRenderer().stopRendering(); |
170 |
} |
171 |
} |
172 |
|
173 |
/** |
174 |
* @return <code>true</code> if the {@link Thread} is busy rendering. |
175 |
*/ |
176 |
public boolean isRunning() { |
177 |
return (renderThread != null && renderThread.isAlive()); |
178 |
} |
179 |
|
180 |
/** |
181 |
* Will stop rendering and remove the reference to the {@link Thread}. |
182 |
*/ |
183 |
public void dispose() { |
184 |
if (renderThread != null) { |
185 |
renderThread.renderer.stopRendering(); |
186 |
renderThread = null; |
187 |
} |
188 |
} |
189 |
|
190 |
} |