/[schmitzm]/branches/2.4.x/src/skrueger/geotools/RenderingExecutor.java
ViewVC logotype

Diff of /branches/2.4.x/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 533 by alfonx, Thu Nov 19 17:27:01 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, worldToScreen));
81                          this.renderer = renderer;                          this.renderer = renderer;
82    
83                          System.out.println("starting render thread " + getName());                          setName("Render" + getName());
84    
85                            // System.out.println("starting render thread " + getName());
86                  }                  }
87    
88                  public GTRenderer getRenderer() {                  public GTRenderer getRenderer() {
# Line 69  public class RenderingExecutor { Line 91  public class RenderingExecutor {
91    
92          }          }
93    
94            /**
95             * This {@link Runnable} will actually start the rendering
96             */
97          class RenderRun implements Runnable, RenderListener {          class RenderRun implements Runnable, RenderListener {
98                  private final Rectangle paintArea;                  private final Rectangle paintArea;
99                  private final Graphics2D graphics;                  private final Graphics2D graphics;
100                  private final AffineTransform worldToScreen;                  private final AffineTransform worldToScreen;
101                  private final GTRenderer renderer;                  private final GTRenderer renderer;
102                    private final ReferencedEnvelope mapEnv;
103    
104                  public RenderRun(Rectangle paintArea, Graphics2D graphics,                  public RenderRun(Rectangle paintArea, Graphics2D graphics,
105                                  GTRenderer renderer, AffineTransform worldToScreen) {                                  GTRenderer renderer, ReferencedEnvelope mapEnv, AffineTransform worldToScreen) {
106                          this.paintArea = paintArea;                          this.paintArea = paintArea;
107                          this.graphics = graphics;                          this.graphics = graphics;
108                          this.renderer = renderer;                          this.renderer = renderer;
109                            this.mapEnv = mapEnv;
110                          this.worldToScreen = worldToScreen;                          this.worldToScreen = worldToScreen;
111                  }                  }
112    
# Line 88  public class RenderingExecutor { Line 115  public class RenderingExecutor {
115                          try {                          try {
116                                  renderer.addRenderListener(this);                                  renderer.addRenderListener(this);
117                                  System.out.println("start rendering...");                                  System.out.println("start rendering...");
118                                  try {                                  // try {
119                                          Thread.sleep(1000);                                  // Thread.sleep(1000);
120                                  } catch (InterruptedException e) {                                  // } catch (InterruptedException e) {
121                                          // TODO Auto-generated catch block                                  // e.printStackTrace();
122                                          e.printStackTrace();                                  // }
123                                  }  
124                                  renderer.paint(graphics, paintArea, worldToScreen);                                  // Clear the graphics context
125                                    graphics.setBackground(mapPane.getMapBackgroundColor());
126                                    graphics.clearRect(paintArea.x, paintArea.y, paintArea.width,
127                                                    paintArea.height);
128    
129                                    renderer.paint(graphics, paintArea, mapEnv, worldToScreen);
130    
131                                    // Kill the reference to this Thread to #isRunning will def. say false
132                                    renderThread = null;
133                                  mapPane.onRenderingCompleted();                                  mapPane.onRenderingCompleted();
134                                    
135                          } finally {                          } finally {
136                                  renderer.removeRenderListener(this);                                  renderer.removeRenderListener(this);
137                          }                          }
# Line 104  public class RenderingExecutor { Line 139  public class RenderingExecutor {
139    
140                  @Override                  @Override
141                  public void errorOccurred(Exception e) {                  public void errorOccurred(Exception e) {
                         // System.out.println("rendering error");  
142                          mapPane.onRenderingFailed(e);                          mapPane.onRenderingFailed(e);
143                  }                  }
144    
# Line 114  public class RenderingExecutor { Line 148  public class RenderingExecutor {
148    
149          }          }
150    
151            /**
152             * Ask to stop the rendering. May be called often.
153             */
154          public void cancelTask() {          public void cancelTask() {
155                  if (renderThread != null && renderThread.isAlive()) {                  if (renderThread != null && renderThread.isAlive()) {
156                          // System.out.println("request stop for thread " +task.getName());                          // System.out.println("request stop for thread " +task.getName());
# Line 121  public class RenderingExecutor { Line 158  public class RenderingExecutor {
158                  }                  }
159          }          }
160    
161            /**
162             * @return <code>true</code> if the {@link Thread} is busy rendering.
163             */
164          public boolean isRunning() {          public boolean isRunning() {
165                  // if (task != null)                  // if (task != null)
166                  // System.out.println("is running "+task.getName()+" = true");                  // System.out.println("is running "+task.getName()+" = true");
167                  return (renderThread != null && renderThread.isAlive());                  return (renderThread != null && renderThread.isAlive());
168          }          }
169    
170            /**
171             * Will stop rendering and remove the reference to the {@link Thread}.
172             */
173          public void dispose() {          public void dispose() {
174                  if (renderThread != null) {                  if (renderThread != null) {
175                          renderThread.renderer.stopRendering();                          renderThread.renderer.stopRendering();

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26