source: josm/src/org/openstreetmap/josm/actions/mapmode/MoveAction.java@ 158

Last change on this file since 158 was 158, checked in by imi, 18 years ago
  • added possibility to support a locale in .josm/lang/*.jar
  • fixed bug that displayed the layer's context menu at wrong place
File size: 5.2 KB
Line 
1package org.openstreetmap.josm.actions.mapmode;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.Cursor;
6import java.awt.Point;
7import java.awt.Rectangle;
8import java.awt.event.KeyEvent;
9import java.awt.event.MouseEvent;
10import java.util.Collection;
11
12import javax.swing.JOptionPane;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.command.Command;
16import org.openstreetmap.josm.command.MoveCommand;
17import org.openstreetmap.josm.data.coor.EastNorth;
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
21import org.openstreetmap.josm.gui.MapFrame;
22import org.openstreetmap.josm.gui.SelectionManager;
23import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
24import org.openstreetmap.josm.tools.ImageProvider;
25/**
26 * Move is an action that can move all kind of OsmPrimitives (except Keys for now).
27 *
28 * If an selected object is under the mouse when dragging, move all selected objects.
29 * If an unselected object is under the mouse when dragging, it becomes selected
30 * and will be moved.
31 * If no object is under the mouse, move all selected objects (if any)
32 *
33 * @author imi
34 */
35public class MoveAction extends MapMode implements SelectionEnded {
36 /**
37 * The old cursor before the user pressed the mouse button.
38 */
39 private Cursor oldCursor;
40 /**
41 * The position of the mouse before the user moves a node.
42 */
43 private Point mousePos;
44 private SelectionManager selectionManager;
45 private boolean selectionMode = false;
46
47 /**
48 * Create a new MoveAction
49 * @param mapFrame The MapFrame, this action belongs to.
50 */
51 public MoveAction(MapFrame mapFrame) {
52 super(tr("Move"),
53 "move",
54 tr("Move around objects that are under the mouse or selected."),
55 KeyEvent.VK_M,
56 mapFrame,
57 ImageProvider.getCursor("normal", "move"));
58 selectionManager = new SelectionManager(this, false, mapFrame.mapView);
59 }
60
61 @Override public void enterMode() {
62 super.enterMode();
63 Main.map.mapView.addMouseListener(this);
64 Main.map.mapView.addMouseMotionListener(this);
65 }
66
67 @Override public void exitMode() {
68 super.exitMode();
69 Main.map.mapView.removeMouseListener(this);
70 Main.map.mapView.removeMouseMotionListener(this);
71 }
72
73
74 /**
75 * If the left mouse button is pressed, move all currently selected
76 * objects (if one of them is under the mouse) or the current one under the
77 * mouse (which will become selected).
78 */
79 @Override public void mouseDragged(MouseEvent e) {
80 if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0)
81 return;
82
83 if (selectionMode)
84 return;
85
86 if (mousePos == null)
87 mousePos = e.getPoint();
88
89 EastNorth mouseEN = Main.map.mapView.getEastNorth(e.getX(), e.getY());
90 EastNorth mouseStartEN = Main.map.mapView.getEastNorth(mousePos.x, mousePos.y);
91 double dx = mouseEN.east() - mouseStartEN.east();
92 double dy = mouseEN.north() - mouseStartEN.north();
93 if (dx == 0 && dy == 0)
94 return;
95
96 Collection<OsmPrimitive> selection = Main.ds.getSelected();
97 Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
98
99 // check if any coordinate would be outside the world
100 for (OsmPrimitive osm : affectedNodes) {
101 if (osm instanceof Node && ((Node)osm).coor.isOutSideWorld()) {
102 JOptionPane.showMessageDialog(Main.parent,tr("Cannot move objects outside of the world."));
103 return;
104 }
105 }
106
107 Command c = !Main.main.editLayer().commands.isEmpty() ? Main.main.editLayer().commands.getLast() : null;
108 if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand)c).objects))
109 ((MoveCommand)c).moveAgain(dx,dy);
110 else
111 Main.main.editLayer().add(new MoveCommand(selection, dx, dy));
112
113 Main.map.mapView.repaint();
114 mousePos = e.getPoint();
115 }
116
117 /**
118 * Look, whether any object is selected. If not, select the nearest node.
119 * If there are no nodes in the dataset, do nothing.
120 *
121 * If the user did not press the left mouse button, do nothing.
122 *
123 * Also remember the starting position of the movement and change the mouse
124 * cursor to movement.
125 */
126 @Override public void mousePressed(MouseEvent e) {
127 if (e.getButton() != MouseEvent.BUTTON1)
128 return;
129
130 Collection<OsmPrimitive> sel = Main.ds.getSelected();
131 OsmPrimitive osm = Main.map.mapView.getNearest(e.getPoint(), (e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) != 0);
132 if (osm != null) {
133 if (!sel.contains(osm))
134 Main.ds.setSelected(osm);
135 oldCursor = Main.map.mapView.getCursor();
136 Main.map.mapView.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
137 } else {
138 selectionMode = true;
139 selectionManager.register(Main.map.mapView);
140 }
141
142 Main.map.mapView.repaint();
143
144 mousePos = e.getPoint();
145 }
146
147 /**
148 * Restore the old mouse cursor.
149 */
150 @Override public void mouseReleased(MouseEvent e) {
151 if (selectionMode) {
152 selectionManager.unregister(Main.map.mapView);
153 selectionMode = false;
154 } else
155 Main.map.mapView.setCursor(oldCursor);
156 }
157
158
159 public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl) {
160 SelectionAction.selectEverythingInRectangle(selectionManager, r, alt, shift, ctrl);
161 }
162}
Note: See TracBrowser for help on using the repository browser.