[526] | 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 |
|
---|
| 20 | /**
|
---|
| 21 | * Moves the selection
|
---|
| 22 | *
|
---|
| 23 | * @author Frederik Ramm
|
---|
| 24 | */
|
---|
| 25 | public class MoveAction extends JosmAction {
|
---|
| 26 |
|
---|
[582] | 27 | public enum Direction { UP, LEFT, RIGHT, DOWN }
|
---|
[526] | 28 | private Direction myDirection;
|
---|
| 29 |
|
---|
| 30 | public MoveAction(Direction dir) {
|
---|
| 31 | super(tr("Move"), null, tr("Moves Objects"),
|
---|
| 32 | (dir == Direction.UP) ? KeyEvent.VK_UP :
|
---|
| 33 | (dir == Direction.DOWN) ? KeyEvent.VK_DOWN :
|
---|
| 34 | (dir == Direction.LEFT) ? KeyEvent.VK_LEFT :
|
---|
| 35 | KeyEvent.VK_RIGHT, 0, true);
|
---|
| 36 | myDirection = dir;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | public void actionPerformed(ActionEvent event) {
|
---|
| 40 |
|
---|
| 41 | // find out how many "real" units the objects have to be moved in order to
|
---|
| 42 | // achive an 1-pixel movement
|
---|
| 43 |
|
---|
| 44 | EastNorth en1 = Main.map.mapView.getEastNorth(100, 100);
|
---|
| 45 | EastNorth en2 = Main.map.mapView.getEastNorth(101, 101);
|
---|
| 46 |
|
---|
| 47 | double distx = en2.east() - en1.east();
|
---|
| 48 | double disty = en2.north() - en1.north();
|
---|
| 49 |
|
---|
| 50 | switch (myDirection) {
|
---|
| 51 | case UP:
|
---|
| 52 | distx = 0;
|
---|
| 53 | disty = -disty;
|
---|
| 54 | break;
|
---|
| 55 | case DOWN:
|
---|
| 56 | distx = 0;
|
---|
| 57 | break;
|
---|
| 58 | case LEFT:
|
---|
| 59 | disty = 0;
|
---|
| 60 | distx = -distx;
|
---|
| 61 | default:
|
---|
| 62 | disty = 0;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | Collection<OsmPrimitive> selection = Main.ds.getSelected();
|
---|
| 66 | Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
|
---|
| 67 |
|
---|
| 68 | Command c = !Main.main.undoRedo.commands.isEmpty()
|
---|
| 69 | ? Main.main.undoRedo.commands.getLast() : null;
|
---|
| 70 |
|
---|
| 71 | if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand)c).objects))
|
---|
| 72 | ((MoveCommand)c).moveAgain(distx, disty);
|
---|
| 73 | else
|
---|
| 74 | Main.main.undoRedo.add(
|
---|
| 75 | c = new MoveCommand(selection, distx, disty));
|
---|
| 76 |
|
---|
| 77 | for (Node n : affectedNodes) {
|
---|
| 78 | if (n.coor.isOutSideWorld()) {
|
---|
| 79 | // Revert move
|
---|
| 80 | ((MoveCommand) c).moveAgain(-distx, -disty);
|
---|
| 81 | JOptionPane.showMessageDialog(Main.parent,
|
---|
| 82 | tr("Cannot move objects outside of the world."));
|
---|
| 83 | return;
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | Main.map.mapView.repaint();
|
---|
| 88 | }
|
---|
| 89 | }
|
---|