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 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.Collection;
|
---|
10 |
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.command.Command;
|
---|
15 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
16 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
17 | import org.openstreetmap.josm.data.osm.Node;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
19 | import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
|
---|
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 | putValue("help", ht("/Action/Move"));
|
---|
61 | }
|
---|
62 |
|
---|
63 | public void actionPerformed(ActionEvent event) {
|
---|
64 |
|
---|
65 | // find out how many "real" units the objects have to be moved in order to
|
---|
66 | // achive an 1-pixel movement
|
---|
67 |
|
---|
68 | EastNorth en1 = Main.map.mapView.getEastNorth(100, 100);
|
---|
69 | EastNorth en2 = Main.map.mapView.getEastNorth(101, 101);
|
---|
70 |
|
---|
71 | double distx = en2.east() - en1.east();
|
---|
72 | double disty = en2.north() - en1.north();
|
---|
73 |
|
---|
74 | switch (myDirection) {
|
---|
75 | case UP:
|
---|
76 | distx = 0;
|
---|
77 | disty = -disty;
|
---|
78 | break;
|
---|
79 | case DOWN:
|
---|
80 | distx = 0;
|
---|
81 | break;
|
---|
82 | case LEFT:
|
---|
83 | disty = 0;
|
---|
84 | distx = -distx;
|
---|
85 | break;
|
---|
86 | default:
|
---|
87 | disty = 0;
|
---|
88 | }
|
---|
89 |
|
---|
90 | Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
|
---|
91 | Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
|
---|
92 |
|
---|
93 | Command c = !Main.main.undoRedo.commands.isEmpty()
|
---|
94 | ? Main.main.undoRedo.commands.getLast() : null;
|
---|
95 |
|
---|
96 | if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand)c).getMovedNodes())) {
|
---|
97 | ((MoveCommand)c).moveAgain(distx, disty);
|
---|
98 | } else {
|
---|
99 | Main.main.undoRedo.add(
|
---|
100 | c = new MoveCommand(selection, distx, disty));
|
---|
101 | }
|
---|
102 |
|
---|
103 | for (Node n : affectedNodes) {
|
---|
104 | if (n.getCoor().isOutSideWorld()) {
|
---|
105 | // Revert move
|
---|
106 | ((MoveCommand) c).moveAgain(-distx, -disty);
|
---|
107 | JOptionPane.showMessageDialog(
|
---|
108 | Main.parent,
|
---|
109 | tr("Cannot move objects outside of the world."),
|
---|
110 | tr("Warning"),
|
---|
111 | JOptionPane.WARNING_MESSAGE
|
---|
112 | );
|
---|
113 | return;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | Main.map.mapView.repaint();
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | protected void updateEnabledState() {
|
---|
122 | if (getCurrentDataSet() == null) {
|
---|
123 | setEnabled(false);
|
---|
124 | } else {
|
---|
125 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Override
|
---|
130 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
131 | setEnabled(selection != null && !selection.isEmpty());
|
---|
132 | }
|
---|
133 | }
|
---|