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