source: josm/trunk/src/org/openstreetmap/josm/actions/mapmode/SelectAction.java@ 982

Last change on this file since 982 was 982, checked in by stoecker, 16 years ago

close bug #1516

  • Property svn:eol-style set to native
File size: 12.6 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions.mapmode;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Cursor;
7import java.awt.Point;
8import java.awt.Rectangle;
9import java.awt.event.ActionEvent;
10import java.awt.event.KeyEvent;
11import java.awt.event.MouseEvent;
12import java.util.Collection;
13import java.util.Collections;
14import java.util.LinkedList;
15
16import javax.swing.JOptionPane;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.actions.MergeNodesAction;
20import org.openstreetmap.josm.command.AddCommand;
21import org.openstreetmap.josm.command.Command;
22import org.openstreetmap.josm.command.ChangeCommand;
23import org.openstreetmap.josm.command.MoveCommand;
24import org.openstreetmap.josm.command.RotateCommand;
25import org.openstreetmap.josm.command.SequenceCommand;
26import org.openstreetmap.josm.data.coor.EastNorth;
27import org.openstreetmap.josm.data.osm.Node;
28import org.openstreetmap.josm.data.osm.OsmPrimitive;
29import org.openstreetmap.josm.data.osm.Way;
30import org.openstreetmap.josm.data.osm.WaySegment;
31import org.openstreetmap.josm.data.osm.visitor.AllNodesVisitor;
32import org.openstreetmap.josm.data.osm.visitor.SimplePaintVisitor;
33import org.openstreetmap.josm.gui.MapFrame;
34import org.openstreetmap.josm.gui.MapView;
35import org.openstreetmap.josm.gui.SelectionManager;
36import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
37import org.openstreetmap.josm.tools.ImageProvider;
38/**
39 * Move is an action that can move all kind of OsmPrimitives (except keys for now).
40 *
41 * If an selected object is under the mouse when dragging, move all selected objects.
42 * If an unselected object is under the mouse when dragging, it becomes selected
43 * and will be moved.
44 * If no object is under the mouse, move all selected objects (if any)
45 *
46 * @author imi
47 */
48public class SelectAction extends MapMode implements SelectionEnded {
49
50 enum Mode { move, rotate, select }
51 private Mode mode = null;
52 private long mouseDownTime = 0;
53 private boolean didMove = false;
54 Node virtualNode = null;
55 WaySegment virtualWay = null;
56 SequenceCommand virtualCmds = null;
57
58 /**
59 * The old cursor before the user pressed the mouse button.
60 */
61 private Cursor oldCursor;
62 /**
63 * The position of the mouse before the user moves a node.
64 */
65 private Point mousePos;
66 private SelectionManager selectionManager;
67
68 /**
69 * The time which needs to pass between click and release before something
70 * counts as a move, in milliseconds
71 */
72 private int initialMoveDelay = 200;
73
74 /**
75 * The screen distance which needs to be travelled before something
76 * counts as a move, in pixels
77 */
78 private int initialMoveThreshold = 15;
79 private boolean initialMoveThresholdExceeded = false;
80 /**
81 * Create a new SelectAction
82 * @param mapFrame The MapFrame this action belongs to.
83 */
84 public SelectAction(MapFrame mapFrame) {
85 super(tr("Select"), "move/move", tr("Select, move and rotate objects"),
86 KeyEvent.VK_S, mapFrame,
87 getCursor("normal", "selection", Cursor.DEFAULT_CURSOR));
88 putValue("help", "Action/Move/Move");
89 selectionManager = new SelectionManager(this, false, mapFrame.mapView);
90 try { initialMoveDelay = Integer.parseInt(Main.pref.get("edit.initial-move-delay","200")); } catch (NumberFormatException x) {}
91 try { initialMoveThreshold = Integer.parseInt(Main.pref.get("edit.initial-move-threshold","5")); } catch (NumberFormatException x) {}
92
93 }
94
95 private static Cursor getCursor(String name, String mod, int def) {
96 try {
97 return ImageProvider.getCursor(name, mod);
98 } catch (Exception e) {
99 }
100 return Cursor.getPredefinedCursor(def);
101 }
102
103 private void setCursor(Cursor c) {
104 if (oldCursor == null) {
105 oldCursor = Main.map.mapView.getCursor();
106 Main.map.mapView.setCursor(c);
107 }
108 }
109
110 private void restoreCursor() {
111 if (oldCursor != null) {
112 Main.map.mapView.setCursor(oldCursor);
113 oldCursor = null;
114 }
115 }
116
117 @Override public void enterMode() {
118 super.enterMode();
119 Main.map.mapView.addMouseListener(this);
120 Main.map.mapView.addMouseMotionListener(this);
121 Main.map.mapView.enableVirtualNodes(
122 Main.pref.getInteger("mappaint.node.virtual-size", 8) != 0);
123 }
124
125 @Override public void exitMode() {
126 super.exitMode();
127 selectionManager.unregister(Main.map.mapView);
128 Main.map.mapView.removeMouseListener(this);
129 Main.map.mapView.removeMouseMotionListener(this);
130 Main.map.mapView.enableVirtualNodes(false);
131 }
132
133 /**
134 * If the left mouse button is pressed, move all currently selected
135 * objects (if one of them is under the mouse) or the current one under the
136 * mouse (which will become selected).
137 */
138 @Override public void mouseDragged(MouseEvent e) {
139 if (mode == Mode.select) return;
140
141 // do not count anything as a move if it lasts less than 100 milliseconds.
142 if ((mode == Mode.move) && (System.currentTimeMillis() - mouseDownTime < initialMoveDelay)) return;
143
144 if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0)
145 return;
146
147 if (mode == Mode.move) {
148 setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
149 }
150
151 if (mousePos == null) {
152 mousePos = e.getPoint();
153 return;
154 }
155
156 if (!initialMoveThresholdExceeded) {
157 int dxp = mousePos.x - e.getX();
158 int dyp = mousePos.y - e.getY();
159 int dp = (int) Math.sqrt(dxp*dxp+dyp*dyp);
160 if (dp < initialMoveThreshold) return;
161 initialMoveThresholdExceeded = true;
162 }
163
164 EastNorth mouseEN = Main.map.mapView.getEastNorth(e.getX(), e.getY());
165 EastNorth mouseStartEN = Main.map.mapView.getEastNorth(mousePos.x, mousePos.y);
166 double dx = mouseEN.east() - mouseStartEN.east();
167 double dy = mouseEN.north() - mouseStartEN.north();
168 if (dx == 0 && dy == 0)
169 return;
170
171 if (virtualWay != null) {
172 Collection<Command> virtualCmds = new LinkedList<Command>();
173 virtualCmds.add(new AddCommand(virtualNode));
174 Way w = virtualWay.way;
175 Way wnew = new Way(w);
176 wnew.nodes.add(virtualWay.lowerIndex+1, virtualNode);
177 virtualCmds.add(new ChangeCommand(w, wnew));
178 virtualCmds.add(new MoveCommand(virtualNode, dx, dy));
179 Main.main.undoRedo.add(new SequenceCommand(tr("Add and move a virtual new node to way"), virtualCmds));
180 selectPrims(Collections.singleton((OsmPrimitive)virtualNode), false, false);
181 virtualWay = null;
182 virtualNode = null;
183 } else {
184 Collection<OsmPrimitive> selection = Main.ds.getSelected();
185 Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
186
187 // when rotating, having only one node makes no sense - quit silently
188 if (mode == Mode.rotate && affectedNodes.size() < 2)
189 return;
190
191 Command c = !Main.main.undoRedo.commands.isEmpty()
192 ? Main.main.undoRedo.commands.getLast() : null;
193 if (c instanceof SequenceCommand)
194 c = ((SequenceCommand)c).getLastCommand();
195
196 if (mode == Mode.move) {
197 if (c instanceof MoveCommand && affectedNodes.equals(((MoveCommand)c).objects))
198 ((MoveCommand)c).moveAgain(dx,dy);
199 else
200 Main.main.undoRedo.add(
201 c = new MoveCommand(selection, dx, dy));
202
203 for (Node n : affectedNodes) {
204 if (n.coor.isOutSideWorld()) {
205 // Revert move
206 ((MoveCommand) c).moveAgain(-dx, -dy);
207
208 JOptionPane.showMessageDialog(Main.parent,
209 tr("Cannot move objects outside of the world."));
210 return;
211 }
212 }
213 } else if (mode == Mode.rotate) {
214 if (c instanceof RotateCommand && affectedNodes.equals(((RotateCommand)c).objects))
215 ((RotateCommand)c).rotateAgain(mouseStartEN, mouseEN);
216 else
217 Main.main.undoRedo.add(new RotateCommand(selection, mouseStartEN, mouseEN));
218 }
219 }
220
221 Main.map.mapView.repaint();
222 mousePos = e.getPoint();
223
224 didMove = true;
225 }
226
227 private Collection<OsmPrimitive> getNearestCollectionVirtual(Point p) {
228 MapView c = Main.map.mapView;
229 int snapDistance = Main.pref.getInteger("mappaint.node.virtual-snap-distance", 8);
230 snapDistance *= snapDistance;
231 OsmPrimitive osm = c.getNearestNode(p);
232 virtualWay = null;
233 virtualNode = null;
234
235 if (osm == null)
236 {
237 WaySegment nearestWaySeg = c.getNearestWaySegment(p);
238 if (nearestWaySeg != null)
239 {
240 osm = nearestWaySeg.way;
241 if(Main.pref.getInteger("mappaint.node.virtual-size", 8) > 0)
242 {
243 Way w = (Way)osm;
244 Point p1 = c.getPoint(w.nodes.get(nearestWaySeg.lowerIndex).eastNorth);
245 Point p2 = c.getPoint(w.nodes.get(nearestWaySeg.lowerIndex+1).eastNorth);
246 if(SimplePaintVisitor.isLargeSegment(p1, p2, Main.pref.getInteger("mappaint.node.virtual-space", 70)))
247 {
248 Point pc = new Point((p1.x+p2.x)/2, (p1.y+p2.y)/2);
249 if (p.distanceSq(pc) < snapDistance)
250 {
251 virtualWay = nearestWaySeg;
252 virtualNode = new Node(Main.map.mapView.getLatLon(pc.x, pc.y));
253 osm = w;
254 }
255 }
256 }
257 }
258 }
259 if (osm == null)
260 return Collections.emptySet();
261 return Collections.singleton(osm);
262 }
263
264 /**
265 * Look, whether any object is selected. If not, select the nearest node.
266 * If there are no nodes in the dataset, do nothing.
267 *
268 * If the user did not press the left mouse button, do nothing.
269 *
270 * Also remember the starting position of the movement and change the mouse
271 * cursor to movement.
272 */
273 @Override public void mousePressed(MouseEvent e) {
274 if (! (Boolean)this.getValue("active")) return;
275 if (e.getButton() != MouseEvent.BUTTON1)
276 return;
277 boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
278 // boolean alt = (e.getModifiers() & ActionEvent.ALT_MASK) != 0;
279 boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
280
281 mouseDownTime = System.currentTimeMillis();
282 didMove = false;
283 initialMoveThresholdExceeded = false;
284
285 Collection<OsmPrimitive> osmColl = getNearestCollectionVirtual(e.getPoint());
286
287 if (ctrl && shift) {
288 if (Main.ds.getSelected().isEmpty()) selectPrims(osmColl, true, false);
289 mode = Mode.rotate;
290 setCursor(ImageProvider.getCursor("rotate", null));
291 } else if (!osmColl.isEmpty()) {
292 // Don't replace the selection now if the user clicked on a
293 // selected object (this would break moving of selected groups).
294 // We'll do that later in mouseReleased if the user didn't try to
295 // move.
296 selectPrims(osmColl,
297 shift || Main.ds.getSelected().containsAll(osmColl),
298 ctrl);
299 mode = Mode.move;
300 } else {
301 mode = Mode.select;
302 oldCursor = Main.map.mapView.getCursor();
303 selectionManager.register(Main.map.mapView);
304 selectionManager.mousePressed(e);
305 }
306 if(mode != Mode.move || shift || ctrl)
307 {
308 virtualNode = null;
309 virtualWay = null;
310 }
311
312 updateStatusLine();
313 Main.map.mapView.repaint();
314
315 mousePos = e.getPoint();
316 }
317
318 /**
319 * Restore the old mouse cursor.
320 */
321 @Override public void mouseReleased(MouseEvent e) {
322 if (mode == Mode.select) {
323 selectionManager.unregister(Main.map.mapView);
324 }
325 restoreCursor();
326
327 if (mode == Mode.move) {
328 boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
329 boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
330 if (!didMove) {
331 selectPrims(
332 Main.map.mapView.getNearestCollection(e.getPoint()),
333 shift, ctrl);
334 } else if (ctrl) {
335 Collection<OsmPrimitive> selection = Main.ds.getSelected();
336 Collection<Node> affectedNodes = AllNodesVisitor.getAllNodes(selection);
337 Collection<Node> nn = Main.map.mapView.getNearestNodes(e.getPoint(), affectedNodes);
338 if (nn != null) {
339 Node n = nn.iterator().next();
340 LinkedList<Node> selNodes = new LinkedList<Node>();
341 for (OsmPrimitive osm : selection)
342 if (osm instanceof Node)
343 selNodes.add((Node)osm);
344 if (selNodes.size() > 0) {
345 selNodes.add(n);
346 MergeNodesAction.mergeNodes(selNodes, n);
347 }
348 }
349 }
350 }
351
352 updateStatusLine();
353 mode = null;
354 updateStatusLine();
355 }
356
357 public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl) {
358 selectPrims(selectionManager.getObjectsInRectangle(r, alt), shift, ctrl);
359 }
360
361 public void selectPrims(Collection<OsmPrimitive> selectionList, boolean shift, boolean ctrl) {
362 if (shift && ctrl)
363 return; // not allowed together
364
365 Collection<OsmPrimitive> curSel;
366 if (!ctrl && !shift)
367 curSel = new LinkedList<OsmPrimitive>(); // new selection will replace the old.
368 else
369 curSel = Main.ds.getSelected();
370
371 for (OsmPrimitive osm : selectionList)
372 if (ctrl)
373 curSel.remove(osm);
374 else
375 curSel.add(osm);
376 Main.ds.setSelected(curSel);
377 Main.map.mapView.repaint();
378 }
379
380 @Override public String getModeHelpText() {
381 if (mode == Mode.select) {
382 return tr("Release the mouse button to select the objects in the rectangle.");
383 } else if (mode == Mode.move) {
384 return tr("Release the mouse button to stop moving. Ctrl to merge with nearest node.");
385 } else if (mode == Mode.rotate) {
386 return tr("Release the mouse button to stop rotating.");
387 } else {
388 return tr("Move objects by dragging; Shift to add to selection (Ctrl to remove); Shift-Ctrl to rotate selected; or change selection");
389 }
390 }
391}
Note: See TracBrowser for help on using the repository browser.