[Schmitzm-commits] r513 - in branches/1.0-gt2-2.6/src: gtmig/org/geotools/swing schmitzm/geotools/gui skrueger/geotools

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Nov 9 12:17:35 CET 2009


Author: alfonx
Date: 2009-11-09 12:17:34 +0100 (Mon, 09 Nov 2009)
New Revision: 513

Modified:
   branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/XMapPane.java
   branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/ZoomXMapPaneMouseListener.java
   branches/1.0-gt2-2.6/src/schmitzm/geotools/gui/GridPanel.java
   branches/1.0-gt2-2.6/src/skrueger/geotools/RenderingExecutor.java
Log:
Fixed an error in the zoom-out code of XMapPane


Modified: branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/XMapPane.java
===================================================================
--- branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/XMapPane.java	2009-11-06 15:00:17 UTC (rev 512)
+++ branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/XMapPane.java	2009-11-09 11:17:34 UTC (rev 513)
@@ -68,8 +68,6 @@
 import org.geotools.renderer.label.LabelCacheImpl;
 import org.geotools.renderer.lite.LabelCache;
 import org.geotools.swing.JMapPane;
-//import org.geotools.swing.RenderingExecutor;  // gt2-2.6.1
-import skrueger.geotools.RenderingExecutor;     // until gt2-2.6.1
 import org.geotools.swing.event.MapMouseEvent;
 import org.geotools.swing.event.MapPaneEvent;
 import org.geotools.swing.event.MapPaneListener;
@@ -86,6 +84,7 @@
 import schmitzm.geotools.map.event.JMapPaneListener;
 import schmitzm.geotools.map.event.MapLayerAdapter;
 import schmitzm.swing.SwingUtil;
+import skrueger.geotools.RenderingExecutor;
 
 import com.vividsolutions.jts.geom.Coordinate;
 import com.vividsolutions.jts.geom.Envelope;
@@ -175,9 +174,26 @@
 	 * @see #setState(int)
 	 */
 	public static final int SELECT_ONE_FROM_TOP = 104;
+//
+//	/**
+//	 * While threads are working, we call {@link XMapPane#updateFinalImage()}
+//	 * regularly and repaint(). This {@link Timer} is stopped when all renderers
+//	 * have finished.
+//	 */
+//	final private Timer repainterTimer;
+//
+//	/**
+//	 * While the {@link #bgExecuter} and {@link #localExecuter} are rendering,
+//	 * the {@link #repainterTimer} is regularly updating the {@link #finalImage}
+//	 * with previews.
+//	 */
+//	public static final long REPEATING_REPAINT_DELAY = 500;
+//	
+//	/**
+//	 * The initial delay in milliseconds until the {@link #finalImage} is updated the first time.  
+//	 */
+//	public static final long INITIAL_REPAINT_DELAY = 333;
 
-	public static final long DEFAULT_REPAINTING_DELAY = 200;
-
 	protected RenderingExecutor bgExecuter;
 
 	/**
@@ -219,7 +235,7 @@
 	/**
 	 * compass and icon are rendered into this image
 	 */
