ActiveEditorDrop with artificial DataFlavor. Drag and drop initiator sometimes needs
to be notified about a target component, where the drop operation was performed.
Initiator should implement this interface and use the required artificial DataFlavor.
Component that will support drop operation of the ActiveEditorDrop should call handleTransfer
method.
Sample usage of the client:
private class MyDrop extends StringSelection implements ActiveEditorDrop {
public MyDrop(String text){
super(text); //NOI18N
}
public boolean isDataFlavorSupported(DataFlavor f) {
return super.isDataFlavorSupported(f) || ActiveEditorDrop.FLAVOR == f;
}
public final DataFlavor[] getTransferDataFlavors() {
DataFlavor delegatorFlavor[] = super.getTransferDataFlavors();
int delegatorFlavorLength = delegatorFlavor.length;
DataFlavor newArray[] = new DataFlavor[delegatorFlavorLength + 1];
System.arraycopy(delegatorFlavor, 0, newArray, 0, delegatorFlavorLength);
newArray[delegatorFlavorLength] = ActiveEditorDrop.FLAVOR;
return newArray;
}
public final Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (flavor == ActiveEditorDrop.FLAVOR) {
return this;
}
return super.getTransferData(flavor);
}
public boolean handleTransfer(JTextComponent targetComponent) {
// your implementation
}
}
or simplified solution:
private class MyDrop implements ActiveEditorDrop, Transferable {
public MyDrop(){
}
public boolean isDataFlavorSupported(DataFlavor f) {
return ActiveEditorDrop.FLAVOR == f;
}
public final DataFlavor[] getTransferDataFlavors() {
DataFlavor delegatorFlavor[] = new DataFlavor[1];
delegatorFlavor[0] = ActiveEditorDrop.FLAVOR;
return delegatorFlavor;
}
public final Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
return (flavor == ActiveEditorDrop.FLAVOR) ? this : null;
}
public boolean handleTransfer(JTextComponent targetComponent) {
//your implementation
}
}