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