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

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