Ticket #2233: fix cursor bug.patch

File fix cursor bug.patch, 6.1 KB (added by xeen, 15 years ago)
  • src/org/openstreetmap/josm/actions/mapmode/DrawAction.java

     
    88import java.awt.BasicStroke;
    99import java.awt.Color;
    1010import java.awt.Cursor;
     11import java.awt.EventQueue;
    1112import java.awt.Graphics;
    1213import java.awt.Graphics2D;
    1314import java.awt.Point;
     
    5859 *
    5960 */
    6061public class DrawAction extends MapMode implements MapViewPaintable, SelectionChangedListener, AWTEventListener {
    61 
     62    final private Cursor cursorCrosshair;
     63    final private Cursor cursorJoinNode;
     64    final private Cursor cursorJoinWay;
     65    enum Cursors { crosshair, node, way }
     66    private Cursors currCursor = Cursors.crosshair;
     67   
    6268    private static Node lastUsedNode = null;
    6369    private double PHI=Math.toRadians(90);
    6470
     
    8793        // Add extra shortcut N
    8894        Main.contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    8995            Shortcut.registerShortcut("mapmode:drawfocus", tr("Mode: Draw Focus"), KeyEvent.VK_N, Shortcut.GROUP_EDIT).getKeyStroke(), tr("Draw"));
     96       
     97        cursorCrosshair = getCursor();
     98        cursorJoinNode = ImageProvider.getCursor("crosshair", "joinnode");
     99        cursorJoinWay = ImageProvider.getCursor("crosshair", "joinway");
    90100    }
    91101
    92102    private static Cursor getCursor() {
     
    98108    }
    99109
    100110    /**
    101      * Sets a special cursor to indicate that the next click will automatically join into
    102      * an existing node or way (if feature is enabled)
    103      *
    104      * @param way If true, the cursor will indicate it is joined into a way; node otherwise
     111     * Displays the given cursor instead of the normal one
     112     * @param Cursors One of the available cursors
    105113     */
    106     private void setJoinCursor(boolean way) {
    107         if(!drawTargetCursor) {
    108             resetCursor();
     114    private void setCursor(final Cursors c) {
     115        if(currCursor.equals(c) || (!drawTargetCursor && currCursor.equals(Cursors.crosshair)))
    109116            return;
    110         }
    111117        try {
    112             Main.map.mapView.setCursor(
    113                     ImageProvider.getCursor("crosshair", (way ? "joinway" : "joinnode"))
    114             );
    115             return;
    116         } catch (Exception e) {
    117             e.printStackTrace();
    118         }
    119         resetCursor();
     118            // We invoke this to prevent strange things from happening
     119            EventQueue.invokeLater(new Runnable() {
     120                public void run() {
     121                    switch(c) {
     122                        case way:
     123                            Main.map.mapView.setCursor(cursorJoinWay);
     124                            break;
     125                        case node:
     126                            Main.map.mapView.setCursor(cursorJoinNode);
     127                            break;
     128                        default:
     129                            Main.map.mapView.setCursor(cursorCrosshair);
     130                            break;   
     131                    }
     132                }
     133            });
     134            currCursor = c;
     135        } catch(Exception e) {}
    120136    }
    121 
    122     /**
    123      * Resets the cursor to the default cursor for drawing mode (i.e. crosshair)
    124      */
    125     private void resetCursor() {
    126         Main.map.mapView.setCursor(getCursor());
     137   
     138    private void redrawIfRequired() {
     139        if ((!drawHelperLine || wayIsFinished) && !drawTargetHighlight) return;
     140        Main.map.mapView.repaint();
    127141    }
    128142
    129143    /**
     
    134148        removeHighlighting();
    135149        // if ctrl key is held ("no join"), don't highlight anything
    136150        if (ctrl) {
    137             resetCursor();
     151            setCursor(Cursors.crosshair);
    138152            return;
    139153        }
    140154
     
    143157            mouseOnExistingNode = Main.map.mapView.getNearestNode(mousePos);
    144158
    145159        if (mouseOnExistingNode != null) {
    146             setJoinCursor(false);
     160            setCursor(Cursors.node);
    147161            // We also need this list for the statusbar help text
    148162            oldHighlights.add(mouseOnExistingNode);
    149163            if(drawTargetHighlight)
     
    153167
    154168        // Insert the node into all the nearby way segments
    155169        if(mouseOnExistingWays.size() == 0) {
    156             resetCursor();
     170            setCursor(Cursors.crosshair);
    157171            return;
    158172        }
    159173
    160         setJoinCursor(true);
     174        setCursor(Cursors.way);
    161175
    162176        // We also need this list for the statusbar help text
    163177        oldHighlights.addAll(mouseOnExistingWays);
     
    218232        if(Main.map == null || Main.map.mapView == null || !Main.map.mapView.isDrawableLayer())
    219233            return;
    220234        updateKeyModifiers((InputEvent) event);
     235        computeHelperLine();
    221236        addHighlighting();
    222         computeHelperLine();
     237        redrawIfRequired();
    223238    }
    224239    /**
    225240     * redraw to (possibly) get rid of helper line if selection changes.
     
    228243        if(!Main.map.mapView.isDrawableLayer())
    229244            return;
    230245        computeHelperLine();
     246        addHighlighting();
     247        redrawIfRequired();
    231248    }
    232249
    233250    private void tryAgain(MouseEvent e) {
     
    246263        Main.map.selectSelectTool(true);
    247264
    248265        // Redraw to remove the helper line stub
     266        computeHelperLine();
    249267        removeHighlighting();
    250         computeHelperLine();
    251         Main.map.mapView.repaint();
     268        redrawIfRequired();
    252269    }
    253270
    254271    /**
     
    476493
    477494        computeHelperLine();
    478495        removeHighlighting();
    479         Main.map.mapView.repaint();
     496        redrawIfRequired();
    480497    }
    481498
    482499    /**
     
    549566        updateKeyModifiers(e);
    550567        mousePos = e.getPoint();
    551568
     569        computeHelperLine();
    552570        addHighlighting();
    553         computeHelperLine();
     571        redrawIfRequired();
    554572    }
    555573
    556574    private void updateKeyModifiers(InputEvent e) {
     
    662680        Main.map.statusLine.setHeading(hdg);
    663681        Main.map.statusLine.setDist(distance);
    664682        updateStatusLine();
    665 
    666         if ((!drawHelperLine || wayIsFinished) && !drawTargetHighlight) return;
    667         Main.map.mapView.repaint();
    668683    }
    669684
    670685    /**