source: josm/trunk/src/org/openstreetmap/josm/actions/MoveAction.java@ 2256

Last change on this file since 2256 was 2256, checked in by Gubaer, 15 years ago

Removed inefficient DataSet:getSelected() when responding to fireSelectionChanged() in JOSM actions, see thread on dev
Still uses DataSet:getSelected() when responding to layer change events, this is less critical.

  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1//License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.util.Collection;
9
10import javax.swing.JOptionPane;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.command.Command;
14import org.openstreetmap.josm.command.MoveCommand;
15import org.openstreetmap.josm.data.coor.EastNorth;
16import org.openstreetmap.josm.data.osm.Node;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
19import org.openstreetmap.josm.tools.Shortcut;
20
21/**
22 * Moves the selection
23 *
24 * @author Frederik Ramm
25 */
26public class MoveAction extends JosmAction {
27
28 public enum Direction { UP, LEFT, RIGHT, DOWN }
29 private Direction myDirection;
30
31 // any better idea?
32 private static Object calltosupermustbefirststatementinconstructor(Direction dir, boolean text) {
33 Shortcut sc;
34 String directiontext;
35 if (dir == Direction.UP) {
36 directiontext = tr("up");
37 sc = Shortcut.registerShortcut("core:moveup", tr("Move objects {0}", directiontext), KeyEvent.VK_UP, Shortcut.GROUPS_ALT1+Shortcut.GROUP_DIRECT);
38 } else if (dir == Direction.DOWN) {
39 directiontext = tr("down");
40 sc = Shortcut.registerShortcut("core:movedown", tr("Move objects {0}", directiontext), KeyEvent.VK_DOWN, Shortcut.GROUPS_ALT1+Shortcut.GROUP_DIRECT);
41 } else if (dir == Direction.LEFT) {
42 directiontext = tr("left");
43 sc = Shortcut.registerShortcut("core:moveleft", tr("Move objects {0}", directiontext), KeyEvent.VK_LEFT, Shortcut.GROUPS_ALT1+Shortcut.GROUP_DIRECT);
44 } else { //dir == Direction.RIGHT) {
45 directiontext = tr("right");
46 sc = Shortcut.registerShortcut("core:moveright", tr("Move objects {0}", directiontext), KeyEvent.VK_RIGHT, Shortcut.GROUPS_ALT1+Shortcut.GROUP_DIRECT);
47 }
48 if (text)
49 return directiontext;
50 else
51 return sc;
52 }
53
54 public MoveAction(Direction dir) {
55 super(tr("Move {0}", calltosupermustbefirststatementinconstructor(dir, true)), null,
56 tr("Moves Objects {0}", calltosupermustbefirststatementinconstructor(dir, true)),
57 (Shortcut)calltosupermustbefirststatementinconstructor(dir, false), true);
58 myDirection = dir;
59 }
60
61 public void actionPerformed(ActionEvent event) {
62
63 // find out how many "real" units the objects have to be moved in order to
64 // achive an 1-pixel movement
65
66 EastNorth en1 = Main.map.mapView.getEastNorth(100, 100);
67 EastNorth en2 = Main.map.mapView.getEastNorth(101, 101);
68
69 double distx = en2.east() - en1.east();
70 double disty = en2.north() - en1.north();
71
72 switch (myDirection) {
73 case UP:
74 distx = 0;
75 disty = -disty;
76 break;
77 case DOWN:
78 distx = 0;
79 break;
80 case LEFT:
81 disty = 0;
82 distx = -distx;
83 default:
84 disty = 0;
85 }
86
87 Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
88 Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
89
90 Command c = !Main.main.undoRedo.commands.isEmpty()
91 ? Main.main.undoRedo.commands.getLast() : null;
92
93 if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand)c).getMovedNodes())) {
94 ((MoveCommand)c).moveAgain(distx, disty);
95 } else {
96 Main.main.undoRedo.add(
97 c = new MoveCommand(selection, distx, disty));
98 }
99
100 for (Node n : affectedNodes) {
101 if (n.getCoor().isOutSideWorld()) {
102 // Revert move
103 ((MoveCommand) c).moveAgain(-distx, -disty);
104 JOptionPane.showMessageDialog(
105 Main.parent,
106 tr("Cannot move objects outside of the world."),
107 tr("Warning"),
108 JOptionPane.WARNING_MESSAGE
109 );
110 return;
111 }
112 }
113
114 Main.map.mapView.repaint();
115 }
116
117 @Override
118 protected void updateEnabledState() {
119 if (getCurrentDataSet() == null) {
120 setEnabled(false);
121 } else {
122 updateEnabledState(getCurrentDataSet().getSelected());
123 }
124 }
125
126 @Override
127 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
128 setEnabled(selection != null && !selection.isEmpty());
129 }
130}
Note: See TracBrowser for help on using the repository browser.