Changeset 3919 in josm


Ignore:
Timestamp:
Feb 20, 2011 3:08:35 PM (2 years ago)
Author:
stoecker
Message:

unify cursor handling, hopefully fix #5381

Location:
trunk/src/org/openstreetmap/josm
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/ImageryAdjustAction.java

    r3847 r3919  
    116116        if (layer.isVisible()) { 
    117117            prevEastNorth=Main.map.mapView.getEastNorth(e.getX(),e.getY()); 
    118             Main.map.mapView.setCursor 
    119             (Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); 
     118            Main.map.mapView.setNewCursor(Cursor.MOVE_CURSOR, this); 
    120119        } 
    121120    } 
     
    137136    @Override public void mouseReleased(MouseEvent e) { 
    138137        Main.map.mapView.repaint(); 
    139         Main.map.mapView.setCursor(Cursor.getDefaultCursor()); 
     138        Main.map.mapView.resetCursor(this); 
    140139        prevEastNorth = null; 
    141140    } 
  • trunk/src/org/openstreetmap/josm/actions/mapmode/DeleteAction.java

    r3835 r3919  
    5353 */ 
    5454public class DeleteAction extends MapMode implements AWTEventListener { 
    55     //private boolean drawTargetHighlight; 
    56     private boolean drawTargetCursor; 
    57     //private Collection<? extends OsmPrimitive> oldPrims = null; 
    58  
    5955    // Cache previous mouse event (needed when only the modifier keys are 
    6056    // pressed but the mouse isn't moved) 
     
    8076        } 
    8177    } 
    82     private DeleteMode currentMode = DeleteMode.none; 
    8378 
    8479    private static class DeleteParameters { 
     
    105100        if (!isEnabled()) 
    106101            return; 
    107         //drawTargetHighlight = Main.pref.getBoolean("draw.target-highlight", true); 
    108         drawTargetCursor = Main.pref.getBoolean("draw.target-cursor", true); 
    109102 
    110103        Main.map.mapView.addMouseListener(this); 
     
    116109            System.out.println(ex); 
    117110        } 
    118  
    119         currentMode = DeleteMode.none; 
    120111    } 
    121112 
     
    189180            return; 
    190181 
    191         // Clean old highlights 
    192         //cleanOldHighlights(); 
    193  
    194182        DeleteParameters parameters = getDeleteParameters(e, modifiers); 
    195         setCursor(parameters.mode); 
    196  
    197         // Needs to implement WaySegment highlight first 
    198         /*if(drawTargetHighlight) { 
    199             // Add new highlights 
    200             for(OsmPrimitive p : prims) { 
    201                 p.highlighted = true; 
    202             } 
    203             oldPrims = prims; 
    204         }*/ 
    205  
    206         // We only need to repaint if the highlights changed 
    207         //Main.map.mapView.repaint(); 
    208     } 
    209  
    210     /** 
    211      * Small helper function that cleans old highlights 
    212      */ 
    213     /*private void cleanOldHighlights() { 
    214         if(oldPrims == null) 
    215             return; 
    216         for(OsmPrimitive p: oldPrims) { 
    217             p.highlighted = false; 
    218         } 
    219     }*/ 
     183        Main.map.mapView.setNewCursor(parameters.mode.cursor(), this); 
     184    } 
    220185 
    221186    /** 
     
    338303 
    339304    /** 
    340      * This function sets the given cursor in a safe way. This implementation 
    341      * differs from the on in DrawAction (it is favorable, too). 
    342      * FIXME: Update DrawAction to use this "setCursor-style" and move function 
    343      * to MapMode. 
    344      * @param c 
    345      */ 
    346     private void setCursor(final DeleteMode c) { 
    347         if(currentMode.equals(c) || (!drawTargetCursor && currentMode.equals(DeleteMode.none))) 
    348             return; 
    349         // We invoke this to prevent strange things from happening 
    350         EventQueue.invokeLater(new Runnable() { 
    351             public void run() { 
    352                 // Don't change cursor when mode has changed already 
    353                 if(!(Main.map.mapMode instanceof DeleteAction)) 
    354                     return; 
    355  
    356                 Main.map.mapView.setCursor(c.cursor()); 
    357                 //System.out.println("Set cursor to: " + c.name()); 
    358             } 
    359         }); 
    360         currentMode = c; 
    361     } 
    362  
    363     /** 
    364305     * This is required to update the cursors when ctrl/shift/alt is pressed 
    365306     */ 
  • trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java

    r3837 r3919  
    6262    //static private final Logger logger = Logger.getLogger(DrawAction.class.getName()); 
    6363 
    64     final private Cursor cursorCrosshair; 
    6564    final private Cursor cursorJoinNode; 
    6665    final private Cursor cursorJoinWay; 
    67     enum Cursors { crosshair, node, way } 
    68     private Cursors currCursor = Cursors.crosshair; 
    6966 
    7067    private Node lastUsedNode = null; 
     
    8077    private boolean wayIsFinished = false; 
    8178    private boolean drawTargetHighlight; 
    82     private boolean drawTargetCursor; 
    8379    private Point mousePos; 
    8480    private Point oldMousePos; 
     
    9389        super(tr("Draw"), "node/autonode", tr("Draw nodes"), 
    9490                Shortcut.registerShortcut("mapmode:draw", tr("Mode: {0}", tr("Draw")), KeyEvent.VK_A, Shortcut.GROUP_EDIT), 
    95                 mapFrame, getCursor()); 
     91                mapFrame, ImageProvider.getCursor("crosshair", null)); 
    9692 
    9793        // Add extra shortcut N 
     
    9995        Main.registerActionShortcut(this, extraShortcut); 
    10096 
    101         cursorCrosshair = getCursor(); 
    10297        cursorJoinNode = ImageProvider.getCursor("crosshair", "joinnode"); 
    10398        cursorJoinWay = ImageProvider.getCursor("crosshair", "joinway"); 
    104     } 
    105  
    106     private static Cursor getCursor() { 
    107         try { 
    108             return ImageProvider.getCursor("crosshair", null); 
    109         } catch (Exception e) { 
    110         } 
    111         return Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR); 
    112     } 
    113  
    114     /** 
    115      * Displays the given cursor instead of the normal one 
    116      * @param Cursors One of the available cursors 
    117      */ 
    118     private void setCursor(final Cursors c) { 
    119         if(currCursor.equals(c) || (!drawTargetCursor && currCursor.equals(Cursors.crosshair))) 
    120             return; 
    121         // We invoke this to prevent strange things from happening 
    122         EventQueue.invokeLater(new Runnable() { 
    123             public void run() { 
    124                 // Don't change cursor when mode has changed already 
    125                 if(!(Main.map.mapMode instanceof DrawAction)) 
    126                     return; 
    127                 switch(c) { 
    128                 case way: 
    129                     Main.map.mapView.setCursor(cursorJoinWay); 
    130                     break; 
    131                 case node: 
    132                     Main.map.mapView.setCursor(cursorJoinNode); 
    133                     break; 
    134                 default: 
    135                     Main.map.mapView.setCursor(cursorCrosshair); 
    136                     break; 
    137                 } 
    138             } 
    139         }); 
    140         currCursor = c; 
    14199    } 
    142100 
     
    158116        // if ctrl key is held ("no join"), don't highlight anything 
    159117        if (ctrl) { 
    160             setCursor(Cursors.crosshair); 
     118            Main.map.mapView.setNewCursor(cursor, this); 
    161119            return; 
    162120        } 
     
    169127 
    170128        if (mouseOnExistingNode != null) { 
    171             setCursor(Cursors.node); 
     129            Main.map.mapView.setNewCursor(cursorJoinNode, this); 
    172130            // We also need this list for the statusbar help text 
    173131            oldHighlights.add(mouseOnExistingNode); 
     
    180138        // Insert the node into all the nearby way segments 
    181139        if (mouseOnExistingWays.size() == 0) { 
    182             setCursor(Cursors.crosshair); 
    183             return; 
    184         } 
    185  
    186         setCursor(Cursors.way); 
     140            Main.map.mapView.setNewCursor(cursor, this); 
     141            return; 
     142        } 
     143 
     144        Main.map.mapView.setNewCursor(cursorJoinWay, this); 
    187145 
    188146        // We also need this list for the statusbar help text 
     
    208166            return; 
    209167        super.enterMode(); 
    210         currCursor = Cursors.crosshair; 
    211168        selectedColor =PaintColors.SELECTED.get(); 
    212169        drawHelperLine = Main.pref.getBoolean("draw.helper-line", true); 
    213170        drawTargetHighlight = Main.pref.getBoolean("draw.target-highlight", true); 
    214         drawTargetCursor = Main.pref.getBoolean("draw.target-cursor", true); 
    215171        wayIsFinished = false; 
    216172 
  • trunk/src/org/openstreetmap/josm/actions/mapmode/ExtrudeAction.java

    r3762 r3919  
    7474 
    7575    /** 
    76      * The old cursor before the user pressed the mouse button. 
    77      */ 
    78     private Cursor oldCursor; 
    79     /** 
    8076     * The position of the mouse cursor when the drag action was initiated. 
    8177     */ 
     
    110106                Shortcut.registerShortcut("mapmode:extrude", tr("Mode: {0}", tr("Extrude")), KeyEvent.VK_X, Shortcut.GROUP_EDIT), 
    111107                mapFrame, 
    112                 getCursor("normal", "rectangle", Cursor.DEFAULT_CURSOR)); 
     108                ImageProvider.getCursor("normal", "rectangle")); 
    113109        putValue("help", ht("/Action/Extrude")); 
    114110        initialMoveDelay = Main.pref.getInteger("edit.initial-move-delay",200); 
     
    263259            updateStatusLine(); 
    264260 
    265             setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); 
     261            Main.map.mapView.setNewCursor(Cursor.MOVE_CURSOR, this); 
    266262 
    267263            if (mode == Mode.extrude) { 
     
    354350 
    355351            // Switch back into select mode 
    356             restoreCursor(); 
     352            Main.map.mapView.setNewCursor(cursor, this); 
    357353            Main.map.mapView.removeTemporaryLayer(this); 
    358354            selectedSegment = null; 
     
    528524        } 
    529525    } 
    530  
    531     private static Cursor getCursor(String name, String mod, int def) { 
    532         try { 
    533             return ImageProvider.getCursor(name, mod); 
    534         } catch (Exception e) { 
    535         } 
    536         return Cursor.getPredefinedCursor(def); 
    537     } 
    538  
    539     private void setCursor(Cursor c) { 
    540         if (oldCursor == null) { 
    541             oldCursor = Main.map.mapView.getCursor(); 
    542             Main.map.mapView.setCursor(c); 
    543         } 
    544     } 
    545  
    546     private void restoreCursor() { 
    547         if (oldCursor != null) { 
    548             Main.map.mapView.setCursor(oldCursor); 
    549             oldCursor = null; 
    550         } 
    551     } 
    552526} 
  • trunk/src/org/openstreetmap/josm/actions/mapmode/MapMode.java

    r2017 r3919  
    2424 */ 
    2525abstract public class MapMode extends JosmAction implements MouseListener, MouseMotionListener { 
    26     private final Cursor cursor; 
    27     private Cursor oldCursor; 
     26    protected final Cursor cursor; 
    2827 
    2928    /** 
     
    4847    public void enterMode() { 
    4948        putValue("active", true); 
    50         oldCursor = Main.map.mapView.getCursor(); 
    51         Main.map.mapView.setCursor(cursor); 
     49        Main.map.mapView.setNewCursor(cursor, this); 
    5250        updateStatusLine(); 
    5351    } 
    5452    public void exitMode() { 
    5553        putValue("active", false); 
    56         Main.map.mapView.setCursor(oldCursor); 
     54        Main.map.mapView.resetCursor(this); 
    5755    } 
    5856 
  • trunk/src/org/openstreetmap/josm/actions/mapmode/SelectAction.java

    r3754 r3919  
    7676     * The old cursor before the user pressed the mouse button. 
    7777     */ 
    78     private Cursor oldCursor; 
    79     /** 
    80      * The position of the mouse before the user starts to drag it while pressing a button. 
    81      */ 
    8278    private Point startingDraggingPos; 
    8379    /** 
     
    109105                Shortcut.registerShortcut("mapmode:select", tr("Mode: {0}", tr("Select")), KeyEvent.VK_S, Shortcut.GROUP_EDIT), 
    110106                mapFrame, 
    111                 getCursor("normal", "selection", Cursor.DEFAULT_CURSOR)); 
     107                ImageProvider.getCursor("normal", "selection")); 
    112108        mv = mapFrame.mapView; 
    113109        putValue("help", ht("/Action/Move/Move")); 
     
    115111        initialMoveDelay = Main.pref.getInteger("edit.initial-move-delay", 200); 
    116112        initialMoveThreshold = Main.pref.getInteger("edit.initial-move-threshold", 5); 
    117     } 
    118  
    119     private static Cursor getCursor(String name, String mod, int def) { 
    120         try { 
    121             return ImageProvider.getCursor(name, mod); 
    122         } catch (Exception e) { 
    123         } 
    124         return Cursor.getPredefinedCursor(def); 
    125     } 
    126  
    127     private void setCursor(Cursor c) { 
    128         if (oldCursor == null) { 
    129             oldCursor = mv.getCursor(); 
    130             mv.setCursor(c); 
    131         } 
    132     } 
    133  
    134     private void restoreCursor() { 
    135         if (oldCursor != null) { 
    136             mv.setCursor(oldCursor); 
    137             oldCursor = null; 
    138         } 
    139113    } 
    140114 
     
    182156 
    183157        if (mode == Mode.move) { 
    184             setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); 
     158            mv.setNewCursor(Cursor.MOVE_CURSOR, this); 
    185159        } 
    186160 
     
    257231                                tr("Warning"), 
    258232                                JOptionPane.WARNING_MESSAGE); 
    259                         restoreCursor(); 
     233                        mv.setNewCursor(cursor, this); 
    260234                        return; 
    261235                    } 
     
    457431            // Mode.move   redraws when mouseDragged is called 
    458432            // Mode.rotate redraws here 
    459             setCursor(ImageProvider.getCursor("rotate", null)); 
     433            mv.setNewCursor(ImageProvider.getCursor("rotate", null), this); 
    460434            mv.repaint(); 
    461435        } else if (alt && ctrl) { 
     
    469443            // Mode.move   redraws when mouseDragged is called 
    470444            // Mode.scale redraws here 
    471             setCursor(ImageProvider.getCursor("scale", null)); 
     445            mv.setNewCursor(ImageProvider.getCursor("scale", null), this); 
    472446            mv.repaint(); 
    473447        } else if (!c.isEmpty()) { 
     
    482456            mode = Mode.select; 
    483457 
    484             oldCursor = mv.getCursor(); 
    485458            selectionManager.register(mv); 
    486459            selectionManager.mousePressed(e); 
     
    500473        startingDraggingPos = null; 
    501474 
    502         restoreCursor(); 
     475        mv.setNewCursor(cursor, this); 
    503476        if (mode == Mode.select) { 
    504477            selectionManager.unregister(mv); 
  • trunk/src/org/openstreetmap/josm/gui/MapMover.java

    r1939 r3919  
    6767     */ 
    6868    private final NavigatableComponent nc; 
    69     /** 
    70      * The old cursor when we changed it to movement cursor. 
    71      */ 
    72     private Cursor oldCursor; 
    7369 
    7470    private boolean movementInPlace = false; 
     
    167163        movementInPlace = true; 
    168164        mousePosMove = nc.getEastNorth(e.getX(), e.getY()); 
    169         oldCursor = nc.getCursor(); 
    170         nc.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); 
     165        nc.setNewCursor(Cursor.MOVE_CURSOR, this); 
    171166    } 
    172167 
     
    178173            return; 
    179174        movementInPlace = false; 
    180         if (oldCursor != null) 
    181             nc.setCursor(oldCursor); 
    182         else 
    183             nc.setCursor(Cursor.getDefaultCursor()); 
     175        nc.resetCursor(this); 
    184176        mousePosMove = null; 
    185         oldCursor = null; 
    186177    } 
    187178 
  • trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java

    r3838 r3919  
    44import static org.openstreetmap.josm.tools.I18n.marktr; 
    55 
     6import java.awt.Cursor; 
    67import java.awt.Point; 
    78import java.awt.Rectangle; 
     
    12131214        SYSTEMS_OF_MEASUREMENT.put(marktr("Imperial"), IMPERIAL_SOM); 
    12141215    } 
     1216 
     1217    private class CursorInfo { 
     1218        public Cursor cursor; 
     1219        public Object object; 
     1220        public CursorInfo(Cursor c, Object o) { 
     1221            cursor = c; 
     1222            object = o; 
     1223        } 
     1224    } 
     1225 
     1226    private LinkedList<CursorInfo> Cursors = new LinkedList<CursorInfo>(); 
     1227    /** 
     1228     * Set new cursor. 
     1229     */ 
     1230    public void setNewCursor(Cursor cursor, Object reference) { 
     1231        if(Cursors.size() > 0) { 
     1232            CursorInfo l = Cursors.getLast(); 
     1233            if(l != null && l.cursor == cursor && l.object == reference) { 
     1234                return; 
     1235            } 
     1236            stripCursors(reference); 
     1237        } 
     1238        Cursors.add(new CursorInfo(cursor, reference)); 
     1239        setCursor(cursor); 
     1240    } 
     1241    public void setNewCursor(int cursor, Object reference) { 
     1242        setNewCursor(Cursor.getPredefinedCursor(cursor), reference); 
     1243    } 
     1244    /** 
     1245     * Remove the new cursor and reset to previous 
     1246     */ 
     1247    public void resetCursor(Object reference) { 
     1248        if(Cursors.size() == 0) { 
     1249            setCursor(null); 
     1250            return; 
     1251        } 
     1252        CursorInfo l = Cursors.getLast(); 
     1253        stripCursors(reference); 
     1254        if(l != null && l.object == reference) { 
     1255            if(Cursors.size() == 0) 
     1256                setCursor(null); 
     1257            else 
     1258                setCursor(Cursors.getLast().cursor); 
     1259        } 
     1260    } 
     1261 
     1262    private void stripCursors(Object reference) { 
     1263        LinkedList<CursorInfo> c = new LinkedList<CursorInfo>(); 
     1264        for(CursorInfo i : Cursors) { 
     1265            if(i.object != reference) 
     1266                c.add(i); 
     1267        } 
     1268        Cursors = c; 
     1269    } 
    12151270} 
Note: See TracChangeset for help on using the changeset viewer.