-//	protected BufferedImage gadgetsImage;
+	// protected BufferedImage gadgetsImage;
 
 	/**
 	 * The default state is ZOOM_IN, hence by default the
@@ -248,11 +264,12 @@
 
 		// aktuelle Geo-Position ueber Transformation des JMapPane berechnen
 		if (e != null && e.getSource() instanceof XMapPane) {
-			
+
 			final XMapPane xMapPane = (XMapPane) e.getSource();
-			
-			if (!xMapPane.isWellDefined()) return null;
-			
+
+			if (!xMapPane.isWellDefined())
+				return null;
+
 			AffineTransform at = xMapPane.getScreenToWorld();
 			if (at != null)
 				return at.transform(e.getPoint(), null);
@@ -297,13 +314,6 @@
 	protected boolean paneResized = false;
 
 	/**
-	 * While threads are working, we call {@link XMapPane#updateFinalImage()}
-	 * regularly and repaint(). This {@link Timer} is stoppen when all renderers
-	 * finished.
-	 */
-	final private Timer repainterTimer;
-
-	/**
 	 * The flag {@link #requestStartRendering} can be set to true by events.
 	 * This {@link Timer} checks the flag regularly and starts one renderer
 	 * thread.
@@ -405,18 +415,18 @@
 
 		});
 
-		repainterTimer = new Timer((int) DEFAULT_REPAINTING_DELAY,
-				new ActionListener() {
+//		repainterTimer = new Timer((int) REPEATING_REPAINT_DELAY,
+//				new ActionListener() {
+//
+//					@Override
+//					public void actionPerformed(ActionEvent e) {
+//						updateFinalImage();
+//						XMapPane.this.repaint();
+//					}
+//				});
+//		repainterTimer.setInitialDelay(300);
+//		repainterTimer.setRepeats(true);
 
-					@Override
-					public void actionPerformed(ActionEvent e) {
-						updateFinalImage();
-						XMapPane.this.repaint(DEFAULT_REPAINTING_DELAY);
-					}
-				});
-		repainterTimer.setInitialDelay(1000);
-		repainterTimer.setRepeats(true);
-
 		startRenderThreadsTimer = new Timer(200, new ActionListener() {
 
 			@Override
@@ -533,19 +543,19 @@
 	 * {@link XMapPane} is visible and has bounds set.
 	 */
 	public boolean isWellDefined() {
-		
+
 		try {
-			
-		if (getContext() == null)
-			return false;
-		if (getContext().getLayerCount() <= 0)
-			return false;
-		if (getMapArea() == null)
-			return false;
-		if (getBounds().getWidth() == 0)
-			return false;
-		if (getBounds().getHeight() == 0)
-			return false;
+
+			if (getContext() == null)
+				return false;
+			if (getContext().getLayerCount() <= 0)
+				return false;
+			if (getMapArea() == null)
+				return false;
+			if (getBounds().getWidth() == 0)
+				return false;
+			if (getBounds().getHeight() == 0)
+				return false;
 		} catch (Exception e) {
 			return false;
 		}
@@ -610,12 +620,12 @@
 			setCursor(WAIT_CURSOR);
 			return;
 		} else {
-			// Allow one last rendering
-			if (repainterTimer.isRunning()) {
-				repainterTimer.stop();
-				updateFinalImage();
-				repaint();
-			}
+//			// Allow one last rendering
+//			if (repainterTimer.isRunning()) {
+//				repainterTimer.stop();
+//				updateFinalImage();
+//				repaint();
+//			}
 		}
 
 		// wenn manueller Cursor gesetzt ist, dann diesen verwenden (unabhaengig
@@ -1016,8 +1026,18 @@
 			return false;
 		}
 
-		// Testen, ob der Unterschied nur im minimalen Rundungsfehlerbereich
-		// liegt
+		// Testing, whether NaN or Infinity are used in the newMapArea
+		if (newMapArea.isNull() || Double.isInfinite(newMapArea.getMaxX())
+				|| Double.isInfinite(newMapArea.getMaxY())
+				|| Double.isInfinite(newMapArea.getMinX())
+				|| Double.isInfinite(newMapArea.getMinY())) {
+			// No change.. ugly new values
+			LOGGER.warn("setMapArea has been called with newArea = "
+					+ newMapArea);
+			return false;
+		}
+
+		// Testing, whether the difference if just minimal
 		if (mapArea != null) {
 			Envelope candNew = bestAllowedMapArea(newMapArea);
 			double tolX = mapArea.getWidth() / 1000.;
@@ -1057,6 +1077,8 @@
 		mapImageInvalid = true;
 		mapAreaChanged = true;
 		repaint();
+
+		LOGGER.debug("New maparea = " + mapArea);
 		return true;
 	}
 
@@ -1145,7 +1167,7 @@
 				finalImage = null;
 				localImage = null;
 				bgImage = null;
-//				gadgetsImage = null;
+				// gadgetsImage = null;
 			}
 
 			// Start the Threads and Timers to render the image
