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