1 |
alfonx |
510 |
package skrueger.geotools; |
2 |
|
|
|
3 |
|
|
import gtmig.org.geotools.swing.XMapPane; |
4 |
|
|
|
5 |
|
|
import java.awt.Graphics2D; |
6 |
|
|
import java.awt.Rectangle; |
7 |
alfonx |
529 |
import java.awt.geom.AffineTransform; |
8 |
alfonx |
510 |
|
9 |
|
|
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 |
|
|
public class RenderingExecutor { |
15 |
|
|
|
16 |
alfonx |
530 |
private volatile RenderThread renderThread; |
17 |
|
|
private final XMapPane mapPane; |
18 |
|
|
|
19 |
alfonx |
529 |
public RenderingExecutor(XMapPane mapPane) { |
20 |
|
|
this.mapPane = mapPane; |
21 |
alfonx |
530 |
} |
22 |
alfonx |
510 |
|
23 |
alfonx |
530 |
/** |
24 |
|
|
* Submit a new rendering task. If no rendering task is presently running |
25 |
|
|
* this new task will be accepted; otherwise it will be rejected (ie. there |
26 |
|
|
* is no task queue). |
27 |
|
|
* |
28 |
|
|
* @param envelope |
29 |
|
|
* the map area (world coordinates) to be rendered |
30 |
|
|
* @param graphics |
31 |
|
|
* the graphics object to draw on |
32 |
|
|
* |
33 |
|
|
* @return true if the rendering task was accepted; false if it was rejected |
34 |
|
|
*/ |
35 |
|
|
public synchronized boolean submit(ReferencedEnvelope envelope, |
36 |
|
|
Rectangle paintArea, Graphics2D graphics, |
37 |
|
|
final GTRenderer renderer, AffineTransform worldToScreen) { |
38 |
|
|
System.out.println("submit..:"); |
39 |
|
|
if (renderThread == null || !renderThread.isAlive()) { |
40 |
|
|
System.out.println("is vacant... starting thread!"); |
41 |
alfonx |
510 |
|
42 |
alfonx |
530 |
renderThread = new RenderThread(paintArea, graphics, renderer, |
43 |
|
|
worldToScreen); |
44 |
|
|
renderThread.start(); |
45 |
alfonx |
510 |
|
46 |
alfonx |
530 |
return true; |
47 |
|
|
} else { |
48 |
|
|
System.out.println("is busy... requesting stop!"); |
49 |
|
|
renderThread.getRenderer().stopRendering(); |
50 |
|
|
} |
51 |
alfonx |
510 |
|
52 |
alfonx |
530 |
return false; |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
class RenderThread extends Thread { |
56 |
|
|
|
57 |
|
|
private final GTRenderer renderer; |
58 |
|
|
|
59 |
|
|
public RenderThread(final Rectangle paintArea, |
60 |
|
|
final Graphics2D graphics, GTRenderer renderer, |
61 |
|
|
AffineTransform worldToScreen) { |
62 |
|
|
super(new RenderRun(paintArea, graphics, renderer, worldToScreen)); |
63 |
alfonx |
529 |
this.renderer = renderer; |
64 |
alfonx |
530 |
|
65 |
|
|
System.out.println("starting render thread " + getName()); |
66 |
alfonx |
529 |
} |
67 |
alfonx |
530 |
|
68 |
|
|
public GTRenderer getRenderer() { |
69 |
alfonx |
529 |
return renderer; |
70 |
|
|
} |
71 |
|
|
|
72 |
alfonx |
530 |
} |
73 |
|
|
|
74 |
|
|
class RenderRun implements Runnable, RenderListener { |
75 |
|
|
private final Rectangle paintArea; |
76 |
alfonx |
529 |
private final Graphics2D graphics; |
77 |
|
|
private final AffineTransform worldToScreen; |
78 |
|
|
private final GTRenderer renderer; |
79 |
alfonx |
510 |
|
80 |
alfonx |
529 |
public RenderRun(Rectangle paintArea, Graphics2D graphics, |
81 |
|
|
GTRenderer renderer, AffineTransform worldToScreen) { |
82 |
alfonx |
530 |
this.paintArea = paintArea; |
83 |
|
|
this.graphics = graphics; |
84 |
|
|
this.renderer = renderer; |
85 |
|
|
this.worldToScreen = worldToScreen; |
86 |
alfonx |
529 |
} |
87 |
alfonx |
530 |
|
88 |
alfonx |
529 |
@Override |
89 |
|
|
public void run() { |
90 |
|
|
try { |
91 |
alfonx |
530 |
renderer.addRenderListener(this); |
92 |
|
|
System.out.println("start rendering..."); |
93 |
|
|
try { |
94 |
alfonx |
529 |
Thread.sleep(1000); |
95 |
|
|
} catch (InterruptedException e) { |
96 |
|
|
// TODO Auto-generated catch block |
97 |
|
|
e.printStackTrace(); |
98 |
|
|
} |
99 |
alfonx |
530 |
renderer.paint(graphics, paintArea, worldToScreen); |
100 |
|
|
|
101 |
|
|
mapPane.onRenderingCompleted(); |
102 |
|
|
} finally { |
103 |
|
|
renderer.removeRenderListener(this); |
104 |
|
|
} |
105 |
alfonx |
529 |
} |
106 |
alfonx |
530 |
|
107 |
alfonx |
529 |
@Override |
108 |
|
|
public void errorOccurred(Exception e) { |
109 |
alfonx |
530 |
// System.out.println("rendering error"); |
110 |
alfonx |
529 |
mapPane.onRenderingFailed(e); |
111 |
|
|
} |
112 |
alfonx |
510 |
|
113 |
alfonx |
529 |
@Override |
114 |
|
|
public void featureRenderer(SimpleFeature feature) { |
115 |
|
|
} |
116 |
alfonx |
510 |
|
117 |
alfonx |
530 |
} |
118 |
|
|
|
119 |
alfonx |
529 |
public void cancelTask() { |
120 |
alfonx |
530 |
if (renderThread != null && renderThread.isAlive()) { |
121 |
|
|
// System.out.println("request stop for thread " +task.getName()); |
122 |
|
|
renderThread.getRenderer().stopRendering(); |
123 |
alfonx |
529 |
} |
124 |
|
|
} |
125 |
alfonx |
510 |
|
126 |
alfonx |
529 |
public boolean isRunning() { |
127 |
alfonx |
530 |
// if (task != null) |
128 |
|
|
// System.out.println("is running "+task.getName()+" = true"); |
129 |
|
|
return (renderThread != null && renderThread.isAlive()); |
130 |
alfonx |
529 |
} |
131 |
alfonx |
510 |
|
132 |
alfonx |
529 |
public void dispose() { |
133 |
alfonx |
530 |
if (renderThread != null) { |
134 |
|
|
renderThread.renderer.stopRendering(); |
135 |
|
|
renderThread = null; |
136 |
alfonx |
529 |
} |
137 |
|
|
} |
138 |
alfonx |
510 |
|
139 |
|
|
} |