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