From 06a3e8b2f4454246c4417e4365ed9806c2c6013a Mon Sep 17 00:00:00 2001
From: Michael Zangl <michael.zangl@student.kit.edu>
Date: Sat, 17 Oct 2015 16:21:08 +0200
Subject: [PATCH] Fixed map status requireing a timeout because notify was not
 received.

---
 src/org/openstreetmap/josm/gui/MapStatus.java |   67 +++++++++++++------------
 1 file changed, 36 insertions(+), 31 deletions(-)

diff --git a/src/org/openstreetmap/josm/gui/MapStatus.java b/src/org/openstreetmap/josm/gui/MapStatus.java
index e969a42..b8db1a4 100644
--- a/src/org/openstreetmap/josm/gui/MapStatus.java
+++ b/src/org/openstreetmap/josm/gui/MapStatus.java
@@ -32,6 +32,8 @@ import java.util.Collection;
 import java.util.ConcurrentModificationException;
 import java.util.List;
 import java.util.TreeSet;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
 
 import javax.swing.AbstractAction;
 import javax.swing.BorderFactory;
@@ -347,6 +349,10 @@ public class MapStatus extends JPanel implements Helpful, Destroyable, Preferenc
 
         private MapFrame parent;
 
+        private BlockingQueue<MouseState> incommingMouseState = new LinkedBlockingQueue<>();
+
+        private Point lastMousePos;
+
         Collector(MapFrame parent) {
             this.parent = parent;
         }
@@ -359,28 +365,16 @@ public class MapStatus extends JPanel implements Helpful, Destroyable, Preferenc
             registerListeners();
             try {
                 for (;;) {
+                    try {
+                        final MouseState ms = incommingMouseState.take();
+                        if (parent != Main.map)
+                            return; // exit, if new parent.
 
-                    final MouseState ms = new MouseState();
-                    synchronized (this) {
-                        // TODO Would be better if the timeout wasn't necessary
-                        try {
-                            wait(1000);
-                        } catch (InterruptedException e) {
-                            // Occurs frequently during JOSM shutdown, log set to trace only
-                            Main.trace("InterruptedException in "+MapStatus.class.getSimpleName());
+                        // Do nothing, if required data is missing
+                        if (ms.mousePos == null || mv.center == null) {
+                            continue;
                         }
-                        ms.modifiers = mouseState.modifiers;
-                        ms.mousePos = mouseState.mousePos;
-                    }
-                    if (parent != Main.map)
-                        return; // exit, if new parent.
 
-                    // Do nothing, if required data is missing
-                    if (ms.mousePos == null || mv.center == null) {
-                        continue;
-                    }
-
-                    try {
                         EventQueue.invokeAndWait(new CollectorWorker(ms));
                     } catch (InterruptedException e) {
                         // Occurs frequently during JOSM shutdown, log set to trace only
@@ -649,6 +643,23 @@ public class MapStatus extends JPanel implements Helpful, Destroyable, Preferenc
             });
             return l;
         }
+
+        /**
+         * Called whenever the mouse position or modifiers changed.
+         * @param mousePos The new mouse position. <code>null</code> if it did not change.
+         * @param modifiers The new modifiers.
+         */
+        public synchronized void updateMousePosition(Point mousePos, int modifiers) {
+            MouseState ms = new MouseState();
+            if (mousePos == null) {
+                ms.mousePos = lastMousePos;
+            } else {
+                lastMousePos = mousePos;
+            }
+            // remove mouse states that are in the queue. Our mouse state is newer.
+            incommingMouseState.clear();
+            incommingMouseState.offer(ms);
+        }
     }
 
     /**
@@ -659,10 +670,6 @@ public class MapStatus extends JPanel implements Helpful, Destroyable, Preferenc
         private Point mousePos;
         private int modifiers;
     }
-    /**
-     * The last sent mouse movement event.
-     */
-    private transient MouseState mouseState = new MouseState();
 
     private transient AWTEventListener awtListener = new AWTEventListener() {
          @Override
@@ -670,11 +677,12 @@ public class MapStatus extends JPanel implements Helpful, Destroyable, Preferenc
             if (event instanceof InputEvent &&
                     ((InputEvent) event).getComponent() == mv) {
                 synchronized (collector) {
-                    mouseState.modifiers = ((InputEvent) event).getModifiersEx();
+                    int modifiers = ((InputEvent) event).getModifiersEx();
+                    Point mousePos = null;
                     if (event instanceof MouseEvent) {
-                        mouseState.mousePos = ((MouseEvent) event).getPoint();
+                        mousePos = ((MouseEvent) event).getPoint();
                     }
-                    collector.notifyAll();
+                    collector.updateMousePosition(mousePos, modifiers);
                 }
             }
         }
@@ -684,9 +692,7 @@ public class MapStatus extends JPanel implements Helpful, Destroyable, Preferenc
         @Override
         public void mouseMoved(MouseEvent e) {
             synchronized (collector) {
-                mouseState.modifiers = e.getModifiersEx();
-                mouseState.mousePos = e.getPoint();
-                collector.notifyAll();
+                collector.updateMousePosition(e.getPoint(), e.getModifiersEx());
             }
         }
 
@@ -699,8 +705,7 @@ public class MapStatus extends JPanel implements Helpful, Destroyable, Preferenc
     private transient KeyAdapter keyAdapter = new KeyAdapter() {
         @Override public void keyPressed(KeyEvent e) {
             synchronized (collector) {
-                mouseState.modifiers = e.getModifiersEx();
-                collector.notifyAll();
+                collector.updateMousePosition(null, e.getModifiersEx());
             }
         }
 
-- 
1.7.9.5

