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
|
b
|
import java.util.Collection;
|
32 | 32 | import java.util.ConcurrentModificationException; |
33 | 33 | import java.util.List; |
34 | 34 | import java.util.TreeSet; |
| 35 | import java.util.concurrent.BlockingQueue; |
| 36 | import java.util.concurrent.LinkedBlockingQueue; |
35 | 37 | |
36 | 38 | import javax.swing.AbstractAction; |
37 | 39 | import javax.swing.BorderFactory; |
… |
… |
public class MapStatus extends JPanel implements Helpful, Destroyable, Preferenc
|
347 | 349 | |
348 | 350 | private MapFrame parent; |
349 | 351 | |
| 352 | private BlockingQueue<MouseState> incommingMouseState = new LinkedBlockingQueue<>(); |
| 353 | |
| 354 | private Point lastMousePos; |
| 355 | |
350 | 356 | Collector(MapFrame parent) { |
351 | 357 | this.parent = parent; |
352 | 358 | } |
… |
… |
public class MapStatus extends JPanel implements Helpful, Destroyable, Preferenc
|
359 | 365 | registerListeners(); |
360 | 366 | try { |
361 | 367 | for (;;) { |
| 368 | try { |
| 369 | final MouseState ms = incommingMouseState.take(); |
| 370 | if (parent != Main.map) |
| 371 | return; // exit, if new parent. |
362 | 372 | |
363 | | final MouseState ms = new MouseState(); |
364 | | synchronized (this) { |
365 | | // TODO Would be better if the timeout wasn't necessary |
366 | | try { |
367 | | wait(1000); |
368 | | } catch (InterruptedException e) { |
369 | | // Occurs frequently during JOSM shutdown, log set to trace only |
370 | | Main.trace("InterruptedException in "+MapStatus.class.getSimpleName()); |
| 373 | // Do nothing, if required data is missing |
| 374 | if (ms.mousePos == null || mv.center == null) { |
| 375 | continue; |
371 | 376 | } |
372 | | ms.modifiers = mouseState.modifiers; |
373 | | ms.mousePos = mouseState.mousePos; |
374 | | } |
375 | | if (parent != Main.map) |
376 | | return; // exit, if new parent. |
377 | 377 | |
378 | | // Do nothing, if required data is missing |
379 | | if (ms.mousePos == null || mv.center == null) { |
380 | | continue; |
381 | | } |
382 | | |
383 | | try { |
384 | 378 | EventQueue.invokeAndWait(new CollectorWorker(ms)); |
385 | 379 | } catch (InterruptedException e) { |
386 | 380 | // Occurs frequently during JOSM shutdown, log set to trace only |
… |
… |
public class MapStatus extends JPanel implements Helpful, Destroyable, Preferenc
|
649 | 643 | }); |
650 | 644 | return l; |
651 | 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Called whenever the mouse position or modifiers changed. |
| 649 | * @param mousePos The new mouse position. <code>null</code> if it did not change. |
| 650 | * @param modifiers The new modifiers. |
| 651 | */ |
| 652 | public synchronized void updateMousePosition(Point mousePos, int modifiers) { |
| 653 | MouseState ms = new MouseState(); |
| 654 | if (mousePos == null) { |
| 655 | ms.mousePos = lastMousePos; |
| 656 | } else { |
| 657 | lastMousePos = mousePos; |
| 658 | } |
| 659 | // remove mouse states that are in the queue. Our mouse state is newer. |
| 660 | incommingMouseState.clear(); |
| 661 | incommingMouseState.offer(ms); |
| 662 | } |
652 | 663 | } |
653 | 664 | |
654 | 665 | /** |
… |
… |
public class MapStatus extends JPanel implements Helpful, Destroyable, Preferenc
|
659 | 670 | private Point mousePos; |
660 | 671 | private int modifiers; |
661 | 672 | } |
662 | | /** |
663 | | * The last sent mouse movement event. |
664 | | */ |
665 | | private transient MouseState mouseState = new MouseState(); |
666 | 673 | |
667 | 674 | private transient AWTEventListener awtListener = new AWTEventListener() { |
668 | 675 | @Override |
… |
… |
public class MapStatus extends JPanel implements Helpful, Destroyable, Preferenc
|
670 | 677 | if (event instanceof InputEvent && |
671 | 678 | ((InputEvent) event).getComponent() == mv) { |
672 | 679 | synchronized (collector) { |
673 | | mouseState.modifiers = ((InputEvent) event).getModifiersEx(); |
| 680 | int modifiers = ((InputEvent) event).getModifiersEx(); |
| 681 | Point mousePos = null; |
674 | 682 | if (event instanceof MouseEvent) { |
675 | | mouseState.mousePos = ((MouseEvent) event).getPoint(); |
| 683 | mousePos = ((MouseEvent) event).getPoint(); |
676 | 684 | } |
677 | | collector.notifyAll(); |
| 685 | collector.updateMousePosition(mousePos, modifiers); |
678 | 686 | } |
679 | 687 | } |
680 | 688 | } |
… |
… |
public class MapStatus extends JPanel implements Helpful, Destroyable, Preferenc
|
684 | 692 | @Override |
685 | 693 | public void mouseMoved(MouseEvent e) { |
686 | 694 | synchronized (collector) { |
687 | | mouseState.modifiers = e.getModifiersEx(); |
688 | | mouseState.mousePos = e.getPoint(); |
689 | | collector.notifyAll(); |
| 695 | collector.updateMousePosition(e.getPoint(), e.getModifiersEx()); |
690 | 696 | } |
691 | 697 | } |
692 | 698 | |
… |
… |
public class MapStatus extends JPanel implements Helpful, Destroyable, Preferenc
|
699 | 705 | private transient KeyAdapter keyAdapter = new KeyAdapter() { |
700 | 706 | @Override public void keyPressed(KeyEvent e) { |
701 | 707 | synchronized (collector) { |
702 | | mouseState.modifiers = e.getModifiersEx(); |
703 | | collector.notifyAll(); |
| 708 | collector.updateMousePosition(null, e.getModifiersEx()); |
704 | 709 | } |
705 | 710 | } |
706 | 711 | |