From 457328d0a8a99fb17135ee910822c1399ca9ccb6 Mon Sep 17 00:00:00 2001
From: Michael Zangl <michael.zangl@student.kit.edu>
Date: Wed, 1 Jul 2015 14:44:35 +0200
Subject: [PATCH 7/8] MapView: Made paint() method shorter by moving draw
 prearation to a new method.

---
 src/org/openstreetmap/josm/gui/MapView.java | 61 ++++++++++++++++++-----------
 1 file changed, 39 insertions(+), 22 deletions(-)

diff --git a/src/org/openstreetmap/josm/gui/MapView.java b/src/org/openstreetmap/josm/gui/MapView.java
index 4e2dee0..b2129bc 100644
--- a/src/org/openstreetmap/josm/gui/MapView.java
+++ b/src/org/openstreetmap/josm/gui/MapView.java
@@ -575,6 +575,11 @@ implements PropertyChangeListener, PreferenceChangedListener, OsmDataLayer.Layer
         }
     }
 
+    /**
+     * Checks if virtual nodes should be drawn. Default is <code>false</code>
+     * @return The virtual nodes property.
+     * @see Rendering#render(DataSet, boolean, Bounds)
+     */
     public boolean isVirtualNodesEnabled() {
         return virtualNodesEnabled;
     }
@@ -688,25 +693,8 @@ implements PropertyChangeListener, PreferenceChangedListener, OsmDataLayer.Layer
      */
     @Override
     public void paint(Graphics g) {
-        if (initialViewport != null) {
-            zoomTo(initialViewport);
-            initialViewport = null;
-        }
-        if (BugReportExceptionHandler.exceptionHandlingInProgress())
+        if (!prepareToDraw()) {
             return;
-
-        if (center == null)
-            return; // no data loaded yet.
-
-        // if the position was remembered, we need to adjust center once before repainting
-        if (oldLoc != null && oldSize != null) {
-            Point l1  = getLocationOnScreen();
-            final EastNorth newCenter = new EastNorth(
-                    center.getX()+ (l1.x-oldLoc.x - (oldSize.width-getWidth())/2.0)*getScale(),
-                    center.getY()+ (oldLoc.y-l1.y + (oldSize.height-getHeight())/2.0)*getScale()
-                    );
-            oldLoc = null; oldSize = null;
-            zoomTo(newCenter);
         }
 
         List<Layer> visibleLayers = getVisibleLayersInZOrder();
@@ -801,22 +789,22 @@ implements PropertyChangeListener, PreferenceChangedListener, OsmDataLayer.Layer
 
         path.moveTo(p.x, p.y);
         double max = b.getMax().lat();
-        for (; lat <= max; lat += 1.0) {
+        for(; lat <= max; lat += 1.0) {
             p = getPoint(new LatLon(lat >= max ? max : lat, lon));
             path.lineTo(p.x, p.y);
         }
         lat = max; max = b.getMax().lon();
-        for (; lon <= max; lon += 1.0) {
+        for(; lon <= max; lon += 1.0) {
             p = getPoint(new LatLon(lat, lon >= max ? max : lon));
             path.lineTo(p.x, p.y);
         }
         lon = max; max = b.getMinLat();
-        for (; lat >= max; lat -= 1.0) {
+        for(; lat >= max; lat -= 1.0) {
             p = getPoint(new LatLon(lat <= max ? max : lat, lon));
             path.lineTo(p.x, p.y);
         }
         lat = max; max = b.getMinLon();
-        for (; lon >= max; lon -= 1.0) {
+        for(; lon >= max; lon -= 1.0) {
             p = getPoint(new LatLon(lat, lon <= max ? max : lon));
             path.lineTo(p.x, p.y);
         }
@@ -845,6 +833,35 @@ implements PropertyChangeListener, PreferenceChangedListener, OsmDataLayer.Layer
     }
 
     /**
+     * Sets up the viewport to prepare for drawing the view.
+     * @return <code>true</code> if the view can be drawn, <code>false</code> otherwise.
+     */
+    public boolean prepareToDraw() {
+        if (initialViewport != null) {
+            zoomTo(initialViewport);
+            initialViewport = null;
+        }
+        if (BugReportExceptionHandler.exceptionHandlingInProgress())
+            return false;
+
+        if (getCenter() == null)
+            return false; // no data loaded yet.
+
+        // if the position was remembered, we need to adjust center once before repainting
+        if (oldLoc != null && oldSize != null) {
+            Point l1  = getLocationOnScreen();
+            final EastNorth newCenter = new EastNorth(
+                    getCenter().getX()+ (l1.x-oldLoc.x - (oldSize.width-getWidth())/2.0)*getScale(),
+                    getCenter().getY()+ (oldLoc.y-l1.y + (oldSize.height-getHeight())/2.0)*getScale()
+                    );
+            oldLoc = null; oldSize = null;
+            zoomTo(newCenter);
+        }
+
+        return true;
+    }
+
+    /**
      * @return An unmodifiable collection of all layers
      */
     public Collection<Layer> getAllLayers() {
-- 
1.9.1

