Package org.openstreetmap.josm.gui
Class SelectionManager
- java.lang.Object
-
- org.openstreetmap.josm.gui.SelectionManager
-
- All Implemented Interfaces:
java.awt.event.MouseListener,java.awt.event.MouseMotionListener,java.beans.PropertyChangeListener,java.util.EventListener
public class SelectionManager extends java.lang.Object implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.beans.PropertyChangeListener
Manages the selection of a rectangle or a lasso loop. Listening to left and right mouse button presses and to mouse motions and draw the rectangle accordingly. Left mouse button selects a rectangle from the press until release. Pressing right mouse button while left is still pressed enable the selection area to move around. Releasing the left button fires an action event to the listener given at constructor, except if the right is still pressed, which just remove the selection rectangle and does nothing. It is possible to switch between lasso selection and rectangle selection by usingsetLassoMode(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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceSelectionManager.SelectionEndedThis is the interface that an user of SelectionManager has to implement to get informed when a selection closes.private classSelectionManager.SelectionHintLayerThis draws the selection hint (rectangle or lasso polygon) on the screen.
-
Field Summary
Fields Modifier and Type Field Description private booleanaspectRatioWhether the selection rectangle must obtain the aspect ratio of the drawComponent.private java.awt.PolygonlassoThe polygon to store the selection outline iflassoModeis used.private booleanlassoModetrueif we should paint a lasso instead of a rectangle.private java.awt.PointmousePosThe last position of the mouse while the mouse button was pressed.private java.awt.PointmousePosStartPosition of the map when the mouse button was pressed.private NavigatableComponentncThe Component that provides us with OSM data and the aspect is taken from.private SelectionManager.SelectionEndedselectionEndedListenerThe listener that receives the events after left mouse button is released.private SelectionManager.SelectionHintLayerselectionHintLayerprivate java.awt.PolygonselectionResultThe result of the last selection.
-
Constructor Summary
Constructors Constructor Description SelectionManager(SelectionManager.SelectionEnded selectionEndedListener, boolean aspectRatio, NavigatableComponent navComp)Create a new SelectionManager.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidabortSelecting()private voidaddLassoPoint(java.awt.Point point)voidendSelecting(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.RectanglegetSelectionRectangle()Calculate and return the current selection rectangleprivate booleanisNoSelection()voidmouseClicked(java.awt.event.MouseEvent e)voidmouseDragged(java.awt.event.MouseEvent e)If the correct button is hold, draw the rectangle.voidmouseEntered(java.awt.event.MouseEvent e)voidmouseExited(java.awt.event.MouseEvent e)voidmouseMoved(java.awt.event.MouseEvent e)voidmousePressed(java.awt.event.MouseEvent e)If the correct button, from the "drawing rectangle" modevoidmouseReleased(java.awt.event.MouseEvent e)Check the state of the keys and buttons and set the selection accordingly.private voidmoveSelection(int dx, int dy)Moves the current selection by some pixels.voidpropertyChange(java.beans.PropertyChangeEvent evt)If the action goes inactive, remove the selection rectangle from screenprivate static java.awt.PolygonrectToPolygon(java.awt.Rectangle r)voidregister(MapView eventSource, boolean lassoMode)Register itself at the given event source and add a hint layer.private voidselectingDone(java.awt.event.MouseEvent e)Stores the current selection and stores the result inselectionResultto be retrieved bygetSelectedObjects(boolean)later.private voidselectionAreaChanged()voidsetLassoMode(boolean lassoMode)Enables or disables the lasso mode.voidunregister(MapView eventSource)Unregister itself from the given event source and hide the selection hint layer.
-
-
-
Field Detail
-
selectionEndedListener
private final SelectionManager.SelectionEnded selectionEndedListener
The listener that receives the events after left mouse button is released.
-
mousePosStart
private java.awt.Point mousePosStart
Position of the map when the mouse button was pressed. If this is notnull, a rectangle/lasso line is drawn on screen. If this isnull, no selection is active.
-
mousePos
private java.awt.Point mousePos
The last position of the mouse while the mouse button was pressed.
-
nc
private final NavigatableComponent nc
The Component that provides us with OSM data and the aspect is taken from.
-
aspectRatio
private final boolean aspectRatio
Whether the selection rectangle must obtain the aspect ratio of the drawComponent.
-
lassoMode
private boolean lassoMode
trueif we should paint a lasso instead of a rectangle.
-
lasso
private final java.awt.Polygon lasso
The polygon to store the selection outline iflassoModeis used.
-
selectionResult
private java.awt.Polygon selectionResult
The result of the last selection.
-
selectionHintLayer
private final SelectionManager.SelectionHintLayer selectionHintLayer
-
-
Constructor Detail
-
SelectionManager
public SelectionManager(SelectionManager.SelectionEnded selectionEndedListener, boolean aspectRatio, NavigatableComponent navComp)
Create a new SelectionManager.- Parameters:
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.
-
-
Method Detail
-
register
public void register(MapView eventSource, boolean lassoMode)
Register itself at the given event source and add a hint layer.- Parameters:
eventSource- The emitter of the mouse events.lassoMode-trueto enable lasso mode,falseto disable it.
-
unregister
public void unregister(MapView eventSource)
Unregister itself from the given event source and hide the selection hint layer.- Parameters:
eventSource- The emitter of the mouse events.
-
mousePressed
public void mousePressed(java.awt.event.MouseEvent e)
If the correct button, from the "drawing rectangle" mode- Specified by:
mousePressedin interfacejava.awt.event.MouseListener
-
mouseDragged
public void mouseDragged(java.awt.event.MouseEvent e)
If the correct button is hold, draw the rectangle.- Specified by:
mouseDraggedin interfacejava.awt.event.MouseMotionListener
-
moveSelection
private void moveSelection(int dx, int dy)
Moves the current selection by some pixels.- Parameters:
dx- How much to move it in x direction.dy- How much to move it in y direction.
-
mouseReleased
public void mouseReleased(java.awt.event.MouseEvent e)
Check the state of the keys and buttons and set the selection accordingly.- Specified by:
mouseReleasedin interfacejava.awt.event.MouseListener
-
endSelecting
public void endSelecting(java.awt.event.MouseEvent e)
Ends the selection of the current area. This simulates a release of mouse button 1.- Parameters:
e- A mouse event that caused this. Needed for backward compatibility.
-
addLassoPoint
private void addLassoPoint(java.awt.Point point)
-
isNoSelection
private boolean isNoSelection()
-
getSelectionRectangle
private java.awt.Rectangle getSelectionRectangle()
Calculate and return the current selection rectangle- Returns:
- A rectangle that spans from mousePos to mouseStartPos
-
propertyChange
public void propertyChange(java.beans.PropertyChangeEvent evt)
If the action goes inactive, remove the selection rectangle from screen- Specified by:
propertyChangein interfacejava.beans.PropertyChangeListener
-
selectingDone
private void selectingDone(java.awt.event.MouseEvent e)
Stores the current selection and stores the result inselectionResultto be retrieved bygetSelectedObjects(boolean)later.- Parameters:
e- The mouse event that caused the selection to be finished.
-
abortSelecting
private void abortSelecting()
-
selectionAreaChanged
private void selectionAreaChanged()
-
getSelectedObjects
public java.util.Collection<OsmPrimitive> getSelectedObjects(boolean alt)
Return a list of all objects in the active/last selection, respecting the different modifier.- Parameters:
alt- Whether the alt key was pressed, which means select all objects that are touched, instead those which are completely covered.- Returns:
- The collection of selected objects.
-
rectToPolygon
private static java.awt.Polygon rectToPolygon(java.awt.Rectangle r)
-
setLassoMode
public void setLassoMode(boolean lassoMode)
Enables or disables the lasso mode.- Parameters:
lassoMode-trueto enable lasso mode,falseto disable it.
-
mouseClicked
public void mouseClicked(java.awt.event.MouseEvent e)
- Specified by:
mouseClickedin interfacejava.awt.event.MouseListener
-
mouseEntered
public void mouseEntered(java.awt.event.MouseEvent e)
- Specified by:
mouseEnteredin interfacejava.awt.event.MouseListener
-
mouseExited
public void mouseExited(java.awt.event.MouseEvent e)
- Specified by:
mouseExitedin interfacejava.awt.event.MouseListener
-
mouseMoved
public void mouseMoved(java.awt.event.MouseEvent e)
- Specified by:
mouseMovedin interfacejava.awt.event.MouseMotionListener
-
-