/[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 539 by alfonx, Fri Nov 20 19:10:05 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, AffineTransform worldToScreen) {
                 System.out.println("submit..:");  
52                  if (renderThread == null || !renderThread.isAlive()) {                  if (renderThread == null || !renderThread.isAlive()) {
53                          System.out.println("is vacant... starting thread!");                          // System.out.println("is vacant... starting thread!");
54                            renderThread = null;
55    
56                          renderThread = new RenderThread(paintArea, graphics, renderer,                          renderThread = new RenderThread(paintArea, graphics, renderer,
57                                          worldToScreen);                                          worldToScreen, envelope);
58                          renderThread.start();                          renderThread.start();
59    
60                          return true;                          return true;
61                  } else {                  } else {
62                          System.out.println("is busy... requesting stop!");                          // System.out.println("is busy... requesting stop!");
63                          renderThread.getRenderer().stopRendering();                          renderThread.getRenderer().stopRendering();
64                            return false;
65                  }                  }
   
                 return false;  
66          }          }
67    
68            /**
69             * For every new rendering job submitted and accepted, an instance of this
70             * {@link Thread} will be started.
71             *
72             */
73          class RenderThread extends Thread {          class RenderThread extends Thread {
74    
75                  private final GTRenderer renderer;                  private final GTRenderer renderer;
76    
77                  public RenderThread(final Rectangle paintArea,                  public RenderThread(final Rectangle paintArea,
78                                  final Graphics2D graphics, GTRenderer renderer,                                  final Graphics2D graphics, GTRenderer renderer,
79                                  AffineTransform worldToScreen) {                                  AffineTransform worldToScreen, ReferencedEnvelope mapEnv) {
80                          super(new RenderRun(paintArea, graphics, renderer, worldToScreen));                          super(new RenderRun(paintArea, graphics, renderer, mapEnv,
81                                            worldToScreen));
82                          this.renderer = renderer;                          this.renderer = renderer;
83    
84                          System.out.println("starting render thread " + getName());                          setName("Render" + getName());
85    
86                            // System.out.println("starting render thread " + getName());
87                  }                  }
88    
89                  public GTRenderer getRenderer() {                  public GTRenderer getRenderer() {
# Line 69  public class RenderingExecutor { Line 92  public class RenderingExecutor {
92    
93          }          }
94    
95            /**
96             * This {@link Runnable} will actually start the rendering
97             */
98          class RenderRun implements Runnable, RenderListener {          class RenderRun implements Runnable, RenderListener {
99                  private final Rectangle paintArea;                  private final Rectangle paintArea;
100                  private final Graphics2D graphics;                  private final Graphics2D graphics;
101                  private final AffineTransform worldToScreen;                  private final AffineTransform worldToScreen;
102                  private final GTRenderer renderer;                  private final GTRenderer renderer;
103                    private final ReferencedEnvelope mapEnv;
104    
105                  public RenderRun(Rectangle paintArea, Graphics2D graphics,                  public RenderRun(Rectangle paintArea, Graphics2D graphics,
106                                  GTRenderer renderer, AffineTransform worldToScreen) {                                  GTRenderer renderer, ReferencedEnvelope mapEnv,
107                                    AffineTransform worldToScreen) {
108                          this.paintArea = paintArea;                          this.paintArea = paintArea;
109                          this.graphics = graphics;                          this.graphics = graphics;
110                          this.renderer = renderer;                          this.renderer = renderer;
111                            this.mapEnv = mapEnv;
112                          this.worldToScreen = worldToScreen;                          this.worldToScreen = worldToScreen;
113                  }                  }
114    
# Line 88  public class RenderingExecutor { Line 117  public class RenderingExecutor {
117                          try {                          try {
118                                  renderer.addRenderListener(this);                                  renderer.addRenderListener(this);
119                                  System.out.println("start rendering...");                                  System.out.println("start rendering...");
120                                  try {                                  // try {
121                                          Thread.sleep(1000);                                  // Thread.sleep(1000);
122                                  } catch (InterruptedException e) {                                  // } catch (InterruptedException e) {
123                                          // TODO Auto-generated catch block                                  // e.printStackTrace();
124                                          e.printStackTrace();                                  // }
125                                  }  
126                                    // Clear the graphics context
127                                    graphics.setBackground(mapPane.getMapBackgroundColor());
128                                    graphics.clearRect(paintArea.x, paintArea.y, paintArea.width,
129                                                    paintArea.height);
130    
131                                  renderer.paint(graphics, paintArea, worldToScreen);                                  renderer.paint(graphics, paintArea, worldToScreen);
132    
133                                    // Kill the reference to this Thread so #isRunning will say
134                                    // false directly
135                                    renderThread = null;
136                                  mapPane.onRenderingCompleted();                                  mapPane.onRenderingCompleted();
137                            } catch (Exception e) {
138                                    mapPane.onRenderingFailed(e);
139                          } finally {                          } finally {
140                                  renderer.removeRenderListener(this);                                  renderer.removeRenderListener(this);
141                          }                          }
# Line 104  public class RenderingExecutor { Line 143  public class RenderingExecutor {
143    
144                  @Override                  @Override
145                  public void errorOccurred(Exception e) {                  public void errorOccurred(Exception e) {
                         // System.out.println("rendering error");  
146                          mapPane.onRenderingFailed(e);                          mapPane.onRenderingFailed(e);
147                  }                  }
148    
# Line 114  public class RenderingExecutor { Line 152  public class RenderingExecutor {
152    
153          }          }
154    
155            /**
156             * Ask to stop the rendering. May be called often.
157             */
158          public void cancelTask() {          public void cancelTask() {
159                  if (renderThread != null && renderThread.isAlive()) {                  if (renderThread != null && renderThread.isAlive()) {
160                          // System.out.println("request stop for thread " +task.getName());                          // System.out.println("request stop for thread " +task.getName());
# Line 121  public class RenderingExecutor { Line 162  public class RenderingExecutor {
162                  }                  }
163          }          }
164    
165            /**
166             * @return <code>true</code> if the {@link Thread} is busy rendering.
167             */
168          public boolean isRunning() {          public boolean isRunning() {
169                  // if (task != null)                  // if (task != null)
170                  // System.out.println("is running "+task.getName()+" = true");                  // System.out.println("is running "+task.getName()+" = true");
171                  return (renderThread != null && renderThread.isAlive());                  return (renderThread != null && renderThread.isAlive());
172          }          }
173    
174            /**
175             * Will stop rendering and remove the reference to the {@link Thread}.
176             */
177          public void dispose() {          public void dispose() {
178                  if (renderThread != null) {                  if (renderThread != null) {
179                          renderThread.renderer.stopRendering();                          renderThread.renderer.stopRendering();

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26