private Point offset = null;
private LocalManifest content = null;
@Override
public boolean beginDrag(Component component, int x, int y) {
ImageView imageView = (ImageView)component;
image = imageView.getImage();
if (image != null) {
imageView.setImage((Image)null);
content = new LocalManifest();
content.putImage(image);
offset = new Point(x - (imageView.getWidth() - image.getWidth()) / 2,
y - (imageView.getHeight() - image.getHeight()) / 2);
}
return (image != null);
}
@Override
public void endDrag(Component component, DropAction dropAction) {
if (dropAction == null) {
ImageView imageView = (ImageView)component;
imageView.setImage(image);
}
image = null;
offset = null;
content = null;
}
@Override
public boolean isNative() {
return false;
}
@Override
public LocalManifest getContent() {
return content;
}
@Override
public Visual getRepresentation() {
return image;
}
@Override
public Point getOffset() {
return offset;
}
@Override
public int getSupportedDropActions() {
return DropAction.MOVE.getMask();
}
};
DropTarget imageDropTarget = new DropTarget() {
@Override
public DropAction dragEnter(Component component, Manifest dragContent,
int supportedDropActions, DropAction userDropAction) {
DropAction dropAction = null;
ImageView imageView = (ImageView)component;
if (imageView.getImage() == null
&& dragContent.containsImage()
&& DropAction.MOVE.isSelected(supportedDropActions)) {
dropAction = DropAction.MOVE;
component.getStyles().put("backgroundColor", IMAGE_VIEW_DROP_HIGHLIGHT_COLOR);
}
return dropAction;
}
@Override
public void dragExit(Component component) {
component.getStyles().put("backgroundColor", IMAGE_VIEW_BACKGROUND_COLOR);
}
@Override
public DropAction dragMove(Component component, Manifest dragContent,
int supportedDropActions, int x, int y, DropAction userDropAction) {
return (dragContent.containsImage() ? DropAction.MOVE : null);
}
@Override
public DropAction userDropActionChange(Component component, Manifest dragContent,
int supportedDropActions, int x, int y, DropAction userDropAction) {
return (dragContent.containsImage() ? DropAction.MOVE : null);
}
@Override
public DropAction drop(Component component, Manifest dragContent,
int supportedDropActions, int x, int y, DropAction userDropAction) {
DropAction dropAction = null;
if (dragContent.containsImage()) {
ImageView imageView = (ImageView)component;
try {
imageView.setImage(dragContent.getImage());
dropAction = DropAction.MOVE;
} catch(IOException exception) {
System.err.println(exception);
}
}
dragExit(component);
return dropAction;
}
};
ImageView imageView1 = new ImageView();
imageView1.setImage(Image.load(getClass().getResource("go-home.png")));
imageView1.getStyles().put("backgroundColor", IMAGE_VIEW_BACKGROUND_COLOR);
imageView1.setDragSource(imageDragSource);
imageView1.setDropTarget(imageDropTarget);
frame1.setContent(imageView1);
frame1.open(display);
frame2.setTitle("Frame 2");
frame2.setPreferredSize(160, 120);
frame2.setLocation(180, 0);
ImageView imageView2 = new ImageView();
imageView2.getStyles().put("backgroundColor", IMAGE_VIEW_BACKGROUND_COLOR);
imageView2.setDragSource(imageDragSource);
imageView2.setDropTarget(imageDropTarget);
frame2.setContent(imageView2);
frame2.open(display);
}