source: osm/applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/actions/ExtractPointAction.java

Last change on this file was 35615, checked in by GerdP, 3 years ago

fix #18666 Extract node (alt+shift-J) Should not work outside downloaded area

  • don't disable it but show confirmation dialog similar to UnglueAction

Adds new I18n strings for the plugin, so size will grow.

File size: 3.9 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.Point;
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.util.Collection;
11import java.util.LinkedList;
12import java.util.List;
13
14import javax.swing.JOptionPane;
15
16import org.openstreetmap.josm.actions.JosmAction;
17import org.openstreetmap.josm.command.AddCommand;
18import org.openstreetmap.josm.command.ChangeNodesCommand;
19import org.openstreetmap.josm.command.Command;
20import org.openstreetmap.josm.command.MoveCommand;
21import org.openstreetmap.josm.command.SequenceCommand;
22import org.openstreetmap.josm.data.UndoRedoHandler;
23import org.openstreetmap.josm.data.osm.DataSet;
24import org.openstreetmap.josm.data.osm.Node;
25import org.openstreetmap.josm.data.osm.OsmPrimitive;
26import org.openstreetmap.josm.data.osm.Way;
27import org.openstreetmap.josm.gui.MainApplication;
28import org.openstreetmap.josm.gui.Notification;
29import org.openstreetmap.josm.tools.Shortcut;
30
31/**
32 * Extracts node from its ways.
33 */
34public 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}
Note: See TracBrowser for help on using the repository browser.