org.openstreetmap.josm.gui
Class SelectionManager

java.lang.Object
  extended by 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. 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 rectangle 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. The point where the left mouse button was pressed and the current mouse position are two opposite corners of the selection rectangle. 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
static interface SelectionManager.SelectionEnded
          This is the interface that an user of SelectionManager has to implement to get informed when a selection closes.
 
Field Summary
private  boolean aspectRatio
          Whether the selection rectangle must obtain the aspect ratio of the drawComponent.
private  java.awt.Polygon lasso
           
private  boolean lassoMode
           
private  java.awt.Point mousePos
          Position of the map when the selection rectangle was last drawn.
private  java.awt.Point mousePosStart
          Position of the map when the mouse button was pressed.
private  NavigatableComponent nc
          The Component, the selection rectangle is drawn onto.
private  SelectionManager.SelectionEnded selectionEndedListener
          The listener that receives the events after left mouse button is released.
 
Constructor Summary
SelectionManager(SelectionManager.SelectionEnded selectionEndedListener, boolean aspectRatio, NavigatableComponent navComp)
          Create a new SelectionManager.
 
Method Summary
 java.util.Collection<OsmPrimitive> getSelectedObjects(boolean alt)
          Return a list of all objects in the selection, respecting the different modifier.
private  java.awt.Rectangle getSelectionRectangle()
          Calculate and return the current selection rectangle
 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 paintLasso()
           
private  void paintRect()
          Draw a selection rectangle on screen.
 void propertyChange(java.beans.PropertyChangeEvent evt)
          If the action goes inactive, remove the selection rectangle from screen
private  java.awt.Polygon rectToPolygon(java.awt.Rectangle r)
           
 void register(NavigatableComponent eventSource, boolean lassoMode)
          Register itself at the given event source.
 void setLassoMode(boolean lassoMode)
           
 void unregister(java.awt.Component eventSource)
          Unregister itself from the given event source.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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 not null, a rectangle is drawn on screen.


mousePos

private java.awt.Point mousePos
Position of the map when the selection rectangle was last drawn.


nc

private final NavigatableComponent nc
The Component, the selection rectangle is drawn onto.


aspectRatio

private boolean aspectRatio
Whether the selection rectangle must obtain the aspect ratio of the drawComponent.


lassoMode

private boolean lassoMode

lasso

private java.awt.Polygon lasso
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, the rectangle is drawn onto.
Method Detail

register

public void register(NavigatableComponent eventSource,
                     boolean lassoMode)
Register itself at the given event source.

Parameters:
eventSource - The emitter of the mouse events.

unregister

public void unregister(java.awt.Component eventSource)
Unregister itself from the given event source. If a selection rectangle is shown, hide it first.

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:
mousePressed in interface java.awt.event.MouseListener

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
If the correct button is hold, draw the rectangle.

Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Check the state of the keys and buttons and set the selection accordingly.

Specified by:
mouseReleased in interface java.awt.event.MouseListener

paintRect

private void paintRect()
Draw a selection rectangle on screen. If already a rectangle is drawn, it is removed instead.


paintLasso

private void paintLasso()

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:
propertyChange in interface java.beans.PropertyChangeListener

getSelectedObjects

public java.util.Collection<OsmPrimitive> getSelectedObjects(boolean alt)
Return a list of all objects in the 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.

rectToPolygon

private java.awt.Polygon rectToPolygon(java.awt.Rectangle r)

setLassoMode

public void setLassoMode(boolean lassoMode)

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Specified by:
mouseClicked in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Specified by:
mouseExited in interface java.awt.event.MouseListener

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener


JOSM