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