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