1 | //License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
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 |
|
---|
31 | private Direction myDirection;
|
---|
32 |
|
---|
33 | // any better idea?
|
---|
34 | private static Object calltosupermustbefirststatementinconstructor(Direction dir, boolean text) {
|
---|
35 | Shortcut sc;
|
---|
36 | String directiontext;
|
---|
37 | if (dir == Direction.UP) {
|
---|
38 | directiontext = tr("up");
|
---|
39 | sc = Shortcut.registerShortcut("core:moveup", tr("Move objects {0}", directiontext), KeyEvent.VK_UP, Shortcut.GROUPS_ALT1+Shortcut.GROUP_DIRECT);
|
---|
40 | } else if (dir == Direction.DOWN) {
|
---|
41 | directiontext = tr("down");
|
---|
42 | sc = Shortcut.registerShortcut("core:movedown", tr("Move objects {0}", directiontext), KeyEvent.VK_DOWN, Shortcut.GROUPS_ALT1+Shortcut.GROUP_DIRECT);
|
---|
43 | } else if (dir == Direction.LEFT) {
|
---|
44 | directiontext = tr("left");
|
---|
45 | sc = Shortcut.registerShortcut("core:moveleft", tr("Move objects {0}", directiontext), KeyEvent.VK_LEFT, Shortcut.GROUPS_ALT1+Shortcut.GROUP_DIRECT);
|
---|
46 | } else { //dir == Direction.RIGHT) {
|
---|
47 | directiontext = tr("right");
|
---|
48 | sc = Shortcut.registerShortcut("core:moveright", tr("Move objects {0}", directiontext), KeyEvent.VK_RIGHT, Shortcut.GROUPS_ALT1+Shortcut.GROUP_DIRECT);
|
---|
49 | }
|
---|
50 | if (text)
|
---|
51 | return directiontext;
|
---|
52 | else
|
---|
53 | return sc;
|
---|
54 | }
|
---|
55 |
|
---|
56 | public MoveAction(Direction dir) {
|
---|
57 | super(tr("Move {0}", calltosupermustbefirststatementinconstructor(dir, true)), null,
|
---|
58 | tr("Moves Objects {0}", calltosupermustbefirststatementinconstructor(dir, true)),
|
---|
59 | (Shortcut)calltosupermustbefirststatementinconstructor(dir, false), true);
|
---|
60 | myDirection = dir;
|
---|
61 | putValue("help", ht("/Action/Move"));
|
---|
62 | }
|
---|
63 |
|
---|
64 | public void actionPerformed(ActionEvent event) {
|
---|
65 |
|
---|
66 | if (!Main.isDisplayingMapView())
|
---|
67 | return;
|
---|
68 |
|
---|
69 | // find out how many "real" units the objects have to be moved in order to
|
---|
70 | // achive an 1-pixel movement
|
---|
71 |
|
---|
72 | EastNorth en1 = Main.map.mapView.getEastNorth(100, 100);
|
---|
73 | EastNorth en2 = Main.map.mapView.getEastNorth(101, 101);
|
---|
74 |
|
---|
75 | double distx = en2.east() - en1.east();
|
---|
76 | double disty = en2.north() - en1.north();
|
---|
77 |
|
---|
78 | switch (myDirection) {
|
---|
79 | case UP:
|
---|
80 | distx = 0;
|
---|
81 | disty = -disty;
|
---|
82 | break;
|
---|
83 | case DOWN:
|
---|
84 | distx = 0;
|
---|
85 | break;
|
---|
86 | case LEFT:
|
---|
87 | disty = 0;
|
---|
88 | distx = -distx;
|
---|
89 | break;
|
---|
90 | default:
|
---|
91 | disty = 0;
|
---|
92 | }
|
---|
93 |
|
---|
94 | Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
|
---|
95 | Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
|
---|
96 |
|
---|
97 | Command c = !Main.main.undoRedo.commands.isEmpty()
|
---|
98 | ? Main.main.undoRedo.commands.getLast() : null;
|
---|
99 |
|
---|
100 | if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand)c).getParticipatingPrimitives())) {
|
---|
101 | ((MoveCommand)c).moveAgain(distx, disty);
|
---|
102 | } else {
|
---|
103 | Main.main.undoRedo.add(
|
---|
104 | c = new MoveCommand(selection, distx, disty));
|
---|
105 | }
|
---|
106 |
|
---|
107 | for (Node n : affectedNodes) {
|
---|
108 | if (n.getCoor().isOutSideWorld()) {
|
---|
109 | // Revert move
|
---|
110 | ((MoveCommand) c).moveAgain(-distx, -disty);
|
---|
111 | JOptionPane.showMessageDialog(
|
---|
112 | Main.parent,
|
---|
113 | tr("Cannot move objects outside of the world."),
|
---|
114 | tr("Warning"),
|
---|
115 | JOptionPane.WARNING_MESSAGE
|
---|
116 | );
|
---|
117 | return;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | Main.map.mapView.repaint();
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Override
|
---|
125 | protected void updateEnabledState() {
|
---|
126 | if (getCurrentDataSet() == null) {
|
---|
127 | setEnabled(false);
|
---|
128 | } else {
|
---|
129 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | @Override
|
---|
134 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
135 | setEnabled(selection != null && !selection.isEmpty());
|
---|
136 | }
|
---|
137 | }
|
---|