throws Exception {
WTKXSerializer wtkxSerializer = new WTKXSerializer();
window = new Window((Component)wtkxSerializer.readObject(getClass().getResource("drag_and_drop.wtkx")));
// Text
final Label label = (Label)wtkxSerializer.getObjectByName("label");
label.setDragSource(new DragSource() {
private LocalManifest content = null;
public boolean beginDrag(Component component, int x, int y) {
String text = label.getText();
if (text != null) {
content = new LocalManifest();
content.putText(label.getText());
}
return (content != null);
}
public void endDrag(Component component, DropAction dropAction) {
content = null;
}
public boolean isNative() {
return true;
}
public LocalManifest getContent() {
return content;
}
public Visual getRepresentation() {
return null;
}
public Point getOffset() {
return null;
}
public int getSupportedDropActions() {
return DropAction.COPY.getMask();
}
});
label.setDropTarget(new DropTarget() {
public DropAction dragEnter(Component component, Manifest dragContent,
int supportedDropActions, DropAction userDropAction) {
DropAction dropAction = null;
if (dragContent.containsText()
&& DropAction.COPY.isSelected(supportedDropActions)) {
dropAction = DropAction.COPY;
}
return dropAction;
}
public void dragExit(Component component) {
}
public DropAction dragMove(Component component, Manifest dragContent,
int supportedDropActions, int x, int y, DropAction userDropAction) {
return (dragContent.containsText() ? DropAction.COPY : null);
}
public DropAction userDropActionChange(Component component, Manifest dragContent,
int supportedDropActions, int x, int y, DropAction userDropAction) {
return (dragContent.containsText() ? DropAction.COPY : null);
}
public DropAction drop(Component component, Manifest dragContent,
int supportedDropActions, int x, int y, DropAction userDropAction) {
DropAction dropAction = null;
if (dragContent.containsText()) {
try {
label.setText(dragContent.getText());
dropAction = DropAction.COPY;
} catch(IOException exception) {
System.err.println(exception);
}
}
dragExit(component);
return dropAction;
}
});
PushButton copyTextButton = (PushButton)wtkxSerializer.getObjectByName("copyTextButton");
copyTextButton.getButtonPressListeners().add(new ButtonPressListener() {
public void buttonPressed(Button button) {
String text = label.getText();
LocalManifest clipboardContent = new LocalManifest();
clipboardContent.putText(text);
Clipboard.setContent(clipboardContent);
}
});
PushButton pasteTextButton = (PushButton)wtkxSerializer.getObjectByName("pasteTextButton");
pasteTextButton.getButtonPressListeners().add(new ButtonPressListener() {
public void buttonPressed(Button button) {
Manifest clipboardContent = Clipboard.getContent();
if (clipboardContent != null
&& clipboardContent.containsText()) {
try {
label.setText(clipboardContent.getText());
} catch(IOException exception) {
System.err.println(exception);
}
}
}