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.awt.event.KeyEvent;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Locale;
|
---|
11 |
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.command.Command;
|
---|
15 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
16 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
17 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.data.osm.Node;
|
---|
20 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
21 | import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
|
---|
22 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
23 | import org.openstreetmap.josm.gui.MapView;
|
---|
24 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Moves the selection
|
---|
28 | *
|
---|
29 | * @author Frederik Ramm
|
---|
30 | */
|
---|
31 | public class MoveAction extends JosmAction {
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Move direction.
|
---|
35 | */
|
---|
36 | public enum Direction {
|
---|
37 | /** Move up */
|
---|
38 | UP(tr("up"), "up", KeyEvent.VK_UP),
|
---|
39 | /* SHORTCUT(Move objects up, core:move:up, SHIFT, UP) */
|
---|
40 | /** Move left */
|
---|
41 | LEFT(tr("left"), "previous", KeyEvent.VK_LEFT),
|
---|
42 | /* SHORTCUT(Move objects left, core:move:left, SHIFT, LEFT) */
|
---|
43 | /** Move right */
|
---|
44 | RIGHT(tr("right"), "next", KeyEvent.VK_RIGHT),
|
---|
45 | /* SHORTCUT(Move objects right, core:move:right, SHIFT, RIGHT) */
|
---|
46 | /** Move down */
|
---|
47 | DOWN(tr("down"), "down", KeyEvent.VK_DOWN);
|
---|
48 | /* SHORTCUT(Move objects down, core:move:down, SHIFT, DOWN) */
|
---|
49 |
|
---|
50 | private final String localizedName;
|
---|
51 | private final String icon;
|
---|
52 | private final int shortcutKey;
|
---|
53 |
|
---|
54 | Direction(String localizedName, String icon, int shortcutKey) {
|
---|
55 | this.localizedName = localizedName;
|
---|
56 | this.icon = icon;
|
---|
57 | this.shortcutKey = shortcutKey;
|
---|
58 | }
|
---|
59 |
|
---|
60 | String getId() {
|
---|
61 | return name().toLowerCase(Locale.ENGLISH);
|
---|
62 | }
|
---|
63 |
|
---|
64 | String getLocalizedName() {
|
---|
65 | return localizedName;
|
---|
66 | }
|
---|
67 |
|
---|
68 | String getIcon() {
|
---|
69 | return "dialogs/" + icon;
|
---|
70 | }
|
---|
71 |
|
---|
72 | String getToolbarName() {
|
---|
73 | return "action/move/" + getId();
|
---|
74 | }
|
---|
75 |
|
---|
76 | int getShortcutKey() {
|
---|
77 | return shortcutKey;
|
---|
78 | }
|
---|
79 |
|
---|
80 | Shortcut getShortcut() {
|
---|
81 | return Shortcut.registerShortcut( /* NO-SHORTCUT - adapt definition above when modified */
|
---|
82 | "core:move" + getId(), tr("Move objects {0}", getLocalizedName()), getShortcutKey(), Shortcut.SHIFT);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | private final Direction myDirection;
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Constructs a new {@code MoveAction}.
|
---|
90 | * @param dir direction
|
---|
91 | */
|
---|
92 | public MoveAction(Direction dir) {
|
---|
93 | super(tr("Move {0}", dir.getLocalizedName()), dir.getIcon(),
|
---|
94 | tr("Moves Objects {0}", dir.getLocalizedName()),
|
---|
95 | dir.getShortcut(), true, dir.getToolbarName(), true);
|
---|
96 | myDirection = dir;
|
---|
97 | setHelpId(ht("/Action/Move"));
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public void actionPerformed(ActionEvent event) {
|
---|
102 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
103 |
|
---|
104 | if (!MainApplication.isDisplayingMapView() || ds == null)
|
---|
105 | return;
|
---|
106 |
|
---|
107 | // find out how many "real" units the objects have to be moved in order to
|
---|
108 | // achive an 1-pixel movement
|
---|
109 |
|
---|
110 | MapView mapView = MainApplication.getMap().mapView;
|
---|
111 | EastNorth en1 = mapView.getEastNorth(100, 100);
|
---|
112 | EastNorth en2 = mapView.getEastNorth(101, 101);
|
---|
113 |
|
---|
114 | double distx = en2.east() - en1.east();
|
---|
115 | double disty = en2.north() - en1.north();
|
---|
116 |
|
---|
117 | switch (myDirection) {
|
---|
118 | case UP:
|
---|
119 | distx = 0;
|
---|
120 | disty = -disty;
|
---|
121 | break;
|
---|
122 | case DOWN:
|
---|
123 | distx = 0;
|
---|
124 | break;
|
---|
125 | case LEFT:
|
---|
126 | disty = 0;
|
---|
127 | distx = -distx;
|
---|
128 | break;
|
---|
129 | default:
|
---|
130 | disty = 0;
|
---|
131 | }
|
---|
132 |
|
---|
133 | Collection<OsmPrimitive> selection = ds.getSelected();
|
---|
134 | Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
|
---|
135 |
|
---|
136 | Command c = UndoRedoHandler.getInstance().getLastCommand();
|
---|
137 |
|
---|
138 | ds.beginUpdate();
|
---|
139 | try {
|
---|
140 | if (c instanceof MoveCommand && ds.equals(c.getAffectedDataSet())
|
---|
141 | && affectedNodes.equals(((MoveCommand) c).getParticipatingPrimitives())) {
|
---|
142 | ((MoveCommand) c).moveAgain(distx, disty);
|
---|
143 | } else {
|
---|
144 | c = new MoveCommand(ds, selection, distx, disty);
|
---|
145 | UndoRedoHandler.getInstance().add(c);
|
---|
146 | }
|
---|
147 | } finally {
|
---|
148 | ds.endUpdate();
|
---|
149 | }
|
---|
150 |
|
---|
151 | for (Node n : affectedNodes) {
|
---|
152 | if (n.isLatLonKnown() && n.isOutSideWorld()) {
|
---|
153 | // Revert move
|
---|
154 | ((MoveCommand) c).moveAgain(-distx, -disty);
|
---|
155 | JOptionPane.showMessageDialog(
|
---|
156 | MainApplication.getMainFrame(),
|
---|
157 | tr("Cannot move objects outside of the world."),
|
---|
158 | tr("Warning"),
|
---|
159 | JOptionPane.WARNING_MESSAGE
|
---|
160 | );
|
---|
161 | return;
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | mapView.repaint();
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | protected void updateEnabledState() {
|
---|
170 | updateEnabledStateOnCurrentSelection();
|
---|
171 | }
|
---|
172 |
|
---|
173 | @Override
|
---|
174 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
175 | updateEnabledStateOnModifiableSelection(selection);
|
---|
176 | }
|
---|
177 | }
|
---|