[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;
|
---|
[86] | 6 | import java.util.LinkedList;
|
---|
[3] | 7 |
|
---|
[22] | 8 | import org.openstreetmap.josm.Main;
|
---|
[4] | 9 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[3] | 10 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
[4] | 11 | import org.openstreetmap.josm.gui.SelectionManager;
|
---|
| 12 | import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
|
---|
[3] | 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
|
---|
[71] | 30 | * completly within (e.g. for ways mean: only if all nodes of the way are
|
---|
[3] | 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 | *
|
---|
[4] | 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
|
---|
[86] | 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
|
---|
[4] | 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
|
---|
[16] | 50 | * this as usual. For more, @see MapView#getNearest(Point, boolean)
|
---|
[4] | 51 | *
|
---|
[3] | 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.
|
---|
[7] | 63 | * @param mapFrame The frame this action belongs to
|
---|
[3] | 64 | */
|
---|
| 65 | public SelectionAction(MapFrame mapFrame) {
|
---|
[68] | 66 | super("Selection", "selection", "Select objects by dragging or clicking.", "S", KeyEvent.VK_S, mapFrame);
|
---|
[16] | 67 | this.selectionManager = new SelectionManager(this, false, mv);
|
---|
[3] | 68 | }
|
---|
| 69 |
|
---|
[86] | 70 | @Override public void registerListener() {
|
---|
[7] | 71 | super.registerListener();
|
---|
[16] | 72 | selectionManager.register(mv);
|
---|
[3] | 73 | }
|
---|
| 74 |
|
---|
[86] | 75 | @Override public void unregisterListener() {
|
---|
[7] | 76 | super.unregisterListener();
|
---|
[16] | 77 | selectionManager.unregister(mv);
|
---|
[3] | 78 | }
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * Check the state of the keys and buttons and set the selection accordingly.
|
---|
| 83 | */
|
---|
[7] | 84 | public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl) {
|
---|
[3] | 85 | if (shift && ctrl)
|
---|
| 86 | return; // not allowed together
|
---|
| 87 |
|
---|
[86] | 88 | Collection<OsmPrimitive> curSel;
|
---|
[7] | 89 | if (!ctrl && !shift)
|
---|
[86] | 90 | curSel = new LinkedList<OsmPrimitive>(); // new selection will replace the old.
|
---|
| 91 | else
|
---|
| 92 | curSel = Main.ds.getSelected();
|
---|
[3] | 93 |
|
---|
[7] | 94 | Collection<OsmPrimitive> selectionList = selectionManager.getObjectsInRectangle(r,alt);
|
---|
| 95 | for (OsmPrimitive osm : selectionList)
|
---|
[86] | 96 | if (ctrl)
|
---|
| 97 | curSel.remove(osm);
|
---|
| 98 | else
|
---|
| 99 | curSel.add(osm);
|
---|
| 100 | Main.ds.setSelected(curSel);
|
---|
[16] | 101 | mv.repaint();
|
---|
[3] | 102 | }
|
---|
| 103 | }
|
---|