1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| DragSource dragSource = DragSource.getDefaultDragSource();
dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY, new DragGestureListener() {
@Override
public void dragGestureRecognized(DragGestureEvent dge) {
Transferable t = new Transferable() {
DataFlavor[] tr ;
{
try{
tr = new DataFlavor[] {new DataFlavor(NODE_MIME_TYPE)};
} catch(ClassNotFoundException e){
e.printStackTrace();
}
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
log.debug("Got mimetype {}",flavor.getMimeType());
return flavor.getMimeType().split(";")[0].equals(NODE_MIME_TYPE);
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return tr;
}
@Override
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if (isDataFlavorSupported(flavor)){
return NodeSource.this;
}
throw new UnsupportedFlavorException(flavor);
}
};
BufferedImage bi = new BufferedImage(64,64, BufferedImage.TYPE_INT_RGB);
bi.getGraphics().drawImage(svgIcon.getBufferedImage(),0,0,null);
try {
File createTempFile = File.createTempFile("test", ".jpeg");
ImageIO.write(bi, "JPEG", createTempFile);
Desktop.getDesktop().browse(createTempFile.toURI());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dge.startDrag(null, bi,new java.awt.Point(0,0),t,null);
}
}); |
Partager