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