@@ -1274,14 +1296,14 @@
 				(int) visibleArea.getMinX(), (int) visibleArea.getMinY(),
 				(int) visibleArea.getMaxX(), (int) visibleArea.getMaxY(),
 				getMapBackgroundColor(), null);
-		
 
 		Rectangle painedArea = new Rectangle(xx1, yy1, xx2 - xx1, yy2 - yy1);
 
 		SwingUtil.clearAround(graphics, painedArea, visibleArea);
-		
+
 		addGadgets(graphics);
-//		graphics.drawImage(getGadgetsImage(), 0,0, (int) visibleArea.getMaxX(), (int) visibleArea.getMaxY(),null);
+		// graphics.drawImage(getGadgetsImage(), 0,0, (int)
+		// visibleArea.getMaxX(), (int) visibleArea.getMaxY(),null);
 
 		quickPreviewHint = 0;
 
@@ -1289,28 +1311,29 @@
 		// Something has been drawn
 		return true;
 	}
-	
+
 	final static Font waitFont = new Font("Arial", Font.BOLD, 30);
 
 	/**
-	 * Paints some optinal stuff into the given {@link Graphics2D}. Usually called as the last paint on the mapImage.
+	 * Paints some optinal stuff into the given {@link Graphics2D}. Usually
+	 * called as the last paint on the mapImage.
 	 */
 	private void addGadgets(Graphics2D graphics) {
 
 		if (mapImage != null)
-			graphics.drawImage(mapImage,
-					getBounds().width - mapImage.getWidth() - 10,
-					getBounds().height - mapImage.getHeight() - 10, this);
+			graphics.drawImage(mapImage, getBounds().width
+					- mapImage.getWidth() - 10, getBounds().height
+					- mapImage.getHeight() - 10, this);
 
 		// If still rendering, paint a gray shadow or so...
 		if (bgExecuter != null && bgExecuter.isRunning()
 				|| localExecuter != null && localExecuter.isRunning()) {
 			graphics.setColor(Color.BLACK);
-			
+
 			graphics.setFont(waitFont);
 			graphics.drawString("Wait...", 40, 70);
 		}
-		
+
 	}
 
 	/**
@@ -1322,20 +1345,20 @@
 
 		final Graphics2D finalG = (Graphics2D) getFinalImage().getGraphics();
 		finalG.setBackground(getMapBackgroundColor());
-		
-		
+
 		// Render the two map images first, into the preFinalImage
 		{
-			final Graphics2D preFinalG = (Graphics2D) getPreFinalImage().getGraphics();
+			final Graphics2D preFinalG = (Graphics2D) getPreFinalImage()
+					.getGraphics();
 			preFinalG.setBackground(getMapBackgroundColor());
-			
-			preFinalG.drawImage(getBgImage(), 0, 0,
-					getMapBackgroundColor(), null);
+
+			preFinalG.drawImage(getBgImage(), 0, 0, getMapBackgroundColor(),
+					null);
 			// // Draw the local layers image
 			preFinalG.drawImage(getLocalImage(), 0, 0, null);
 			preFinalG.dispose();
 		}
-		
+
 		finalG.drawImage(getPreFinalImage(), imageOrigin.x, imageOrigin.y,
 				getMapBackgroundColor(), null);
 
@@ -1360,7 +1383,7 @@
 		SwingUtil.clearAround(finalG, painedArea, getVisibleRect());
 
 		addGadgets(finalG);
-//		finalG.drawImage(getGadgetsImage(), 0, 0, null);
+		// finalG.drawImage(getGadgetsImage(), 0, 0, null);
 
 		finalG.dispose();
 
@@ -1388,16 +1411,16 @@
 		}
 		return finalImage;
 	}
-	
+
 	private Image getPreFinalImage() {
-		
+
 		if (preFinalImage == null) {
 			preFinalImage = null;
 			Rectangle curPaintArea = getVisibleRect();
 
 			preFinalImage = new BufferedImage(curPaintArea.width,
 					curPaintArea.height, BufferedImage.TYPE_INT_RGB);
-			
+
 			requestStartRendering();
 		}
 		return preFinalImage;
@@ -1464,7 +1487,7 @@
 		 */
 
 		if (getBgContext() != null) {
-			bgExecuter = new RenderingExecutor(this);
+			bgExecuter = new RenderingExecutor(this, 333l);
 			LOGGER.debug("starting bg renderer:");
 			// /* System.out.println("rendering"); */
 			final GTRenderer createGTRenderer = GTUtil.createGTRenderer(
@@ -1475,7 +1498,7 @@
 		}
 
 		if (getContext() != null) {
-			localExecuter = new RenderingExecutor(this);
+			localExecuter = new RenderingExecutor(this, 150l);
 			LOGGER.debug("starting local renderer:");
 			final GTRenderer createGTRenderer = GTUtil.createGTRenderer(
 					localContext, getRendererHints());
@@ -1486,11 +1509,11 @@
 		}
 
 		updateCursorAndRepaintTimer();
+//
+//		// start regular repaints until all renderers are done.
+//		repainterTimer.setRepeats(true);
+//		repainterTimer.restart();
 
-		// start regular repaints until all renderers are done.
-		repainterTimer.setRepeats(true);
-		repainterTimer.restart();
-
 	}
 
 	/**
@@ -1536,7 +1559,7 @@
 	 */
 	public void setMapImage(BufferedImage mapImage) {
 		this.mapImage = mapImage;
-//		gadgetsImage = null;
+		// gadgetsImage = null;
 	}
 
 	/**
@@ -1558,33 +1581,34 @@
 
 		return localImage;
 	}
-//
-//	/**
-//	 * Lazyly initializes a {@link BufferedImage} for the background renderer.
-//	 */
-//	private BufferedImage getGadgetsImage() {
-//
-//		if (gadgetsImage == null) {
-//			LOGGER.debug("creating a new gadgets image");
-//
-//			Rectangle curPaintArea = getVisibleRect();
-//			// allow a single pixel margin at the right and bottom edges
-//			curPaintArea.width -= 1;
-//			curPaintArea.height -= 1;
-//
-//			gadgetsImage = new BufferedImage(curPaintArea.width + 1,
-//					curPaintArea.height + 1, BufferedImage.TYPE_INT_ARGB);
-//
-//			if (mapImage != null)
-//				gadgetsImage.getGraphics().drawImage(mapImage,
-//						curPaintArea.width - mapImage.getWidth() - 10,
-//						curPaintArea.height - mapImage.getHeight() - 10, this);
-//			
-//		}
-//
-//		return gadgetsImage;
-//	}
 
