source: josm/src/org/openstreetmap/josm/actions/mapmode/SelectionAction.java@ 101

Last change on this file since 101 was 101, checked in by imi, 18 years ago
  • added zoom/move around with Ctrl+direction keys / Ctrl +, Ctrl -
  • added progress bar counter of downloaded bytes
  • added support for little-osm (relaxed assumptions about osm/xml input format)
File size: 3.8 KB
Line 
1package org.openstreetmap.josm.actions.mapmode;
2
3import java.awt.Rectangle;
4import java.awt.event.KeyEvent;
5import java.util.Collection;
6import java.util.LinkedList;
7
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.data.osm.OsmPrimitive;
10import org.openstreetmap.josm.gui.MapFrame;
11import org.openstreetmap.josm.gui.SelectionManager;
12import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
13import org.openstreetmap.josm.tools.ImageProvider;
14
15/**
16 * This MapMode enables the user to easy make a selection of different objects.
17 *
18 * The selected objects are drawn in a different style.
19 *
20 * Holding and dragging the left mouse button draws an selection rectangle.
21 * When releasing the left mouse button, all objects within the rectangle get
22 * selected.
23 *
24 * When releasing the left mouse button while the right mouse button pressed,
25 * nothing happens (the selection rectangle will be cleared, however).
26 *
27 * When releasing the mouse button and one of the following keys was hold:
28 *
29 * If Alt key was hold, select all objects that are touched by the
30 * selection rectangle. If the Alt key was not hold, select only those objects
31 * completly within (e.g. for ways mean: only if all nodes of the way are
32 * within).
33 *
34 * If Shift key was hold, the objects are added to the current selection. If
35 * Shift key wasn't hold, the current selection get replaced.
36 *
37 * If Ctrl key was hold, remove all objects under the current rectangle from
38 * the active selection (if there were any). Nothing is added to the current
39 * selection.
40 *
41 * Alt can be combined with Ctrl or Shift. Ctrl and Shift cannot be combined.
42 * If both are pressed, nothing happens when releasing the mouse button.
43 *
44 * The user can also only click on the map. All total movements of 2 or less
45 * pixel are considered "only click". If that happens, the nearest Node will
46 * be selected if there is any within 10 pixel range. If there is no Node within
47 * 10 pixel, the nearest Segment (or Street, if user hold down the Alt-Key)
48 * within 10 pixel range is selected. If there is no Segment within 10 pixel
49 * and the user clicked in or 10 pixel away from an area, this area is selected.
50 * If there is even no area, nothing is selected. Shift and Ctrl key applies to
51 * this as usual. For more, @see MapView#getNearest(Point, boolean)
52 *
53 * @author imi
54 */
55public class SelectionAction extends MapMode implements SelectionEnded {
56
57 /**
58 * The SelectionManager that manages the selection rectangle.
59 */
60 private SelectionManager selectionManager;
61
62 /**
63 * Create a new SelectionAction in the given frame.
64 * @param mapFrame The frame this action belongs to
65 */
66 public SelectionAction(MapFrame mapFrame) {
67 super("Selection",
68 "selection",
69 "Select objects by dragging or clicking.",
70 "S",
71 KeyEvent.VK_S,
72 mapFrame,
73 ImageProvider.getCursor("normal", "selection"));
74 this.selectionManager = new SelectionManager(this, false, mapFrame.mapView);
75 }
76
77 @Override public void enterMode() {
78 super.enterMode();
79 selectionManager.register(Main.map.mapView);
80 }
81
82 @Override public void exitMode() {
83 super.exitMode();
84 selectionManager.unregister(Main.map.mapView);
85 }
86
87
88 /**
89 * Check the state of the keys and buttons and set the selection accordingly.
90 */
91 public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl) {
92 if (shift && ctrl)
93 return; // not allowed together
94
95 Collection<OsmPrimitive> curSel;
96 if (!ctrl && !shift)
97 curSel = new LinkedList<OsmPrimitive>(); // new selection will replace the old.
98 else
99 curSel = Main.ds.getSelected();
100
101 Collection<OsmPrimitive> selectionList = selectionManager.getObjectsInRectangle(r,alt);
102 for (OsmPrimitive osm : selectionList)
103 if (ctrl)
104 curSel.remove(osm);
105 else
106 curSel.add(osm);
107 Main.ds.setSelected(curSel);
108 Main.map.mapView.repaint();
109 }
110}
Note: See TracBrowser for help on using the repository browser.