|
A
DropTargetEvent
contains information about a drag and drop operation that might
transfer data into the component that contains the event handler
that receives the
DropTargetEvent.
The five event handlers that receive
DropTargetEvents
are listed below.
drop
deals exclusively with
DropTargetEvents.
The other four can receive
DropTargetEvents
and
DragSourceEvents
and it is the type assigned to the argument in their declaration,
which should be
DropTargetEvent,
DragSourceEvent,
or
Object,
that determines which events are forwarded to your event handler.
DropTargetEvent
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
DropTargetEvent
also can not be assigned to an
Event
object or posted to a component using
postEvent.
There are no special fields in a
DropTargetEvent
that event handlers change when they want to affect a drag and drop operation,
however most return
TRUE
or
FALSE
to indicate the success or failure of a drop or their ability to handle
the data that is being transferred by the drag and drop operation.
The fields in a
DropTargetEvent
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.
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.
| | dragowner |
An
int
that is
1
if the component that receives this event started the drag and
0
if it did not.
| | 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.
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.
| | mimetypes |
An
Array
that will be
NULL
or a list of
Strings
that are the MIME types that can be supplied when the user decides
to end the drag and drop operation with a drop.
An event handler like
dragEnter
can use
mimetypes
to decide if it should accept the data that is being transferred by the
drag and drop operation.
Using
transferable
to make the decision is much easier, but only works on Java 1.5.0 and later.
| | screenlocation |
A
Point
that indicates where on the screen the cursor's hotspot was
at the time of the event.
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.
| | transferable |
An
Object
that is the data that the drag and drop operation would like to transfer
into the component or
NULL
if the value is not available.
Right now
drop
is the only event handler that can count on finding a non-null value in
transferable,
however
dragEnter,
dragOver,
and
dropActionChanged
can use
transferable
to make decisions if they are running under Java 1.5.0 or later.
|
| |
| Event Handlers: |
dragEnter,
dragExit,
dragOver,
drop,
dropActionChanged
| | |
| Example: |
The program,
import yoix.*.*;
JFrame colorframe = {
Dimension size = NULL;
double border = 72/8;
int initialized = FALSE;
Array layout = {
new JColorChooser {
Color color = Color.red;
int dragenabled = TRUE;
},
};
};
JFrame textframe = {
double border = 72/4;
Dimension size = {
double width = 5*72;
double height = 3*72;
};
Point location = {
double x = VM.screen.width - size.width - 72/2;
double y = (VM.screen.height - size.height)/2;
};
Array layout = {
new JTextArea {
String text = "Now is the time\n\tfor all good men...";
Object transferhandler = NULL;
int edit = TRUE;
int scroll = NONE;
dragGestureRecognized(DragGestureEvent e) {
Array ends = selectedends;
int index = viewToModel(e.location);
//
// We only start the drag (by returning a non
// NULL value) if the event happen inside the
// the current selection.
//
if ((ends = selectedends) != NULL) {
if (index >= ends[0] && index < ends[1])
e.visual = selected;
}
return(e.visual);
}
dragEnter(DropTargetEvent e) {
//
// Return value means we won't accept anything
// if we started the drag.
//
if (e.transferable instanceof Color)
caretmodel = 1;
else caretmodel = 0;
return(!e.dragowner);
}
drop(DropTargetEvent e) {
int succeeded = TRUE;
if (e.transferable instanceof String)
insertText(this, e.transferable, viewToModel(e.location));
else if (e.transferable instanceof Color)
foreground = e.transferable;
else succeeded = FALSE;
return(succeeded);
}
},
};
};
textframe.visible = TRUE;
colorframe.visible = TRUE;
lets you drag colors from a JColorChooser using Java's
automatic drag handling and uses custom drag and drop
event handlers in a JTextArea to handle text and colors.
As usual we were forced to set the
field in the
JTextArea
to
NULL
before the drag and drop event handlers start working.
| | |
| See Also: |
ActionEvent,
AdjustmentEvent,
CaretEvent,
ChangeEvent,
ComponentEvent,
DragGestureEvent,
DragSourceEvent,
Event,
FocusEvent,
HyperlinkEvent,
InvocationEvent,
invokeLater,
isDispatchThread,
ItemEvent,
KeyEvent,
ListSelectionEvent,
MouseEvent,
MouseWheelEvent,
PaintEvent,
postEvent,
TextEvent,
TreeSelectionEvent,
WindowEvent
|
|