1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package utilsplugin2;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.HashSet;
|
---|
11 | import java.util.LinkedList;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Set;
|
---|
14 |
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
19 | import org.openstreetmap.josm.command.AddCommand;
|
---|
20 | import org.openstreetmap.josm.command.Command;
|
---|
21 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
22 | import org.openstreetmap.josm.data.osm.Node;
|
---|
23 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
24 | import org.openstreetmap.josm.data.osm.Way;
|
---|
25 | import org.openstreetmap.josm.tools.Geometry;
|
---|
26 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
27 |
|
---|
28 | public class AddIntersectionsAction extends JosmAction {
|
---|
29 | public AddIntersectionsAction() {
|
---|
30 | super(tr("Add nodes at intersections"), "addintersect", tr("Add missing nodes at intersections of selected ways."),
|
---|
31 | Shortcut.registerShortcut("tools:addintersect", tr("Tool: {0}", tr("Add nodes at intersections")), KeyEvent.VK_N, Shortcut.ALT_CTRL_SHIFT), true);
|
---|
32 | putValue("help", ht("/Action/AddIntersections"));
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public void actionPerformed(ActionEvent arg0) {
|
---|
37 | if (!isEnabled())
|
---|
38 | return;
|
---|
39 | List<Way> ways = OsmPrimitive.getFilteredList(getCurrentDataSet().getSelected(), Way.class);
|
---|
40 | if (ways.isEmpty()) {
|
---|
41 | JOptionPane.showMessageDialog(
|
---|
42 | Main.parent,
|
---|
43 | tr("Please select one or more ways with intersections of segments."),
|
---|
44 | tr("Information"),
|
---|
45 | JOptionPane.INFORMATION_MESSAGE
|
---|
46 | );
|
---|
47 | return;
|
---|
48 | }
|
---|
49 |
|
---|
50 | LinkedList<Command> cmds = new LinkedList<Command>();
|
---|
51 | Geometry.addIntersections(ways, false, cmds);
|
---|
52 | if (!cmds.isEmpty()) {
|
---|
53 | Main.main.undoRedo.add(new SequenceCommand(tr("Add nodes at intersections"),cmds));
|
---|
54 | Set<Node> nodes = new HashSet<Node>(10);
|
---|
55 | // find and select newly added nodes
|
---|
56 | for (Command cmd: cmds) if (cmd instanceof AddCommand){
|
---|
57 | Collection<? extends OsmPrimitive> pp = cmd.getParticipatingPrimitives();
|
---|
58 | for ( OsmPrimitive p : pp) { // find all affected nodes
|
---|
59 | if (p instanceof Node && p.isNew()) nodes.add((Node)p);
|
---|
60 | }
|
---|
61 | if (!nodes.isEmpty()) {
|
---|
62 | getCurrentDataSet().setSelected(nodes);
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | protected void updateEnabledState() {
|
---|
70 | if (getCurrentDataSet() == null) {
|
---|
71 | setEnabled(false);
|
---|
72 | } else {
|
---|
73 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
79 | setEnabled(selection != null && !selection.isEmpty());
|
---|
80 | }
|
---|
81 | }
|
---|