1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.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.event.ActionEvent;
|
---|
8 | import java.util.Collection;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
12 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
14 | import org.openstreetmap.josm.data.osm.Node;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
17 | import org.openstreetmap.josm.gui.dialogs.LatLonDialog;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * This action displays a dialog with the coordinates of a node where the user can change them,
|
---|
21 | * and when ok is pressed, the node is relocated to the specified position.
|
---|
22 | */
|
---|
23 | public final class MoveNodeAction extends JosmAction {
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Constructs a new {@code MoveNodeAction}.
|
---|
27 | */
|
---|
28 | public MoveNodeAction() {
|
---|
29 | super(tr("Move Node..."), "movenode", tr("Edit latitude and longitude of a node."),
|
---|
30 | null, /* no shortcut */
|
---|
31 | true);
|
---|
32 | putValue("help", ht("/Action/MoveNode"));
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public void actionPerformed(ActionEvent e) {
|
---|
37 | Collection<Node> selNodes = getLayerManager().getEditDataSet().getSelectedNodes();
|
---|
38 | if (!isEnabled() || selNodes.size() != 1)
|
---|
39 | return;
|
---|
40 |
|
---|
41 | LatLonDialog dialog = new LatLonDialog(Main.parent, tr("Move Node..."), ht("/Action/MoveNode"));
|
---|
42 | Node n = (Node) selNodes.toArray()[0];
|
---|
43 | dialog.setCoordinates(n.getCoor());
|
---|
44 | dialog.showDialog();
|
---|
45 | if (dialog.getValue() != 1)
|
---|
46 | return;
|
---|
47 |
|
---|
48 | LatLon coordinates = dialog.getCoordinates();
|
---|
49 | if (coordinates == null)
|
---|
50 | return;
|
---|
51 |
|
---|
52 | // move the node
|
---|
53 | MainApplication.undoRedo.add(new MoveCommand(n, coordinates));
|
---|
54 | MainApplication.getMap().mapView.repaint();
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | protected void updateEnabledState() {
|
---|
59 | updateEnabledStateOnCurrentSelection();
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
64 | if (selection == null || selection.isEmpty()
|
---|
65 | || selection.stream().map(OsmPrimitive::getDataSet).anyMatch(DataSet::isReadOnly)) {
|
---|
66 | setEnabled(false);
|
---|
67 | return;
|
---|
68 | }
|
---|
69 | if ((selection.size()) == 1 && (selection.toArray()[0] instanceof Node)) {
|
---|
70 | setEnabled(true);
|
---|
71 | } else {
|
---|
72 | setEnabled(false);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|