Ticket #11636: 0010-Moved-cursor-management-to-new-class-and-used-java-l.patch

File 0010-Moved-cursor-management-to-new-class-and-used-java-l.patch, 6.9 KB (added by michael2402, 11 years ago)
  • src/org/openstreetmap/josm/gui/NavigatableComponent.java

    From 9030153876f46cedbc9639fc7aa19c771655c34c Mon Sep 17 00:00:00 2001
    From: Michael Zangl <michael.zangl@student.kit.edu>
    Date: Wed, 1 Jul 2015 17:03:30 +0200
    Subject: [PATCH 10/11] Moved cursor management to new class and used java
     library fuctnions for the cursor stack
    
    ---
     .../josm/gui/NavigatableComponent.java             | 52 +++----------
     .../josm/gui/navigate/NavigationCursorManager.java | 87 ++++++++++++++++++++++
     2 files changed, 98 insertions(+), 41 deletions(-)
     create mode 100644 src/org/openstreetmap/josm/gui/navigate/NavigationCursorManager.java
    
    diff --git a/src/org/openstreetmap/josm/gui/NavigatableComponent.java b/src/org/openstreetmap/josm/gui/NavigatableComponent.java
    index c5505f9..6bd0808 100644
    a b import org.openstreetmap.josm.gui.download.DownloadDialog;  
    5151import org.openstreetmap.josm.gui.help.Helpful;
    5252import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
    5353import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
     54import org.openstreetmap.josm.gui.navigate.NavigationCursorManager;
    5455import org.openstreetmap.josm.tools.Predicate;
    5556import org.openstreetmap.josm.tools.Utils;
    5657
    public class NavigatableComponent extends JComponent implements Helpful {  
    186187
    187188    protected transient ViewportData initialViewport;
    188189
     190    protected transient final NavigationCursorManager cursorManager = new NavigationCursorManager(this);
     191
    189192    /**
    190193     * Constructs a new {@code NavigatableComponent}.
    191194     */
    public class NavigatableComponent extends JComponent implements Helpful {  
    14351438        return (int) id.getValue();
    14361439    }
    14371440
    1438     private static class CursorInfo {
    1439         private final Cursor cursor;
    1440         private final Object object;
    1441 
    1442         public CursorInfo(Cursor c, Object o) {
    1443             cursor = c;
    1444             object = o;
    1445         }
    1446     }
    1447 
    1448     private LinkedList<CursorInfo> cursors = new LinkedList<>();
    1449 
    14501441    /**
    14511442     * Set new cursor.
    14521443     */
    14531444    public void setNewCursor(Cursor cursor, Object reference) {
    1454         if (!cursors.isEmpty()) {
    1455             CursorInfo l = cursors.getLast();
    1456             if (l != null && l.cursor == cursor && l.object == reference)
    1457                 return;
    1458             stripCursors(reference);
    1459         }
    1460         cursors.add(new CursorInfo(cursor, reference));
    1461         setCursor(cursor);
     1445        cursorManager.setNewCursor(cursor, reference);
    14621446    }
    14631447
    14641448    public void setNewCursor(int cursor, Object reference) {
    public class NavigatableComponent extends JComponent implements Helpful {  
    14691453     * Remove the new cursor and reset to previous
    14701454     */
    14711455    public void resetCursor(Object reference) {
    1472         if (cursors.isEmpty()) {
    1473             setCursor(null);
    1474             return;
    1475         }
    1476         CursorInfo l = cursors.getLast();
    1477         stripCursors(reference);
    1478         if (l != null && l.object == reference) {
    1479             if (cursors.isEmpty()) {
    1480                 setCursor(null);
    1481             } else {
    1482                 setCursor(cursors.getLast().cursor);
    1483             }
    1484         }
     1456        cursorManager.resetCursor(reference);
    14851457    }
    14861458
    1487     private void stripCursors(Object reference) {
    1488         LinkedList<CursorInfo> c = new LinkedList<>();
    1489         for (CursorInfo i : cursors) {
    1490             if (i.object != reference) {
    1491                 c.add(i);
    1492             }
    1493         }
    1494         cursors = c;
     1459    /**
     1460     * Gets the cursor manager that is used for this NavigatableComponent.
     1461     * @return The cursor manager.
     1462     */
     1463    public NavigationCursorManager getCursorManager() {
     1464        return cursorManager;
    14951465    }
    14961466
    14971467    @Override
  • new file src/org/openstreetmap/josm/gui/navigate/NavigationCursorManager.java

    diff --git a/src/org/openstreetmap/josm/gui/navigate/NavigationCursorManager.java b/src/org/openstreetmap/josm/gui/navigate/NavigationCursorManager.java
    new file mode 100644
    index 0000000..b2a810b
    - +  
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.gui.navigate;
     3
     4import java.awt.Component;
     5import java.awt.Cursor;
     6import java.util.Iterator;
     7import java.util.LinkedHashMap;
     8import java.util.concurrent.CopyOnWriteArrayList;
     9
     10/**
     11 * This class manages multiple cursors for multiple components. All components share the same cursor that was last set using {@link #setNewCursor(Cursor, Object)}
     12 * @author Michael Zangl
     13 */
     14public class NavigationCursorManager {
     15
     16    private final LinkedHashMap<Object, Cursor> cursors = new LinkedHashMap<>();
     17    private final CopyOnWriteArrayList<Component> components = new CopyOnWriteArrayList<>();
     18
     19    /**
     20     * Creates a new NavigationCursorManager
     21     * @param forComponent The initial component the cursor should be managed for.
     22     */
     23    public NavigationCursorManager(Component forComponent) {
     24        addComponent(forComponent);
     25    }
     26
     27    /**
     28     * Adds a component that this manager should send cursor changes to.
     29     * @param forComponent The component.
     30     */
     31    public synchronized void addComponent(Component forComponent) {
     32        components.addIfAbsent(forComponent);
     33        forComponent.setCursor(getCurrentCursor());
     34    }
     35
     36    /**
     37     * Removes a component that this manager should send cursor changes to. The current cursor is not reset.
     38     * @param forComponent The component.
     39     */
     40    public synchronized void removeComponent(Component forComponent) {
     41        components.remove(forComponent);
     42    }
     43
     44    /**
     45     * Set new cursor.
     46     * @param cursor The new cursor to use.
     47     * @param reference A reference object that can be passed to the next set/reset calls to identify the caller.
     48     */
     49    public synchronized void setNewCursor(Cursor cursor, Object reference) {
     50        if (reference == null) {
     51            throw new NullPointerException("Cannot register a cursor that can never be removed.");
     52        }
     53        // re-insert to allow overriding.
     54        cursors.remove(reference);
     55        cursors.put(reference, cursor);
     56        updateCursor();
     57    }
     58
     59    /**
     60     * Remove the new cursor that was set with the given reference object. and reset to previous
     61     * @param reference A reference object that can be passed to the next set/reset calls to identify the caller.
     62     */
     63    public synchronized void resetCursor(Object reference) {
     64        if (reference == null) {
     65            return;
     66        }
     67        cursors.remove(reference);
     68        updateCursor();
     69    }
     70
     71    private void updateCursor() {
     72        Cursor cursor = getCurrentCursor();
     73        for (Component c : components) {
     74            c.setCursor(cursor);
     75        }
     76    }
     77
     78    private Cursor getCurrentCursor() {
     79        Iterator<Cursor> it = cursors.values().iterator();
     80        Cursor cursor = null;
     81        while (it.hasNext()) {
     82            cursor = it.next();
     83        }
     84        return cursor;
     85    }
     86
     87}