/[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 604 - (hide annotations)
Wed Dec 9 14:15:53 2009 UTC (15 years, 2 months ago) by alfonx
Original Path: branches/2.0-RC1/src/skrueger/geotools/RenderingExecutor.java
File size: 4979 byte(s)
2.0-RC1 branch ist für GP1.3 bugfixes...  1.0-gt2-2.6  ist der entwicklungs brnach 
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 510 import org.geotools.geometry.jts.ReferencedEnvelope;
9     import org.geotools.renderer.GTRenderer;
10     import org.geotools.renderer.RenderListener;
11     import org.opengis.feature.simple.SimpleFeature;
12    
13 alfonx 532 /**
14     * This class is used by {@link XMapPane} to start and stop the rendering a
15     * {@link Thread} for rendering.
16     */
17     class RenderingExecutor {
18 alfonx 555 private final static Logger LOGGER = Logger.getLogger(RenderingExecutor.class);
19 alfonx 532 /**
20     * Instance to a {@link RenderThread} doing any work. It's volatile so the
21     * correct value will always be visible to any {@link Thread}
22     **/
23 alfonx 530 private volatile RenderThread renderThread;
24 alfonx 532
25 alfonx 530 private final XMapPane mapPane;
26    
27 alfonx 529 public RenderingExecutor(XMapPane mapPane) {
28     this.mapPane = mapPane;
29 alfonx 530 }
30 alfonx 510
31 alfonx 530 /**
32     * Submit a new rendering task. If no rendering task is presently running
33 alfonx 532 * this new job will be accepted; otherwise it will be rejected and it
34     * returns <code>false</code>.
35 alfonx 530 *
36     * @param envelope
37 alfonx 532 * the map area (world coordinates) to be rendered.
38 alfonx 530 * @param graphics
39 alfonx 532 * the graphics object to draw on.
40     * @param paintArea
41     * size of the area to paint the world into.
42     * @param worldToScreen
43     * the {@link AffineTransform} from world coordinates to screen
44     * coordinates.
45     * @param renderer
46     * the {@link GTRenderer} to use.
47 alfonx 530 *
48     * @return true if the rendering task was accepted; false if it was rejected
49     */
50     public synchronized boolean submit(ReferencedEnvelope envelope,
51 alfonx 555 Rectangle paintArea, Graphics2D graphics, final GTRenderer renderer
52     // , AffineTransform worldToScreen
53     ) {
54 alfonx 530 if (renderThread == null || !renderThread.isAlive()) {
55 alfonx 532 // System.out.println("is vacant... starting thread!");
56     renderThread = null;
57 alfonx 510
58 alfonx 530 renderThread = new RenderThread(paintArea, graphics, renderer,
59 alfonx 555 // worldToScreen,
60 alfonx 544 envelope);
61 alfonx 530 renderThread.start();
62 alfonx 510
63 alfonx 530 return true;
64     } else {
65 alfonx 532 // System.out.println("is busy... requesting stop!");
66 alfonx 530 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 555 // AffineTransform worldToScreen,
83 alfonx 544 ReferencedEnvelope mapEnv) {
84 alfonx 555 super(new RenderRun(paintArea, graphics, renderer, mapEnv
85     // , worldToScreen
86 alfonx 544 ));
87 alfonx 529 this.renderer = renderer;
88 alfonx 530
89 alfonx 532 setName("Render" + getName());
90    
91     // System.out.println("starting render thread " + getName());
92 alfonx 529 }
93 alfonx 530
94     public GTRenderer getRenderer() {
95 alfonx 529 return renderer;
96     }
97    
98 alfonx 530 }
99    
100 alfonx 532 /**
101     * This {@link Runnable} will actually start the rendering
102     */
103 alfonx 530 class RenderRun implements Runnable, RenderListener {
104     private final Rectangle paintArea;
105 alfonx 529 private final Graphics2D graphics;
106 alfonx 555 // private final AffineTransform worldToScreen;
107 alfonx 529 private final GTRenderer renderer;
108 alfonx 533 private final ReferencedEnvelope mapEnv;
109 alfonx 510
110 alfonx 529 public RenderRun(Rectangle paintArea, Graphics2D graphics,
111 alfonx 544 GTRenderer renderer, ReferencedEnvelope mapEnv
112 alfonx 555 // ,
113     // AffineTransform worldToScreen
114     ) {
115 alfonx 530 this.paintArea = paintArea;
116     this.graphics = graphics;
117     this.renderer = renderer;
118 alfonx 533 this.mapEnv = mapEnv;
119 alfonx 555 // this.worldToScreen = worldToScreen;
120 alfonx 529 }
121 alfonx 530
122 alfonx 529 @Override
123     public void run() {
124     try {
125 alfonx 530 renderer.addRenderListener(this);
126 alfonx 555 LOGGER.debug("start rendering...");
127 alfonx 532
128     // Clear the graphics context
129     graphics.setBackground(mapPane.getMapBackgroundColor());
130     graphics.clearRect(paintArea.x, paintArea.y, paintArea.width,
131     paintArea.height);
132    
133 alfonx 544 renderer.paint(graphics, paintArea, mapEnv);
134 alfonx 530
135 alfonx 539 // Kill the reference to this Thread so #isRunning will say
136     // false directly
137 alfonx 533 renderThread = null;
138     mapPane.onRenderingCompleted();
139 alfonx 539 } catch (Exception e) {
140     mapPane.onRenderingFailed(e);
141 alfonx 530 } finally {
142     renderer.removeRenderListener(this);
143     }
144 alfonx 529 }
145 alfonx 530
146 alfonx 529 @Override
147     public void errorOccurred(Exception e) {
148     mapPane.onRenderingFailed(e);
149     }
150 alfonx 510
151 alfonx 529 @Override
152     public void featureRenderer(SimpleFeature feature) {
153     }
154 alfonx 510
155 alfonx 530 }
156    
157 alfonx 532 /**
158     * Ask to stop the rendering. May be called often.
159     */
160 alfonx 529 public void cancelTask() {
161 alfonx 555 if (renderThread != null)
162     synchronized (renderThread) {
163     if (renderThread.isAlive()) {
164     if (renderThread.getRenderer() != null)
165     renderThread.getRenderer().stopRendering();
166     }
167     }
168 alfonx 529 }
169 alfonx 510
170 alfonx 532 /**
171     * @return <code>true</code> if the {@link Thread} is busy rendering.
172     */
173 alfonx 529 public boolean isRunning() {
174 alfonx 530 return (renderThread != null && renderThread.isAlive());
175 alfonx 529 }
176 alfonx 510
177 alfonx 532 /**
178     * Will stop rendering and remove the reference to the {@link Thread}.
179     */
180 alfonx 529 public void dispose() {
181 alfonx 530 if (renderThread != null) {
182     renderThread.renderer.stopRendering();
183     renderThread = null;
184 alfonx 529 }
185     }
186 alfonx 510
187     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26