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