+	//
+	// /**
+	// * Lazyly initializes a {@link BufferedImage} for the background renderer.
+	// */
+	// private BufferedImage getGadgetsImage() {
+	//
+	// if (gadgetsImage == null) {
+	// LOGGER.debug("creating a new gadgets image");
+	//
+	// Rectangle curPaintArea = getVisibleRect();
+	// // allow a single pixel margin at the right and bottom edges
+	// curPaintArea.width -= 1;
+	// curPaintArea.height -= 1;
+	//
+	// gadgetsImage = new BufferedImage(curPaintArea.width + 1,
+	// curPaintArea.height + 1, BufferedImage.TYPE_INT_ARGB);
+	//
+	// if (mapImage != null)
+	// gadgetsImage.getGraphics().drawImage(mapImage,
+	// curPaintArea.width - mapImage.getWidth() - 10,
+	// curPaintArea.height - mapImage.getHeight() - 10, this);
+	//			
+	// }
+	//
+	// return gadgetsImage;
+	// }
+
 	/**
 	 * Called by the {@linkplain XMapPane.RenderingTask} when rendering has been
 	 * completed Publishes a {@linkplain MapPaneEvent} of type {@code
@@ -2021,14 +2045,14 @@
 		}
 
 		startRenderThreadsTimer.stop();
-		repainterTimer.stop();
+//		repainterTimer.stop();
 
 		if (bgImage != null)
 			bgImage.flush();
 		if (localImage != null)
 			localImage.flush();
-//		if (gadgetsImage != null)
-//			gadgetsImage.flush();
+		// if (gadgetsImage != null)
+		// gadgetsImage.flush();
 		if (finalImage != null)
 			finalImage.flush();
 		if (preFinalImage != null)
@@ -2088,27 +2112,26 @@
 
 		Point2D gcenter = getScreenToWorld().transform(center, null);
 		center = null;
-		
-		if (
-				Double.isNaN(gcenter.getX()) || Double.isNaN(gcenter.getY())
-				||
-				Double.isInfinite(gcenter.getX()) || Double.isInfinite(gcenter.getY())
-				
+
+		if (Double.isNaN(gcenter.getX()) || Double.isNaN(gcenter.getY())
+				|| Double.isInfinite(gcenter.getX())
+				|| Double.isInfinite(gcenter.getY())
+
 		) {
 			// Not inside valid CRS area! cancel
 			return;
 		}
 
 		final Envelope mapArea = getMapArea();
-		
+
 		Envelope newMapArea = new Envelope(mapArea);
-		newMapArea
-				.expandBy((mapArea.getWidth() * zoomFaktor - mapArea
-						.getWidth()) / 2., (mapArea.getHeight()
-						* zoomFaktor - mapArea.getHeight()) / 2.);
+		newMapArea.expandBy((mapArea.getWidth() * zoomFaktor - mapArea
+				.getWidth()) / 2., (mapArea.getHeight() * zoomFaktor - mapArea
+				.getHeight()) / 2.);
 
 		// Move the newMapArea above the new center
-		newMapArea.translate(gcenter.getX() - mapArea.centre().x, gcenter.getY()
+		newMapArea.translate(gcenter.getX() - mapArea.centre().x, gcenter
+				.getY()
 				- mapArea.centre().y);
 
 		setMapArea(newMapArea);
@@ -2233,7 +2256,7 @@
 		imageOrigin.x = 0;
 		imageOrigin.y = 0;
 
-		if (!setMapArea(newMapArea)){
+		if (!setMapArea(newMapArea)) {
 			updateFinalImage();
 			repaint();
 		}
@@ -2242,4 +2265,10 @@
 			setCursor(SwingUtil.PAN_CURSOR);
 	}
 
+	public void onRenderingPending() {
+		LOGGER.debug("Pending rendering updates the preview...");
+		updateFinalImage();
+		XMapPane.this.repaint();
+	}
+
 }

Modified: branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/ZoomXMapPaneMouseListener.java
===================================================================
--- branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/ZoomXMapPaneMouseListener.java	2009-11-06 15:00:17 UTC (rev 512)
+++ branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/ZoomXMapPaneMouseListener.java	2009-11-09 11:17:34 UTC (rev 513)
@@ -97,9 +97,12 @@
 
 			drawRectangle(xMapPane.getGraphics(), e.getPoint());
 
-			// Don't zoom too small areas
-			if ((Math.abs(startPos.x - e.getPoint().x) * Math.abs(e.getPoint().y - startPos.y)) < 160)
+
+			// Don't zoom too small areas. Handle it like a click then
+			if ((Math.abs(startPos.x - e.getPoint().x) * Math.abs(e.getPoint().y - startPos.y)) < 160) {
+				xMapPane.zoomTo(e.getPoint(), .5);
 				return;
+			}
 
 			final double left = Math.min(startX, endX);
 			final double right = Math.max(startX, endX);
@@ -114,7 +117,15 @@
 
 			// Remove the rectangle
 			drawRectangle(xMapPane.getGraphics(), e.getPoint());
+			
+			
 
+			// Don't zoom too small areas
+			if ((Math.abs(startPos.x - e.getPoint().x) * Math.abs(e.getPoint().y - startPos.y)) < 100) {
+				xMapPane.zoomTo(e.getPoint(), 2.);
+				return;
+			}
+
 			// make the dragged rectangle in screen coords the new map size?
 			final double left = Math.min(startX, endX);
 			final double right = Math.max(startX, endX);

Modified: branches/1.0-gt2-2.6/src/schmitzm/geotools/gui/GridPanel.java
===================================================================
--- branches/1.0-gt2-2.6/src/schmitzm/geotools/gui/GridPanel.java	2009-11-06 15:00:17 UTC (rev 512)
+++ branches/1.0-gt2-2.6/src/schmitzm/geotools/gui/GridPanel.java	2009-11-09 11:17:34 UTC (rev 513)
@@ -213,8 +213,8 @@
     if ( getParent() == null || !mapPane.isWellDefined())
       return;
 
-    CoordinateReferenceSystem mapCRS = null;   // (aktuelles) CRS des MapPane
-    CoordinateReferenceSystem gridCRS = null;  // CRS fuer die Grad/Min/Sek-Anzeige
+    CoordinateReferenceSystem mapCRS = mapPane.getContext().getCoordinateReferenceSystem();   // (aktuelles) CRS des MapPane
+    CoordinateReferenceSystem gridCRS = formatter.getCRS();  // CRS fuer die Grad/Min/Sek-Anzeige
 //    try {
 //      final CoordinateReferenceSystem UTM   = CRS.decode("EPSG:32631"); // utm zone 31n
 //      final CoordinateReferenceSystem WGS84 = CRS.decode("EPSG:4326"); // lat/lon (WGS84)
@@ -227,8 +227,7 @@
 //      return;
 //    }
 
-    mapCRS  = mapPane.getContext().getCoordinateReferenceSystem();
-    gridCRS = formatter.getCRS();// lat/lon
+//    gridCRS = // lat/lon
 //    LOGGER.debug("mapCRS  = "+LangUtil.getSimpleClassName(mapCRS));
 //    LOGGER.debug("gridCRS = "+LangUtil.getSimpleClassName(gridCRS));
 //    LOGGER.debug("mapCRS: "+mapCRS);

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-06 15:00:17 UTC (rev 512)
+++ branches/1.0-gt2-2.6/src/skrueger/geotools/RenderingExecutor.java	2009-11-09 11:17:34 UTC (rev 513)
@@ -81,8 +81,8 @@
     private final ExecutorService taskExecutor;
     private final ScheduledExecutorService watchExecutor;
 
-    /** The default interval (milliseconds) for polling the result of a rendering task */
-    public static final long DEFAULT_POLLING_INTERVAL = 100L;
+//    /** The default interval (milliseconds) for polling the result of a rendering task */
+//    public static final long DEFAULT_POLLING_INTERVAL = 300L;
 
     private long pollingInterval;
 
