1 | //License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.util.Collection;
|
---|
9 |
|
---|
10 | import javax.swing.JOptionPane;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.command.Command;
|
---|
14 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
15 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
16 | import org.openstreetmap.josm.data.osm.Node;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
|
---|
19 | import org.openstreetmap.josm.gui.OptionPaneUtil;
|
---|
20 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Moves the selection
|
---|
24 | *
|
---|
25 | * @author Frederik Ramm
|
---|
26 | */
|
---|
27 | public class MoveAction extends JosmAction {
|
---|
28 |
|
---|
29 | public enum Direction { UP, LEFT, RIGHT, DOWN }
|
---|
30 | private Direction myDirection;
|
---|
31 |
|
---|
32 | // any better idea?
|
---|
33 | private static Object calltosupermustbefirststatementinconstructor(Direction dir, boolean text) {
|
---|
34 | Shortcut sc;
|
---|
35 | String directiontext;
|
---|
36 | if (dir == Direction.UP) {
|
---|
37 | directiontext = tr("up");
|
---|
38 | sc = Shortcut.registerShortcut("core:moveup", tr("Move objects {0}", directiontext), KeyEvent.VK_UP, Shortcut.GROUPS_ALT1+Shortcut.GROUP_DIRECT);
|
---|
39 | } else if (dir == Direction.DOWN) {
|
---|
40 | directiontext = tr("down");
|
---|
41 | sc = Shortcut.registerShortcut("core:movedown", tr("Move objects {0}", directiontext), KeyEvent.VK_DOWN, Shortcut.GROUPS_ALT1+Shortcut.GROUP_DIRECT);
|
---|
42 | } else if (dir == Direction.LEFT) {
|
---|
43 | directiontext = tr("left");
|
---|
44 | sc = Shortcut.registerShortcut("core:moveleft", tr("Move objects {0}", directiontext), KeyEvent.VK_LEFT, Shortcut.GROUPS_ALT1+Shortcut.GROUP_DIRECT);
|
---|
45 | } else { //dir == Direction.RIGHT) {
|
---|
46 | directiontext = tr("right");
|
---|
47 | sc = Shortcut.registerShortcut("core:moveright", tr("Move objects {0}", directiontext), KeyEvent.VK_RIGHT, Shortcut.GROUPS_ALT1+Shortcut.GROUP_DIRECT);
|
---|
48 | }
|
---|
49 | if (text)
|
---|
50 | return directiontext;
|
---|
51 | else
|
---|
52 | return sc;
|
---|
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 = getCurrentDataSet().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).getMovedNodes())) {
|
---|
95 | ((MoveCommand)c).moveAgain(distx, disty);
|
---|
96 | } else {
|
---|
97 | Main.main.undoRedo.add(
|
---|
98 | c = new MoveCommand(selection, distx, disty));
|
---|
99 | }
|
---|
100 |
|
---|
101 | for (Node n : affectedNodes) {
|
---|
102 | if (n.getCoor().isOutSideWorld()) {
|
---|
103 | // Revert move
|
---|
104 | ((MoveCommand) c).moveAgain(-distx, -disty);
|
---|
105 | OptionPaneUtil.showMessageDialog(
|
---|
106 | Main.parent,
|
---|
107 | tr("Cannot move objects outside of the world."),
|
---|
108 | tr("Warning"),
|
---|
109 | JOptionPane.WARNING_MESSAGE
|
---|
110 | );
|
---|
111 | return;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | Main.map.mapView.repaint();
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Override
|
---|
119 | protected void updateEnabledState() {
|
---|
120 | setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
|
---|
121 | }
|
---|
122 | }
|
---|