Ticket #6730: backspace.patch

File backspace.patch, 3.1 KB (added by akks, 14 years ago)
  • src/org/openstreetmap/josm/actions/mapmode/DrawAction.java

     
    1212import java.awt.Point;
    1313import java.awt.Toolkit;
    1414import java.awt.event.AWTEventListener;
     15import java.awt.event.ActionEvent;
    1516import java.awt.event.InputEvent;
    1617import java.awt.event.KeyEvent;
    1718import java.awt.event.MouseEvent;
     
    2728import java.util.Map;
    2829import java.util.Set;
    2930
     31import javax.swing.AbstractAction;
    3032import javax.swing.JOptionPane;
    3133
    3234import org.openstreetmap.josm.Main;
     
    7880    private EastNorth currentMouseEastNorth;
    7981
    8082    private Shortcut extraShortcut;
    81 
     83    private Shortcut backspaceShortcut;
     84           
    8285    public DrawAction(MapFrame mapFrame) {
    8386        super(tr("Draw"), "node/autonode", tr("Draw nodes"),
    8487                Shortcut.registerShortcut("mapmode:draw", tr("Mode: {0}", tr("Draw")), KeyEvent.VK_A, Shortcut.GROUP_EDIT),
     
    175178        drawHelperLine = Main.pref.getBoolean("draw.helper-line", true);
    176179        drawTargetHighlight = Main.pref.getBoolean("draw.target-highlight", true);
    177180        wayIsFinished = false;
     181       
     182        backspaceShortcut = Shortcut.registerShortcut("mapmode:backspace", tr("Backspace in Add mode"), KeyEvent.VK_BACK_SPACE, Shortcut.GROUP_EDIT);
     183        Main.registerActionShortcut(new BackSpaceAction(), backspaceShortcut);
    178184
    179185        Main.map.mapView.addMouseListener(this);
    180186        Main.map.mapView.addMouseMotionListener(this);
     
    195201        Main.map.mapView.removeMouseMotionListener(this);
    196202        Main.map.mapView.removeTemporaryLayer(this);
    197203        DataSet.removeSelectionListener(this);
     204        Main.unregisterActionShortcut(backspaceShortcut);
     205
    198206        removeHighlighting();
    199207        try {
    200208            Toolkit.getDefaultToolkit().removeAWTEventListener(this);
     
    967975        super.destroy();
    968976        Main.unregisterActionShortcut(extraShortcut);
    969977    }
     978   
     979    public static class BackSpaceAction extends AbstractAction {
     980
     981        @Override
     982        public void actionPerformed(ActionEvent e) {
     983            Main.main.undoRedo.undo();
     984            Node n=null;
     985            Command lastCmd=Main.main.undoRedo.commands.peekLast(); 
     986            if (lastCmd==null) return;
     987            for (OsmPrimitive p: lastCmd.getParticipatingPrimitives()) {
     988                if (p instanceof Node) {
     989                    if (n==null) {
     990                        n=(Node) p; // found one node
     991                    }  else {
     992                    // if more than 1 node were affected by previous command,
     993                    // we have no way to continue, so we forget about found node
     994                        n=null;
     995                        break;
     996                    }
     997                }
     998            }
     999            // select last added node - maybe we will continue drawing from it
     1000            if (n!=null) getCurrentDataSet().addSelected(n);
     1001    }
     1002    }
     1003
    9701004}