/[schmitzm]/branches/2.0-RC1/src/skrueger/geotools/RenderingExecutor.java
ViewVC logotype

Diff of /branches/2.0-RC1/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 532 by alfonx, Thu Nov 19 10:39:48 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 javax.swing.SwingUtilities;
8    
9  import org.geotools.geometry.jts.ReferencedEnvelope;  import org.geotools.geometry.jts.ReferencedEnvelope;
10  import org.geotools.renderer.GTRenderer;  import org.geotools.renderer.GTRenderer;
11  import org.geotools.renderer.RenderListener;  import org.geotools.renderer.RenderListener;
12  import org.opengis.feature.simple.SimpleFeature;  import org.opengis.feature.simple.SimpleFeature;
13    
14  public class RenderingExecutor {  import schmitzm.swing.SwingUtil;
15    
16    /**
17     * This class is used by {@link XMapPane} to start and stop the rendering a
18     * {@link Thread} for rendering.
19     */
20    class RenderingExecutor {
21    
22            /**
23             * Instance to a {@link RenderThread} doing any work. It's volatile so the
24             * correct value will always be visible to any {@link Thread}
25             **/
26          private volatile RenderThread renderThread;          private volatile RenderThread renderThread;
27    
28          private final XMapPane mapPane;          private final XMapPane mapPane;
29    
30          public RenderingExecutor(XMapPane mapPane) {          public RenderingExecutor(XMapPane mapPane) {
# Line 20  public class RenderingExecutor { Line 33  public class RenderingExecutor {
33    
34          /**          /**
35           * Submit a new rendering task. If no rendering task is presently running           * Submit a new rendering task. If no rendering task is presently running
36           * 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
37           * is no task queue).           * returns <code>false</code>.
38           *           *
39           * @param envelope           * @param envelope
40           *            the map area (world coordinates) to be rendered           *            the map area (world coordinates) to be rendered.
41           * @param graphics           * @param graphics
42           *            the graphics object to draw on           *            the graphics object to draw on.
43             * @param paintArea
44             *            size of the area to paint the world into.
45             * @param worldToScreen
46             *            the {@link AffineTransform} from world coordinates to screen
47             *            coordinates.
48             * @param renderer
49             *            the {@link GTRenderer} to use.
50           *           *
51           * @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
52           */           */
53          public synchronized boolean submit(ReferencedEnvelope envelope,          public synchronized boolean submit(ReferencedEnvelope envelope,
54                          Rectangle paintArea, Graphics2D graphics,                          Rectangle paintArea, Graphics2D graphics,
55                          final GTRenderer renderer, AffineTransform worldToScreen) {                          final GTRenderer renderer, AffineTransform worldToScreen) {
                 System.out.println("submit..:");  
56                  if (renderThread == null || !renderThread.isAlive()) {                  if (renderThread == null || !renderThread.isAlive()) {
57                          System.out.println("is vacant... starting thread!");                          // System.out.println("is vacant... starting thread!");
58                            renderThread = null;
59    
60                          renderThread = new RenderThread(paintArea, graphics, renderer,                          renderThread = new RenderThread(paintArea, graphics, renderer,
61                                          worldToScreen);                                          worldToScreen);
# Line 43  public class RenderingExecutor { Line 63  public class RenderingExecutor {
63    
64                          return true;                          return true;
65                  } else {                  } else {
66                          System.out.println("is busy... requesting stop!");                          // System.out.println("is busy... requesting stop!");
67                          renderThread.getRenderer().stopRendering();                          renderThread.getRenderer().stopRendering();
68                            return false;
69                  }                  }
   
                 return false;  
70          }          }
71    
72            /**
73             * For every new rendering job submitted and accepted, an instance of this
74             * {@link Thread} will be started.
75             *
76             */
77          class RenderThread extends Thread {          class RenderThread extends Thread {
78    
79                  private final GTRenderer renderer;                  private final GTRenderer renderer;
# Line 60  public class RenderingExecutor { Line 84  public class RenderingExecutor {
84                          super(new RenderRun(paintArea, graphics, renderer, worldToScreen));                          super(new RenderRun(paintArea, graphics, renderer, worldToScreen));
85                          this.renderer = renderer;                          this.renderer = renderer;
86    
87                          System.out.println("starting render thread " + getName());                          setName("Render" + getName());
88    
89                            // System.out.println("starting render thread " + getName());
90                  }                  }
91    
92                  public GTRenderer getRenderer() {                  public GTRenderer getRenderer() {
# Line 69  public class RenderingExecutor { Line 95  public class RenderingExecutor {
95    
96          }          }
97    
98            /**
99             * This {@link Runnable} will actually start the rendering
100             */
101          class RenderRun implements Runnable, RenderListener {          class RenderRun implements Runnable, RenderListener {
102                  private final Rectangle paintArea;                  private final Rectangle paintArea;
103                  private final Graphics2D graphics;                  private final Graphics2D graphics;
# 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                                  mapPane.onRenderingCompleted();                                  // Invoked later, so when onRenderingCompleted is called, the Thread is not running anymore (#isRunning = false)
134                                    SwingUtilities.invokeLater(new Runnable() {
135                                            @Override
136                                            public void run() {
137                                                    mapPane.onRenderingCompleted();
138                                            };
139                                    });
140                          } finally {                          } finally {
141                                  renderer.removeRenderListener(this);                                  renderer.removeRenderListener(this);
142                          }                          }
# 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 && renderThread.isAlive()) {
162                          // System.out.println("request stop for thread " +task.getName());                          // System.out.println("request stop for thread " +task.getName());
# Line 121  public class RenderingExecutor { Line 164  public class RenderingExecutor {
164                  }                  }
165          }          }
166    
167            /**
168             * @return <code>true</code> if the {@link Thread} is busy rendering.
169             */
170          public boolean isRunning() {          public boolean isRunning() {
171                  // if (task != null)                  // if (task != null)
172                  // System.out.println("is running "+task.getName()+" = true");                  // System.out.println("is running "+task.getName()+" = true");
173                  return (renderThread != null && renderThread.isAlive());                  return (renderThread != null && renderThread.isAlive());
174          }          }
175    
176            /**
177             * Will stop rendering and remove the reference to the {@link Thread}.
178             */
179          public void dispose() {          public void dispose() {
180                  if (renderThread != null) {                  if (renderThread != null) {
181                          renderThread.renderer.stopRendering();                          renderThread.renderer.stopRendering();

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26