public class SelectionManager extends java.lang.Object implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.beans.PropertyChangeListener
setLassoMode(boolean)
.
The point where the left mouse button was pressed and the current mouse
position are two opposite corners of the selection rectangle.
For rectangle mode, it is possible to specify an aspect ratio (width per height) which the
selection rectangle always must have. In this case, the selection rectangle
will be the largest window with this aspect ratio, where the position the left
mouse button was pressed and the corner of the current mouse position are at
opposite sites (the mouse position corner is the corner nearest to the mouse
cursor).
When the left mouse button was released, an ActionEvent is send to the
ActionListener given at constructor. The source of this event is this manager.Modifier and Type | Class and Description |
---|---|
static interface |
SelectionManager.SelectionEnded
This is the interface that an user of SelectionManager has to implement
to get informed when a selection closes.
|
private class |
SelectionManager.SelectionHintLayer
This draws the selection hint (rectangle or lasso polygon) on the screen.
|
Modifier and Type | Field and Description |
---|---|
private boolean |
aspectRatio
Whether the selection rectangle must obtain the aspect ratio of the drawComponent.
|
private java.awt.Polygon |
lasso
The polygon to store the selection outline if
lassoMode is used. |
private boolean |
lassoMode
true if we should paint a lasso instead of a rectangle. |
private java.awt.Point |
mousePos
The last position of the mouse while the mouse button was pressed.
|
private java.awt.Point |
mousePosStart
Position of the map when the mouse button was pressed.
|
private NavigatableComponent |
nc
The Component that provides us with OSM data and the aspect is taken from.
|
private SelectionManager.SelectionEnded |
selectionEndedListener
The listener that receives the events after left mouse button is released.
|
private SelectionManager.SelectionHintLayer |
selectionHintLayer |
private java.awt.Polygon |
selectionResult
The result of the last selection.
|
Constructor and Description |
---|
SelectionManager(SelectionManager.SelectionEnded selectionEndedListener,
boolean aspectRatio,
NavigatableComponent navComp)
Create a new SelectionManager.
|
Modifier and Type | Method and Description |
---|---|
private void |
abortSelecting() |
private void |
addLassoPoint(java.awt.Point point) |
void |
endSelecting(java.awt.event.MouseEvent e)
Ends the selection of the current area.
|
java.util.Collection<OsmPrimitive> |
getSelectedObjects(boolean alt)
Return a list of all objects in the active/last selection, respecting the different
modifier.
|
private java.awt.Rectangle |
getSelectionRectangle()
Calculate and return the current selection rectangle
|
private boolean |
isNoSelection() |
void |
mouseClicked(java.awt.event.MouseEvent e) |
void |
mouseDragged(java.awt.event.MouseEvent e)
If the correct button is hold, draw the rectangle.
|
void |
mouseEntered(java.awt.event.MouseEvent e) |
void |
mouseExited(java.awt.event.MouseEvent e) |
void |
mouseMoved(java.awt.event.MouseEvent e) |
void |
mousePressed(java.awt.event.MouseEvent e)
If the correct button, from the "drawing rectangle" mode
|
void |
mouseReleased(java.awt.event.MouseEvent e)
Check the state of the keys and buttons and set the selection accordingly.
|
private void |
moveSelection(int dx,
int dy)
Moves the current selection by some pixels.
|
void |
propertyChange(java.beans.PropertyChangeEvent evt)
If the action goes inactive, remove the selection rectangle from screen
|
private static java.awt.Polygon |
rectToPolygon(java.awt.Rectangle r) |
void |
register(MapView eventSource,
boolean lassoMode)
Register itself at the given event source and add a hint layer.
|
private void |
selectingDone(java.awt.event.MouseEvent e)
Stores the current selection and stores the result in
selectionResult to be retrieved by
getSelectedObjects(boolean) later. |
private static void |
selectionAreaChanged() |
void |
setLassoMode(boolean lassoMode)
Enables or disables the lasso mode.
|
void |
unregister(MapView eventSource)
Unregister itself from the given event source and hide the selection hint layer.
|
private final SelectionManager.SelectionEnded selectionEndedListener
private java.awt.Point mousePosStart
null
, a rectangle/lasso line is drawn on screen.
If this is null
, no selection is active.private java.awt.Point mousePos
private final NavigatableComponent nc
private final boolean aspectRatio
private boolean lassoMode
true
if we should paint a lasso instead of a rectangle.private final java.awt.Polygon lasso
lassoMode
is used.private java.awt.Polygon selectionResult
private final SelectionManager.SelectionHintLayer selectionHintLayer
public SelectionManager(SelectionManager.SelectionEnded selectionEndedListener, boolean aspectRatio, NavigatableComponent navComp)
selectionEndedListener
- The action listener that receives the event when
the left button is released.aspectRatio
- If true, the selection window must obtain the aspect
ratio of the drawComponent.navComp
- The component that provides us with OSM data and the aspect is taken from.public void register(MapView eventSource, boolean lassoMode)
eventSource
- The emitter of the mouse events.lassoMode
- true
to enable lasso mode, false
to disable it.public void unregister(MapView eventSource)
eventSource
- The emitter of the mouse events.public void mousePressed(java.awt.event.MouseEvent e)
mousePressed
in interface java.awt.event.MouseListener
public void mouseDragged(java.awt.event.MouseEvent e)
mouseDragged
in interface java.awt.event.MouseMotionListener
private void moveSelection(int dx, int dy)
dx
- How much to move it in x direction.dy
- How much to move it in y direction.public void mouseReleased(java.awt.event.MouseEvent e)
mouseReleased
in interface java.awt.event.MouseListener
public void endSelecting(java.awt.event.MouseEvent e)
e
- A mouse event that caused this. Needed for backward compatibility.private void addLassoPoint(java.awt.Point point)
private boolean isNoSelection()
private java.awt.Rectangle getSelectionRectangle()
public void propertyChange(java.beans.PropertyChangeEvent evt)
propertyChange
in interface java.beans.PropertyChangeListener
private void selectingDone(java.awt.event.MouseEvent e)
selectionResult
to be retrieved by
getSelectedObjects(boolean)
later.e
- The mouse event that caused the selection to be finished.private void abortSelecting()
private static void selectionAreaChanged()
public java.util.Collection<OsmPrimitive> getSelectedObjects(boolean alt)
alt
- Whether the alt key was pressed, which means select all
objects that are touched, instead those which are completely covered.private static java.awt.Polygon rectToPolygon(java.awt.Rectangle r)
public void setLassoMode(boolean lassoMode)
lassoMode
- true
to enable lasso mode, false
to disable it.public void mouseClicked(java.awt.event.MouseEvent e)
mouseClicked
in interface java.awt.event.MouseListener
public void mouseEntered(java.awt.event.MouseEvent e)
mouseEntered
in interface java.awt.event.MouseListener
public void mouseExited(java.awt.event.MouseEvent e)
mouseExited
in interface java.awt.event.MouseListener
public void mouseMoved(java.awt.event.MouseEvent e)
mouseMoved
in interface java.awt.event.MouseMotionListener