/[schmitzm]/branches/2.0-RC2/src/skrueger/geotools/RenderingExecutor.java
ViewVC logotype

Annotation of /branches/2.0-RC2/src/skrueger/geotools/RenderingExecutor.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 543 - (hide annotations)
Sat Nov 21 15:21:41 2009 UTC (15 years, 3 months ago) by alfonx
Original Path: branches/1.0-gt2-2.6/src/skrueger/geotools/RenderingExecutor.java
File size: 4934 byte(s)
* Even better selecting.. BBOX filter was the wrong choice.. INTERSECTS is correct.
1 alfonx 510 package skrueger.geotools;
2    
3     import java.awt.Graphics2D;
4     import java.awt.Rectangle;
5 alfonx 529 import java.awt.geom.AffineTransform;
6 alfonx 510
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 alfonx 532 /**
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 alfonx 530 private volatile RenderThread renderThread;
23 alfonx 532
24 alfonx 530 private final XMapPane mapPane;
25    
26 alfonx 529 public RenderingExecutor(XMapPane mapPane) {
27     this.mapPane = mapPane;
28 alfonx 530 }
29 alfonx 510
30 alfonx 530 /**
31     * Submit a new rendering task. If no rendering task is presently running
32 alfonx 532 * this new job will be accepted; otherwise it will be rejected and it
33     * returns <code>false</code>.
34 alfonx 530 *
35     * @param envelope
36 alfonx 532 * the map area (world coordinates) to be rendered.
37 alfonx 530 * @param graphics
38 alfonx 532 * 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 alfonx 530 *
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 alfonx 532 // System.out.println("is vacant... starting thread!");
54     renderThread = null;
55 alfonx 510
56 alfonx 530 renderThread = new RenderThread(paintArea, graphics, renderer,
57 alfonx 533 worldToScreen, envelope);
58 alfonx 530 renderThread.start();
59 alfonx 510
60 alfonx 530 return true;
61     } else {
62 alfonx 532 // System.out.println("is busy... requesting stop!");
63 alfonx 530 renderThread.getRenderer().stopRendering();
64 alfonx 532 return false;
65 alfonx 530 }
66     }
67    
68 alfonx 532 /**
69     * For every new rendering job submitted and accepted, an instance of this
70     * {@link Thread} will be started.
71     *
72     */
73 alfonx 530 class RenderThread extends Thread {
74    
75     private final GTRenderer renderer;
76    
77     public RenderThread(final Rectangle paintArea,
78     final Graphics2D graphics, GTRenderer renderer,
79 alfonx 533 AffineTransform worldToScreen, ReferencedEnvelope mapEnv) {
80 alfonx 539 super(new RenderRun(paintArea, graphics, renderer, mapEnv,
81     worldToScreen));
82 alfonx 529 this.renderer = renderer;
83 alfonx 530
84 alfonx 532 setName("Render" + getName());
85    
86     // System.out.println("starting render thread " + getName());
87 alfonx 529 }
88 alfonx 530
89     public GTRenderer getRenderer() {
90 alfonx 529 return renderer;
91     }
92    
93 alfonx 530 }
94    
95 alfonx 532 /**
96     * This {@link Runnable} will actually start the rendering
97     */
98 alfonx 530 class RenderRun implements Runnable, RenderListener {
99     private final Rectangle paintArea;
100 alfonx 529 private final Graphics2D graphics;
101     private final AffineTransform worldToScreen;
102     private final GTRenderer renderer;
103 alfonx 533 private final ReferencedEnvelope mapEnv;
104 alfonx 510
105 alfonx 529 public RenderRun(Rectangle paintArea, Graphics2D graphics,
106 alfonx 539 GTRenderer renderer, ReferencedEnvelope mapEnv,
107     AffineTransform worldToScreen) {
108 alfonx 530 this.paintArea = paintArea;
109     this.graphics = graphics;
110     this.renderer = renderer;
111 alfonx 533 this.mapEnv = mapEnv;
112 alfonx 530 this.worldToScreen = worldToScreen;
113 alfonx 529 }
114 alfonx 530
115 alfonx 529 @Override
116     public void run() {
117     try {
118 alfonx 530 renderer.addRenderListener(this);
119     System.out.println("start rendering...");
120 alfonx 532 // 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 alfonx 539 renderer.paint(graphics, paintArea, worldToScreen);
132 alfonx 530
133 alfonx 539 // Kill the reference to this Thread so #isRunning will say
134     // false directly
135 alfonx 533 renderThread = null;
136     mapPane.onRenderingCompleted();
137 alfonx 539 } catch (Exception e) {
138     mapPane.onRenderingFailed(e);
139 alfonx 530 } finally {
140     renderer.removeRenderListener(this);
141     }
142 alfonx 529 }
143 alfonx 530
144 alfonx 529 @Override
145     public void errorOccurred(Exception e) {
146     mapPane.onRenderingFailed(e);
147     }
148 alfonx 510
149 alfonx 529 @Override
150     public void featureRenderer(SimpleFeature feature) {
151     }
152 alfonx 510
153 alfonx 530 }
154    
155 alfonx 532 /**
156     * Ask to stop the rendering. May be called often.
157     */
158 alfonx 529 public void cancelTask() {
159 alfonx 530 if (renderThread != null && renderThread.isAlive()) {
160     // System.out.println("request stop for thread " +task.getName());
161     renderThread.getRenderer().stopRendering();
162 alfonx 529 }
163     }
164 alfonx 510
165 alfonx 532 /**
166     * @return <code>true</code> if the {@link Thread} is busy rendering.
167     */
168 alfonx 529 public boolean isRunning() {
169 alfonx 530 return (renderThread != null && renderThread.isAlive());
170 alfonx 529 }
171 alfonx 510
172 alfonx 532 /**
173     * Will stop rendering and remove the reference to the {@link Thread}.
174     */
175 alfonx 529 public void dispose() {
176 alfonx 530 if (renderThread != null) {
177     renderThread.renderer.stopRendering();
178     renderThread = null;
179 alfonx 529 }
180     }
181 alfonx 510
182     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26