/[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

revision 531 by alfonx, Thu Nov 19 09:42:59 2009 UTC revision 544 by alfonx, Sat Nov 21 17:13:31 2009 UTC
# Line 9  import org.geotools.renderer.GTRenderer; Line 9  import org.geotools.renderer.GTRenderer;
9  import org.geotools.renderer.RenderListener;  import org.geotools.renderer.RenderListener;
10  import org.opengis.feature.simple.SimpleFeature;  import org.opengis.feature.simple.SimpleFeature;
11    
12  public class RenderingExecutor {  /**
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          private volatile RenderThread renderThread;          private volatile RenderThread renderThread;
23    
24          private final XMapPane mapPane;          private final XMapPane mapPane;
25    
26          public RenderingExecutor(XMapPane mapPane) {          public RenderingExecutor(XMapPane mapPane) {
# Line 20  public class RenderingExecutor { Line 29  public class RenderingExecutor {
29    
30          /**          /**
31           * Submit a new rendering task. If no rendering task is presently running           * Submit a new rendering task. If no rendering task is presently running
32           * 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
33           * is no task queue).           * returns <code>false</code>.
34           *           *
35           * @param envelope           * @param envelope
36           *            the map area (world coordinates) to be rendered           *            the map area (world coordinates) to be rendered.
37           * @param graphics           * @param graphics
38           *            the graphics object to draw on           *            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           *           *
47           * @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
48           */           */
49          public synchronized boolean submit(ReferencedEnvelope envelope,          public synchronized boolean submit(ReferencedEnvelope envelope,
50                          Rectangle paintArea, Graphics2D graphics,                          Rectangle paintArea, Graphics2D graphics,
51                          final GTRenderer renderer, AffineTransform worldToScreen) {                          final GTRenderer renderer
52                  System.out.println("submit..:");  //                      , AffineTransform worldToScreen
53                            ) {
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,
85                                            mapEnv
86    //                                      ,                                       worldToScreen
87                                            ));
88                          this.renderer = renderer;                          this.renderer = renderer;
89    
90                          System.out.println("starting render thread " + getName());                          setName("Render" + getName());
91    
92                            // System.out.println("starting render thread " + getName());
93                  }                  }
94    
95                  public GTRenderer getRenderer() {                  public GTRenderer getRenderer() {
# Line 69  public class RenderingExecutor { Line 98  public class RenderingExecutor {
98    
99          }          }
100    
101            /**
102             * This {@link Runnable} will actually start the rendering
103             */
104          class RenderRun implements Runnable, RenderListener {          class RenderRun implements Runnable, RenderListener {
105                  private final Rectangle paintArea;                  private final Rectangle paintArea;
106                  private final Graphics2D graphics;                  private final Graphics2D graphics;
107                  private final AffineTransform worldToScreen;  //              private final AffineTransform worldToScreen;
108                  private final GTRenderer renderer;                  private final GTRenderer renderer;
109                    private final ReferencedEnvelope mapEnv;
110    
111                  public RenderRun(Rectangle paintArea, Graphics2D graphics,                  public RenderRun(Rectangle paintArea, Graphics2D graphics,
112                                  GTRenderer renderer, AffineTransform worldToScreen) {                                  GTRenderer renderer, ReferencedEnvelope mapEnv
113    //                              ,
114    //                              AffineTransform worldToScreen
115                                    ) {
116                          this.paintArea = paintArea;                          this.paintArea = paintArea;
117                          this.graphics = graphics;                          this.graphics = graphics;
118                          this.renderer = renderer;                          this.renderer = renderer;
119                          this.worldToScreen = worldToScreen;                          this.mapEnv = mapEnv;
120    //                      this.worldToScreen = worldToScreen;
121                  }                  }
122    
123                  @Override                  @Override
# Line 88  public class RenderingExecutor { Line 125  public class RenderingExecutor {
125                          try {                          try {
126                                  renderer.addRenderListener(this);                                  renderer.addRenderListener(this);
127                                  System.out.println("start rendering...");                                  System.out.println("start rendering...");
128                                  try {                                  // try {
129                                          Thread.sleep(1000);                                  // Thread.sleep(1000);
130                                  } catch (InterruptedException e) {                                  // } catch (InterruptedException e) {
131                                          // TODO Auto-generated catch block                                  // e.printStackTrace();
132                                          e.printStackTrace();                                  // }
133                                  }  
134                                  renderer.paint(graphics, paintArea, worldToScreen);                                  // Clear the graphics context
135                                    graphics.setBackground(mapPane.getMapBackgroundColor());
136                                    graphics.clearRect(paintArea.x, paintArea.y, paintArea.width,
137                                                    paintArea.height);
138    
139                                    renderer.paint(graphics, paintArea, mapEnv);
140    
141                                    // Kill the reference to this Thread so #isRunning will say
142                                    // false directly
143                                    renderThread = null;
144                                  mapPane.onRenderingCompleted();                                  mapPane.onRenderingCompleted();
145                            } catch (Exception e) {
146                                    mapPane.onRenderingFailed(e);
147                          } finally {                          } finally {
148                                  renderer.removeRenderListener(this);                                  renderer.removeRenderListener(this);
149                          }                          }
# Line 104  public class RenderingExecutor { Line 151  public class RenderingExecutor {
151    
152                  @Override                  @Override
153                  public void errorOccurred(Exception e) {                  public void errorOccurred(Exception e) {
                         // System.out.println("rendering error");  
154                          mapPane.onRenderingFailed(e);                          mapPane.onRenderingFailed(e);
155                  }                  }
156    
# Line 114  public class RenderingExecutor { Line 160  public class RenderingExecutor {
160    
161          }          }
162    
163            /**
164             * Ask to stop the rendering. May be called often.
165             */
166          public void cancelTask() {          public void cancelTask() {
167                  if (renderThread != null && renderThread.isAlive()) {                  if (renderThread != null && renderThread.isAlive()) {
168                          // System.out.println("request stop for thread " +task.getName());                          // System.out.println("request stop for thread " +task.getName());
# Line 121  public class RenderingExecutor { Line 170  public class RenderingExecutor {
170                  }                  }
171          }          }
172    
173            /**
174             * @return <code>true</code> if the {@link Thread} is busy rendering.
175             */
176          public boolean isRunning() {          public boolean isRunning() {
                 // if (task != null)  
                 // System.out.println("is running "+task.getName()+" = true");  
177                  return (renderThread != null && renderThread.isAlive());                  return (renderThread != null && renderThread.isAlive());
178          }          }
179    
180            /**
181             * Will stop rendering and remove the reference to the {@link Thread}.
182             */
183          public void dispose() {          public void dispose() {
184                  if (renderThread != null) {                  if (renderThread != null) {
185                          renderThread.renderer.stopRendering();                          renderThread.renderer.stopRendering();

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26