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

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