1 11 12 package org.jivesoftware.messenger.launcher; 13 14 import javax.swing.JFrame ; 15 16 import java.awt.datatransfer.DataFlavor ; 17 import java.awt.datatransfer.Transferable ; 18 import java.awt.datatransfer.UnsupportedFlavorException ; 19 import java.awt.dnd.*; 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 29 public class DroppableFrame extends JFrame implements DropTargetListener, DragSourceListener, 30 DragGestureListener 31 { 32 33 private DragSource dragSource = DragSource.getDefaultDragSource(); 34 35 38 public DroppableFrame() { 39 new DropTarget(this, this); 40 dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this); 41 } 42 43 public void dragDropEnd(DragSourceDropEvent DragSourceDropEvent) { 44 } 45 46 public void dragEnter(DragSourceDragEvent DragSourceDragEvent) { 47 } 48 49 public void dragExit(DragSourceEvent DragSourceEvent) { 50 } 51 52 public void dragOver(DragSourceDragEvent DragSourceDragEvent) { 53 } 54 55 public void dropActionChanged(DragSourceDragEvent DragSourceDragEvent) { 56 } 57 58 public void dragEnter(DropTargetDragEvent dropTargetDragEvent) { 59 dropTargetDragEvent.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE); 60 } 61 62 public void dragExit(DropTargetEvent dropTargetEvent) { 63 } 64 65 public void dragOver(DropTargetDragEvent dropTargetDragEvent) { 66 } 67 68 public void dropActionChanged(DropTargetDragEvent dropTargetDragEvent) { 69 } 70 71 public void drop(DropTargetDropEvent dropTargetDropEvent) { 72 try { 73 Transferable transferable = dropTargetDropEvent.getTransferable(); 74 if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { 75 dropTargetDropEvent.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); 76 List fileList = (List ) transferable.getTransferData(DataFlavor.javaFileListFlavor); 77 Iterator iterator = fileList.iterator(); 78 while (iterator.hasNext()) { 79 File file = (File ) iterator.next(); 80 if (file.isFile()) { 81 fileDropped(file); 82 } 83 84 if (file.isDirectory()) { 85 directoryDropped(file); 86 } 87 } 88 dropTargetDropEvent.getDropTargetContext().dropComplete(true); 89 } 90 else { 91 dropTargetDropEvent.rejectDrop(); 92 } 93 } 94 catch (IOException io) { 95 io.printStackTrace(); 96 dropTargetDropEvent.rejectDrop(); 97 } 98 catch (UnsupportedFlavorException ufe) { 99 ufe.printStackTrace(); 100 dropTargetDropEvent.rejectDrop(); 101 } 102 } 103 104 public void dragGestureRecognized(DragGestureEvent dragGestureEvent) { 105 106 } 107 108 113 public void fileDropped(File file){ 114 115 } 116 117 122 public void directoryDropped(File file){ 123 124 } 125 } | Popular Tags |