/[schmitzm]/trunk/src/skrueger/geotools/RenderingExecutor.java
ViewVC logotype

Annotation of /trunk/src/skrueger/geotools/RenderingExecutor.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1145 - (hide annotations)
Sun Oct 17 23:56:07 2010 UTC (14 years, 4 months ago) by alfonx
File size: 5119 byte(s)
AtlasStyler now allows to configure textclasses per language... still a bit experimental
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 1145 // TODO It should be enough to set only the Local Value, but it isn't
126     EnvFunction.setGlobalValue(XMapPane.ENV_LANG,
127     mapPane.getRenderLanguage());
128     EnvFunction.setLocalValue(XMapPane.ENV_LANG,
129     mapPane.getRenderLanguage());
130    
131     // LOGGER.debug("Renderer language set to "
132     // + mapPane.getRenderLanguage());
133    
134 alfonx 544 renderer.paint(graphics, paintArea, mapEnv);
135 alfonx 530
136 alfonx 539 // Kill the reference to this Thread so #isRunning will say
137     // false directly
138 alfonx 533 renderThread = null;
139 alfonx 1145 mapPane.onRenderingCompleted(System.currentTimeMillis()
140     - startT);
141 alfonx 539 } catch (Exception e) {
142     mapPane.onRenderingFailed(e);
143 alfonx 530 } finally {
144     renderer.removeRenderListener(this);
145     }
146 alfonx 529 }
147 alfonx 530
148 alfonx 529 @Override
149     public void errorOccurred(Exception e) {
150     mapPane.onRenderingFailed(e);
151     }
152 alfonx 510
153 alfonx 529 @Override
154     public void featureRenderer(SimpleFeature feature) {
155     }
156 alfonx 510
157 alfonx 530 }
158    
159 alfonx 532 /**
160     * Ask to stop the rendering. May be called often.
161     */
162 alfonx 529 public void cancelTask() {
163 alfonx 555 if (renderThread != null)
164     synchronized (renderThread) {
165     if (renderThread.isAlive()) {
166     if (renderThread.getRenderer() != null)
167     renderThread.getRenderer().stopRendering();
168     }
169     }
170 alfonx 529 }
171 alfonx 510
172 alfonx 532 /**
173     * @return <code>true</code> if the {@link Thread} is busy rendering.
174     */
175 alfonx 529 public boolean isRunning() {
176 alfonx 530 return (renderThread != null && renderThread.isAlive());
177 alfonx 529 }
178 alfonx 510
179 alfonx 532 /**
180     * Will stop rendering and remove the reference to the {@link Thread}.
181     */
182 alfonx 529 public void dispose() {
183 alfonx 530 if (renderThread != null) {
184     renderThread.renderer.stopRendering();
185     renderThread = null;
186 alfonx 529 }
187     }
188 alfonx 510
189     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26