|
A
DragSourceEvent
contains information about a drag and drop operation started by the
dragGestureRecognized
event handler defined in the same component as the event handler
that receives the
DragSourceEvent.
The six event handlers that receive
DragSourceEvents
are listed below.
Two of them, namely
dragDropEnd
and
dragMouseMoved,
deal exclusively with
DragSourceEvents.
The other four can receive
DragSourceEvents
and
DropTargetEvents
and it is the type assigned to the argument in their declaration,
which should be
DragSourceEvent,
DropTargetEvent,
or
Object,
that determines which events are forwarded to your event handler.
DragSourceEvent
is one of three special drag and drop events that currently do not
behave like the AWT and Swing events that you may be familiar with.
For example, Swing provides automatic drag handling for some components
that must be disabled by storing
NULL
in the component's
transferhandler
field before the drag and drop event handlers will start working.
A
DragSourceEvent
also can not be assigned to an
Event
object or posted to a component using
postEvent.
In addition, any event handler that gets a
DragSourceEvent
can change the
cursor
field in its event argument and expect to see the new cursor used by
the drag and drop operation.
The fields in a
DragSourceEvent
are:
| action |
An
int
that describes what is currently supposed to happen in this drag operation,
which can vary depending on the modifier keys that the user may or may not
be holding down.
The value will be one of
COPY,
MOVE,
LINK,
or
NONE,
which are all defined in
yoix.awt
and
yoix.swing.
| | coordinates |
A
Point
that indicates where the cursor's hotspot was at the time of the event or
NULL
if the location is not available,
which always happens with versions of Java that are older than 1.4.0.
The
coordinates
and
location
fields may or may not match, but
coordinates
should only be used by event handlers that are defined in drawable objects
(i.e., components that define a
graphics
field).
In that case
coordinates
will be the location of the cursor's hotspot in the coordinate system
described by the drawable object's
graphics.CTM
at the time the event arrived, otherwise
coordinates
and
location
will match.
| | cursor |
An
Object
that can be an
int,
Image,
or
String
that represents the cursor shown during the drag and drop operation.
A
NULL
value is special and means Java's drag and drop machinery picks the cursor.
A
cursor
that is an
int
should be one of the cursors defined in the
yoix.awt.Cursor
dictionary.
A
cursor
that is an
Image
can describe the cursor using its
size
and
hotspot
fields and often draws it using its
paint
function.
A
cursor
that is a
String
should be the name of a cursor that is already defined in
yoix.awt.Cursor
or the name a local a file or URL that contains a GIF or JPEG image
that will be used as the cursor.
The
cursor
field in the
DragSourceEvent
that is received by an event handler describes the current cursor,
but the event handler can change it and the drag and drop operation
will start using the new cursor when the event handler finishes its work.
| | id |
An
Object
that must be an
int
or
String,
that identifies the type of this event.
A value that is a
String
must be the name of an event handler that can process this event.
A value that is an
int
must be a number that the
yoix.event.HandlerID
dictionary associates with an event handler that can process this event.
| | location |
A
Point
that indicates where the cursor's hotspot was at the time of the event or
NULL
if the location is not available,
which always happens with versions of Java that are older than 1.4.0.
A non-null
location
always describes a point in the component that
requested the event in a coordinate system that has its origin
at that component's upper left corner, positive x to the right,
positive y down, and a resolution of 72 dots per inch.
| | screenlocation |
A
Point
that indicates where on the screen the cursor's hotspot was
at the time of the event or
NULL
if the location is not available,
which always happens with versions of Java that are older than 1.4.0.
A non-null
screenlocation
always describes a point on the screen in a coordinate system
that has its origin at upper left corner of the screen,
positive x to the right, positive y down,
and a resolution of 72 dots per inch.
| | succeeded |
An
int
that is
1
if the drag and drop operation ended in a successful drop
0
if it failed or has not ended yet.
dragDropEnd,
which is called at the end of a drag operation,
is the only event handler that should be interested in this field.
|
| |
| Event Handlers: |
dragDropEnd,
dragEnter,
dragExit,
dragMouseMoved,
dragOver,
dropActionChanged
| | |
| Example: |
Our only example does not transfer anything, but instead illustrates
cursor animation using
dragOver
and a small collection of custom cursors.
It looks harder than it really is, because the first half of the program
builds and installs the custom cursors that the two frames use for their
drag and drop cursor animation.
Run the program
import yoix.*.*;
PaintScrew(double angle) {
Image image = {
int type = TYPE_RGBA;
Dimension size = {
double width = 72/3;
double height = 72/3;
};
Graphics graphics = {
int rendering = 1; // probably no help
int antialiasing = 1;
};
double angle = angle;
paint(Rectangle r) {
Rectangle bbox;
double thickness;
double radius;
double alpha;
double cx;
double cy;
graphics {
gsave();
initclip();
clippath();
bbox = pathbbox();
newpath();
cx = bbox.width/2;
cy = bbox.height/2;
thickness = cx/4.0;
radius = cx - thickness - 1;
alpha = 180*atan2(thickness/2, radius)/PI;
translate(cx, cy);
rotate(angle);
moveto(0, thickness/2);
arc(0, 0, radius, alpha, 180 - alpha);
closepath();
moveto(0, -thickness/2);
arc(0, 0, radius, 180 + alpha, 360 - alpha);
closepath();
moveto(radius + thickness, 0);
arc(0, 0, radius + thickness, 0, 360);
closepath();
gsave();
setrgbcolor(0, 0, 1);
eofill();
grestore();
setlinewidth(1);
setrgbcolor(1, 1, 0);
stroke();
grestore();
hotspot = new Point {
double x = cx;
double y = cy;
};
}
}
};
return(image);
}
Array names[30];
for (n = 0; n < names@length; n++) {
names[n] = "SCREW_" + toString(n);
addCursor(names[n], PaintScrew(n*180/names@length));
}
JFrame frame1 = {
String title = "Frame 1";
Object transferhandler = NULL; // just in case
Color background = Color.lightGray;
int next = 1;
int incr = 1;
Dimension size = {
double width = VM.screen.width/3;
double height = VM.screen.height/3;
};
Point location = {
double x = 72/2;
double y = (VM.screen.height - size.height)/2;
};
dragGestureRecognized(DragGestureEvent e) {
//
// We have to return something that's not NULL, even though
// this example doesn't transfer anything, otherwise dragging
// won't start.
//
e.cursor = names[next++ % names@length];
return(""); // bogus return
}
dragEnter(DropTargetEvent e) {
return(TRUE);
}
dragOver(DragSourceEvent e) {
e.cursor = names[next];
if ((next += incr) < 0)
next = names@length - 1;
else if (next >= names@length)
next = 0;
}
};
JFrame frame2 = {
String title = "Frame 2";
Object transferhandler = NULL; // just in case
Color background = Color.darkGray;
int next = 1;
int incr = -1;
Dimension size = {
double width = VM.screen.width/3;
double height = VM.screen.height/3;
};
Point location = {
double x = VM.screen.width - size.width - 72/2;
double y = (VM.screen.height - size.height)/2;
};
dragGestureRecognized(DragGestureEvent e) {
//
// We have to return something that's not NULL, even though
// this example doesn't transfer anything, otherwise dragging
// won't start.
//
e.cursor = names[next++ % names@length];
return(""); // bogus return
}
dragOver(Object e) {
//
// The argument will be a DropTargetEvent or DragSourceEvent.
// We accept by always returning TRUE (the return value is
// ignored when e is a DragSourceEvent) and we change the
// when e is a DragSourceEvent.
//
if (e instanceof DragSourceEvent) {
e.cursor = names[next];
if ((next += incr) < 0)
next = names@length - 1;
else if (next >= names@length)
next = 0;
}
return(TRUE);
}
};
frame1.visible = TRUE;
frame2.visible = TRUE;
and start dragging in either frame and you should see cursors that
rotate as they move, and the direction of the rotation depends on
the frame that started the drag.
See what happens if you let
dragMouseMoved
handle the animation.
| | |
| See Also: |
ActionEvent,
AdjustmentEvent,
CaretEvent,
ChangeEvent,
ComponentEvent,
DragGestureEvent,
DropTargetEvent,
Event,
FocusEvent,
HyperlinkEvent,
InvocationEvent,
invokeLater,
isDispatchThread,
ItemEvent,
KeyEvent,
ListSelectionEvent,
MouseEvent,
MouseWheelEvent,
PaintEvent,
postEvent,
TextEvent,
TreeSelectionEvent,
WindowEvent
|
|