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.geotools.geometry.jts.ReferencedEnvelope; |
import org.geotools.geometry.jts.ReferencedEnvelope; |
8 |
import org.geotools.renderer.GTRenderer; |
import org.geotools.renderer.GTRenderer; |
9 |
import org.geotools.renderer.RenderListener; |
import org.geotools.renderer.RenderListener; |
|
import org.geotools.swing.JMapPane; |
|
10 |
import org.opengis.feature.simple.SimpleFeature; |
import org.opengis.feature.simple.SimpleFeature; |
11 |
|
|
12 |
/** |
/** |
13 |
* 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 |
14 |
* 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 |
|
15 |
*/ |
*/ |
16 |
public class RenderingExecutor { |
class RenderingExecutor { |
17 |
|
|
18 |
private final XMapPane mapPane; |
/** |
19 |
private final ExecutorService taskExecutor; |
* Instance to a {@link RenderThread} doing any work. It's volatile so the |
20 |
private final ScheduledExecutorService watchExecutor; |
* correct value will always be visible to any {@link Thread} |
21 |
|
**/ |
22 |
// /** The default interval (milliseconds) for polling the result of a rendering task */ |
private volatile RenderThread renderThread; |
23 |
// public static final long DEFAULT_POLLING_INTERVAL = 300L; |
|
24 |
|
private final XMapPane mapPane; |
25 |
private long pollingInterval; |
|
26 |
|
public RenderingExecutor(XMapPane mapPane) { |
27 |
/* |
this.mapPane = mapPane; |
28 |
* This latch is used to avoid a race between the cancellation of |
} |
29 |
* a current task and the submittal of a new task |
|
30 |
*/ |
/** |
31 |
private CountDownLatch cancelLatch; |
* Submit a new rendering task. If no rendering task is presently running |
32 |
|
* this new job will be accepted; otherwise it will be rejected and it |
33 |
/** |
* returns <code>false</code>. |
34 |
* Constants to indicate the result of a rendering task |
* |
35 |
*/ |
* @param envelope |
36 |
public enum TaskResult { |
* the map area (world coordinates) to be rendered. |
37 |
PENDING, |
* @param graphics |
38 |
COMPLETED, |
* the graphics object to draw on. |
39 |
CANCELLED, |
* @param paintArea |
40 |
FAILED; |
* size of the area to paint the world into. |
41 |
} |
* @param worldToScreen |
42 |
|
* the {@link AffineTransform} from world coordinates to screen |
43 |
private long numFeatures; |
* coordinates. |
44 |
|
* @param renderer |
45 |
/** |
* the {@link GTRenderer} to use. |
46 |
* A rendering task |
* |
47 |
*/ |
* @return true if the rendering task was accepted; false if it was rejected |
48 |
private class Task implements Callable<TaskResult>, RenderListener { |
*/ |
49 |
|
public synchronized boolean submit(ReferencedEnvelope envelope, |
50 |
private final ReferencedEnvelope envelope; |
Rectangle paintArea, Graphics2D graphics, |
51 |
private final Rectangle paintArea; |
final GTRenderer renderer, AffineTransform worldToScreen) { |
52 |
private final Graphics2D graphics; |
if (renderThread == null || !renderThread.isAlive()) { |
53 |
|
// System.out.println("is vacant... starting thread!"); |
54 |
private boolean cancelled; |
renderThread = null; |
55 |
private boolean failed; |
|
56 |
final private GTRenderer renderer; |
renderThread = new RenderThread(paintArea, graphics, renderer, |
57 |
|
worldToScreen, envelope); |
58 |
/** |
renderThread.start(); |
59 |
* Constructor. Creates a new rendering task |
|
60 |
* |
return true; |
61 |
* @param envelope map area to render (world coordinates) |
} else { |
62 |
* @param paintArea drawing area (image or display coordinates)a |
// System.out.println("is busy... requesting stop!"); |
63 |
* @param graphics graphics object used to draw into the image or display |
renderThread.getRenderer().stopRendering(); |
64 |
*/ |
return false; |
65 |
public Task(final ReferencedEnvelope envelope, final Rectangle paintArea, final Graphics2D graphics, GTRenderer renderer) { |
} |
66 |
this.envelope = envelope; |
} |
67 |
this.paintArea = paintArea; |
|
68 |
this.graphics = graphics; |
/** |
69 |
this.cancelled = false; |
* For every new rendering job submitted and accepted, an instance of this |
70 |
this.renderer = renderer; |
* {@link Thread} will be started. |
71 |
failed = false; |
* |
72 |
} |
*/ |
73 |
|
class RenderThread extends Thread { |
74 |
/** |
|
75 |
* Called by the executor to run this rendering task. |
private final GTRenderer renderer; |
76 |
* |
|
77 |
* @return result of the task: completed, cancelled or failed |
public RenderThread(final Rectangle paintArea, |
78 |
* @throws Exception |
final Graphics2D graphics, GTRenderer renderer, |
79 |
*/ |
AffineTransform worldToScreen, ReferencedEnvelope mapEnv) { |
80 |
public TaskResult call() throws Exception { |
super(new RenderRun(paintArea, graphics, renderer, mapEnv, worldToScreen)); |
81 |
if (!cancelled) { |
this.renderer = renderer; |
82 |
|
|
83 |
try { |
setName("Render" + getName()); |
84 |
renderer.addRenderListener(this); |
|
85 |
|
// System.out.println("starting render thread " + getName()); |
86 |
Composite composite = graphics.getComposite(); |
} |
87 |
//graphics.setComposite(AlphaComposite.Src); |
|
88 |
//graphics.setBackground(Color.WHITE); |
public GTRenderer getRenderer() { |
89 |
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f)); |
return renderer; |
90 |
graphics.fill(paintArea); |
} |
91 |
graphics.setComposite(composite); |
|
92 |
|
} |
93 |
|
|
94 |
numFeatures = 0; |
/** |
95 |
renderer.paint(graphics, mapPane.getVisibleRect(), envelope, mapPane.getWorldToScreenTransform()); |
* This {@link Runnable} will actually start the rendering |
96 |
|
*/ |
97 |
} finally { |
class RenderRun implements Runnable, RenderListener { |
98 |
renderer.removeRenderListener(this); |
private final Rectangle paintArea; |
99 |
} |
private final Graphics2D graphics; |
100 |
} |
private final AffineTransform worldToScreen; |
101 |
|
private final GTRenderer renderer; |
102 |
if (cancelled) { |
private final ReferencedEnvelope mapEnv; |
103 |
return TaskResult.CANCELLED; |
|
104 |
} else if (failed) { |
public RenderRun(Rectangle paintArea, Graphics2D graphics, |
105 |
return TaskResult.FAILED; |
GTRenderer renderer, ReferencedEnvelope mapEnv, AffineTransform worldToScreen) { |
106 |
} else { |
this.paintArea = paintArea; |
107 |
return TaskResult.COMPLETED; |
this.graphics = graphics; |
108 |
} |
this.renderer = renderer; |
109 |
} |
this.mapEnv = mapEnv; |
110 |
|
this.worldToScreen = worldToScreen; |
111 |
/** |
} |
112 |
* Cancel the rendering task if it is running. If called before |
|
113 |
* being run the task will be abandoned. |
@Override |
114 |
*/ |
public void run() { |
115 |
public synchronized void cancel() { |
try { |
116 |
if (isRunning()) { |
renderer.addRenderListener(this); |
117 |
cancelled = true; |
System.out.println("start rendering..."); |
118 |
renderer.stopRendering(); |
// try { |
119 |
} |
// Thread.sleep(1000); |
120 |
} |
// } catch (InterruptedException e) { |
121 |
|
// e.printStackTrace(); |
122 |
/** |
// } |
123 |
* Called by the renderer when each feature is drawn. |
|
124 |
* |
// Clear the graphics context |
125 |
* @param feature the feature just drawn |
graphics.setBackground(mapPane.getMapBackgroundColor()); |
126 |
*/ |
graphics.clearRect(paintArea.x, paintArea.y, paintArea.width, |
127 |
public void featureRenderer(SimpleFeature feature) { |
paintArea.height); |
128 |
// @todo update a progress listener |
|
129 |
numFeatures++ ; |
renderer.paint(graphics, paintArea, mapEnv, worldToScreen); |
130 |
} |
|
131 |
|
// Kill the reference to this Thread to #isRunning will def. say false |
132 |
/** |
renderThread = null; |
133 |
* Called by the renderer on error |
mapPane.onRenderingCompleted(); |
134 |
* |
|
135 |
* @param e cause of the error |
} finally { |
136 |
*/ |
renderer.removeRenderListener(this); |
137 |
public void errorOccurred(Exception e) { |
} |
138 |
renderingException = e; |
} |
139 |
graphics.setColor(Color.white); |
|
140 |
graphics.drawString(e.getMessage(), 11, 11); |
@Override |
141 |
graphics.drawString(e.getMessage(), 9, 9); |
public void errorOccurred(Exception e) { |
142 |
graphics.setColor(Color.black); |
mapPane.onRenderingFailed(e); |
143 |
graphics.drawString(e.getMessage(), 10, 10); |
} |
144 |
failed = true; |
|
145 |
} |
@Override |
146 |
|
public void featureRenderer(SimpleFeature feature) { |
147 |
} |
} |
148 |
|
|
149 |
private AtomicBoolean taskRunning; |
} |
150 |
private Task task; |
|
151 |
private Future<TaskResult> taskResult; |
/** |
152 |
private ScheduledFuture<?> watcher; |
* Ask to stop the rendering. May be called often. |
153 |
private Exception renderingException; |
*/ |
154 |
|
public void cancelTask() { |
155 |
/** |
if (renderThread != null && renderThread.isAlive()) { |
156 |
* Constructor. Creates a new executor to service the specified map pane. |
// System.out.println("request stop for thread " +task.getName()); |
157 |
* |
renderThread.getRenderer().stopRendering(); |
158 |
* @param mapPane the map pane to be serviced |
} |
159 |
* @param l |
} |
160 |
*/ |
|
161 |
public RenderingExecutor(final XMapPane mapPane, long pollingInterval) { |
/** |
162 |
taskRunning = new AtomicBoolean(false); |
* @return <code>true</code> if the {@link Thread} is busy rendering. |
163 |
this.mapPane = mapPane; |
*/ |
164 |
taskExecutor = Executors.newSingleThreadExecutor(); |
public boolean isRunning() { |
165 |
watchExecutor = Executors.newSingleThreadScheduledExecutor(); |
// if (task != null) |
166 |
this.pollingInterval = pollingInterval; |
// System.out.println("is running "+task.getName()+" = true"); |
167 |
cancelLatch = new CountDownLatch(0); |
return (renderThread != null && renderThread.isAlive()); |
168 |
} |
} |
169 |
|
|
170 |
/** |
/** |
171 |
* Get the interval for polling the result of a rendering task |
* Will stop rendering and remove the reference to the {@link Thread}. |
172 |
* |
*/ |
173 |
* @return polling interval in milliseconds |
public void dispose() { |
174 |
*/ |
if (renderThread != null) { |
175 |
public long getPollingInterval() { |
renderThread.renderer.stopRendering(); |
176 |
return pollingInterval; |
renderThread = null; |
177 |
} |
} |
178 |
|
} |
|
/** |
|
|
* 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()) { |
|
|
// 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(); |
|
|
|
|
|
// Cancelling to often... can that be the reason? |
|
|
if (cancelLatch.getCount() < 1) { |
|
|
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(); |
|
|
} |
|
|
} |
|
|
|
|
|
public void dispose() { |
|
|
taskExecutor.shutdownNow(); |
|
|
watchExecutor.shutdownNow(); |
|
|
} |
|
|
} |
|
179 |
|
|
180 |
|
} |