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

Last change on this file since 12051 was 11978, checked in by Don-vip, 7 years ago

improve coverage and javadoc of enum classes for package actions

  • Property svn:eol-style set to native
File size: 5.6 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;
10
11import javax.swing.JOptionPane;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.command.Command;
15import org.openstreetmap.josm.command.MoveCommand;
16import org.openstreetmap.josm.data.coor.EastNorth;
17import org.openstreetmap.josm.data.osm.DataSet;
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
21import org.openstreetmap.josm.tools.Shortcut;
22
23/**
24 * Moves the selection
25 *
26 * @author Frederik Ramm
27 */
28public class MoveAction extends JosmAction {
29
30 /**
31 * Move direction.
32 */
33 public enum Direction {
34 /** Move up */
35 UP,
36 /** Move left */
37 LEFT,
38 /** Move right */
39 RIGHT,
40 /** Move down */
41 DOWN
42 }
43
44 private final Direction myDirection;
45
46 // any better idea?
47 private static String calltosupermustbefirststatementinconstructortext(Direction dir) {
48 String directiontext;
49 if (dir == Direction.UP) {
50 directiontext = tr("up");
51 } else if (dir == Direction.DOWN) {
52 directiontext = tr("down");
53 } else if (dir == Direction.LEFT) {
54 directiontext = tr("left");
55 } else {
56 directiontext = tr("right");
57 }
58 return directiontext;
59 }
60
61 // any better idea?
62 private static Shortcut calltosupermustbefirststatementinconstructor(Direction dir) {
63 Shortcut sc;
64 // CHECKSTYLE.OFF: SingleSpaceSeparator
65 if (dir == Direction.UP) {
66 sc = Shortcut.registerShortcut("core:moveup", tr("Move objects {0}", tr("up")), KeyEvent.VK_UP, Shortcut.SHIFT);
67 } else if (dir == Direction.DOWN) {
68 sc = Shortcut.registerShortcut("core:movedown", tr("Move objects {0}", tr("down")), KeyEvent.VK_DOWN, Shortcut.SHIFT);
69 } else if (dir == Direction.LEFT) {
70 sc = Shortcut.registerShortcut("core:moveleft", tr("Move objects {0}", tr("left")), KeyEvent.VK_LEFT, Shortcut.SHIFT);
71 } else { //dir == Direction.RIGHT
72 sc = Shortcut.registerShortcut("core:moveright", tr("Move objects {0}", tr("right")), KeyEvent.VK_RIGHT, Shortcut.SHIFT);
73 }
74 // CHECKSTYLE.ON: SingleSpaceSeparator
75 return sc;
76 }
77
78 /**
79 * Constructs a new {@code MoveAction}.
80 * @param dir direction
81 */
82 public MoveAction(Direction dir) {
83 super(tr("Move {0}", calltosupermustbefirststatementinconstructortext(dir)), null,
84 tr("Moves Objects {0}", calltosupermustbefirststatementinconstructortext(dir)),
85 calltosupermustbefirststatementinconstructor(dir), false);
86 myDirection = dir;
87 putValue("help", ht("/Action/Move"));
88 if (dir == Direction.UP) {
89 putValue("toolbar", "action/move/up");
90 } else if (dir == Direction.DOWN) {
91 putValue("toolbar", "action/move/down");
92 } else if (dir == Direction.LEFT) {
93 putValue("toolbar", "action/move/left");
94 } else { //dir == Direction.RIGHT
95 putValue("toolbar", "action/move/right");
96 }
97 Main.toolbar.register(this);
98 }
99
100 @Override
101 public void actionPerformed(ActionEvent event) {
102
103 if (!Main.isDisplayingMapView())
104 return;
105
106 // find out how many "real" units the objects have to be moved in order to
107 // achive an 1-pixel movement
108
109 EastNorth en1 = Main.map.mapView.getEastNorth(100, 100);
110 EastNorth en2 = Main.map.mapView.getEastNorth(101, 101);
111
112 double distx = en2.east() - en1.east();
113 double disty = en2.north() - en1.north();
114
115 switch (myDirection) {
116 case UP:
117 distx = 0;
118 disty = -disty;
119 break;
120 case DOWN:
121 distx = 0;
122 break;
123 case LEFT:
124 disty = 0;
125 distx = -distx;
126 break;
127 default:
128 disty = 0;
129 }
130
131 DataSet ds = getLayerManager().getEditDataSet();
132 Collection<OsmPrimitive> selection = ds.getSelected();
133 Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
134
135 Command c = !Main.main.undoRedo.commands.isEmpty()
136 ? Main.main.undoRedo.commands.getLast() : null;
137
138 ds.beginUpdate();
139 if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand) c).getParticipatingPrimitives())) {
140 ((MoveCommand) c).moveAgain(distx, disty);
141 } else {
142 c = new MoveCommand(selection, distx, disty);
143 Main.main.undoRedo.add(c);
144 }
145 ds.endUpdate();
146
147 for (Node n : affectedNodes) {
148 if (n.getCoor().isOutSideWorld()) {
149 // Revert move
150 ((MoveCommand) c).moveAgain(-distx, -disty);
151 JOptionPane.showMessageDialog(
152 Main.parent,
153 tr("Cannot move objects outside of the world."),
154 tr("Warning"),
155 JOptionPane.WARNING_MESSAGE
156 );
157 return;
158 }
159 }
160
161 Main.map.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 setEnabled(selection != null && !selection.isEmpty());
172 }
173}
Note: See TracBrowser for help on using the repository browser.