source: subversion/applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/actions/AddIntersectionsAction.java

Last change on this file was 34812, checked in by gerdp, 3 months ago

see #17187:

  • Replace deprecated methods
  • Remove dead code
  • Make Shift+Z (Select last modified nodes) work again + Repated Shift+Z cycles through the commands available in the undo tree + Ignore changes in other layers
  • Make Alt+Shift+Z (Select last modified Ways) work again (same logic as with Shift+Z)
  • fix some javadoc errors


  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.plugins.utilsplugin2.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.ArrayList;
10import java.util.Collection;
11import java.util.HashSet;
12import java.util.LinkedList;
13import java.util.List;
14import java.util.Set;
15
16import javax.swing.JOptionPane;
17
18import org.openstreetmap.josm.actions.JosmAction;
19import org.openstreetmap.josm.command.AddCommand;
20import org.openstreetmap.josm.command.Command;
21import org.openstreetmap.josm.command.SequenceCommand;
22import org.openstreetmap.josm.data.UndoRedoHandler;
23import org.openstreetmap.josm.data.osm.Node;
24import org.openstreetmap.josm.data.osm.OsmPrimitive;
25import org.openstreetmap.josm.data.osm.Way;
26import org.openstreetmap.josm.gui.Notification;
27import org.openstreetmap.josm.tools.Geometry;
28import org.openstreetmap.josm.tools.Shortcut;
29
30/**
31 * Add missing nodes at intersections of selected ways.
32 */
33public class AddIntersectionsAction extends JosmAction {
34
35    /**
36     * Constructs a new {@code AddIntersectionsAction}.
37     */
38    public AddIntersectionsAction() {
39        super(tr("Add nodes at intersections"), "addintersect", tr("Add missing nodes at intersections of selected ways."),
40                Shortcut.registerShortcut("tools:addintersect", tr("Tool: {0}", tr("Add nodes at intersections")),
41                        KeyEvent.VK_I, Shortcut.SHIFT),
42                true);
43        putValue("help", ht("/Action/AddIntersections"));
44    }
45
46    @Override
47    public void actionPerformed(ActionEvent arg0) {
48        if (!isEnabled())
49            return;
50        List<Way> ways = new ArrayList<>(getLayerManager().getEditDataSet().getSelectedWays());
51        if (ways.isEmpty()) {
52            new Notification(
53                    tr("Please select one or more ways with intersections of segments."))
54            .setIcon(JOptionPane.INFORMATION_MESSAGE)
55            .show();
56            return;
57        }
58
59        LinkedList<Command> cmds = new LinkedList<>();
60        Geometry.addIntersections(ways, false, cmds);
61        if (!cmds.isEmpty()) {
62            UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Add nodes at intersections"), cmds));
63            Set<Node> nodes = new HashSet<>(10);
64            // find and select newly added nodes
65            for (Command cmd: cmds) if (cmd instanceof AddCommand) {
66                Collection<? extends OsmPrimitive> pp = cmd.getParticipatingPrimitives();
67                for (OsmPrimitive p : pp) { // find all affected nodes
68                    if (p instanceof Node && p.isNew())
69                        nodes.add((Node) p);
70                }
71                if (!nodes.isEmpty()) {
72                    getLayerManager().getEditDataSet().setSelected(nodes);
73                }
74            }
75        }
76    }
77
78    @Override
79    protected void updateEnabledState() {
80        updateEnabledStateOnCurrentSelection();
81    }
82
83    @Override
84    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
85        setEnabled(selection != null && !selection.isEmpty());
86    }
87}
Note: See TracBrowser for help on using the repository browser.