1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions.mapmode;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Rectangle;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.awt.event.MouseEvent;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.HashSet;
|
---|
12 | import java.util.LinkedList;
|
---|
13 | import java.util.Map;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.actions.GroupAction;
|
---|
17 | import org.openstreetmap.josm.data.osm.Node;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
19 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
20 | import org.openstreetmap.josm.gui.SelectionManager;
|
---|
21 | import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
|
---|
22 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * This MapMode enables the user to easy make a selection of different objects.
|
---|
26 | *
|
---|
27 | * The selected objects are drawn in a different style.
|
---|
28 | *
|
---|
29 | * Holding and dragging the left mouse button draws an selection rectangle.
|
---|
30 | * When releasing the left mouse button, all objects within the rectangle get
|
---|
31 | * selected.
|
---|
32 | *
|
---|
33 | * When releasing the left mouse button while the right mouse button pressed,
|
---|
34 | * nothing happens (the selection rectangle will be cleared, however).
|
---|
35 | *
|
---|
36 | * When releasing the mouse button and one of the following keys was hold:
|
---|
37 | *
|
---|
38 | * If Alt key was hold, select all objects that are touched by the
|
---|
39 | * selection rectangle. If the Alt key was not hold, select only those objects
|
---|
40 | * completly within (e.g. for ways mean: only if all nodes of the way are
|
---|
41 | * within).
|
---|
42 | *
|
---|
43 | * If Shift key was hold, the objects are added to the current selection. If
|
---|
44 | * Shift key wasn't hold, the current selection get replaced.
|
---|
45 | *
|
---|
46 | * If Ctrl key was hold, remove all objects under the current rectangle from
|
---|
47 | * the active selection (if there were any). Nothing is added to the current
|
---|
48 | * selection.
|
---|
49 | *
|
---|
50 | * Alt can be combined with Ctrl or Shift. Ctrl and Shift cannot be combined.
|
---|
51 | * If both are pressed, nothing happens when releasing the mouse button.
|
---|
52 | *
|
---|
53 | * The user can also only click on the map. All total movements of 2 or less
|
---|
54 | * pixel are considered "only click". If that happens, the nearest Node will
|
---|
55 | * be selected if there is any within 10 pixel range. If there is no Node within
|
---|
56 | * 10 pixel, the nearest Way within 10 pixel range is selected. If there is no
|
---|
57 | * Way within 10 pixel and the user clicked in or 10 pixel away from an area,
|
---|
58 | * this area is selected. If there is even no area, nothing is selected.
|
---|
59 | * Shift and Ctrl key applies to this as usual. For more,
|
---|
60 | * @see MapView#getNearest(Point)
|
---|
61 | *
|
---|
62 | * @author imi
|
---|
63 | */
|
---|
64 | public class SelectionAction extends MapMode implements SelectionEnded {
|
---|
65 |
|
---|
66 | public static class Group extends GroupAction {
|
---|
67 | public Group(MapFrame mf) {
|
---|
68 | super(KeyEvent.VK_S,0);
|
---|
69 | putValue("help", "Action/Selection");
|
---|
70 | actions.add(new SelectionAction(mf, tr("Selection"), tr("Select objects by dragging or clicking.")));
|
---|
71 | setCurrent(0);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * The SelectionManager that manages the selection rectangle.
|
---|
78 | */
|
---|
79 | private SelectionManager selectionManager;
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Create a new SelectionAction in the given frame.
|
---|
83 | * @param mapFrame The frame this action belongs to
|
---|
84 | */
|
---|
85 | public SelectionAction(MapFrame mapFrame, String name, String desc) {
|
---|
86 | super(name, "selection/select", desc, mapFrame, ImageProvider.getCursor("normal", "selection"));
|
---|
87 | putValue("help", "Action/Selection");
|
---|
88 | this.selectionManager = new SelectionManager(this, false, mapFrame.mapView);
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override public void enterMode() {
|
---|
92 | super.enterMode();
|
---|
93 | selectionManager.register(Main.map.mapView);
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override public void exitMode() {
|
---|
97 | super.exitMode();
|
---|
98 | selectionManager.unregister(Main.map.mapView);
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Check the state of the keys and buttons and set the selection accordingly.
|
---|
104 | */
|
---|
105 | public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl) {
|
---|
106 | selectEverythingInRectangle(selectionManager, r, alt, shift, ctrl);
|
---|
107 | }
|
---|
108 |
|
---|
109 | public static void selectEverythingInRectangle(SelectionManager selectionManager, Rectangle r, boolean alt, boolean shift, boolean ctrl) {
|
---|
110 | if (shift && ctrl)
|
---|
111 | return; // not allowed together
|
---|
112 |
|
---|
113 | Collection<OsmPrimitive> curSel;
|
---|
114 | if (!ctrl && !shift)
|
---|
115 | curSel = new LinkedList<OsmPrimitive>(); // new selection will replace the old.
|
---|
116 | else
|
---|
117 | curSel = Main.ds.getSelected();
|
---|
118 |
|
---|
119 | Collection<OsmPrimitive> selectionList = selectionManager.getObjectsInRectangle(r,alt);
|
---|
120 | for (OsmPrimitive osm : selectionList)
|
---|
121 | if (ctrl)
|
---|
122 | curSel.remove(osm);
|
---|
123 | else
|
---|
124 | curSel.add(osm);
|
---|
125 | Main.ds.setSelected(curSel);
|
---|
126 | Main.map.mapView.repaint();
|
---|
127 | }
|
---|
128 | }
|
---|