1 |
package skrueger.geotools; |
package skrueger.geotools; |
2 |
|
|
|
/* |
|
|
* GeoTools - The Open Source Java GIS Toolkit |
|
|
* http://geotools.org |
|
|
* |
|
|
* (C) 2002-2008, Open Source Geospatial Foundation (OSGeo) |
|
|
* |
|
|
* This library is free software; you can redistribute it and/or |
|
|
* modify it under the terms of the GNU Lesser General Public |
|
|
* License as published by the Free Software Foundation; |
|
|
* version 2.1 of the License. |
|
|
* |
|
|
* This library is distributed in the hope that it will be useful, |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
|
* Lesser General Public License for more details. |
|
|
*/ |
|
|
|
|
|
|
|
|
import gtmig.org.geotools.swing.XMapPane; |
|
|
|
|
|
import java.awt.AlphaComposite; |
|
|
import java.awt.Color; |
|
|
import java.awt.Composite; |
|
3 |
import java.awt.Graphics2D; |
import java.awt.Graphics2D; |
4 |
import java.awt.Rectangle; |
import java.awt.Rectangle; |
5 |
import java.util.concurrent.Callable; |
import java.awt.geom.AffineTransform; |
|
import java.util.concurrent.CountDownLatch; |
|
|
import java.util.concurrent.ExecutorService; |
|
|
import java.util.concurrent.Executors; |
|
|
import java.util.concurrent.Future; |
|
|
import java.util.concurrent.ScheduledExecutorService; |
|
|
import java.util.concurrent.ScheduledFuture; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
import java.util.concurrent.atomic.AtomicBoolean; |
|
6 |
|
|
7 |
|
import org.apache.log4j.Logger; |
8 |
import org.geotools.geometry.jts.ReferencedEnvelope; |
import org.geotools.geometry.jts.ReferencedEnvelope; |
9 |
import org.geotools.renderer.GTRenderer; |
import org.geotools.renderer.GTRenderer; |
10 |
import org.geotools.renderer.RenderListener; |
import org.geotools.renderer.RenderListener; |
|
import org.geotools.swing.JMapPane; |
|
11 |
import org.opengis.feature.simple.SimpleFeature; |
import org.opengis.feature.simple.SimpleFeature; |
12 |
|
|
13 |
|
import schmitzm.geotools.gui.XMapPane; |
14 |
|
|
15 |
/** |
/** |
16 |
* This class is used by {@code JMapPane} to handle the scheduling and running of |
* This class is used by {@link XMapPane} to start and stop the rendering a |
17 |
* rendering tasks on a background thread. It functions as a single thread, non- |
* {@link Thread} for rendering. |
|
* queueing executor, ie. only one rendering task can run at any given time and, |
|
|
* while it is running, any other submitted tasks will be rejected. |
|
|
* <p> |
|
|
* Whether a rendering task is accepted or rejected can be tested on submission: |
|
|
* <pre><code> |
|
|
* ReferencedEnvelope areaToDraw = ... |
|
|
* Graphics2D graphicsToDrawInto = ... |
|
|
* boolean accepted = renderingExecutor.submit(areaToDraw, graphicsToDrawInto); |
|
|
* </code></pre> |
|
|
* |
|
|
* The status of the executor can also be checked at any time like this: |
|
|
* <pre><code> |
|
|
* boolean busy = renderingExecutor.isRunning(); |
|
|
* </code></pre> |
|
|
* |
|
|
* While a rendering task is running it is regularly polled to see if it has completed |
|
|
* and, if so, whether it finished normally, was cancelled or failed. The interval between |
|
|
* polling can be adjusted which might be useful to tune the executor for particular |
|
|
* applications: |
|
|
* <pre><code> |
|
|
* RenderingExecutor re = new RenderingExecutor( mapPane ); |
|
|
* re.setPollingInterval( 150 ); // 150 milliseconds |
|
|
* </code></pre> |
|
|
* |
|
|
* @author Michael Bedward |
|
|
* @since 2.7 |
|
|
* @source $URL: http://svn.osgeo.org/geotools/branches/2.6.x/modules/unsupported/swing/src/main/java/org/geotools/swing/RenderingExecutor.java $ |
|
|
* @version $Id: RenderingExecutor.java 34285 2009-10-30 10:48:49Z mbedward $ |
|
|
* |
|
|
* @see JMapPane |
|
18 |
*/ |
*/ |
19 |
public class RenderingExecutor { |
public class RenderingExecutor { |
20 |
|
private final static Logger LOGGER = Logger.getLogger(RenderingExecutor.class); |
21 |
|
/** |
22 |
|
* Instance to a {@link RenderThread} doing any work. It's volatile so the |
23 |
|
* correct value will always be visible to any {@link Thread} |
24 |
|
**/ |
25 |
|
private volatile RenderThread renderThread; |
26 |
|
|
27 |
|
private final XMapPane mapPane; |
28 |
|
|
29 |
|
public RenderingExecutor(XMapPane mapPane) { |
30 |
|
this.mapPane = mapPane; |
31 |
|
} |
32 |
|
|
33 |
|
/** |
34 |
|
* Submit a new rendering task. If no rendering task is presently running |
35 |
|
* this new job will be accepted; otherwise it will be rejected and it |
36 |
|
* returns <code>false</code>. |
37 |
|
* |
38 |
|
* @param envelope |
39 |
|
* the map area (world coordinates) to be rendered. |
40 |
|
* @param graphics |
41 |
|
* the graphics object to draw on. |
42 |
|
* @param paintArea |
43 |
|
* size of the area to paint the world into. |
44 |
|
* @param worldToScreen |
45 |
|
* the {@link AffineTransform} from world coordinates to screen |
46 |
|
* coordinates. |
47 |
|
* @param renderer |
48 |
|
* the {@link GTRenderer} to use. |
49 |
|
* |
50 |
|
* @return true if the rendering task was accepted; false if it was rejected |
51 |
|
*/ |
52 |
|
public synchronized boolean submit(ReferencedEnvelope envelope, |
53 |
|
Rectangle paintArea, Graphics2D graphics, final GTRenderer renderer |
54 |
|
// , AffineTransform worldToScreen |
55 |
|
) { |
56 |
|
if (renderThread == null || !renderThread.isAlive()) { |
57 |
|
// System.out.println("is vacant... starting thread!"); |
58 |
|
renderThread = null; |
59 |
|
|
60 |
|
renderThread = new RenderThread(paintArea, graphics, renderer, |
61 |
|
// worldToScreen, |
62 |
|
envelope); |
63 |
|
renderThread.start(); |
64 |
|
|
65 |
|
return true; |
66 |
|
} else { |
67 |
|
// System.out.println("is busy... requesting stop!"); |
68 |
|
renderThread.getRenderer().stopRendering(); |
69 |
|
return false; |
70 |
|
} |
71 |
|
} |
72 |
|
|
73 |
|
/** |
74 |
|
* For every new rendering job submitted and accepted, an instance of this |
75 |
|
* {@link Thread} will be started. |
76 |
|
* |
77 |
|
*/ |
78 |
|
class RenderThread extends Thread { |
79 |
|
|
80 |
|
private final GTRenderer renderer; |
81 |
|
|
82 |
|
public RenderThread(final Rectangle paintArea, |
83 |
|
final Graphics2D graphics, GTRenderer renderer, |
84 |
|
// AffineTransform worldToScreen, |
85 |
|
ReferencedEnvelope mapEnv) { |
86 |
|
super(new RenderRun(paintArea, graphics, renderer, mapEnv |
87 |
|
// , worldToScreen |
88 |
|
)); |
89 |
|
this.renderer = renderer; |
90 |
|
|
91 |
|
setName("Render" + getName()); |
92 |
|
|
93 |
|
// System.out.println("starting render thread " + getName()); |
94 |
|
} |
95 |
|
|
96 |
|
public GTRenderer getRenderer() { |
97 |
|
return renderer; |
98 |
|
} |
99 |
|
|
100 |
|
} |
101 |
|
|
102 |
|
/** |
103 |
|
* This {@link Runnable} will actually start the rendering |
104 |
|
*/ |
105 |
|
class RenderRun implements Runnable, RenderListener { |
106 |
|
private final Rectangle paintArea; |
107 |
|
private final Graphics2D graphics; |
108 |
|
// private final AffineTransform worldToScreen; |
109 |
|
private final GTRenderer renderer; |
110 |
|
private final ReferencedEnvelope mapEnv; |
111 |
|
|
112 |
|
public RenderRun(Rectangle paintArea, Graphics2D graphics, |
113 |
|
GTRenderer renderer, ReferencedEnvelope mapEnv |
114 |
|
// , |
115 |
|
// AffineTransform worldToScreen |
116 |
|
) { |
117 |
|
this.paintArea = paintArea; |
118 |
|
this.graphics = graphics; |
119 |
|
this.renderer = renderer; |
120 |
|
this.mapEnv = mapEnv; |
121 |
|
// this.worldToScreen = worldToScreen; |
122 |
|
} |
123 |
|
|
124 |
|
@Override |
125 |
|
public void run() { |
126 |
|
long startT = System.currentTimeMillis(); |
127 |
|
try { |
128 |
|
renderer.addRenderListener(this); |
129 |
|
// LOGGER.debug("start rendering..."); |
130 |
|
|
131 |
|
// Clear the graphics context |
132 |
|
graphics.setBackground(mapPane.getMapBackgroundColor()); |
133 |
|
graphics.clearRect(paintArea.x, paintArea.y, paintArea.width, |
134 |
|
paintArea.height); |
135 |
|
|
136 |
|
renderer.paint(graphics, paintArea, mapEnv); |
137 |
|
|
138 |
|
// Kill the reference to this Thread so #isRunning will say |
139 |
|
// false directly |
140 |
|
renderThread = null; |
141 |
|
mapPane.onRenderingCompleted(System.currentTimeMillis()-startT); |
142 |
|
} catch (Exception e) { |
143 |
|
mapPane.onRenderingFailed(e); |
144 |
|
} finally { |
145 |
|
renderer.removeRenderListener(this); |
146 |
|
} |
147 |
|
} |
148 |
|
|
149 |
|
@Override |
150 |
|
public void errorOccurred(Exception e) { |
151 |
|
mapPane.onRenderingFailed(e); |
152 |
|
} |
153 |
|
|
154 |
|
@Override |
155 |
|
public void featureRenderer(SimpleFeature feature) { |
156 |
|
} |
157 |
|
|
158 |
|
} |
159 |
|
|
160 |
|
/** |
161 |
|
* Ask to stop the rendering. May be called often. |
162 |
|
*/ |
163 |
|
public void cancelTask() { |
164 |
|
if (renderThread != null) |
165 |
|
synchronized (renderThread) { |
166 |
|
if (renderThread.isAlive()) { |
167 |
|
if (renderThread.getRenderer() != null) |
168 |
|
renderThread.getRenderer().stopRendering(); |
169 |
|
} |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
|
/** |
174 |
|
* @return <code>true</code> if the {@link Thread} is busy rendering. |
175 |
|
*/ |
176 |
|
public boolean isRunning() { |
177 |
|
return (renderThread != null && renderThread.isAlive()); |
178 |
|
} |
179 |
|
|
180 |
|
/** |
181 |
|
* Will stop rendering and remove the reference to the {@link Thread}. |
182 |
|
*/ |
183 |
|
public void dispose() { |
184 |
|
if (renderThread != null) { |
185 |
|
renderThread.renderer.stopRendering(); |
186 |
|
renderThread = null; |
187 |
|
} |
188 |
|
} |
189 |
|
|
|
private final XMapPane mapPane; |
|
|
private final ExecutorService taskExecutor; |
|
|
private final ScheduledExecutorService watchExecutor; |
|
|
|
|
|
// /** The default interval (milliseconds) for polling the result of a rendering task */ |
|
|
// public static final long DEFAULT_POLLING_INTERVAL = 300L; |
|
|
|
|
|
private long pollingInterval; |
|
|
|
|
|
/* |
|
|
* This latch is used to avoid a race between the cancellation of |
|
|
* a current task and the submittal of a new task |
|
|
*/ |
|
|
private CountDownLatch cancelLatch; |
|
|
|
|
|
/** |
|
|
* Constants to indicate the result of a rendering task |
|
|
*/ |
|
|
public enum TaskResult { |
|
|
PENDING, |
|
|
COMPLETED, |
|
|
CANCELLED, |
|
|
FAILED; |
|
|
} |
|
|
|
|
|
private long numFeatures; |
|
|
|
|
|
/** |
|
|
* A rendering task |
|
|
*/ |
|
|
private class Task implements Callable<TaskResult>, RenderListener { |
|
|
|
|
|
private final ReferencedEnvelope envelope; |
|
|
private final Rectangle paintArea; |
|
|
private final Graphics2D graphics; |
|
|
|
|
|
private boolean cancelled; |
|
|
private boolean failed; |
|
|
final private GTRenderer renderer; |
|
|
|
|
|
/** |
|
|
* Constructor. Creates a new rendering task |
|
|
* |
|
|
* @param envelope map area to render (world coordinates) |
|
|
* @param paintArea drawing area (image or display coordinates)a |
|
|
* @param graphics graphics object used to draw into the image or display |
|
|
*/ |
|
|
public Task(final ReferencedEnvelope envelope, final Rectangle paintArea, final Graphics2D graphics, GTRenderer renderer) { |
|
|
this.envelope = envelope; |
|
|
this.paintArea = paintArea; |
|
|
this.graphics = graphics; |
|
|
this.cancelled = false; |
|
|
this.renderer = renderer; |
|
|
failed = false; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Called by the executor to run this rendering task. |
|
|
* |
|
|
* @return result of the task: completed, cancelled or failed |
|
|
* @throws Exception |
|
|
*/ |
|
|
public TaskResult call() throws Exception { |
|
|
if (!cancelled) { |
|
|
|
|
|
try { |
|
|
renderer.addRenderListener(this); |
|
|
|
|
|
Composite composite = graphics.getComposite(); |
|
|
//graphics.setComposite(AlphaComposite.Src); |
|
|
//graphics.setBackground(Color.WHITE); |
|
|
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f)); |
|
|
graphics.fill(paintArea); |
|
|
graphics.setComposite(composite); |
|
|
|
|
|
|
|
|
numFeatures = 0; |
|
|
renderer.paint(graphics, mapPane.getVisibleRect(), envelope, mapPane.getWorldToScreenTransform()); |
|
|
|
|
|
} finally { |
|
|
renderer.removeRenderListener(this); |
|
|
} |
|
|
} |
|
|
|
|
|
if (cancelled) { |
|
|
return TaskResult.CANCELLED; |
|
|
} else if (failed) { |
|
|
return TaskResult.FAILED; |
|
|
} else { |
|
|
return TaskResult.COMPLETED; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* Cancel the rendering task if it is running. If called before |
|
|
* being run the task will be abandoned. |
|
|
*/ |
|
|
public synchronized void cancel() { |
|
|
if (isRunning()) { |
|
|
cancelled = true; |
|
|
renderer.stopRendering(); |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* Called by the renderer when each feature is drawn. |
|
|
* |
|
|
* @param feature the feature just drawn |
|
|
*/ |
|
|
public void featureRenderer(SimpleFeature feature) { |
|
|
// @todo update a progress listener |
|
|
numFeatures++ ; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Called by the renderer on error |
|
|
* |
|
|
* @param e cause of the error |
|
|
*/ |
|
|
public void errorOccurred(Exception e) { |
|
|
renderingException = e; |
|
|
graphics.setColor(Color.white); |
|
|
graphics.drawString(e.getMessage(), 11, 11); |
|
|
graphics.drawString(e.getMessage(), 9, 9); |
|
|
graphics.setColor(Color.black); |
|
|
graphics.drawString(e.getMessage(), 10, 10); |
|
|
failed = true; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
private AtomicBoolean taskRunning; |
|
|
private Task task; |
|
|
private Future<TaskResult> taskResult; |
|
|
private ScheduledFuture<?> watcher; |
|
|
private Exception renderingException; |
|
|
|
|
|
/** |
|
|
* Constructor. Creates a new executor to service the specified map pane. |
|
|
* |
|
|
* @param mapPane the map pane to be serviced |
|
|
* @param l |
|
|
*/ |
|
|
public RenderingExecutor(final XMapPane mapPane, long pollingInterval) { |
|
|
taskRunning = new AtomicBoolean(false); |
|
|
this.mapPane = mapPane; |
|
|
taskExecutor = Executors.newSingleThreadExecutor(); |
|
|
watchExecutor = Executors.newSingleThreadScheduledExecutor(); |
|
|
this.pollingInterval = pollingInterval; |
|
|
cancelLatch = new CountDownLatch(0); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Get the interval for polling the result of a rendering task |
|
|
* |
|
|
* @return polling interval in milliseconds |
|
|
*/ |
|
|
public long getPollingInterval() { |
|
|
return pollingInterval; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Set the interval for polling the result of a rendering task |
|
|
* |
|
|
* @param interval interval in milliseconds (values {@code <=} 0 are ignored) |
|
|
*/ |
|
|
public void setPollingInterval(long interval) { |
|
|
if (interval > 0) { |
|
|
pollingInterval = interval; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* Submit a new rendering task. If no rendering task is presently running |
|
|
* this new task will be accepted; otherwise it will be rejected (ie. there |
|
|
* is no task queue). |
|
|
* |
|
|
* @param envelope the map area (world coordinates) to be rendered |
|
|
* @param graphics the graphics object to draw on |
|
|
* |
|
|
* @return true if the rendering task was accepted; false if it was |
|
|
* rejected |
|
|
*/ |
|
|
public synchronized boolean submit(ReferencedEnvelope envelope, Rectangle paintArea, Graphics2D graphics, final GTRenderer renderer) { |
|
|
if (!isRunning() || cancelLatch.getCount() > 0) { |
|
|
try { |
|
|
// wait for any cancelled task to finish its shutdown |
|
|
cancelLatch.await(); |
|
|
} catch (InterruptedException ex) { |
|
|
return false; |
|
|
} |
|
|
|
|
|
task = new Task(envelope, paintArea, graphics, renderer); |
|
|
taskRunning.set(true); |
|
|
taskResult = taskExecutor.submit(task); |
|
|
watcher = watchExecutor.scheduleAtFixedRate(new Runnable() { |
|
|
|
|
|
public void run() { |
|
|
pollTaskResult(); |
|
|
} |
|
|
}, pollingInterval, pollingInterval, TimeUnit.MILLISECONDS); |
|
|
|
|
|
return true; |
|
|
} |
|
|
|
|
|
return false; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Cancel the current rendering task if one is running |
|
|
*/ |
|
|
public synchronized void cancelTask() { |
|
|
if (isRunning()) { |
|
|
task.cancel(); |
|
|
cancelLatch = new CountDownLatch(1); |
|
|
} |
|
|
} |
|
|
|
|
|
private void pollTaskResult() { |
|
|
if (!taskResult.isDone()) { |
|
|
return; |
|
|
} |
|
|
|
|
|
TaskResult result = TaskResult.PENDING; |
|
|
|
|
|
try { |
|
|
result = taskResult.get(); |
|
|
} catch (Exception ex) { |
|
|
throw new IllegalStateException("When getting rendering result", ex); |
|
|
} |
|
|
|
|
|
watcher.cancel(false); |
|
|
taskRunning.set(false); |
|
|
|
|
|
switch (result) { |
|
|
case CANCELLED: |
|
|
cancelLatch.countDown(); |
|
|
mapPane.onRenderingCancelled(); |
|
|
break; |
|
|
|
|
|
case COMPLETED: |
|
|
mapPane.onRenderingCompleted(); |
|
|
break; |
|
|
|
|
|
case FAILED: |
|
|
mapPane.onRenderingFailed(renderingException); |
|
|
break; |
|
|
|
|
|
case PENDING: |
|
|
mapPane.onRenderingPending(); |
|
|
break; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
public synchronized boolean isRunning() { |
|
|
return taskRunning.get(); |
|
|
} |
|
|
|
|
|
@Override |
|
|
protected void finalize() throws Throwable { |
|
|
if (this.isRunning()) { |
|
|
taskExecutor.shutdownNow(); |
|
|
watchExecutor.shutdownNow(); |
|
|
} |
|
|
} |
|
190 |
} |
} |
|
|
|