001// License: GPL. See LICENSE file for details.
002package org.openstreetmap.josm.actions.mapmode;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.marktr;
006import static org.openstreetmap.josm.tools.I18n.tr;
007import static org.openstreetmap.josm.tools.I18n.trn;
008
009import java.awt.BasicStroke;
010import java.awt.Color;
011import java.awt.Cursor;
012import java.awt.Graphics2D;
013import java.awt.Point;
014import java.awt.Stroke;
015import java.awt.event.ActionEvent;
016import java.awt.event.KeyEvent;
017import java.awt.event.MouseEvent;
018import java.awt.event.MouseListener;
019import java.awt.geom.GeneralPath;
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.Collection;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.HashSet;
026import java.util.Iterator;
027import java.util.LinkedList;
028import java.util.List;
029import java.util.Map;
030import java.util.Set;
031
032import javax.swing.AbstractAction;
033import javax.swing.JCheckBoxMenuItem;
034import javax.swing.JMenuItem;
035import javax.swing.JOptionPane;
036import javax.swing.JPopupMenu;
037
038import org.openstreetmap.josm.Main;
039import org.openstreetmap.josm.actions.JosmAction;
040import org.openstreetmap.josm.command.AddCommand;
041import org.openstreetmap.josm.command.ChangeCommand;
042import org.openstreetmap.josm.command.Command;
043import org.openstreetmap.josm.command.SequenceCommand;
044import org.openstreetmap.josm.data.Bounds;
045import org.openstreetmap.josm.data.SelectionChangedListener;
046import org.openstreetmap.josm.data.coor.EastNorth;
047import org.openstreetmap.josm.data.coor.LatLon;
048import org.openstreetmap.josm.data.osm.DataSet;
049import org.openstreetmap.josm.data.osm.Node;
050import org.openstreetmap.josm.data.osm.OsmPrimitive;
051import org.openstreetmap.josm.data.osm.Way;
052import org.openstreetmap.josm.data.osm.WaySegment;
053import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
054import org.openstreetmap.josm.gui.MainMenu;
055import org.openstreetmap.josm.gui.MapFrame;
056import org.openstreetmap.josm.gui.MapView;
057import org.openstreetmap.josm.gui.NavigatableComponent;
058import org.openstreetmap.josm.gui.layer.Layer;
059import org.openstreetmap.josm.gui.layer.MapViewPaintable;
060import org.openstreetmap.josm.gui.layer.OsmDataLayer;
061import org.openstreetmap.josm.gui.util.GuiHelper;
062import org.openstreetmap.josm.gui.util.KeyPressReleaseListener;
063import org.openstreetmap.josm.gui.util.ModifierListener;
064import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
065import org.openstreetmap.josm.tools.Geometry;
066import org.openstreetmap.josm.tools.ImageProvider;
067import org.openstreetmap.josm.tools.Pair;
068import org.openstreetmap.josm.tools.Shortcut;
069import org.openstreetmap.josm.tools.Utils;
070
071/**
072 * Mapmode to add nodes, create and extend ways.
073 */
074public class DrawAction extends MapMode implements MapViewPaintable, SelectionChangedListener, KeyPressReleaseListener, ModifierListener {
075    private final Cursor cursorJoinNode;
076    private final Cursor cursorJoinWay;
077
078    private Node lastUsedNode = null;
079    private static final double PHI = Math.toRadians(90);
080    private double toleranceMultiplier;
081
082    private Node mouseOnExistingNode;
083    private Set<Way> mouseOnExistingWays = new HashSet<>();
084    // old highlights store which primitives are currently highlighted. This
085    // is true, even if target highlighting is disabled since the status bar
086    // derives its information from this list as well.
087    private Set<OsmPrimitive> oldHighlights = new HashSet<>();
088    // new highlights contains a list of primitives that should be highlighted
089    // but haven’t been so far. The idea is to compare old and new and only
090    // repaint if there are changes.
091    private Set<OsmPrimitive> newHighlights = new HashSet<>();
092    private boolean drawHelperLine;
093    private boolean wayIsFinished = false;
094    private boolean drawTargetHighlight;
095    private Point mousePos;
096    private Point oldMousePos;
097    private Color rubberLineColor;
098
099    private Node currentBaseNode;
100    private Node previousNode;
101    private EastNorth currentMouseEastNorth;
102
103    private final SnapHelper snapHelper = new SnapHelper();
104
105    private final Shortcut backspaceShortcut;
106    private final BackSpaceAction backspaceAction;
107    private final Shortcut snappingShortcut;
108    private boolean ignoreNextKeyRelease;
109
110    private final SnapChangeAction snapChangeAction;
111    private final JCheckBoxMenuItem snapCheckboxMenuItem;
112    private boolean useRepeatedShortcut;
113    private Stroke rubberLineStroke;
114    private static final BasicStroke BASIC_STROKE = new BasicStroke(1);
115
116    private static int snapToIntersectionThreshold;
117
118    public DrawAction(MapFrame mapFrame) {
119        super(tr("Draw"), "node/autonode", tr("Draw nodes"),
120                Shortcut.registerShortcut("mapmode:draw", tr("Mode: {0}", tr("Draw")), KeyEvent.VK_A, Shortcut.DIRECT),
121                mapFrame, ImageProvider.getCursor("crosshair", null));
122
123        snappingShortcut = Shortcut.registerShortcut("mapmode:drawanglesnapping",
124                tr("Mode: Draw Angle snapping"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
125        snapChangeAction = new SnapChangeAction();
126        snapCheckboxMenuItem = addMenuItem();
127        snapHelper.setMenuCheckBox(snapCheckboxMenuItem);
128        backspaceShortcut = Shortcut.registerShortcut("mapmode:backspace",
129                tr("Backspace in Add mode"), KeyEvent.VK_BACK_SPACE, Shortcut.DIRECT);
130        backspaceAction = new BackSpaceAction();
131        cursorJoinNode = ImageProvider.getCursor("crosshair", "joinnode");
132        cursorJoinWay = ImageProvider.getCursor("crosshair", "joinway");
133
134        readPreferences();
135        snapHelper.init();
136    }
137
138    private JCheckBoxMenuItem addMenuItem() {
139        int n=Main.main.menu.editMenu.getItemCount();
140        for (int i=n-1;i>0;i--) {
141            JMenuItem item = Main.main.menu.editMenu.getItem(i);
142            if (item!=null && item.getAction() !=null && item.getAction() instanceof SnapChangeAction) {
143                Main.main.menu.editMenu.remove(i);
144            }
145        }
146        return MainMenu.addWithCheckbox(Main.main.menu.editMenu, snapChangeAction, MainMenu.WINDOW_MENU_GROUP.VOLATILE);
147    }
148
149    /**
150     * Checks if a map redraw is required and does so if needed. Also updates the status bar
151     */
152    private boolean redrawIfRequired() {
153        updateStatusLine();
154        // repaint required if the helper line is active.
155        boolean needsRepaint = drawHelperLine && !wayIsFinished;
156        if(drawTargetHighlight) {
157            // move newHighlights to oldHighlights; only update changed primitives
158            for(OsmPrimitive x : newHighlights) {
159                if(oldHighlights.contains(x)) {
160                    continue;
161                }
162                x.setHighlighted(true);
163                needsRepaint = true;
164            }
165            oldHighlights.removeAll(newHighlights);
166            for(OsmPrimitive x : oldHighlights) {
167                x.setHighlighted(false);
168                needsRepaint = true;
169            }
170        }
171        // required in order to print correct help text
172        oldHighlights = newHighlights;
173
174        if (!needsRepaint && !drawTargetHighlight)
175            return false;
176
177        // update selection to reflect which way being modified
178        DataSet currentDataSet = getCurrentDataSet();
179        if (getCurrentBaseNode() != null && currentDataSet != null && !currentDataSet.getSelected().isEmpty()) {
180            Way continueFrom = getWayForNode(getCurrentBaseNode());
181            if (alt && continueFrom != null && (!getCurrentBaseNode().isSelected() || continueFrom.isSelected())) {
182                addRemoveSelection(currentDataSet, getCurrentBaseNode(), continueFrom);
183                needsRepaint = true;
184            } else if (!alt && continueFrom != null && !continueFrom.isSelected()) {
185                currentDataSet.addSelected(continueFrom);
186                needsRepaint = true;
187            }
188        }
189
190        if(needsRepaint) {
191            Main.map.mapView.repaint();
192        }
193        return needsRepaint;
194    }
195
196    private static void addRemoveSelection(DataSet ds, OsmPrimitive toAdd, OsmPrimitive toRemove) {
197        ds.beginUpdate(); // to prevent the selection listener to screw around with the state
198        ds.addSelected(toAdd);
199        ds.clearSelection(toRemove);
200        ds.endUpdate();
201    }
202
203    @Override
204    public void enterMode() {
205        if (!isEnabled())
206            return;
207        super.enterMode();
208        readPreferences();
209
210        // determine if selection is suitable to continue drawing. If it
211        // isn't, set wayIsFinished to true to avoid superfluous repaints.
212        determineCurrentBaseNodeAndPreviousNode(getCurrentDataSet().getSelected());
213        wayIsFinished = getCurrentBaseNode() == null;
214
215        toleranceMultiplier = 0.01 * NavigatableComponent.PROP_SNAP_DISTANCE.get();
216
217        snapHelper.init();
218        snapCheckboxMenuItem.getAction().setEnabled(true);
219
220        Main.map.statusLine.getAnglePanel().addMouseListener(snapHelper.anglePopupListener);
221        Main.registerActionShortcut(backspaceAction, backspaceShortcut);
222
223        Main.map.mapView.addMouseListener(this);
224        Main.map.mapView.addMouseMotionListener(this);
225        Main.map.mapView.addTemporaryLayer(this);
226        DataSet.addSelectionListener(this);
227
228        Main.map.keyDetector.addKeyListener(this);
229        Main.map.keyDetector.addModifierListener(this);
230        ignoreNextKeyRelease = true;
231        // would like to but haven't got mouse position yet:
232        // computeHelperLine(false, false, false);
233    }
234
235    private void readPreferences() {
236        rubberLineColor = Main.pref.getColor(marktr("helper line"), null);
237        if (rubberLineColor == null) rubberLineColor = PaintColors.SELECTED.get();
238
239        rubberLineStroke = GuiHelper.getCustomizedStroke(Main.pref.get("draw.stroke.helper-line","3"));
240        drawHelperLine = Main.pref.getBoolean("draw.helper-line", true);
241        drawTargetHighlight = Main.pref.getBoolean("draw.target-highlight", true);
242        snapToIntersectionThreshold = Main.pref.getInteger("edit.snap-intersection-threshold",10);
243    }
244
245    @Override
246    public void exitMode() {
247        super.exitMode();
248        Main.map.mapView.removeMouseListener(this);
249        Main.map.mapView.removeMouseMotionListener(this);
250        Main.map.mapView.removeTemporaryLayer(this);
251        DataSet.removeSelectionListener(this);
252        Main.unregisterActionShortcut(backspaceAction, backspaceShortcut);
253        snapHelper.unsetFixedMode();
254        snapCheckboxMenuItem.getAction().setEnabled(false);
255
256        Main.map.statusLine.getAnglePanel().removeMouseListener(snapHelper.anglePopupListener);
257        Main.map.statusLine.activateAnglePanel(false);
258
259        removeHighlighting();
260        Main.map.keyDetector.removeKeyListener(this);
261        Main.map.keyDetector.removeModifierListener(this);
262
263        // when exiting we let everybody know about the currently selected
264        // primitives
265        //
266        DataSet ds = getCurrentDataSet();
267        if(ds != null) {
268            ds.fireSelectionChanged();
269        }
270    }
271
272    /**
273     * redraw to (possibly) get rid of helper line if selection changes.
274     */
275    @Override
276    public void modifiersChanged(int modifiers) {
277        if (!Main.isDisplayingMapView() || !Main.map.mapView.isActiveLayerDrawable())
278            return;
279        updateKeyModifiers(modifiers);
280        computeHelperLine();
281        addHighlighting();
282    }
283
284    @Override
285    public void doKeyPressed(KeyEvent e) {
286        if (!snappingShortcut.isEvent(e) && !(useRepeatedShortcut && getShortcut().isEvent(e)))
287            return;
288        snapHelper.setFixedMode();
289        computeHelperLine();
290        redrawIfRequired();
291    }
292
293    @Override
294    public void doKeyReleased(KeyEvent e) {
295        if (!snappingShortcut.isEvent(e) && !(useRepeatedShortcut && getShortcut().isEvent(e)))
296            return;
297        if (ignoreNextKeyRelease) {
298            ignoreNextKeyRelease = false;
299            return;
300        }
301        snapHelper.unFixOrTurnOff();
302        computeHelperLine();
303        redrawIfRequired();
304    }
305
306    /**
307     * redraw to (possibly) get rid of helper line if selection changes.
308     */
309    @Override
310    public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
311        if(!Main.map.mapView.isActiveLayerDrawable())
312            return;
313        computeHelperLine();
314        addHighlighting();
315    }
316
317    private void tryAgain(MouseEvent e) {
318        getCurrentDataSet().setSelected();
319        mouseReleased(e);
320    }
321
322    /**
323     * This function should be called when the user wishes to finish his current draw action.
324     * If Potlatch Style is enabled, it will switch to select tool, otherwise simply disable
325     * the helper line until the user chooses to draw something else.
326     */
327    private void finishDrawing() {
328        // let everybody else know about the current selection
329        //
330        Main.main.getCurrentDataSet().fireSelectionChanged();
331        lastUsedNode = null;
332        wayIsFinished = true;
333        Main.map.selectSelectTool(true);
334        snapHelper.noSnapNow();
335
336        // Redraw to remove the helper line stub
337        computeHelperLine();
338        removeHighlighting();
339    }
340
341    private Point rightClickPressPos;
342
343    @Override
344    public void mousePressed(MouseEvent e) {
345        if (e.getButton() == MouseEvent.BUTTON3) {
346            rightClickPressPos = e.getPoint();
347        }
348    }
349
350    /**
351     * If user clicked with the left button, add a node at the current mouse
352     * position.
353     *
354     * If in nodeway mode, insert the node into the way.
355     */
356    @Override
357    public void mouseReleased(MouseEvent e) {
358        if (e.getButton() == MouseEvent.BUTTON3) {
359            Point curMousePos = e.getPoint();
360            if (curMousePos.equals(rightClickPressPos)) {
361                tryToSetBaseSegmentForAngleSnap();
362            }
363            return;
364        }
365        if (e.getButton() != MouseEvent.BUTTON1)
366            return;
367        if(!Main.map.mapView.isActiveLayerDrawable())
368            return;
369        // request focus in order to enable the expected keyboard shortcuts
370        //
371        Main.map.mapView.requestFocus();
372
373        if(e.getClickCount() > 1 && mousePos != null && mousePos.equals(oldMousePos)) {
374            // A double click equals "user clicked last node again, finish way"
375            // Change draw tool only if mouse position is nearly the same, as
376            // otherwise fast clicks will count as a double click
377            finishDrawing();
378            return;
379        }
380        oldMousePos = mousePos;
381
382        // we copy ctrl/alt/shift from the event just in case our global
383        // keyDetector didn't make it through the security manager. Unclear
384        // if that can ever happen but better be safe.
385        updateKeyModifiers(e);
386        mousePos = e.getPoint();
387
388        DataSet ds = getCurrentDataSet();
389        Collection<OsmPrimitive> selection = new ArrayList<>(ds.getSelected());
390        Collection<Command> cmds = new LinkedList<>();
391        Collection<OsmPrimitive> newSelection = new LinkedList<>(ds.getSelected());
392
393        List<Way> reuseWays = new ArrayList<>(),
394                replacedWays = new ArrayList<>();
395        boolean newNode = false;
396        Node n = null;
397
398        n = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive.isSelectablePredicate);
399        if (ctrl) {
400            Iterator<Way> it = getCurrentDataSet().getSelectedWays().iterator();
401            if (it.hasNext()) {
402                // ctrl-click on node of selected way = reuse node despite of ctrl
403                if (!it.next().containsNode(n)) n = null;
404            } else {
405                n=null; // ctrl-click + no selected way = new node
406            }
407        }
408
409        if (n != null && !snapHelper.isActive()) {
410            // user clicked on node
411            if (selection.isEmpty() || wayIsFinished) {
412                // select the clicked node and do nothing else
413                // (this is just a convenience option so that people don't
414                // have to switch modes)
415
416                getCurrentDataSet().setSelected(n);
417                // If we extend/continue an existing way, select it already now to make it obvious
418                Way continueFrom = getWayForNode(n);
419                if (continueFrom != null) {
420                    getCurrentDataSet().addSelected(continueFrom);
421                }
422
423                // The user explicitly selected a node, so let him continue drawing
424                wayIsFinished = false;
425                return;
426            }
427        } else {
428            EastNorth newEN;
429            if (n!=null) {
430                EastNorth foundPoint = n.getEastNorth();
431                // project found node to snapping line
432                newEN = snapHelper.getSnapPoint(foundPoint);
433                // do not add new node if there is some node within snapping distance
434                double tolerance = Main.map.mapView.getDist100Pixel() * toleranceMultiplier;
435                if (foundPoint.distance(newEN) > tolerance) {
436                    n = new Node(newEN); // point != projected, so we create new node
437                    newNode = true;
438                }
439            } else { // n==null, no node found in clicked area
440                EastNorth mouseEN = Main.map.mapView.getEastNorth(e.getX(), e.getY());
441                newEN = snapHelper.isSnapOn() ? snapHelper.getSnapPoint(mouseEN) : mouseEN;
442                n = new Node(newEN); //create node at clicked point
443                newNode = true;
444            }
445            snapHelper.unsetFixedMode();
446        }
447
448        if (newNode) {
449            if (n.getCoor().isOutSideWorld()) {
450                JOptionPane.showMessageDialog(
451                        Main.parent,
452                        tr("Cannot add a node outside of the world."),
453                        tr("Warning"),
454                        JOptionPane.WARNING_MESSAGE
455                        );
456                return;
457            }
458            cmds.add(new AddCommand(n));
459
460            if (!ctrl) {
461                // Insert the node into all the nearby way segments
462                List<WaySegment> wss = Main.map.mapView.getNearestWaySegments(
463                        Main.map.mapView.getPoint(n), OsmPrimitive.isSelectablePredicate);
464                if (snapHelper.isActive()) {
465                    tryToMoveNodeOnIntersection(wss,n);
466                }
467                insertNodeIntoAllNearbySegments(wss, n, newSelection, cmds, replacedWays, reuseWays);
468            }
469        }
470        // now "n" is newly created or reused node that shoud be added to some way
471
472        // This part decides whether or not a "segment" (i.e. a connection) is made to an
473        // existing node.
474
475        // For a connection to be made, the user must either have a node selected (connection
476        // is made to that node), or he must have a way selected *and* one of the endpoints
477        // of that way must be the last used node (connection is made to last used node), or
478        // he must have a way and a node selected (connection is made to the selected node).
479
480        // If the above does not apply, the selection is cleared and a new try is started
481
482        boolean extendedWay = false;
483        boolean wayIsFinishedTemp = wayIsFinished;
484        wayIsFinished = false;
485
486        // don't draw lines if shift is held
487        if (!selection.isEmpty() && !shift) {
488            Node selectedNode = null;
489            Way selectedWay = null;
490
491            for (OsmPrimitive p : selection) {
492                if (p instanceof Node) {
493                    if (selectedNode != null) {
494                        // Too many nodes selected to do something useful
495                        tryAgain(e);
496                        return;
497                    }
498                    selectedNode = (Node) p;
499                } else if (p instanceof Way) {
500                    if (selectedWay != null) {
501                        // Too many ways selected to do something useful
502                        tryAgain(e);
503                        return;
504                    }
505                    selectedWay = (Way) p;
506                }
507            }
508
509            // the node from which we make a connection
510            Node n0 = findNodeToContinueFrom(selectedNode, selectedWay);
511            // We have a selection but it isn't suitable. Try again.
512            if(n0 == null) {
513                tryAgain(e);
514                return;
515            }
516            if(!wayIsFinishedTemp){
517                if(isSelfContainedWay(selectedWay, n0, n))
518                    return;
519
520                // User clicked last node again, finish way
521                if(n0 == n) {
522                    finishDrawing();
523                    return;
524                }
525
526                // Ok we know now that we'll insert a line segment, but will it connect to an
527                // existing way or make a new way of its own? The "alt" modifier means that the
528                // user wants a new way.
529                Way way = alt ? null : (selectedWay != null) ? selectedWay : getWayForNode(n0);
530                Way wayToSelect;
531
532                // Don't allow creation of self-overlapping ways
533                if(way != null) {
534                    int nodeCount=0;
535                    for (Node p : way.getNodes())
536                        if(p.equals(n0)) {
537                            nodeCount++;
538                        }
539                    if(nodeCount > 1) {
540                        way = null;
541                    }
542                }
543
544                if (way == null) {
545                    way = new Way();
546                    way.addNode(n0);
547                    cmds.add(new AddCommand(way));
548                    wayToSelect = way;
549                } else {
550                    int i;
551                    if ((i = replacedWays.indexOf(way)) != -1) {
552                        way = reuseWays.get(i);
553                        wayToSelect = way;
554                    } else {
555                        wayToSelect = way;
556                        Way wnew = new Way(way);
557                        cmds.add(new ChangeCommand(way, wnew));
558                        way = wnew;
559                    }
560                }
561
562                // Connected to a node that's already in the way
563                if(way.containsNode(n)) {
564                    wayIsFinished = true;
565                    selection.clear();
566                }
567
568                // Add new node to way
569                if (way.getNode(way.getNodesCount() - 1) == n0) {
570                    way.addNode(n);
571                } else {
572                    way.addNode(0, n);
573                }
574
575                extendedWay = true;
576                newSelection.clear();
577                newSelection.add(wayToSelect);
578            }
579        }
580
581        String title;
582        if (!extendedWay) {
583            if (!newNode)
584                return; // We didn't do anything.
585            else if (reuseWays.isEmpty()) {
586                title = tr("Add node");
587            } else {
588                title = tr("Add node into way");
589                for (Way w : reuseWays) {
590                    newSelection.remove(w);
591                }
592            }
593            newSelection.clear();
594            newSelection.add(n);
595        } else if (!newNode) {
596            title = tr("Connect existing way to node");
597        } else if (reuseWays.isEmpty()) {
598            title = tr("Add a new node to an existing way");
599        } else {
600            title = tr("Add node into way and connect");
601        }
602
603        Command c = new SequenceCommand(title, cmds);
604
605        Main.main.undoRedo.add(c);
606        if(!wayIsFinished) {
607            lastUsedNode = n;
608        }
609
610        getCurrentDataSet().setSelected(newSelection);
611
612        // "viewport following" mode for tracing long features
613        // from aerial imagery or GPS tracks.
614        if (n != null && Main.map.mapView.viewportFollowing) {
615            Main.map.mapView.smoothScrollTo(n.getEastNorth());
616        }
617        computeHelperLine();
618        removeHighlighting();
619    }
620
621    private void insertNodeIntoAllNearbySegments(List<WaySegment> wss, Node n, Collection<OsmPrimitive> newSelection, Collection<Command> cmds, List<Way> replacedWays, List<Way> reuseWays) {
622        Map<Way, List<Integer>> insertPoints = new HashMap<>();
623        for (WaySegment ws : wss) {
624            List<Integer> is;
625            if (insertPoints.containsKey(ws.way)) {
626                is = insertPoints.get(ws.way);
627            } else {
628                is = new ArrayList<>();
629                insertPoints.put(ws.way, is);
630            }
631
632            is.add(ws.lowerIndex);
633        }
634
635        Set<Pair<Node,Node>> segSet = new HashSet<>();
636
637        for (Map.Entry<Way, List<Integer>> insertPoint : insertPoints.entrySet()) {
638            Way w = insertPoint.getKey();
639            List<Integer> is = insertPoint.getValue();
640
641            Way wnew = new Way(w);
642
643            pruneSuccsAndReverse(is);
644            for (int i : is) {
645                segSet.add(Pair.sort(new Pair<>(w.getNode(i), w.getNode(i+1))));
646            }
647            for (int i : is) {
648                wnew.addNode(i + 1, n);
649            }
650
651            // If ALT is pressed, a new way should be created and that new way should get
652            // selected. This works everytime unless the ways the nodes get inserted into
653            // are already selected. This is the case when creating a self-overlapping way
654            // but pressing ALT prevents this. Therefore we must de-select the way manually
655            // here so /only/ the new way will be selected after this method finishes.
656            if(alt) {
657                newSelection.add(insertPoint.getKey());
658            }
659
660            cmds.add(new ChangeCommand(insertPoint.getKey(), wnew));
661            replacedWays.add(insertPoint.getKey());
662            reuseWays.add(wnew);
663        }
664
665        adjustNode(segSet, n);
666    }
667
668    /**
669     * Prevent creation of ways that look like this: &lt;----&gt;
670     * This happens if users want to draw a no-exit-sideway from the main way like this:
671     * ^
672     * |&lt;----&gt;
673     * |
674     * The solution isn't ideal because the main way will end in the side way, which is bad for
675     * navigation software ("drive straight on") but at least easier to fix. Maybe users will fix
676     * it on their own, too. At least it's better than producing an error.
677     *
678     * @param selectedWay the way to check
679     * @param currentNode the current node (i.e. the one the connection will be made from)
680     * @param targetNode the target node (i.e. the one the connection will be made to)
681     * @return {@code true} if this would create a selfcontaining way, {@code false} otherwise.
682     */
683    private boolean isSelfContainedWay(Way selectedWay, Node currentNode, Node targetNode) {
684        if(selectedWay != null) {
685            int posn0 = selectedWay.getNodes().indexOf(currentNode);
686            if( posn0 != -1 && // n0 is part of way
687                    (posn0 >= 1                             && targetNode.equals(selectedWay.getNode(posn0-1))) || // previous node
688                    (posn0 < selectedWay.getNodesCount()-1) && targetNode.equals(selectedWay.getNode(posn0+1))) {  // next node
689                getCurrentDataSet().setSelected(targetNode);
690                lastUsedNode = targetNode;
691                return true;
692            }
693        }
694
695        return false;
696    }
697
698    /**
699     * Finds a node to continue drawing from. Decision is based upon given node and way.
700     * @param selectedNode Currently selected node, may be null
701     * @param selectedWay Currently selected way, may be null
702     * @return Node if a suitable node is found, null otherwise
703     */
704    private Node findNodeToContinueFrom(Node selectedNode, Way selectedWay) {
705        // No nodes or ways have been selected, this occurs when a relation
706        // has been selected or the selection is empty
707        if(selectedNode == null && selectedWay == null)
708            return null;
709
710        if (selectedNode == null) {
711            if (selectedWay.isFirstLastNode(lastUsedNode))
712                return lastUsedNode;
713
714            // We have a way selected, but no suitable node to continue from. Start anew.
715            return null;
716        }
717
718        if (selectedWay == null)
719            return selectedNode;
720
721        if (selectedWay.isFirstLastNode(selectedNode))
722            return selectedNode;
723
724        // We have a way and node selected, but it's not at the start/end of the way. Start anew.
725        return null;
726    }
727
728    @Override
729    public void mouseDragged(MouseEvent e) {
730        mouseMoved(e);
731    }
732
733    @Override
734    public void mouseMoved(MouseEvent e) {
735        if(!Main.map.mapView.isActiveLayerDrawable())
736            return;
737
738        // we copy ctrl/alt/shift from the event just in case our global
739        // keyDetector didn't make it through the security manager. Unclear
740        // if that can ever happen but better be safe.
741        updateKeyModifiers(e);
742        mousePos = e.getPoint();
743        if (snapHelper.isSnapOn() && ctrl)
744            tryToSetBaseSegmentForAngleSnap();
745
746        computeHelperLine();
747        addHighlighting();
748    }
749
750    /**
751     * This method is used to detect segment under mouse and use it as reference for angle snapping
752     */
753    private void tryToSetBaseSegmentForAngleSnap() {
754        WaySegment seg = Main.map.mapView.getNearestWaySegment(mousePos, OsmPrimitive.isSelectablePredicate);
755        if (seg!=null) {
756            snapHelper.setBaseSegment(seg);
757        }
758    }
759
760    /**
761     * This method prepares data required for painting the "helper line" from
762     * the last used position to the mouse cursor. It duplicates some code from
763     * mouseReleased() (FIXME).
764     */
765    private void computeHelperLine() {
766        MapView mv = Main.map.mapView;
767        if (mousePos == null) {
768            // Don't draw the line.
769            currentMouseEastNorth = null;
770            currentBaseNode = null;
771            return;
772        }
773
774        Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
775
776        Node currentMouseNode = null;
777        mouseOnExistingNode = null;
778        mouseOnExistingWays = new HashSet<>();
779
780        showStatusInfo(-1, -1, -1, snapHelper.isSnapOn());
781
782        if (!ctrl && mousePos != null) {
783            currentMouseNode = mv.getNearestNode(mousePos, OsmPrimitive.isSelectablePredicate);
784        }
785
786        // We need this for highlighting and we'll only do so if we actually want to re-use
787        // *and* there is no node nearby (because nodes beat ways when re-using)
788        if(!ctrl && currentMouseNode == null) {
789            List<WaySegment> wss = mv.getNearestWaySegments(mousePos, OsmPrimitive.isSelectablePredicate);
790            for(WaySegment ws : wss) {
791                mouseOnExistingWays.add(ws.way);
792            }
793        }
794
795        if (currentMouseNode != null) {
796            // user clicked on node
797            if (selection.isEmpty()) return;
798            currentMouseEastNorth = currentMouseNode.getEastNorth();
799            mouseOnExistingNode = currentMouseNode;
800        } else {
801            // no node found in clicked area
802            currentMouseEastNorth = mv.getEastNorth(mousePos.x, mousePos.y);
803        }
804
805        determineCurrentBaseNodeAndPreviousNode(selection);
806        if (previousNode == null) {
807            snapHelper.noSnapNow();
808        }
809
810        if (getCurrentBaseNode() == null || getCurrentBaseNode() == currentMouseNode)
811            return; // Don't create zero length way segments.
812
813
814        double curHdg = Math.toDegrees(getCurrentBaseNode().getEastNorth()
815                .heading(currentMouseEastNorth));
816        double baseHdg=-1;
817        if (previousNode != null) {
818            baseHdg =  Math.toDegrees(previousNode.getEastNorth()
819                    .heading(getCurrentBaseNode().getEastNorth()));
820        }
821
822        snapHelper.checkAngleSnapping(currentMouseEastNorth,baseHdg, curHdg);
823
824        // status bar was filled by snapHelper
825    }
826
827    private void showStatusInfo(double angle, double hdg, double distance, boolean activeFlag) {
828        Main.map.statusLine.setAngle(angle);
829        Main.map.statusLine.activateAnglePanel(activeFlag);
830        Main.map.statusLine.setHeading(hdg);
831        Main.map.statusLine.setDist(distance);
832    }
833
834    /**
835     * Helper function that sets fields currentBaseNode and previousNode
836     * @param selection
837     * uses also lastUsedNode field
838     */
839    private void determineCurrentBaseNodeAndPreviousNode(Collection<OsmPrimitive>  selection) {
840        Node selectedNode = null;
841        Way selectedWay = null;
842        for (OsmPrimitive p : selection) {
843            if (p instanceof Node) {
844                if (selectedNode != null)
845                    return;
846                selectedNode = (Node) p;
847            } else if (p instanceof Way) {
848                if (selectedWay != null)
849                    return;
850                selectedWay = (Way) p;
851            }
852        }
853        // we are here, if not more than 1 way or node is selected,
854
855        // the node from which we make a connection
856        currentBaseNode = null;
857        previousNode = null;
858
859        // Try to find an open way to measure angle from it. The way is not to be continued!
860        // warning: may result in changes of currentBaseNode and previousNode
861        // please remove if bugs arise
862        if (selectedWay == null && selectedNode != null) {
863            for (OsmPrimitive p: selectedNode.getReferrers()) {
864                if (p.isUsable() && p instanceof Way && ((Way) p).isFirstLastNode(selectedNode)) {
865                    if (selectedWay!=null) { // two uncontinued ways, nothing to take as reference
866                        selectedWay=null;
867                        break;
868                    } else {
869                        // set us ~continue this way (measure angle from it)
870                        selectedWay = (Way) p;
871                    }
872                }
873            }
874        }
875
876        if (selectedNode == null) {
877            if (selectedWay == null)
878                return;
879            continueWayFromNode(selectedWay, lastUsedNode);
880        } else if (selectedWay == null) {
881            currentBaseNode = selectedNode;
882        } else if (!selectedWay.isDeleted()) { // fix #7118
883            continueWayFromNode(selectedWay, selectedNode);
884        }
885    }
886
887    /**
888     * if one of the ends of @param way is given @param node ,
889     * then set  currentBaseNode = node and previousNode = adjacent node of way
890     */
891    private void continueWayFromNode(Way way, Node node) {
892        int n = way.getNodesCount();
893        if (node == way.firstNode()){
894            currentBaseNode = node;
895            if (n>1) previousNode = way.getNode(1);
896        } else if (node == way.lastNode()) {
897            currentBaseNode = node;
898            if (n>1) previousNode = way.getNode(n-2);
899        }
900    }
901
902    /**
903     * Repaint on mouse exit so that the helper line goes away.
904     */
905    @Override
906    public void mouseExited(MouseEvent e) {
907        if(!Main.map.mapView.isActiveLayerDrawable())
908            return;
909        mousePos = e.getPoint();
910        snapHelper.noSnapNow();
911        boolean repaintIssued = removeHighlighting();
912        // force repaint in case snapHelper needs one. If removeHighlighting
913        // caused one already, don’t do it again.
914        if(!repaintIssued) {
915            Main.map.mapView.repaint();
916        }
917    }
918
919    /**
920     * @return If the node is the end of exactly one way, return this.
921     *  <code>null</code> otherwise.
922     */
923    public static Way getWayForNode(Node n) {
924        Way way = null;
925        for (Way w : Utils.filteredCollection(n.getReferrers(), Way.class)) {
926            if (!w.isUsable() || w.getNodesCount() < 1) {
927                continue;
928            }
929            Node firstNode = w.getNode(0);
930            Node lastNode = w.getNode(w.getNodesCount() - 1);
931            if ((firstNode == n || lastNode == n) && (firstNode != lastNode)) {
932                if (way != null)
933                    return null;
934                way = w;
935            }
936        }
937        return way;
938    }
939
940    /**
941     * Replies the current base node, after having checked it is still usable (see #11105).
942     * @return the current base node (can be null). If not-null, it's guaranteed the node is usable
943     */
944    public Node getCurrentBaseNode() {
945        if (currentBaseNode != null && (currentBaseNode.getDataSet() == null || !currentBaseNode.isUsable())) {
946            currentBaseNode = null;
947        }
948        return currentBaseNode;
949    }
950
951    private static void pruneSuccsAndReverse(List<Integer> is) {
952        HashSet<Integer> is2 = new HashSet<>();
953        for (int i : is) {
954            if (!is2.contains(i - 1) && !is2.contains(i + 1)) {
955                is2.add(i);
956            }
957        }
958        is.clear();
959        is.addAll(is2);
960        Collections.sort(is);
961        Collections.reverse(is);
962    }
963
964    /**
965     * Adjusts the position of a node to lie on a segment (or a segment
966     * intersection).
967     *
968     * If one or more than two segments are passed, the node is adjusted
969     * to lie on the first segment that is passed.
970     *
971     * If two segments are passed, the node is adjusted to be at their
972     * intersection.
973     *
974     * No action is taken if no segments are passed.
975     *
976     * @param segs the segments to use as a reference when adjusting
977     * @param n the node to adjust
978     */
979    private static void adjustNode(Collection<Pair<Node,Node>> segs, Node n) {
980
981        switch (segs.size()) {
982        case 0:
983            return;
984        case 2:
985            // This computes the intersection between
986            // the two segments and adjusts the node position.
987            Iterator<Pair<Node,Node>> i = segs.iterator();
988            Pair<Node,Node> seg = i.next();
989            EastNorth A = seg.a.getEastNorth();
990            EastNorth B = seg.b.getEastNorth();
991            seg = i.next();
992            EastNorth C = seg.a.getEastNorth();
993            EastNorth D = seg.b.getEastNorth();
994
995            double u=det(B.east() - A.east(), B.north() - A.north(), C.east() - D.east(), C.north() - D.north());
996
997            // Check for parallel segments and do nothing if they are
998            // In practice this will probably only happen when a way has been duplicated
999
1000            if (u == 0)
1001                return;
1002
1003            // q is a number between 0 and 1
1004            // It is the point in the segment where the intersection occurs
1005            // if the segment is scaled to lenght 1
1006
1007            double q = det(B.north() - C.north(), B.east() - C.east(), D.north() - C.north(), D.east() - C.east()) / u;
1008            EastNorth intersection = new EastNorth(
1009                    B.east() + q * (A.east() - B.east()),
1010                    B.north() + q * (A.north() - B.north()));
1011
1012
1013            // only adjust to intersection if within snapToIntersectionThreshold pixel of mouse click; otherwise
1014            // fall through to default action.
1015            // (for semi-parallel lines, intersection might be miles away!)
1016            if (Main.map.mapView.getPoint2D(n).distance(Main.map.mapView.getPoint2D(intersection)) < snapToIntersectionThreshold) {
1017                n.setEastNorth(intersection);
1018                return;
1019            }
1020        default:
1021            EastNorth P = n.getEastNorth();
1022            seg = segs.iterator().next();
1023            A = seg.a.getEastNorth();
1024            B = seg.b.getEastNorth();
1025            double a = P.distanceSq(B);
1026            double b = P.distanceSq(A);
1027            double c = A.distanceSq(B);
1028            q = (a - b + c) / (2*c);
1029            n.setEastNorth(new EastNorth(B.east() + q * (A.east() - B.east()), B.north() + q * (A.north() - B.north())));
1030        }
1031    }
1032
1033    // helper for adjustNode
1034    static double det(double a, double b, double c, double d) {
1035        return a * d - b * c;
1036    }
1037
1038    private void tryToMoveNodeOnIntersection(List<WaySegment> wss, Node n) {
1039        if (wss.isEmpty())
1040            return;
1041        WaySegment ws = wss.get(0);
1042        EastNorth p1=ws.getFirstNode().getEastNorth();
1043        EastNorth p2=ws.getSecondNode().getEastNorth();
1044        if (snapHelper.dir2 != null && getCurrentBaseNode() != null) {
1045            EastNorth xPoint = Geometry.getSegmentSegmentIntersection(p1, p2, snapHelper.dir2,
1046                    getCurrentBaseNode().getEastNorth());
1047            if (xPoint!=null) {
1048                n.setEastNorth(xPoint);
1049            }
1050        }
1051    }
1052    /**
1053     * Takes the data from computeHelperLine to determine which ways/nodes should be highlighted
1054     * (if feature enabled). Also sets the target cursor if appropriate. It adds the to-be-
1055     * highlighted primitives to newHighlights but does not actually highlight them. This work is
1056     * done in redrawIfRequired. This means, calling addHighlighting() without redrawIfRequired()
1057     * will leave the data in an inconsistent state.
1058     *
1059     * The status bar derives its information from oldHighlights, so in order to update the status
1060     * bar both addHighlighting() and repaintIfRequired() are needed, since former fills newHighlights
1061     * and latter processes them into oldHighlights.
1062     */
1063    private void addHighlighting() {
1064        newHighlights = new HashSet<>();
1065
1066        // if ctrl key is held ("no join"), don't highlight anything
1067        if (ctrl) {
1068            Main.map.mapView.setNewCursor(cursor, this);
1069            redrawIfRequired();
1070            return;
1071        }
1072
1073        // This happens when nothing is selected, but we still want to highlight the "target node"
1074        if (mouseOnExistingNode == null && getCurrentDataSet().getSelected().isEmpty()
1075                && mousePos != null) {
1076            mouseOnExistingNode = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive.isSelectablePredicate);
1077        }
1078
1079        if (mouseOnExistingNode != null) {
1080            Main.map.mapView.setNewCursor(cursorJoinNode, this);
1081            newHighlights.add(mouseOnExistingNode);
1082            redrawIfRequired();
1083            return;
1084        }
1085
1086        // Insert the node into all the nearby way segments
1087        if (mouseOnExistingWays.isEmpty()) {
1088            Main.map.mapView.setNewCursor(cursor, this);
1089            redrawIfRequired();
1090            return;
1091        }
1092
1093        Main.map.mapView.setNewCursor(cursorJoinWay, this);
1094        newHighlights.addAll(mouseOnExistingWays);
1095        redrawIfRequired();
1096    }
1097
1098    /**
1099     * Removes target highlighting from primitives. Issues repaint if required.
1100     * Returns true if a repaint has been issued.
1101     */
1102    private boolean removeHighlighting() {
1103        newHighlights = new HashSet<>();
1104        return redrawIfRequired();
1105    }
1106
1107    @Override
1108    public void paint(Graphics2D g, MapView mv, Bounds box) {
1109        // sanity checks
1110        if (Main.map.mapView == null || mousePos == null
1111                // don't draw line if we don't know where from or where to
1112                || getCurrentBaseNode() == null || currentMouseEastNorth == null
1113                // don't draw line if mouse is outside window
1114                || !Main.map.mapView.getBounds().contains(mousePos))
1115            return;
1116
1117        Graphics2D g2 = g;
1118        snapHelper.drawIfNeeded(g2,mv);
1119        if (!drawHelperLine || wayIsFinished || shift)
1120            return;
1121
1122        if (!snapHelper.isActive()) { // else use color and stoke from  snapHelper.draw
1123            g2.setColor(rubberLineColor);
1124            g2.setStroke(rubberLineStroke);
1125        } else if (!snapHelper.drawConstructionGeometry)
1126            return;
1127        GeneralPath b = new GeneralPath();
1128        Point p1=mv.getPoint(getCurrentBaseNode());
1129        Point p2=mv.getPoint(currentMouseEastNorth);
1130
1131        double t = Math.atan2(p2.y-p1.y, p2.x-p1.x) + Math.PI;
1132
1133        b.moveTo(p1.x,p1.y); b.lineTo(p2.x, p2.y);
1134
1135        // if alt key is held ("start new way"), draw a little perpendicular line
1136        if (alt) {
1137            b.moveTo((int)(p1.x + 8*Math.cos(t+PHI)), (int)(p1.y + 8*Math.sin(t+PHI)));
1138            b.lineTo((int)(p1.x + 8*Math.cos(t-PHI)), (int)(p1.y + 8*Math.sin(t-PHI)));
1139        }
1140
1141        g2.draw(b);
1142        g2.setStroke(BASIC_STROKE);
1143    }
1144
1145    @Override
1146    public String getModeHelpText() {
1147        StringBuilder rv;
1148        /*
1149         *  No modifiers: all (Connect, Node Re-Use, Auto-Weld)
1150         *  CTRL: disables node re-use, auto-weld
1151         *  Shift: do not make connection
1152         *  ALT: make connection but start new way in doing so
1153         */
1154
1155        /*
1156         * Status line text generation is split into two parts to keep it maintainable.
1157         * First part looks at what will happen to the new node inserted on click and
1158         * the second part will look if a connection is made or not.
1159         *
1160         * Note that this help text is not absolutely accurate as it doesn't catch any special
1161         * cases (e.g. when preventing <---> ways). The only special that it catches is when
1162         * a way is about to be finished.
1163         *
1164         * First check what happens to the new node.
1165         */
1166
1167        // oldHighlights stores the current highlights. If this
1168        // list is empty we can assume that we won't do any joins
1169        if (ctrl || oldHighlights.isEmpty()) {
1170            rv = new StringBuilder(tr("Create new node."));
1171        } else {
1172            // oldHighlights may store a node or way, check if it's a node
1173            OsmPrimitive x = oldHighlights.iterator().next();
1174            if (x instanceof Node) {
1175                rv = new StringBuilder(tr("Select node under cursor."));
1176            } else {
1177                rv = new StringBuilder(trn("Insert new node into way.", "Insert new node into {0} ways.",
1178                        oldHighlights.size(), oldHighlights.size()));
1179            }
1180        }
1181
1182        /*
1183         * Check whether a connection will be made
1184         */
1185        if (getCurrentBaseNode() != null && !wayIsFinished) {
1186            if (alt) {
1187                rv.append(" ").append(tr("Start new way from last node."));
1188            } else {
1189                rv.append(" ").append(tr("Continue way from last node."));
1190            }
1191            if (snapHelper.isSnapOn()) {
1192                rv.append(" ").append(tr("Angle snapping active."));
1193            }
1194        }
1195
1196        Node n = mouseOnExistingNode;
1197        /*
1198         * Handle special case: Highlighted node == selected node => finish drawing
1199         */
1200        if (n != null && getCurrentDataSet() != null && getCurrentDataSet().getSelectedNodes().contains(n)) {
1201            if (wayIsFinished) {
1202                rv = new StringBuilder(tr("Select node under cursor."));
1203            } else {
1204                rv = new StringBuilder(tr("Finish drawing."));
1205            }
1206        }
1207
1208        /*
1209         * Handle special case: Self-Overlapping or closing way
1210         */
1211        if (getCurrentDataSet() != null && !getCurrentDataSet().getSelectedWays().isEmpty() && !wayIsFinished && !alt) {
1212            Way w = getCurrentDataSet().getSelectedWays().iterator().next();
1213            for (Node m : w.getNodes()) {
1214                if (m.equals(mouseOnExistingNode) || mouseOnExistingWays.contains(w)) {
1215                    rv.append(" ").append(tr("Finish drawing."));
1216                    break;
1217                }
1218            }
1219        }
1220        return rv.toString();
1221    }
1222
1223    /**
1224     * Get selected primitives, while draw action is in progress.
1225     *
1226     * While drawing a way, technically the last node is selected.
1227     * This is inconvenient when the user tries to add/edit tags to the way.
1228     * For this case, this method returns the current way as selection,
1229     * to work around this issue.
1230     * Otherwise the normal selection of the current data layer is returned.
1231     */
1232    public Collection<OsmPrimitive> getInProgressSelection() {
1233        DataSet ds = getCurrentDataSet();
1234        if (ds == null) return null;
1235        if (getCurrentBaseNode() != null && !ds.getSelected().isEmpty()) {
1236            Way continueFrom = getWayForNode(getCurrentBaseNode());
1237            if (continueFrom != null)
1238                return Collections.<OsmPrimitive>singleton(continueFrom);
1239        }
1240        return ds.getSelected();
1241    }
1242
1243    @Override
1244    public boolean layerIsSupported(Layer l) {
1245        return l instanceof OsmDataLayer;
1246    }
1247
1248    @Override
1249    protected void updateEnabledState() {
1250        setEnabled(getEditLayer() != null);
1251    }
1252
1253    @Override
1254    public void destroy() {
1255        super.destroy();
1256        snapChangeAction.destroy();
1257    }
1258
1259    public class BackSpaceAction extends AbstractAction {
1260
1261        @Override
1262        public void actionPerformed(ActionEvent e) {
1263            Main.main.undoRedo.undo();
1264            Node n=null;
1265            Command lastCmd=Main.main.undoRedo.commands.peekLast();
1266            if (lastCmd==null) return;
1267            for (OsmPrimitive p: lastCmd.getParticipatingPrimitives()) {
1268                if (p instanceof Node) {
1269                    if (n==null) {
1270                        n=(Node) p; // found one node
1271                        wayIsFinished=false;
1272                    }  else {
1273                        // if more than 1 node were affected by previous command,
1274                        // we have no way to continue, so we forget about found node
1275                        n=null;
1276                        break;
1277                    }
1278                }
1279            }
1280            // select last added node - maybe we will continue drawing from it
1281            if (n!=null) {
1282                getCurrentDataSet().addSelected(n);
1283            }
1284        }
1285    }
1286
1287    private class SnapHelper {
1288        boolean snapOn; // snapping is turned on
1289
1290        private boolean active; // snapping is active for current mouse position
1291        private boolean fixed; // snap angle is fixed
1292        private boolean absoluteFix; // snap angle is absolute
1293
1294        private boolean drawConstructionGeometry;
1295        private boolean showProjectedPoint;
1296        private boolean showAngle;
1297
1298        private boolean snapToProjections;
1299
1300        EastNorth dir2;
1301        EastNorth projected;
1302        String labelText;
1303        double lastAngle;
1304
1305        double customBaseHeading=-1; // angle of base line, if not last segment)
1306        private EastNorth segmentPoint1; // remembered first point of base segment
1307        private EastNorth segmentPoint2; // remembered second point of base segment
1308        private EastNorth projectionSource; // point that we are projecting to the line
1309
1310        double[] snapAngles;
1311        double snapAngleTolerance;
1312
1313        double pe,pn; // (pe,pn) - direction of snapping line
1314        double e0,n0; // (e0,n0) - origin of snapping line
1315
1316        final String fixFmt="%d "+tr("FIX");
1317        Color snapHelperColor;
1318        private Color highlightColor;
1319
1320        private Stroke normalStroke;
1321        private Stroke helperStroke;
1322        private Stroke highlightStroke;
1323
1324        JCheckBoxMenuItem checkBox;
1325        public final Color ORANGE_TRANSPARENT = new Color(Color.ORANGE.getRed(),Color.ORANGE.getGreen(),Color.ORANGE.getBlue(),128);
1326
1327        public void init() {
1328            snapOn=false;
1329            checkBox.setState(snapOn);
1330            fixed=false; absoluteFix=false;
1331
1332            Collection<String> angles = Main.pref.getCollection("draw.anglesnap.angles",
1333                    Arrays.asList("0","30","45","60","90","120","135","150","180"));
1334
1335            snapAngles = new double[2*angles.size()];
1336            int i=0;
1337            for (String s: angles) {
1338                try {
1339                    snapAngles[i] = Double.parseDouble(s); i++;
1340                    snapAngles[i] = 360-Double.parseDouble(s); i++;
1341                } catch (NumberFormatException e) {
1342                    Main.warn("Incorrect number in draw.anglesnap.angles preferences: "+s);
1343                    snapAngles[i]=0;i++;
1344                    snapAngles[i]=0;i++;
1345                }
1346            }
1347            snapAngleTolerance = Main.pref.getDouble("draw.anglesnap.tolerance", 5.0);
1348            drawConstructionGeometry = Main.pref.getBoolean("draw.anglesnap.drawConstructionGeometry", true);
1349            showProjectedPoint = Main.pref.getBoolean("draw.anglesnap.drawProjectedPoint", true);
1350            snapToProjections = Main.pref.getBoolean("draw.anglesnap.projectionsnap", true);
1351
1352            showAngle = Main.pref.getBoolean("draw.anglesnap.showAngle", true);
1353            useRepeatedShortcut = Main.pref.getBoolean("draw.anglesnap.toggleOnRepeatedA", true);
1354
1355            normalStroke = rubberLineStroke;
1356            snapHelperColor = Main.pref.getColor(marktr("draw angle snap"), Color.ORANGE);
1357
1358            highlightColor = Main.pref.getColor(marktr("draw angle snap highlight"), ORANGE_TRANSPARENT);
1359            highlightStroke = GuiHelper.getCustomizedStroke(Main.pref.get("draw.anglesnap.stroke.highlight","10"));
1360            helperStroke = GuiHelper.getCustomizedStroke(Main.pref.get("draw.anglesnap.stroke.helper","1 4"));
1361        }
1362
1363        public void saveAngles(String ... angles) {
1364            Main.pref.putCollection("draw.anglesnap.angles", Arrays.asList(angles));
1365        }
1366
1367        public  void setMenuCheckBox(JCheckBoxMenuItem checkBox) {
1368            this.checkBox = checkBox;
1369        }
1370
1371        public  void drawIfNeeded(Graphics2D g2, MapView mv) {
1372            if (!snapOn || !active)
1373                return;
1374            Point p1=mv.getPoint(getCurrentBaseNode());
1375            Point p2=mv.getPoint(dir2);
1376            Point p3=mv.getPoint(projected);
1377            GeneralPath b;
1378            if (drawConstructionGeometry) {
1379                g2.setColor(snapHelperColor);
1380                g2.setStroke(helperStroke);
1381
1382                b = new GeneralPath();
1383                if (absoluteFix) {
1384                    b.moveTo(p2.x,p2.y);
1385                    b.lineTo(2*p1.x-p2.x,2*p1.y-p2.y); // bi-directional line
1386                } else {
1387                    b.moveTo(p2.x,p2.y);
1388                    b.lineTo(p3.x,p3.y);
1389                }
1390                g2.draw(b);
1391            }
1392            if (projectionSource != null) {
1393                g2.setColor(snapHelperColor);
1394                g2.setStroke(helperStroke);
1395                b = new GeneralPath();
1396                b.moveTo(p3.x,p3.y);
1397                Point pp=mv.getPoint(projectionSource);
1398                b.lineTo(pp.x,pp.y);
1399                g2.draw(b);
1400            }
1401
1402            if (customBaseHeading >= 0) {
1403                g2.setColor(highlightColor);
1404                g2.setStroke(highlightStroke);
1405                b = new GeneralPath();
1406                Point pp1=mv.getPoint(segmentPoint1);
1407                Point pp2=mv.getPoint(segmentPoint2);
1408                b.moveTo(pp1.x,pp1.y);
1409                b.lineTo(pp2.x,pp2.y);
1410                g2.draw(b);
1411            }
1412
1413            g2.setColor(rubberLineColor);
1414            g2.setStroke(normalStroke);
1415            b = new GeneralPath();
1416            b.moveTo(p1.x,p1.y);
1417            b.lineTo(p3.x,p3.y);
1418            g2.draw(b);
1419
1420            g2.drawString(labelText, p3.x-5, p3.y+20);
1421            if (showProjectedPoint) {
1422                g2.setStroke(normalStroke);
1423                g2.drawOval(p3.x-5, p3.y-5, 10, 10); // projected point
1424            }
1425
1426            g2.setColor(snapHelperColor);
1427            g2.setStroke(helperStroke);
1428        }
1429
1430        /* If mouse position is close to line at 15-30-45-... angle, remembers this direction
1431         */
1432        public void checkAngleSnapping(EastNorth currentEN, double baseHeading, double curHeading) {
1433            EastNorth p0 = getCurrentBaseNode().getEastNorth();
1434            EastNorth snapPoint = currentEN;
1435            double angle = -1;
1436
1437            double activeBaseHeading = (customBaseHeading>=0)? customBaseHeading : baseHeading;
1438
1439            if (snapOn && (activeBaseHeading>=0)) {
1440                angle = curHeading - activeBaseHeading;
1441                if (angle < 0) {
1442                    angle+=360;
1443                }
1444                if (angle > 360) {
1445                    angle=0;
1446                }
1447
1448                double nearestAngle;
1449                if (fixed) {
1450                    nearestAngle = lastAngle; // if direction is fixed use previous angle
1451                    active = true;
1452                } else {
1453                    nearestAngle = getNearestAngle(angle);
1454                    if (getAngleDelta(nearestAngle, angle) < snapAngleTolerance) {
1455                        active = (customBaseHeading>=0)? true : Math.abs(nearestAngle - 180) > 1e-3;
1456                        // if angle is to previous segment, exclude 180 degrees
1457                        lastAngle = nearestAngle;
1458                    } else {
1459                        active=false;
1460                    }
1461                }
1462
1463                if (active) {
1464                    double phi;
1465                    e0 = p0.east();
1466                    n0 = p0.north();
1467                    buildLabelText((nearestAngle<=180) ? nearestAngle : nearestAngle-360);
1468
1469                    phi = (nearestAngle + activeBaseHeading) * Math.PI / 180;
1470                    // (pe,pn) - direction of snapping line
1471                    pe = Math.sin(phi);
1472                    pn = Math.cos(phi);
1473                    double scale = 20 * Main.map.mapView.getDist100Pixel();
1474                    dir2 = new EastNorth(e0 + scale * pe, n0 + scale * pn);
1475                    snapPoint = getSnapPoint(currentEN);
1476                } else {
1477                    noSnapNow();
1478                }
1479            }
1480
1481            // find out the distance, in metres, between the base point and projected point
1482            LatLon mouseLatLon = Main.map.mapView.getProjection().eastNorth2latlon(snapPoint);
1483            double distance = getCurrentBaseNode().getCoor().greatCircleDistance(mouseLatLon);
1484            double hdg = Math.toDegrees(p0.heading(snapPoint));
1485            // heading of segment from current to calculated point, not to mouse position
1486
1487            if (baseHeading >=0 ) { // there is previous line segment with some heading
1488                angle = hdg - baseHeading;
1489                if (angle < 0) {
1490                    angle+=360;
1491                }
1492                if (angle > 360) {
1493                    angle=0;
1494                }
1495            }
1496            showStatusInfo(angle, hdg, distance, isSnapOn());
1497        }
1498
1499        private void buildLabelText(double nearestAngle) {
1500            if (showAngle) {
1501                if (fixed) {
1502                    if (absoluteFix) {
1503                        labelText = "=";
1504                    } else {
1505                        labelText = String.format(fixFmt, (int) nearestAngle);
1506                    }
1507                } else {
1508                    labelText = String.format("%d", (int) nearestAngle);
1509                }
1510            } else {
1511                if (fixed) {
1512                    if (absoluteFix) {
1513                        labelText = "=";
1514                    } else {
1515                        labelText = String.format(tr("FIX"), 0);
1516                    }
1517                } else {
1518                    labelText = "";
1519                }
1520            }
1521        }
1522
1523        public  EastNorth getSnapPoint(EastNorth p) {
1524            if (!active)
1525                return p;
1526            double de=p.east()-e0;
1527            double dn=p.north()-n0;
1528            double l = de*pe+dn*pn;
1529            double delta = Main.map.mapView.getDist100Pixel()/20;
1530            if (!absoluteFix && l<delta) {
1531                active=false;
1532                return p;
1533            } //  do not go backward!
1534
1535            projectionSource=null;
1536            if (snapToProjections) {
1537                DataSet ds = getCurrentDataSet();
1538                Collection<Way> selectedWays = ds.getSelectedWays();
1539                if (selectedWays.size()==1) {
1540                    Way w = selectedWays.iterator().next();
1541                    Collection <EastNorth> pointsToProject = new ArrayList<>();
1542                    if (w.getNodesCount()<1000) {
1543                        for (Node n: w.getNodes()) {
1544                            pointsToProject.add(n.getEastNorth());
1545                        }
1546                    }
1547                    if (customBaseHeading >=0 ) {
1548                        pointsToProject.add(segmentPoint1);
1549                        pointsToProject.add(segmentPoint2);
1550                    }
1551                    EastNorth enOpt=null;
1552                    double dOpt=1e5;
1553                    for (EastNorth en: pointsToProject) { // searching for besht projection
1554                        double l1 = (en.east()-e0)*pe+(en.north()-n0)*pn;
1555                        double d1 = Math.abs(l1-l);
1556                        if (d1 < delta && d1 < dOpt) {
1557                            l=l1;
1558                            enOpt = en;
1559                            dOpt = d1;
1560                        }
1561                    }
1562                    if (enOpt!=null) {
1563                        projectionSource =  enOpt;
1564                    }
1565                }
1566            }
1567            return projected = new EastNorth(e0+l*pe, n0+l*pn);
1568        }
1569
1570
1571        public void noSnapNow() {
1572            active=false;
1573            dir2=null; projected=null;
1574            labelText=null;
1575        }
1576
1577        public void setBaseSegment(WaySegment seg) {
1578            if (seg==null) return;
1579            segmentPoint1=seg.getFirstNode().getEastNorth();
1580            segmentPoint2=seg.getSecondNode().getEastNorth();
1581
1582            double hdg = segmentPoint1.heading(segmentPoint2);
1583            hdg=Math.toDegrees(hdg);
1584            if (hdg<0) {
1585                hdg+=360;
1586            }
1587            if (hdg>360) {
1588                hdg-=360;
1589            }
1590            customBaseHeading=hdg;
1591        }
1592
1593        private void nextSnapMode() {
1594            if (snapOn) {
1595                // turn off snapping if we are in fixed mode or no actile snapping line exist
1596                if (fixed || !active) { snapOn=false; unsetFixedMode(); } else {
1597                    setFixedMode();
1598                }
1599            } else {
1600                snapOn=true;
1601                unsetFixedMode();
1602            }
1603            checkBox.setState(snapOn);
1604            customBaseHeading=-1;
1605        }
1606
1607        private void enableSnapping() {
1608            snapOn = true;
1609            checkBox.setState(snapOn);
1610            customBaseHeading=-1;
1611            unsetFixedMode();
1612        }
1613
1614        private void toggleSnapping() {
1615            snapOn = !snapOn;
1616            checkBox.setState(snapOn);
1617            customBaseHeading=-1;
1618            unsetFixedMode();
1619        }
1620
1621        public void setFixedMode() {
1622            if (active) {
1623                fixed=true;
1624            }
1625        }
1626
1627
1628        public  void unsetFixedMode() {
1629            fixed=false;
1630            absoluteFix=false;
1631            lastAngle=0;
1632            active=false;
1633        }
1634
1635        public  boolean isActive() {
1636            return active;
1637        }
1638
1639        public  boolean isSnapOn() {
1640            return snapOn;
1641        }
1642
1643        private double getNearestAngle(double angle) {
1644            double delta,minDelta=1e5, bestAngle=0.0;
1645            for (double snapAngle : snapAngles) {
1646                delta = getAngleDelta(angle, snapAngle);
1647                if (delta < minDelta) {
1648                    minDelta = delta;
1649                    bestAngle = snapAngle;
1650                }
1651            }
1652            if (Math.abs(bestAngle-360) < 1e-3) {
1653                bestAngle=0;
1654            }
1655            return bestAngle;
1656        }
1657
1658        private double getAngleDelta(double a, double b) {
1659            double delta = Math.abs(a-b);
1660            if (delta>180)
1661                return 360-delta;
1662            else
1663                return delta;
1664        }
1665
1666        private void unFixOrTurnOff() {
1667            if (absoluteFix) {
1668                unsetFixedMode();
1669            } else {
1670                toggleSnapping();
1671            }
1672        }
1673
1674        MouseListener anglePopupListener = new PopupMenuLauncher( new JPopupMenu() {
1675            JCheckBoxMenuItem repeatedCb = new JCheckBoxMenuItem(new AbstractAction(tr("Toggle snapping by {0}", getShortcut().getKeyText())){
1676                @Override public void actionPerformed(ActionEvent e) {
1677                    boolean sel=((JCheckBoxMenuItem) e.getSource()).getState();
1678                    Main.pref.put("draw.anglesnap.toggleOnRepeatedA", sel);
1679                    init();
1680                }
1681            });
1682            JCheckBoxMenuItem helperCb = new JCheckBoxMenuItem(new AbstractAction(tr("Show helper geometry")){
1683                @Override public void actionPerformed(ActionEvent e) {
1684                    boolean sel=((JCheckBoxMenuItem) e.getSource()).getState();
1685                    Main.pref.put("draw.anglesnap.drawConstructionGeometry", sel);
1686                    Main.pref.put("draw.anglesnap.drawProjectedPoint", sel);
1687                    Main.pref.put("draw.anglesnap.showAngle", sel);
1688                    init();
1689                    enableSnapping();
1690                }
1691            });
1692            JCheckBoxMenuItem projectionCb = new JCheckBoxMenuItem(new AbstractAction(tr("Snap to node projections")){
1693                @Override public void actionPerformed(ActionEvent e) {
1694                    boolean sel=((JCheckBoxMenuItem) e.getSource()).getState();
1695                    Main.pref.put("draw.anglesnap.projectionsnap", sel);
1696                    init();
1697                    enableSnapping();
1698                }
1699            });
1700            {
1701                helperCb.setState(Main.pref.getBoolean("draw.anglesnap.drawConstructionGeometry",true));
1702                projectionCb.setState(Main.pref.getBoolean("draw.anglesnap.projectionsnapgvff",true));
1703                repeatedCb.setState(Main.pref.getBoolean("draw.anglesnap.toggleOnRepeatedA",true));
1704                add(repeatedCb);
1705                add(helperCb);
1706                add(projectionCb);
1707                add(new AbstractAction(tr("Disable")) {
1708                    @Override public void actionPerformed(ActionEvent e) {
1709                        saveAngles("180");
1710                        init();
1711                        enableSnapping();
1712                    }
1713                });
1714                add(new AbstractAction(tr("0,90,...")) {
1715                    @Override public void actionPerformed(ActionEvent e) {
1716                        saveAngles("0","90","180");
1717                        init();
1718                        enableSnapping();
1719                    }
1720                });
1721                add(new AbstractAction(tr("0,45,90,...")) {
1722                    @Override public void actionPerformed(ActionEvent e) {
1723                        saveAngles("0","45","90","135","180");
1724                        init();
1725                        enableSnapping();
1726                    }
1727                });
1728                add(new AbstractAction(tr("0,30,45,60,90,...")) {
1729                    @Override public void actionPerformed(ActionEvent e) {
1730                        saveAngles("0","30","45","60","90","120","135","150","180");
1731                        init();
1732                        enableSnapping();
1733                    }
1734                });
1735            }
1736        }) {
1737            @Override
1738            public void mouseClicked(MouseEvent e) {
1739                super.mouseClicked(e);
1740                if (e.getButton() == MouseEvent.BUTTON1) {
1741                    toggleSnapping();
1742                    updateStatusLine();
1743                }
1744            }
1745        };
1746    }
1747
1748    private class SnapChangeAction extends JosmAction {
1749        public SnapChangeAction() {
1750            super(tr("Angle snapping"), /* ICON() */ "anglesnap",
1751                    tr("Switch angle snapping mode while drawing"), null, false);
1752            putValue("help", ht("/Action/Draw/AngleSnap"));
1753        }
1754
1755        @Override
1756        public void actionPerformed(ActionEvent e) {
1757            if (snapHelper!=null) {
1758                snapHelper.toggleSnapping();
1759            }
1760        }
1761    }
1762}