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