source: josm/trunk/src/org/openstreetmap/josm/actions/MoveAction.java@ 15652

Last change on this file since 15652 was 15630, checked in by Don-vip, 4 years ago

fix #18510 - Add icons to move up/down/left/right actions

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