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

branches/1.0-gt2-2.6/src/skrueger/geotools/RenderingExecutor.java revision 531 by alfonx, Thu Nov 19 09:42:59 2009 UTC trunk/src/skrueger/geotools/RenderingExecutor.java revision 1353 by alfonx, Fri Dec 17 23:08:37 2010 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.filter.function.EnvFunction;
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.geotools.gui.XMapPane;
15    
16    /**
17     * This class is used by {@link XMapPane} to start and stop the rendering a
18     * {@link Thread} for rendering.
19     */
20    public class RenderingExecutor {
21            private final static Logger LOGGER = Logger
22                            .getLogger(RenderingExecutor.class);
23            /**
24             * Instance to a {@link RenderThread} doing any work. It's volatile so the
25             * correct value will always be visible to any {@link Thread}
26             **/
27          private volatile RenderThread renderThread;          private volatile RenderThread renderThread;
28    
29          private final XMapPane mapPane;          private final XMapPane mapPane;
30    
31          public RenderingExecutor(XMapPane mapPane) {          public RenderingExecutor(XMapPane mapPane) {
# Line 20  public class RenderingExecutor { Line 34  public class RenderingExecutor {
34    
35          /**          /**
36           * Submit a new rendering task. If no rendering task is presently running           * Submit a new rendering task. If no rendering task is presently running
37           * 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
38           * is no task queue).           * returns <code>false</code>.
39           *           *
40           * @param envelope           * @param envelope
41           *            the map area (world coordinates) to be rendered           *            the map area (world coordinates) to be rendered.
42           * @param graphics           * @param graphics
43           *            the graphics object to draw on           *            the graphics object to draw on.
44             * @param paintArea
45             *            size of the area to paint the world into.
46             * @param worldToScreen
47             *            the {@link AffineTransform} from world coordinates to screen
48             *            coordinates.
49             * @param renderer
50             *            the {@link GTRenderer} to use.
51           *           *
52           * @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
53           */           */
54          public synchronized boolean submit(ReferencedEnvelope envelope,          public synchronized boolean submit(ReferencedEnvelope envelope,
55                          Rectangle paintArea, Graphics2D graphics,                          Rectangle paintArea, Graphics2D graphics, final GTRenderer renderer) {
                         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);                                          envelope);
62                          renderThread.start();                          renderThread.start();
63    
64                          return true;                          return true;
65                  } else {                  } else {
                         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) {                                  ReferencedEnvelope mapEnv) {
83                          super(new RenderRun(paintArea, graphics, renderer, worldToScreen));                          super(new RenderRun(paintArea, graphics, renderer, mapEnv));
84                          this.renderer = renderer;                          this.renderer = renderer;
85    
86                          System.out.println("starting render thread " + getName());                          setName("Render" + getName());
87    
88                  }                  }
89    
90                  public GTRenderer getRenderer() {                  public GTRenderer getRenderer() {
# Line 69  public class RenderingExecutor { Line 93  public class RenderingExecutor {
93    
94          }          }
95    
96            /**
97             * This {@link Runnable} will actually start the rendering
98             */
99          class RenderRun implements Runnable, RenderListener {          class RenderRun implements Runnable, RenderListener {
100                  private final Rectangle paintArea;                  private final Rectangle paintArea;
101                  private final Graphics2D graphics;                  private final Graphics2D graphics;
                 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                          this.paintArea = paintArea;                          this.paintArea = paintArea;
108                          this.graphics = graphics;                          this.graphics = graphics;
109                          this.renderer = renderer;                          this.renderer = renderer;
110                          this.worldToScreen = worldToScreen;                          this.mapEnv = mapEnv;
111                  }                  }
112    
113                  @Override                  @Override
114                  public void run() {                  public void run() {
115                            long startT = System.currentTimeMillis();
116                          try {                          try {
117                                  renderer.addRenderListener(this);                                  renderer.addRenderListener(this);
118                                  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);  
119    
120                                  mapPane.onRenderingCompleted();                                  // Clear the graphics context
121                                    graphics.setBackground(mapPane.getMapBackgroundColor());
122                                    graphics.clearRect(paintArea.x, paintArea.y, paintArea.width,
123                                                    paintArea.height);
124    
125                                    // TODO It should be enough to set only the Local Value, but it
126                                    // isn't
127                                    EnvFunction.setGlobalValue(XMapPane.ENV_LANG,
128                                                    mapPane.getRenderLanguage());
129                                    EnvFunction.setLocalValue(XMapPane.ENV_LANG,
130                                                    mapPane.getRenderLanguage());
131    
132                                    // LOGGER.debug("Renderer language set to "
133                                    // + mapPane.getRenderLanguage());
134    
135                                    renderer.paint(graphics, paintArea, mapEnv);
136    
137                                    // Kill the reference to this Thread so #isRunning will say
138                                    // false directly
139                                    renderThread = null;
140                                    mapPane.onRenderingCompleted(System.currentTimeMillis()
141                                                    - startT);
142                            } catch (Exception e) {
143                                    if (e != null && !e.toString().equals("0"))
144                                            mapPane.onRenderingFailed(e);
145                          } finally {                          } finally {
146                                  renderer.removeRenderListener(this);                                  renderer.removeRenderListener(this);
147                          }                          }
# Line 104  public class RenderingExecutor { Line 149  public class RenderingExecutor {
149    
150                  @Override                  @Override
151                  public void errorOccurred(Exception e) {                  public void errorOccurred(Exception e) {
                         // System.out.println("rendering error");  
152                          mapPane.onRenderingFailed(e);                          mapPane.onRenderingFailed(e);
153                  }                  }
154    
# Line 114  public class RenderingExecutor { Line 158  public class RenderingExecutor {
158    
159          }          }
160    
161            /**
162             * Ask to stop the rendering. May be called often.
163             */
164          public void cancelTask() {          public void cancelTask() {
165                  if (renderThread != null && renderThread.isAlive()) {                  if (renderThread != null)
166                          // System.out.println("request stop for thread " +task.getName());                          synchronized (renderThread) {
167                          renderThread.getRenderer().stopRendering();                                  if (renderThread.isAlive()) {
168                  }                                          if (renderThread.getRenderer() != null)
169                                                    renderThread.getRenderer().stopRendering();
170                                    }
171                            }
172          }          }
173    
174            /**
175             * @return <code>true</code> if the {@link Thread} is busy rendering.
176             */
177          public boolean isRunning() {          public boolean isRunning() {
                 // if (task != null)  
                 // System.out.println("is running "+task.getName()+" = true");  
178                  return (renderThread != null && renderThread.isAlive());                  return (renderThread != null && renderThread.isAlive());
179          }          }
180    
181            /**
182             * Will stop rendering and remove the reference to the {@link Thread}.
183             */
184          public void dispose() {          public void dispose() {
185                  if (renderThread != null) {                  if (renderThread != null) {
186                          renderThread.renderer.stopRendering();                          renderThread.renderer.stopRendering();

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26