/[schmitzm]/branches/2.4.x/src/skrueger/geotools/RenderingExecutor.java
ViewVC logotype

Annotation of /branches/2.4.x/src/skrueger/geotools/RenderingExecutor.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1383 - (hide annotations)
Wed Jan 26 13:46:20 2011 UTC (14 years, 1 month ago) by alfonx
File size: 5174 byte(s)
trunk becomes 2.4.x ... starting to create multiple modules

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 alfonx 555 import org.apache.log4j.Logger;
8 alfonx 1145 import org.geotools.filter.function.EnvFunction;
9 alfonx 510 import org.geotools.geometry.jts.ReferencedEnvelope;
10     import org.geotools.renderer.GTRenderer;
11     import org.geotools.renderer.RenderListener;
12     import org.opengis.feature.simple.SimpleFeature;
13    
14 alfonx 740 import schmitzm.geotools.gui.XMapPane;
15    
16 alfonx 532 /**
17     * This class is used by {@link XMapPane} to start and stop the rendering a
18     * {@link Thread} for rendering.
19     */
20 alfonx 740 public class RenderingExecutor {
21 alfonx 1145 private final static Logger LOGGER = Logger
22     .getLogger(RenderingExecutor.class);
23 alfonx 532 /**
24     * Instance to a {@link RenderThread} doing any work. It's volatile so the
25     * correct value will always be visible to any {@link Thread}
26     **/
27 alfonx 530 private volatile RenderThread renderThread;
28 alfonx 532
29 alfonx 530 private final XMapPane mapPane;
30    
31 alfonx 529 public RenderingExecutor(XMapPane mapPane) {
32     this.mapPane = mapPane;
33 alfonx 530 }
34 alfonx 510
35 alfonx 530 /**
36     * Submit a new rendering task. If no rendering task is presently running
37 alfonx 532 * this new job will be accepted; otherwise it will be rejected and it
38     * returns <code>false</code>.
39 alfonx 530 *
40     * @param envelope
41 alfonx 532 * the map area (world coordinates) to be rendered.
42 alfonx 530 * @param graphics
43 alfonx 532 * the graphics object to draw on.
44     * @param paintArea
45     * size of the area to paint the world into.
46     * @param worldToScreen
47     * the {@link AffineTransform} from world coordinates to screen
48     * coordinates.
49     * @param renderer
50     * the {@link GTRenderer} to use.
51 alfonx 530 *
52     * @return true if the rendering task was accepted; false if it was rejected
53     */
54     public synchronized boolean submit(ReferencedEnvelope envelope,
55 alfonx 1145 Rectangle paintArea, Graphics2D graphics, final GTRenderer renderer) {
56 alfonx 530 if (renderThread == null || !renderThread.isAlive()) {
57 alfonx 532 // System.out.println("is vacant... starting thread!");
58     renderThread = null;
59 alfonx 510
60 alfonx 530 renderThread = new RenderThread(paintArea, graphics, renderer,
61 alfonx 544 envelope);
62 alfonx 530 renderThread.start();
63 alfonx 510
64 alfonx 530 return true;
65     } else {
66     renderThread.getRenderer().stopRendering();
67 alfonx 532 return false;
68 alfonx 530 }
69     }
70    
71 alfonx 532 /**
72     * For every new rendering job submitted and accepted, an instance of this
73     * {@link Thread} will be started.
74     *
75     */
76 alfonx 530 class RenderThread extends Thread {
77    
78     private final GTRenderer renderer;
79    
80     public RenderThread(final Rectangle paintArea,
81     final Graphics2D graphics, GTRenderer renderer,
82 alfonx 544 ReferencedEnvelope mapEnv) {
83 alfonx 1145 super(new RenderRun(paintArea, graphics, renderer, mapEnv));
84 alfonx 529 this.renderer = renderer;
85 alfonx 530
86 alfonx 532 setName("Render" + getName());
87    
88 alfonx 529 }
89 alfonx 530
90     public GTRenderer getRenderer() {
91 alfonx 529 return renderer;
92     }
93    
94 alfonx 530 }
95    
96 alfonx 532 /**
97     * This {@link Runnable} will actually start the rendering
98     */
99 alfonx 530 class RenderRun implements Runnable, RenderListener {
100     private final Rectangle paintArea;
101 alfonx 529 private final Graphics2D graphics;
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 1145 GTRenderer renderer, ReferencedEnvelope mapEnv) {
107 alfonx 530 this.paintArea = paintArea;
108     this.graphics = graphics;
109     this.renderer = renderer;
110 alfonx 533 this.mapEnv = mapEnv;
111 alfonx 529 }
112 alfonx 530
113 alfonx 529 @Override
114     public void run() {
115 alfonx 607 long startT = System.currentTimeMillis();
116 alfonx 529 try {
117 alfonx 530 renderer.addRenderListener(this);
118 alfonx 1145 // LOGGER.debug("start rendering...");
119 alfonx 532
120     // Clear the graphics context
121     graphics.setBackground(mapPane.getMapBackgroundColor());
122     graphics.clearRect(paintArea.x, paintArea.y, paintArea.width,
123     paintArea.height);
124    
125 alfonx 1353 // TODO It should be enough to set only the Local Value, but it
126     // isn't
127 alfonx 1145 EnvFunction.setGlobalValue(XMapPane.ENV_LANG,
128     mapPane.getRenderLanguage());
129     EnvFunction.setLocalValue(XMapPane.ENV_LANG,
130     mapPane.getRenderLanguage());
131    
132 alfonx 1353 // LOGGER.debug("Renderer language set to "
133     // + mapPane.getRenderLanguage());
134 alfonx 1145
135 alfonx 544 renderer.paint(graphics, paintArea, mapEnv);
136 alfonx 530
137 alfonx 539 // Kill the reference to this Thread so #isRunning will say
138     // false directly
139 alfonx 533 renderThread = null;
140 alfonx 1145 mapPane.onRenderingCompleted(System.currentTimeMillis()
141     - startT);
142 alfonx 539 } catch (Exception e) {
143 alfonx 1353 if (e != null && !e.toString().equals("0"))
144     mapPane.onRenderingFailed(e);
145 alfonx 530 } finally {
146     renderer.removeRenderListener(this);
147     }
148 alfonx 529 }
149 alfonx 530
150 alfonx 529 @Override
151     public void errorOccurred(Exception e) {
152     mapPane.onRenderingFailed(e);
153     }
154 alfonx 510
155 alfonx 529 @Override
156     public void featureRenderer(SimpleFeature feature) {
157     }
158 alfonx 510
159 alfonx 530 }
160    
161 alfonx 532 /**
162     * Ask to stop the rendering. May be called often.
163     */
164 alfonx 529 public void cancelTask() {
165 alfonx 555 if (renderThread != null)
166     synchronized (renderThread) {
167     if (renderThread.isAlive()) {
168     if (renderThread.getRenderer() != null)
169     renderThread.getRenderer().stopRendering();
170     }
171     }
172 alfonx 529 }
173 alfonx 510
174 alfonx 532 /**
175     * @return <code>true</code> if the {@link Thread} is busy rendering.
176     */
177 alfonx 529 public boolean isRunning() {
178 alfonx 530 return (renderThread != null && renderThread.isAlive());
179 alfonx 529 }
180 alfonx 510
181 alfonx 532 /**
182     * Will stop rendering and remove the reference to the {@link Thread}.
183     */
184 alfonx 529 public void dispose() {
185 alfonx 530 if (renderThread != null) {
186     renderThread.renderer.stopRendering();
187     renderThread = null;
188 alfonx 529 }
189     }
190 alfonx 510
191     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26