/[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 544 - (hide annotations)
Sat Nov 21 17:13:31 2009 UTC (15 years, 3 months ago) by alfonx
Original Path: branches/1.0-gt2-2.6/src/skrueger/geotools/RenderingExecutor.java
File size: 4988 byte(s)
* XMapPane now fine....The repaint timer may stop itself if not needed anymore...
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 alfonx 544 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 544 // worldToScreen,
60     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 544 // AffineTransform worldToScreen,
83     ReferencedEnvelope mapEnv) {
84     super(new RenderRun(paintArea, graphics, renderer,
85     mapEnv
86     // , worldToScreen
87     ));
88 alfonx 529 this.renderer = renderer;
89 alfonx 530
90 alfonx 532 setName("Render" + getName());
91    
92     // System.out.println("starting render thread " + getName());
93 alfonx 529 }
94 alfonx 530
95     public GTRenderer getRenderer() {
96 alfonx 529 return renderer;
97     }
98    
99 alfonx 530 }
100    
101 alfonx 532 /**
102     * This {@link Runnable} will actually start the rendering
103     */
104 alfonx 530 class RenderRun implements Runnable, RenderListener {
105     private final Rectangle paintArea;
106 alfonx 529 private final Graphics2D graphics;
107 alfonx 544 // private final AffineTransform worldToScreen;
108 alfonx 529 private final GTRenderer renderer;
109 alfonx 533 private final ReferencedEnvelope mapEnv;
110 alfonx 510
111 alfonx 529 public RenderRun(Rectangle paintArea, Graphics2D graphics,
112 alfonx 544 GTRenderer renderer, ReferencedEnvelope mapEnv
113     // ,
114     // AffineTransform worldToScreen
115     ) {
116 alfonx 530 this.paintArea = paintArea;
117     this.graphics = graphics;
118     this.renderer = renderer;
119 alfonx 533 this.mapEnv = mapEnv;
120 alfonx 544 // this.worldToScreen = worldToScreen;
121 alfonx 529 }
122 alfonx 530
123 alfonx 529 @Override
124     public void run() {
125     try {
126 alfonx 530 renderer.addRenderListener(this);
127     System.out.println("start rendering...");
128 alfonx 532 // try {
129     // Thread.sleep(1000);
130     // } catch (InterruptedException e) {
131     // e.printStackTrace();
132     // }
133    
134     // Clear the graphics context
135     graphics.setBackground(mapPane.getMapBackgroundColor());
136     graphics.clearRect(paintArea.x, paintArea.y, paintArea.width,
137     paintArea.height);
138    
139 alfonx 544 renderer.paint(graphics, paintArea, mapEnv);
140 alfonx 530
141 alfonx 539 // Kill the reference to this Thread so #isRunning will say
142     // false directly
143 alfonx 533 renderThread = null;
144     mapPane.onRenderingCompleted();
145 alfonx 539 } catch (Exception e) {
146     mapPane.onRenderingFailed(e);
147 alfonx 530 } finally {
148     renderer.removeRenderListener(this);
149     }
150 alfonx 529 }
151 alfonx 530
152 alfonx 529 @Override
153     public void errorOccurred(Exception e) {
154     mapPane.onRenderingFailed(e);
155     }
156 alfonx 510
157 alfonx 529 @Override
158     public void featureRenderer(SimpleFeature feature) {
159     }
160 alfonx 510
161 alfonx 530 }
162    
163 alfonx 532 /**
164     * Ask to stop the rendering. May be called often.
165     */
166 alfonx 529 public void cancelTask() {
167 alfonx 530 if (renderThread != null && renderThread.isAlive()) {
168     // System.out.println("request stop for thread " +task.getName());
169     renderThread.getRenderer().stopRendering();
170 alfonx 529 }
171     }
172 alfonx 510
173 alfonx 532 /**
174     * @return <code>true</code> if the {@link Thread} is busy rendering.
175     */
176 alfonx 529 public boolean isRunning() {
177 alfonx 530 return (renderThread != null && renderThread.isAlive());
178 alfonx 529 }
179 alfonx 510
180 alfonx 532 /**
181     * Will stop rendering and remove the reference to the {@link Thread}.
182     */
183 alfonx 529 public void dispose() {
184 alfonx 530 if (renderThread != null) {
185     renderThread.renderer.stopRendering();
186     renderThread = null;
187 alfonx 529 }
188     }
189 alfonx 510
190     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26