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

Last change on this file since 1724 was 1640, checked in by stoecker, 15 years ago

little bit more refactoring of coordinate access - patch by jttt

  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
1//License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.util.Collection;
9
10import javax.swing.JOptionPane;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.command.Command;
14import org.openstreetmap.josm.command.MoveCommand;
15import org.openstreetmap.josm.data.coor.EastNorth;
16import org.openstreetmap.josm.data.osm.Node;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
19import org.openstreetmap.josm.tools.Shortcut;
20
21/**
22 * Moves the selection
23 *
24 * @author Frederik Ramm
25 */
26public class MoveAction extends JosmAction {
27
28 public enum Direction { UP, LEFT, RIGHT, DOWN }
29 private Direction myDirection;
30
31 // any better idea?
32 private static Object calltosupermustbefirststatementinconstructor(Direction dir, boolean text) {
33 Shortcut sc;
34 String directiontext;
35 if (dir == Direction.UP) {
36 directiontext = tr("up");
37 sc = Shortcut.registerShortcut("core:moveup", tr("Move objects {0}", directiontext), KeyEvent.VK_UP, Shortcut.GROUPS_ALT1+Shortcut.GROUP_DIRECT);
38 } else if (dir == Direction.DOWN) {
39 directiontext = tr("down");
40 sc = Shortcut.registerShortcut("core:movedown", tr("Move objects {0}", directiontext), KeyEvent.VK_DOWN, Shortcut.GROUPS_ALT1+Shortcut.GROUP_DIRECT);
41 } else if (dir == Direction.LEFT) {
42 directiontext = tr("left");
43 sc = Shortcut.registerShortcut("core:moveleft", tr("Move objects {0}", directiontext), KeyEvent.VK_LEFT, Shortcut.GROUPS_ALT1+Shortcut.GROUP_DIRECT);
44 } else { //dir == Direction.RIGHT) {
45 directiontext = tr("right");
46 sc = Shortcut.registerShortcut("core:moveright", tr("Move objects {0}", directiontext), KeyEvent.VK_RIGHT, Shortcut.GROUPS_ALT1+Shortcut.GROUP_DIRECT);
47 }
48 if (text) {
49 return directiontext;
50 } else {
51 return sc;
52 }
53 }
54
55 public MoveAction(Direction dir) {
56 super(tr("Move {0}", calltosupermustbefirststatementinconstructor(dir, true)), null,
57 tr("Moves Objects {0}", calltosupermustbefirststatementinconstructor(dir, true)),
58 (Shortcut)calltosupermustbefirststatementinconstructor(dir, false), true);
59 myDirection = dir;
60 }
61
62 public void actionPerformed(ActionEvent event) {
63
64 // find out how many "real" units the objects have to be moved in order to
65 // achive an 1-pixel movement
66
67 EastNorth en1 = Main.map.mapView.getEastNorth(100, 100);
68 EastNorth en2 = Main.map.mapView.getEastNorth(101, 101);
69
70 double distx = en2.east() - en1.east();
71 double disty = en2.north() - en1.north();
72
73 switch (myDirection) {
74 case UP:
75 distx = 0;
76 disty = -disty;
77 break;
78 case DOWN:
79 distx = 0;
80 break;
81 case LEFT:
82 disty = 0;
83 distx = -distx;
84 default:
85 disty = 0;
86 }
87
88 Collection<OsmPrimitive> selection = Main.ds.getSelected();
89 Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
90
91 Command c = !Main.main.undoRedo.commands.isEmpty()
92 ? Main.main.undoRedo.commands.getLast() : null;
93
94 if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand)c).objects))
95 ((MoveCommand)c).moveAgain(distx, disty);
96 else
97 Main.main.undoRedo.add(
98 c = new MoveCommand(selection, distx, disty));
99
100 for (Node n : affectedNodes) {
101 if (n.getCoor().isOutSideWorld()) {
102 // Revert move
103 ((MoveCommand) c).moveAgain(-distx, -disty);
104 JOptionPane.showMessageDialog(Main.parent,
105 tr("Cannot move objects outside of the world."));
106 return;
107 }
108 }
109
110 Main.map.mapView.repaint();
111 }
112}
Note: See TracBrowser for help on using the repository browser.