1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins.utilsplugin2.actions;
|
---|
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.Point;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.LinkedList;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
17 | import org.openstreetmap.josm.command.AddCommand;
|
---|
18 | import org.openstreetmap.josm.command.ChangeNodesCommand;
|
---|
19 | import org.openstreetmap.josm.command.Command;
|
---|
20 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
21 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
22 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
23 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
24 | import org.openstreetmap.josm.data.osm.Node;
|
---|
25 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
26 | import org.openstreetmap.josm.data.osm.Way;
|
---|
27 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
28 | import org.openstreetmap.josm.gui.Notification;
|
---|
29 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Extracts node from its ways.
|
---|
33 | */
|
---|
34 | public class ExtractPointAction extends JosmAction {
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Constructs a new {@code ExtractPointAction}.
|
---|
38 | */
|
---|
39 | public ExtractPointAction() {
|
---|
40 | super(tr("Extract node"), "extnode",
|
---|
41 | tr("Extracts node from a way"),
|
---|
42 | Shortcut.registerShortcut("tools:extnode", tr("More tools: {0}", "Extract node"),
|
---|
43 | KeyEvent.VK_J, Shortcut.ALT_SHIFT), true);
|
---|
44 | putValue("help", ht("/Action/ExtractNode"));
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public void actionPerformed(ActionEvent e) {
|
---|
49 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
50 | Collection<Node> selectedNodes = ds.getSelectedNodes();
|
---|
51 | if (selectedNodes.size() != 1) {
|
---|
52 | new Notification(tr("This tool extracts node from its ways and requires single node to be selected."))
|
---|
53 | .setIcon(JOptionPane.WARNING_MESSAGE).show();
|
---|
54 | return;
|
---|
55 | }
|
---|
56 | final boolean ok = checkAndConfirmOutlyingOperation("extnode",
|
---|
57 | tr("Extract node confirmation"),
|
---|
58 | tr("You are about to extract a node which can have other referrers not yet downloaded."
|
---|
59 | + "<br>"
|
---|
60 | + "This can cause problems because other objects (that you do not see) might use them."
|
---|
61 | + "<br>"
|
---|
62 | + "Do you really want to extract?"),
|
---|
63 | "", // incomplete node should not happen
|
---|
64 | selectedNodes, null);
|
---|
65 | if (!ok) {
|
---|
66 | return;
|
---|
67 | }
|
---|
68 |
|
---|
69 | Node nd = selectedNodes.iterator().next();
|
---|
70 | Node ndCopy = new Node(nd.getCoor());
|
---|
71 | List<Command> cmds = new LinkedList<>();
|
---|
72 |
|
---|
73 | Point p = MainApplication.getMap().mapView.getMousePosition();
|
---|
74 | if (p != null)
|
---|
75 | cmds.add(new MoveCommand(nd, MainApplication.getMap().mapView.getLatLon(p.x, p.y)));
|
---|
76 | List<OsmPrimitive> refs = nd.getReferrers();
|
---|
77 | cmds.add(new AddCommand(ds, ndCopy));
|
---|
78 |
|
---|
79 | for (OsmPrimitive pr: refs) {
|
---|
80 | if (pr instanceof Way) {
|
---|
81 | Way w = (Way) pr;
|
---|
82 | List<Node> nodes = w.getNodes();
|
---|
83 | int idx = nodes.indexOf(nd);
|
---|
84 | nodes.set(idx, ndCopy); // replace node with its copy
|
---|
85 | cmds.add(new ChangeNodesCommand(w, nodes));
|
---|
86 | }
|
---|
87 | }
|
---|
88 | if (cmds.size() > 1) UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Extract node from line"), cmds));
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | protected void updateEnabledState() {
|
---|
93 | updateEnabledStateOnCurrentSelection();
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
98 | if (selection == null) {
|
---|
99 | setEnabled(false);
|
---|
100 | return;
|
---|
101 | }
|
---|
102 | setEnabled(selection.size() == 1);
|
---|
103 | }
|
---|
104 | }
|
---|