When the active interactor of a view changes, the view fires an InteractorChangedEvent event. A class must
implement the InteractorListener
interface in order to be notified that a view interactor has been
modified and must register itself using the addInteractorListener method of
IlvManagerView
. You can also specify that the listener no longer be notified of
such events by using the removeInteractorListener method.
When the interactor of a view changes, the view calls the interactorChanged method of the listeners.
void interactorChanged(InteractorChangedEvent event)
This method is called with an instance of the class InteractorChangedEvent as a parameter
containing information on the new and the old interactor.
Example: Implementing the DragRectangleInteractor class
This example shows how the methods of the predefined view
interactor
IlvDragRectangleInteractor
are implemented. You can use this example as a starting point for
creating your own interactor functionality. The class IlvDragRectangleInteractor is used to
specify a rectangular region in a view. When this rectangle is
selected, the fireRectangleDraggedEvent method is called.
The rectangle can then be used for various purposes in derived
interactors. For example, you can create a subclass of this
interactor to zoom in on the selected area. You can see the
complete code example file DragRectangleInteractor.java
located at codefragments/interactors/dragrectinter/src/DragRectangleInteractor.java
in the installed product. For details, see <installdir> /jviews-framework810/codefragments/interactors/dragrectinter/srchtml/DragRectangleInteractor.java.html.
The
DragRectangleInteractor
class defines the following attributes:
start
,
rectangle
, and
dragging
.
public class DragRectangleInteractor extends IlvManagerViewInteractor { /** The anchor point of the rectangle. */ private final IlvPoint start = new IlvPoint(); /** The rectangle when dragging. */ private final IlvRect rectangle = new IlvRect(); /** True if dragging. */ private boolean dragging = false; ... }
The attribute
start
is the point at the start of the drag action;
rectangle
is the rectangle that is drawn when dragging;
dragging
is a Boolean variable whose value is
true
when the user drags.
The
enableEvents
method called in the constructor takes the
MOUSE_EVENT_MASK
and
MOUSE_MOTION_EVENT_MASK
as parameters. Events must be enabled to be taken into account by
the interactor:
public DragRectangleInteractor() { enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK); }
The
processMouseEvent
method handles the
MOUSE_PRESSED
and
MOUSE_RELEASED
events:
protected void processMouseEvent(MouseEvent event) { switch (event.getID()) { case MouseEvent.MOUSE_PRESSED: { if (dragging) break; if ((event.getModifiers() & InputEvent.BUTTON2_MASK) == 0 && (event.getModifiers() & InputEvent.BUTTON3_MASK) == 0) { dragging = true; IlvTransformer t = getTransformer(); start.move(event.getX(), event.getY()); t.inverse(start); rectangle.width = 0; rectangle.height = 0; } break; } case MouseEvent.MOUSE_RELEASED: if (dragging) { dragging = false; drawGhost(); rectangle.width = 0; rectangle.height = 0; fireRectangleDraggedEvent(new IlvRect(rectangle), event); } } }
When the mouse button is pressed, the mouse pointer coordinates
are stored in the
start
variable and are converted for storage in the coordinate system
of the manager. When the mouse is released, the
drawGhost
method of
IlvManagerViewInteractor
is called to erase the ghost image. The width and height of the
rectangle are set to
0
to prevent further drawings of the ghost, and the
fireRectangleDraggedEvent
method is called to notify the end of the drag operation. The
following code demonstrates the dragged rectangle.
Note
The drawGhost method can be used to perform a
temporary drawing that gives the user feedback on the action of
his present operation.
The
processMouseMotionEvents
handles the
MOUSE_DRAGGED
events:
protected void processMouseMotionEvent(MouseEvent event) { if (event.getID() == MouseEvent.MOUSE_DRAGGED && dragging) { drawGhost(); IlvTransformer t = getTransformer(); IlvPoint p = new IlvPoint(event.getX(), event.getY()); ensureVisible(p); rectangle.reshape(start.x, start.y, 0,0); t.inverse(p); rectangle.add(p.x, p.y); drawGhost(); } }
First the rectangle is erased by a call to
drawGhost
. The call to
ensureVisible
ensures that the dragged point remains visible on the screen. The
new rectangle is then computed in the coordinate system of the
manager and
drawGhost
is called to draw the new rectangle.
The
drawGhost
method simply draws the dragged rectangle. Since the rectangle is
in the manager coordinate system, the method needs to apply the
view transformer before drawing.
protected void drawGhost(Graphics g) { IlvRect rect = new IlvRect(rectangle); IlvTransformer t = getTransformer(); if (t != null) t.apply(rect); if (rect.width > 0 && rect.height >0) { g.drawRect((int)Math.floor(rect.x), (int)Math.floor(rect.y), (int)Math.floor(rect.width), (int)Math.floor(rect.height)); } }