[Schmitzm-commits] r532 - branches/1.0-gt2-2.6/src/skrueger/geotools

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Nov 19 11:39:49 CET 2009


Author: alfonx
Date: 2009-11-19 11:39:48 +0100 (Thu, 19 Nov 2009)
New Revision: 532

Modified:
   branches/1.0-gt2-2.6/src/skrueger/geotools/RenderingExecutor.java
Log:
* More documentation in RenderingExecutor

Modified: branches/1.0-gt2-2.6/src/skrueger/geotools/RenderingExecutor.java
===================================================================
--- branches/1.0-gt2-2.6/src/skrueger/geotools/RenderingExecutor.java	2009-11-19 09:42:59 UTC (rev 531)
+++ branches/1.0-gt2-2.6/src/skrueger/geotools/RenderingExecutor.java	2009-11-19 10:39:48 UTC (rev 532)
@@ -4,14 +4,27 @@
 import java.awt.Rectangle;
 import java.awt.geom.AffineTransform;
 
+import javax.swing.SwingUtilities;
+
 import org.geotools.geometry.jts.ReferencedEnvelope;
 import org.geotools.renderer.GTRenderer;
 import org.geotools.renderer.RenderListener;
 import org.opengis.feature.simple.SimpleFeature;
 
-public class RenderingExecutor {
+import schmitzm.swing.SwingUtil;
 
+/**
+ * This class is used by {@link XMapPane} to start and stop the rendering a
+ * {@link Thread} for rendering.
+ */
+class RenderingExecutor {
+
+	/**
+	 * Instance to a {@link RenderThread} doing any work. It's volatile so the
+	 * correct value will always be visible to any {@link Thread}
+	 **/
 	private volatile RenderThread renderThread;
+
 	private final XMapPane mapPane;
 
 	public RenderingExecutor(XMapPane mapPane) {
@@ -20,22 +33,29 @@
 
 	/**
 	 * Submit a new rendering task. If no rendering task is presently running
-	 * this new task will be accepted; otherwise it will be rejected (ie. there
-	 * is no task queue).
+	 * this new job will be accepted; otherwise it will be rejected and it
+	 * returns <code>false</code>.
 	 * 
 	 * @param envelope
-	 *            the map area (world coordinates) to be rendered
+	 *            the map area (world coordinates) to be rendered.
 	 * @param graphics
-	 *            the graphics object to draw on
+	 *            the graphics object to draw on.
+	 * @param paintArea
+	 *            size of the area to paint the world into.
+	 * @param worldToScreen
+	 *            the {@link AffineTransform} from world coordinates to screen
+	 *            coordinates.
+	 * @param renderer
+	 *            the {@link GTRenderer} to use.
 	 * 
 	 * @return true if the rendering task was accepted; false if it was rejected
 	 */
 	public synchronized boolean submit(ReferencedEnvelope envelope,
 			Rectangle paintArea, Graphics2D graphics,
 			final GTRenderer renderer, AffineTransform worldToScreen) {
-		System.out.println("submit..:");
 		if (renderThread == null || !renderThread.isAlive()) {
-			System.out.println("is vacant... starting thread!");
+			// System.out.println("is vacant... starting thread!");
+			renderThread = null;
 
 			renderThread = new RenderThread(paintArea, graphics, renderer,
 					worldToScreen);
@@ -43,13 +63,17 @@
 
 			return true;
 		} else {
-			System.out.println("is busy... requesting stop!");
+			// System.out.println("is busy... requesting stop!");
 			renderThread.getRenderer().stopRendering();
+			return false;
 		}
-
-		return false;
 	}
 
+	/**
+	 * For every new rendering job submitted and accepted, an instance of this
+	 * {@link Thread} will be started.
+	 * 
+	 */
 	class RenderThread extends Thread {
 
 		private final GTRenderer renderer;
@@ -60,7 +84,9 @@
 			super(new RenderRun(paintArea, graphics, renderer, worldToScreen));
 			this.renderer = renderer;
 
-			System.out.println("starting render thread " + getName());
+			setName("Render" + getName());
+
+			// System.out.println("starting render thread " + getName());
 		}
 
 		public GTRenderer getRenderer() {
@@ -69,6 +95,9 @@
 
 	}
 
+	/**
+	 * This {@link Runnable} will actually start the rendering
+	 */
 	class RenderRun implements Runnable, RenderListener {
 		private final Rectangle paintArea;
 		private final Graphics2D graphics;
@@ -88,15 +117,26 @@
 			try {
 				renderer.addRenderListener(this);
 				System.out.println("start rendering...");
-				try {
-					Thread.sleep(1000);
-				} catch (InterruptedException e) {
-					// TODO Auto-generated catch block
-					e.printStackTrace();
-				}
+				// try {
+				// Thread.sleep(1000);
+				// } catch (InterruptedException e) {
+				// e.printStackTrace();
+				// }
+
+				// Clear the graphics context
+				graphics.setBackground(mapPane.getMapBackgroundColor());
+				graphics.clearRect(paintArea.x, paintArea.y, paintArea.width,
+						paintArea.height);
+
 				renderer.paint(graphics, paintArea, worldToScreen);
 
-				mapPane.onRenderingCompleted();
+				// Invoked later, so when onRenderingCompleted is called, the Thread is not running anymore (#isRunning = false)
+				SwingUtilities.invokeLater(new Runnable() {
+					@Override
+					public void run() {
+						mapPane.onRenderingCompleted();
+					};
+				});
 			} finally {
 				renderer.removeRenderListener(this);
 			}
@@ -114,6 +154,9 @@
 
 	}
 
+	/**
+	 * Ask to stop the rendering. May be called often.
+	 */
 	public void cancelTask() {
 		if (renderThread != null && renderThread.isAlive()) {
 			// System.out.println("request stop for thread " +task.getName());
@@ -121,12 +164,18 @@
 		}
 	}
 
+	/**
+	 * @return <code>true</code> if the {@link Thread} is busy rendering.
+	 */
 	public boolean isRunning() {
 		// if (task != null)
 		// System.out.println("is running "+task.getName()+" = true");
 		return (renderThread != null && renderThread.isAlive());
 	}
 
+	/**
+	 * Will stop rendering and remove the reference to the {@link Thread}.
+	 */
 	public void dispose() {
 		if (renderThread != null) {
 			renderThread.renderer.stopRendering();



More information about the Schmitzm-commits mailing list