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

Last change on this file since 9999 was 9067, checked in by Don-vip, 8 years ago

sonar - Immutable Field

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