org.geotoolkit.gui.swing
Class MouseSelectionTracker

Object
  extended by MouseAdapter
      extended by MouseInputAdapter
          extended by MouseSelectionTracker
All Implemented Interfaces:
MouseListener, MouseMotionListener, MouseWheelListener, EventListener, MouseInputListener

public abstract class MouseSelectionTracker
extends MouseInputAdapter

Controller which allows the user to select a region of a component. The user must click on a point in the component, then drag the mouse pointer whilst keeping the button pressed. During the dragging, the shape which is drawn is usually a rectangle. But other shapes can be used such as, for example, an ellipse. To use this class, it is necessary to create a derived class which defines the following methods:

This controller should then be registered with one, and only one, component using the following syntax:

Component component = ...
MouseSelectionTracker control = ...
component.addMouseListener(control);

Since:
2.0
Version:
3.00
Author:
Martin Desruisseaux (IRD)
Module:
display/geotk-widgets-swing (download)    View source code for this class

Constructor Summary
MouseSelectionTracker()
          Constructs an object which will allow rectangular regions to be selected using the mouse.
 
Method Summary
protected  Shape getModel(MouseEvent event)
          Returns the geometric shape to use for marking the boundaries of a region.
 Shape getSelectedArea(AffineTransform transform)
          Returns the geometric shape surrounding the last region to be selected by the user.
 void mouseDragged(MouseEvent event)
          Informs this controller that the mouse has been dragged.
 void mouseMoved(MouseEvent event)
          Informs this controller that the mouse has been moved but not as a result of the user selecting a region.
 void mousePressed(MouseEvent event)
          Informs this controller that the mouse button has been pressed.
 void mouseReleased(MouseEvent event)
          Informs this controller that the mouse button has been released.
protected abstract  void selectionPerformed(int ox, int oy, int px, int py)
          Method which is automatically invoked after the user selects a region with the mouse.
 void setXORColors(Color a, Color b)
          Specifies the colors to be used for drawing the outline of a box when the user selects a region.
 
Methods inherited from class MouseAdapter
mouseClicked, mouseEntered, mouseExited, mouseWheelMoved
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface MouseListener
mouseClicked, mouseEntered, mouseExited
 

Constructor Detail

MouseSelectionTracker

public MouseSelectionTracker()
Constructs an object which will allow rectangular regions to be selected using the mouse.

Method Detail

setXORColors

public void setXORColors(Color a,
                         Color b)
Specifies the colors to be used for drawing the outline of a box when the user selects a region. All a colors will be replaced by b colors and vice versa.

Parameters:
a - The color to be replaced by color b.
b - The color replacing the color a.

getModel

protected Shape getModel(MouseEvent event)
Returns the geometric shape to use for marking the boundaries of a region. This shape is usually a rectangle but could also be an ellipse, an arrow or other rectangular shapes. The coordinates of the returned shape will not be taken into account. In fact, these coordinates will regularly be discarded. Only the class of the returned shape matter (for example, Ellipse2D vs Rectangle2D) and their parameters which are not related to their position (for example, the arc size of a rectangle with rounded corners).

The shape returned will usually be an instance of a class derived from RectangularShape, but could also be an instance of the Line2D class. Any other class risks throwing a ClassCastException when executed.

The default implementation always returns an instance of Rectangle.

Parameters:
event - Mouse coordinate when the button is pressed. This information can be used by subclasses overriding this method if the mouse location is relevant to the choice of a geometric shape.
Returns:
Shape from the class RectangularShape or Line2D, or null to indicate that we do not want to make a selection.

selectionPerformed

protected abstract void selectionPerformed(int ox,
                                           int oy,
                                           int px,
                                           int py)
Method which is automatically invoked after the user selects a region with the mouse. All coordinates passed in as parameters are expressed in pixels.

Parameters:
ox - x coordinate of the mouse when the user pressed the mouse button.
oy - y coordinate of the mouse when the user pressed the mouse button.
px - x coordinate of the mouse when the user released the mouse button.
py - y coordinate of the mouse when the user released the mouse button.

getSelectedArea

public Shape getSelectedArea(AffineTransform transform)
                      throws NoninvertibleTransformException
Returns the geometric shape surrounding the last region to be selected by the user. An optional affine transform can be specified to convert the region selected by the user into logical coordinates. The class of the shape returned depends on the model returned by getModel(java.awt.event.MouseEvent):

Parameters:
transform - Affine transform which converts logical coordinates into pixel coordinates. This is usually the same transform than the one used for drawing in a Graphics2D object.
Returns:
A geometric shape enclosing the last region to be selected by the user, or null if no selection has yet been made.
Throws:
NoninvertibleTransformException - If the affine transform can't be inverted.

mousePressed

public void mousePressed(MouseEvent event)
                  throws ClassCastException
Informs this controller that the mouse button has been pressed. The default implementation memorizes the mouse coordinate (which will become one of the corners of the future rectangle to be drawn) and prepares this MouseSelectionTracker to observe the mouse movements.

Specified by:
mousePressed in interface MouseListener
Overrides:
mousePressed in class MouseAdapter
Parameters:
event - Contains mouse coordinates where the button has been pressed.
Throws:
ClassCastException - if getModel(java.awt.event.MouseEvent) doesn't return a shape from the class RectangularShape or Line2D.

mouseDragged

public void mouseDragged(MouseEvent event)
Informs this controller that the mouse has been dragged. The default implementation moves a corner of the rectangle used to select the region. The other corner remains fixed at the point where the mouse was at the moment it was pressed.

Specified by:
mouseDragged in interface MouseMotionListener
Overrides:
mouseDragged in class MouseAdapter
Parameters:
event - Contains mouse coordinates when the cursor is being dragged.

mouseReleased

public void mouseReleased(MouseEvent event)
Informs this controller that the mouse button has been released. The default implementation invokes selectionPerformed(int, int, int, int) with the bounds of the selected region as parameters.

Specified by:
mouseReleased in interface MouseListener
Overrides:
mouseReleased in class MouseAdapter
Parameters:
event - Contains mouse coordinates where the button has been released.

mouseMoved

public void mouseMoved(MouseEvent event)
Informs this controller that the mouse has been moved but not as a result of the user selecting a region. The default implementation signals to the source component that this MouseSelectionTracker is no longer interested in being informed about mouse movements.
Note: Normally not necessary, but it seems that this listener sometimes stays registered when it shouldn't.

Specified by:
mouseMoved in interface MouseMotionListener
Overrides:
mouseMoved in class MouseAdapter
Parameters:
event - Contains mouse coordinates when the cursor is being moved.


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.