/[schmitzm]/branches/1.0-gt2-2.6/src/skrueger/geotools/RenderingExecutor.java
ViewVC logotype

Contents of /branches/1.0-gt2-2.6/src/skrueger/geotools/RenderingExecutor.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 543 - (show annotations)
Sat Nov 21 15:21:41 2009 UTC (15 years, 3 months ago) by alfonx
File size: 4934 byte(s)
* Even better selecting.. BBOX filter was the wrong choice.. INTERSECTS is correct.
1 package skrueger.geotools;
2
3 import java.awt.Graphics2D;
4 import java.awt.Rectangle;
5 import java.awt.geom.AffineTransform;
6
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 /**
13 * This class is used by {@link XMapPane} to start and stop the rendering a
14 * {@link Thread} for rendering.
15 */
16 class RenderingExecutor {
17
18 /**
19 * Instance to a {@link RenderThread} doing any work. It's volatile so the
20 * correct value will always be visible to any {@link Thread}
21 **/
22 private volatile RenderThread renderThread;
23
24 private final XMapPane mapPane;
25
26 public RenderingExecutor(XMapPane mapPane) {
27 this.mapPane = mapPane;
28 }
29
30 /**
31 * 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 *
35 * @param envelope
36 * the map area (world coordinates) to be rendered.
37 * @param graphics
38 * the graphics object to draw on.
39 * @param paintArea
40 * size of the area to paint the world into.
41 * @param worldToScreen
42 * the {@link AffineTransform} from world coordinates to screen
43 * coordinates.
44 * @param renderer
45 * the {@link GTRenderer} to use.
46 *
47 * @return true if the rendering task was accepted; false if it was rejected
48 */
49 public synchronized boolean submit(ReferencedEnvelope envelope,
50 Rectangle paintArea, Graphics2D graphics,
51 final GTRenderer renderer, AffineTransform worldToScreen) {
52 if (renderThread == null || !renderThread.isAlive()) {
53 // System.out.println("is vacant... starting thread!");
54 renderThread = null;
55
56 renderThread = new RenderThread(paintArea, graphics, renderer,
57 worldToScreen, envelope);
58 renderThread.start();
59
60 return true;
61 } else {
62 // System.out.println("is busy... requesting stop!");
63 renderThread.getRenderer().stopRendering();
64 return false;
65 }
66 }
67
68 /**
69 * For every new rendering job submitted and accepted, an instance of this
70 * {@link Thread} will be started.
71 *
72 */
73 class RenderThread extends Thread {
74
75 private final GTRenderer renderer;
76
77 public RenderThread(final Rectangle paintArea,
78 final Graphics2D graphics, GTRenderer renderer,
79 AffineTransform worldToScreen, ReferencedEnvelope mapEnv) {
80 super(new RenderRun(paintArea, graphics, renderer, mapEnv,
81 worldToScreen));
82 this.renderer = renderer;
83
84 setName("Render" + getName());
85
86 // System.out.println("starting render thread " + getName());
87 }
88
89 public GTRenderer getRenderer() {
90 return renderer;
91 }
92
93 }
94
95 /**
96 * This {@link Runnable} will actually start the rendering
97 */
98 class RenderRun implements Runnable, RenderListener {
99 private final Rectangle paintArea;
100 private final Graphics2D graphics;
101 private final AffineTransform worldToScreen;
102 private final GTRenderer renderer;
103 private final ReferencedEnvelope mapEnv;
104
105 public RenderRun(Rectangle paintArea, Graphics2D graphics,
106 GTRenderer renderer, ReferencedEnvelope mapEnv,
107 AffineTransform worldToScreen) {
108 this.paintArea = paintArea;
109 this.graphics = graphics;
110 this.renderer = renderer;
111 this.mapEnv = mapEnv;
112 this.worldToScreen = worldToScreen;
113 }
114
115 @Override
116 public void run() {
117 try {
118 renderer.addRenderListener(this);
119 System.out.println("start rendering...");
120 // try {
121 // Thread.sleep(1000);
122 // } catch (InterruptedException e) {
123 // e.printStackTrace();
124 // }
125
126 // Clear the graphics context
127 graphics.setBackground(mapPane.getMapBackgroundColor());
128 graphics.clearRect(paintArea.x, paintArea.y, paintArea.width,
129 paintArea.height);
130
131 renderer.paint(graphics, paintArea, worldToScreen);
132
133 // Kill the reference to this Thread so #isRunning will say
134 // false directly
135 renderThread = null;
136 mapPane.onRenderingCompleted();
137 } catch (Exception e) {
138 mapPane.onRenderingFailed(e);
139 } finally {
140 renderer.removeRenderListener(this);
141 }
142 }
143
144 @Override
145 public void errorOccurred(Exception e) {
146 mapPane.onRenderingFailed(e);
147 }
148
149 @Override
150 public void featureRenderer(SimpleFeature feature) {
151 }
152
153 }
154
155 /**
156 * Ask to stop the rendering. May be called often.
157 */
158 public void cancelTask() {
159 if (renderThread != null && renderThread.isAlive()) {
160 // System.out.println("request stop for thread " +task.getName());
161 renderThread.getRenderer().stopRendering();
162 }
163 }
164
165 /**
166 * @return <code>true</code> if the {@link Thread} is busy rendering.
167 */
168 public boolean isRunning() {
169 return (renderThread != null && renderThread.isAlive());
170 }
171
172 /**
173 * Will stop rendering and remove the reference to the {@link Thread}.
174 */
175 public void dispose() {
176 if (renderThread != null) {
177 renderThread.renderer.stopRendering();
178 renderThread = null;
179 }
180 }
181
182 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26