1 | package org.openstreetmap.josm.actions.mapmode;
|
---|
2 |
|
---|
3 | import java.awt.Cursor;
|
---|
4 | import java.awt.Point;
|
---|
5 | import java.awt.event.KeyEvent;
|
---|
6 | import java.awt.event.MouseEvent;
|
---|
7 | import java.util.Collection;
|
---|
8 |
|
---|
9 | import javax.swing.JOptionPane;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.command.Command;
|
---|
13 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
14 | import org.openstreetmap.josm.data.GeoPoint;
|
---|
15 | import org.openstreetmap.josm.data.osm.Node;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Move is an action that can move all kind of OsmPrimitives (except Keys for now).
|
---|
21 | *
|
---|
22 | * If any object is selected, all selected objects are moved. If no object is
|
---|
23 | * selected, the nearest object will be selected and moved. In this case, the
|
---|
24 | * object will be unselected as soon as movement stopped.
|
---|
25 | *
|
---|
26 | * @author imi
|
---|
27 | */
|
---|
28 | public class MoveAction extends MapMode {
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * The old cursor before the user pressed the mouse button.
|
---|
32 | */
|
---|
33 | private Cursor oldCursor;
|
---|
34 | /**
|
---|
35 | * The position of the mouse before the user moves a node.
|
---|
36 | */
|
---|
37 | private Point mousePos;
|
---|
38 | /**
|
---|
39 | * Non-<code>null</code>, if no object was selected before movement
|
---|
40 | * (and so the object get unselected after mouse release).
|
---|
41 | */
|
---|
42 | private OsmPrimitive singleOsmPrimitive;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Create a new MoveAction
|
---|
46 | * @param mapFrame The MapFrame, this action belongs to.
|
---|
47 | */
|
---|
48 | public MoveAction(MapFrame mapFrame) {
|
---|
49 | super("Move", "move", "Move selected objects around.", "M", KeyEvent.VK_M, mapFrame);
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public void registerListener() {
|
---|
54 | super.registerListener();
|
---|
55 | mv.addMouseListener(this);
|
---|
56 | mv.addMouseMotionListener(this);
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public void unregisterListener() {
|
---|
61 | super.unregisterListener();
|
---|
62 | mv.removeMouseListener(this);
|
---|
63 | mv.removeMouseMotionListener(this);
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * If the left mouse button is pressed, move all currently selected
|
---|
69 | * objects.
|
---|
70 | */
|
---|
71 | @Override
|
---|
72 | public void mouseDragged(MouseEvent e) {
|
---|
73 | if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0)
|
---|
74 | return;
|
---|
75 |
|
---|
76 | if (mousePos == null) {
|
---|
77 | mousePos = e.getPoint();
|
---|
78 | singleOsmPrimitive = null;
|
---|
79 | }
|
---|
80 |
|
---|
81 | GeoPoint mouseGeo = mv.getPoint(e.getX(), e.getY(), false);
|
---|
82 | GeoPoint mouseStartGeo = mv.getPoint(mousePos.x, mousePos.y, false);
|
---|
83 | double dx = mouseGeo.x - mouseStartGeo.x;
|
---|
84 | double dy = mouseGeo.y - mouseStartGeo.y;
|
---|
85 | if (dx == 0 && dy == 0)
|
---|
86 | return;
|
---|
87 |
|
---|
88 | Collection<OsmPrimitive> selection = Main.main.ds.getSelected();
|
---|
89 | Collection<Node> affectedNodes = MoveCommand.getAffectedNodes(selection);
|
---|
90 |
|
---|
91 | // check if any coordinate would be outside the world
|
---|
92 | for (OsmPrimitive osm : affectedNodes) {
|
---|
93 | if (osm instanceof Node && ((Node)osm).coor.isOutSideWorld()) {
|
---|
94 | JOptionPane.showMessageDialog(Main.main, "Cannot move objects outside of the world.");
|
---|
95 | return;
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | Command c = mv.editLayer().lastCommand();
|
---|
100 | if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand)c).objects))
|
---|
101 | ((MoveCommand)c).moveAgain(dx,dy);
|
---|
102 | else
|
---|
103 | mv.editLayer().add(new MoveCommand(selection, dx, dy));
|
---|
104 |
|
---|
105 | mv.repaint();
|
---|
106 | mousePos = e.getPoint();
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Look, whether any object is selected. If not, select the nearest node.
|
---|
111 | * If there are no nodes in the dataset, do nothing.
|
---|
112 | *
|
---|
113 | * If the user did not press the left mouse button, do nothing.
|
---|
114 | *
|
---|
115 | * Also remember the starting position of the movement and change the mouse
|
---|
116 | * cursor to movement.
|
---|
117 | */
|
---|
118 | @Override
|
---|
119 | public void mousePressed(MouseEvent e) {
|
---|
120 | if (e.getButton() != MouseEvent.BUTTON1)
|
---|
121 | return;
|
---|
122 |
|
---|
123 | if (Main.main.ds.getSelected().size() == 0) {
|
---|
124 | OsmPrimitive osm = mv.getNearest(e.getPoint(), (e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) != 0);
|
---|
125 | if (osm != null)
|
---|
126 | osm.setSelected(true);
|
---|
127 | singleOsmPrimitive = osm;
|
---|
128 | mv.repaint();
|
---|
129 | } else
|
---|
130 | singleOsmPrimitive = null;
|
---|
131 |
|
---|
132 | mousePos = e.getPoint();
|
---|
133 | oldCursor = mv.getCursor();
|
---|
134 | mv.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Restore the old mouse cursor.
|
---|
139 | */
|
---|
140 | @Override
|
---|
141 | public void mouseReleased(MouseEvent e) {
|
---|
142 | mv.setCursor(oldCursor);
|
---|
143 | if (singleOsmPrimitive != null) {
|
---|
144 | singleOsmPrimitive.setSelected(false);
|
---|
145 | mv.repaint();
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|