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

Last change on this file since 6327 was 6296, checked in by Don-vip, 11 years ago

Sonar/Findbugs - Avoid commented-out lines of code, javadoc

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1//License: GPL. Copyright 2007 by Immanuel Scholz and others
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 Direction myDirection;
32
33 // any better idea?
34 private static String calltosupermustbefirststatementinconstructor_text(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 public MoveAction(Direction dir) {
64 super(tr("Move {0}", calltosupermustbefirststatementinconstructor_text(dir)), null,
65 tr("Moves Objects {0}", calltosupermustbefirststatementinconstructor_text(dir)),
66 calltosupermustbefirststatementinconstructor(dir), false);
67 myDirection = dir;
68 putValue("help", ht("/Action/Move"));
69 if (dir == Direction.UP) {
70 putValue("toolbar", "action/move/up");
71 } else if (dir == Direction.DOWN) {
72 putValue("toolbar", "action/move/down");
73 } else if (dir == Direction.LEFT) {
74 putValue("toolbar", "action/move/left");
75 } else { //dir == Direction.RIGHT) {
76 putValue("toolbar", "action/move/right");
77 }
78 Main.toolbar.register(this);
79 }
80
81 @Override
82 public void actionPerformed(ActionEvent event) {
83
84 if (!Main.isDisplayingMapView())
85 return;
86
87 // find out how many "real" units the objects have to be moved in order to
88 // achive an 1-pixel movement
89
90 EastNorth en1 = Main.map.mapView.getEastNorth(100, 100);
91 EastNorth en2 = Main.map.mapView.getEastNorth(101, 101);
92
93 double distx = en2.east() - en1.east();
94 double disty = en2.north() - en1.north();
95
96 switch (myDirection) {
97 case UP:
98 distx = 0;
99 disty = -disty;
100 break;
101 case DOWN:
102 distx = 0;
103 break;
104 case LEFT:
105 disty = 0;
106 distx = -distx;
107 break;
108 default:
109 disty = 0;
110 }
111
112 Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
113 Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
114
115 Command c = !Main.main.undoRedo.commands.isEmpty()
116 ? Main.main.undoRedo.commands.getLast() : null;
117
118 getCurrentDataSet().beginUpdate();
119 if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand)c).getParticipatingPrimitives())) {
120 ((MoveCommand)c).moveAgain(distx, disty);
121 } else {
122 Main.main.undoRedo.add(
123 c = new MoveCommand(selection, distx, disty));
124 }
125 getCurrentDataSet().endUpdate();
126
127 for (Node n : affectedNodes) {
128 if (n.getCoor().isOutSideWorld()) {
129 // Revert move
130 ((MoveCommand) c).moveAgain(-distx, -disty);
131 JOptionPane.showMessageDialog(
132 Main.parent,
133 tr("Cannot move objects outside of the world."),
134 tr("Warning"),
135 JOptionPane.WARNING_MESSAGE
136 );
137 return;
138 }
139 }
140
141 Main.map.mapView.repaint();
142 }
143
144 @Override
145 protected void updateEnabledState() {
146 if (getCurrentDataSet() == null) {
147 setEnabled(false);
148 } else {
149 updateEnabledState(getCurrentDataSet().getSelected());
150 }
151 }
152
153 @Override
154 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
155 setEnabled(selection != null && !selection.isEmpty());
156 }
157}
Note: See TracBrowser for help on using the repository browser.