@@ -218,13 +218,14 @@
      * Constructor. Creates a new executor to service the specified map pane.
      *
      * @param mapPane the map pane to be serviced
+     * @param l 
      */
-    public RenderingExecutor(final XMapPane mapPane) {
+	public RenderingExecutor(final XMapPane mapPane, long pollingInterval) {
         taskRunning = new AtomicBoolean(false);
         this.mapPane = mapPane;
         taskExecutor = Executors.newSingleThreadExecutor();
         watchExecutor = Executors.newSingleThreadScheduledExecutor();
-        pollingInterval = DEFAULT_POLLING_INTERVAL;
+        this.pollingInterval = pollingInterval;
         cancelLatch = new CountDownLatch(0);
     }
 
@@ -276,7 +277,7 @@
                 public void run() {
                     pollTaskResult();
                 }
-            }, DEFAULT_POLLING_INTERVAL, DEFAULT_POLLING_INTERVAL, TimeUnit.MILLISECONDS);
+            }, pollingInterval, pollingInterval, TimeUnit.MILLISECONDS);
 
             return true;
         }
@@ -323,6 +324,11 @@
             case FAILED:
                 mapPane.onRenderingFailed(renderingException);
                 break;
+                
+            case PENDING:
+                mapPane.onRenderingPending();
+                break;
+                
         }
     }
 



More information about the Schmitzm-commits mailing list