/[schmitzm]/branches/2.3.KECK/src/skrueger/geotools/RenderingExecutor.java
ViewVC logotype

Diff of /branches/2.3.KECK/src/skrueger/geotools/RenderingExecutor.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

branches/1.0-gt2-2.6/src/skrueger/geotools/RenderingExecutor.java revision 531 by alfonx, Thu Nov 19 09:42:59 2009 UTC branches/2.0-RC1/src/skrueger/geotools/RenderingExecutor.java revision 604 by alfonx, Wed Dec 9 14:15:53 2009 UTC
# Line 4  import java.awt.Graphics2D; Line 4  import java.awt.Graphics2D;
4  import java.awt.Rectangle;  import java.awt.Rectangle;
5  import java.awt.geom.AffineTransform;  import java.awt.geom.AffineTransform;
6    
7    import org.apache.log4j.Logger;
8  import org.geotools.geometry.jts.ReferencedEnvelope;  import org.geotools.geometry.jts.ReferencedEnvelope;
9  import org.geotools.renderer.GTRenderer;  import org.geotools.renderer.GTRenderer;
10  import org.geotools.renderer.RenderListener;  import org.geotools.renderer.RenderListener;
11  import org.opengis.feature.simple.SimpleFeature;  import org.opengis.feature.simple.SimpleFeature;
12    
13  public class RenderingExecutor {  /**
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            private final static Logger LOGGER = Logger.getLogger(RenderingExecutor.class);
19            /**
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          private volatile RenderThread renderThread;          private volatile RenderThread renderThread;
24    
25          private final XMapPane mapPane;          private final XMapPane mapPane;
26    
27          public RenderingExecutor(XMapPane mapPane) {          public RenderingExecutor(XMapPane mapPane) {
# Line 20  public class RenderingExecutor { Line 30  public class RenderingExecutor {
30    
31          /**          /**
32           * Submit a new rendering task. If no rendering task is presently running           * Submit a new rendering task. If no rendering task is presently running
33           * this new task will be accepted; otherwise it will be rejected (ie. there           * this new job will be accepted; otherwise it will be rejected and it
34           * is no task queue).           * returns <code>false</code>.
35           *           *
36           * @param envelope           * @param envelope
37           *            the map area (world coordinates) to be rendered           *            the map area (world coordinates) to be rendered.
38           * @param graphics           * @param graphics
39           *            the graphics object to draw on           *            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           *           *
48           * @return true if the rendering task was accepted; false if it was rejected           * @return true if the rendering task was accepted; false if it was rejected
49           */           */
50          public synchronized boolean submit(ReferencedEnvelope envelope,          public synchronized boolean submit(ReferencedEnvelope envelope,
51                          Rectangle paintArea, Graphics2D graphics,                          Rectangle paintArea, Graphics2D graphics, final GTRenderer renderer
52                          final GTRenderer renderer, AffineTransform worldToScreen) {          // , AffineTransform worldToScreen
53                  System.out.println("submit..:");          ) {
54                  if (renderThread == null || !renderThread.isAlive()) {                  if (renderThread == null || !renderThread.isAlive()) {
55                          System.out.println("is vacant... starting thread!");                          // System.out.println("is vacant... starting thread!");
56                            renderThread = null;
57    
58                          renderThread = new RenderThread(paintArea, graphics, renderer,                          renderThread = new RenderThread(paintArea, graphics, renderer,
59                                          worldToScreen);                          // worldToScreen,
60                                            envelope);
61                          renderThread.start();                          renderThread.start();
62    
63                          return true;                          return true;
64                  } else {                  } else {
65                          System.out.println("is busy... requesting stop!");                          // System.out.println("is busy... requesting stop!");
66                          renderThread.getRenderer().stopRendering();                          renderThread.getRenderer().stopRendering();
67                            return false;
68                  }                  }
   
                 return false;  
69          }          }
70    
71            /**
72             * For every new rendering job submitted and accepted, an instance of this
73             * {@link Thread} will be started.
74             *
75             */
76          class RenderThread extends Thread {          class RenderThread extends Thread {
77    
78                  private final GTRenderer renderer;                  private final GTRenderer renderer;
79    
80                  public RenderThread(final Rectangle paintArea,                  public RenderThread(final Rectangle paintArea,
81                                  final Graphics2D graphics, GTRenderer renderer,                                  final Graphics2D graphics, GTRenderer renderer,
82                                  AffineTransform worldToScreen) {                                  // AffineTransform worldToScreen,
83                          super(new RenderRun(paintArea, graphics, renderer, worldToScreen));                                  ReferencedEnvelope mapEnv) {
84                            super(new RenderRun(paintArea, graphics, renderer, mapEnv
85                            // , worldToScreen
86                                            ));
87                          this.renderer = renderer;                          this.renderer = renderer;
88    
89                          System.out.println("starting render thread " + getName());                          setName("Render" + getName());
90    
91                            // System.out.println("starting render thread " + getName());
92                  }                  }
93    
94                  public GTRenderer getRenderer() {                  public GTRenderer getRenderer() {
# Line 69  public class RenderingExecutor { Line 97  public class RenderingExecutor {
97    
98          }          }
99    
100            /**
101             * This {@link Runnable} will actually start the rendering
102             */
103          class RenderRun implements Runnable, RenderListener {          class RenderRun implements Runnable, RenderListener {
104                  private final Rectangle paintArea;                  private final Rectangle paintArea;
105                  private final Graphics2D graphics;                  private final Graphics2D graphics;
106                  private final AffineTransform worldToScreen;                  // private final AffineTransform worldToScreen;
107                  private final GTRenderer renderer;                  private final GTRenderer renderer;
108                    private final ReferencedEnvelope mapEnv;
109    
110                  public RenderRun(Rectangle paintArea, Graphics2D graphics,                  public RenderRun(Rectangle paintArea, Graphics2D graphics,
111                                  GTRenderer renderer, AffineTransform worldToScreen) {                                  GTRenderer renderer, ReferencedEnvelope mapEnv
112                    // ,
113                    // AffineTransform worldToScreen
114                    ) {
115                          this.paintArea = paintArea;                          this.paintArea = paintArea;
116                          this.graphics = graphics;                          this.graphics = graphics;
117                          this.renderer = renderer;                          this.renderer = renderer;
118                          this.worldToScreen = worldToScreen;                          this.mapEnv = mapEnv;
119                            // this.worldToScreen = worldToScreen;
120                  }                  }
121    
122                  @Override                  @Override
123                  public void run() {                  public void run() {
124                          try {                          try {
125                                  renderer.addRenderListener(this);                                  renderer.addRenderListener(this);
126                                  System.out.println("start rendering...");                                  LOGGER.debug("start rendering...");
                                 try {  
                                         Thread.sleep(1000);  
                                 } catch (InterruptedException e) {  
                                         // TODO Auto-generated catch block  
                                         e.printStackTrace();  
                                 }  
                                 renderer.paint(graphics, paintArea, worldToScreen);  
127    
128                                    // Clear the graphics context
129                                    graphics.setBackground(mapPane.getMapBackgroundColor());
130                                    graphics.clearRect(paintArea.x, paintArea.y, paintArea.width,
131                                                    paintArea.height);
132    
133                                    renderer.paint(graphics, paintArea, mapEnv);
134    
135                                    // Kill the reference to this Thread so #isRunning will say
136                                    // false directly
137                                    renderThread = null;
138                                  mapPane.onRenderingCompleted();                                  mapPane.onRenderingCompleted();
139                            } catch (Exception e) {
140                                    mapPane.onRenderingFailed(e);
141                          } finally {                          } finally {
142                                  renderer.removeRenderListener(this);                                  renderer.removeRenderListener(this);
143                          }                          }
# Line 104  public class RenderingExecutor { Line 145  public class RenderingExecutor {
145    
146                  @Override                  @Override
147                  public void errorOccurred(Exception e) {                  public void errorOccurred(Exception e) {
                         // System.out.println("rendering error");  
148                          mapPane.onRenderingFailed(e);                          mapPane.onRenderingFailed(e);
149                  }                  }
150    
# Line 114  public class RenderingExecutor { Line 154  public class RenderingExecutor {
154    
155          }          }
156    
157            /**
158             * Ask to stop the rendering. May be called often.
159             */
160          public void cancelTask() {          public void cancelTask() {
161                  if (renderThread != null && renderThread.isAlive()) {                  if (renderThread != null)
162                          // System.out.println("request stop for thread " +task.getName());                          synchronized (renderThread) {
163                          renderThread.getRenderer().stopRendering();                                  if (renderThread.isAlive()) {
164                  }                                          if (renderThread.getRenderer() != null)
165                                                    renderThread.getRenderer().stopRendering();
166                                    }
167                            }
168          }          }
169    
170            /**
171             * @return <code>true</code> if the {@link Thread} is busy rendering.
172             */
173          public boolean isRunning() {          public boolean isRunning() {
                 // if (task != null)  
                 // System.out.println("is running "+task.getName()+" = true");  
174                  return (renderThread != null && renderThread.isAlive());                  return (renderThread != null && renderThread.isAlive());
175          }          }
176    
177            /**
178             * Will stop rendering and remove the reference to the {@link Thread}.
179             */
180          public void dispose() {          public void dispose() {
181                  if (renderThread != null) {                  if (renderThread != null) {
182                          renderThread.renderer.stopRendering();                          renderThread.renderer.stopRendering();

Legend:
Removed from v.531  
changed lines
  Added in v.604

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26