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