1 | package org.openstreetmap.josm.actions.mapmode;
|
---|
2 |
|
---|
3 | import java.awt.Rectangle;
|
---|
4 | import java.awt.event.KeyEvent;
|
---|
5 | import java.util.Collection;
|
---|
6 |
|
---|
7 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
8 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
9 | import org.openstreetmap.josm.gui.SelectionManager;
|
---|
10 | import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * This MapMode enables the user to easy make a selection of different objects.
|
---|
14 | *
|
---|
15 | * The selected objects are drawn in a different style.
|
---|
16 | *
|
---|
17 | * Holding and dragging the left mouse button draws an selection rectangle.
|
---|
18 | * When releasing the left mouse button, all objects within the rectangle get
|
---|
19 | * selected.
|
---|
20 | *
|
---|
21 | * When releasing the left mouse button while the right mouse button pressed,
|
---|
22 | * nothing happens (the selection rectangle will be cleared, however).
|
---|
23 | *
|
---|
24 | * When releasing the mouse button and one of the following keys was hold:
|
---|
25 | *
|
---|
26 | * If Alt key was hold, select all objects that are touched by the
|
---|
27 | * selection rectangle. If the Alt key was not hold, select only those objects
|
---|
28 | * completly within (e.g. for tracks mean: only if all nodes of the track are
|
---|
29 | * within).
|
---|
30 | *
|
---|
31 | * If Shift key was hold, the objects are added to the current selection. If
|
---|
32 | * Shift key wasn't hold, the current selection get replaced.
|
---|
33 | *
|
---|
34 | * If Ctrl key was hold, remove all objects under the current rectangle from
|
---|
35 | * the active selection (if there were any). Nothing is added to the current
|
---|
36 | * selection.
|
---|
37 | *
|
---|
38 | * Alt can be combined with Ctrl or Shift. Ctrl and Shift cannot be combined.
|
---|
39 | * If both are pressed, nothing happens when releasing the mouse button.
|
---|
40 | *
|
---|
41 | * The user can also only click on the map. All total movements of 2 or less
|
---|
42 | * pixel are considered "only click". If that happens, the nearest Node will
|
---|
43 | * be selected if there is any within 10 pixel range. If there is no Node within
|
---|
44 | * 10 pixel, the nearest LineSegment (or Street, if user hold down the Alt-Key)
|
---|
45 | * within 10 pixel range is selected. If there is no LineSegment within 10 pixel
|
---|
46 | * and the user clicked in or 10 pixel away from an area, this area is selected.
|
---|
47 | * If there is even no area, nothing is selected. Shift and Ctrl key applies to
|
---|
48 | * this as usual. For more, @see MapView#getNearest(Point, boolean)
|
---|
49 | *
|
---|
50 | * @author imi
|
---|
51 | */
|
---|
52 | public class SelectionAction extends MapMode implements SelectionEnded {
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * The SelectionManager that manages the selection rectangle.
|
---|
56 | */
|
---|
57 | private SelectionManager selectionManager;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Create a new SelectionAction in the given frame.
|
---|
61 | * @param mapFrame The frame this action belongs to
|
---|
62 | */
|
---|
63 | public SelectionAction(MapFrame mapFrame) {
|
---|
64 | super("Selection", "selection", "Select objects by dragging or clicking", KeyEvent.VK_S, mapFrame);
|
---|
65 | this.selectionManager = new SelectionManager(this, false, mv);
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public void registerListener() {
|
---|
70 | super.registerListener();
|
---|
71 | selectionManager.register(mv);
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public void unregisterListener() {
|
---|
76 | super.unregisterListener();
|
---|
77 | selectionManager.unregister(mv);
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Check the state of the keys and buttons and set the selection accordingly.
|
---|
83 | */
|
---|
84 | public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl) {
|
---|
85 | if (shift && ctrl)
|
---|
86 | return; // not allowed together
|
---|
87 |
|
---|
88 | if (!ctrl && !shift)
|
---|
89 | ds.clearSelection(); // new selection will replace the old.
|
---|
90 |
|
---|
91 | Collection<OsmPrimitive> selectionList = selectionManager.getObjectsInRectangle(r,alt);
|
---|
92 | for (OsmPrimitive osm : selectionList)
|
---|
93 | osm.setSelected(!ctrl, ds);
|
---|
94 | mv.repaint();
|
---|
95 | }
|
---|
96 | }
|
---|