1 package com.opensymphony.workflow.designer.swing; 2 3 import java.awt.*; 4 import java.awt.datatransfer.DataFlavor ; 5 import java.awt.datatransfer.Transferable ; 6 import java.awt.datatransfer.UnsupportedFlavorException ; 7 import java.awt.dnd.*; 8 import java.awt.event.HierarchyEvent ; 9 import java.awt.event.HierarchyListener ; 10 import java.io.File ; 11 import java.io.IOException ; 12 import java.util.List ; 13 import java.util.TooManyListenersException ; 14 import javax.swing.*; 15 16 public abstract class FileDropHandler 17 { 18 private transient java.awt.dnd.DropTargetListener dropListener; 19 20 public FileDropHandler(Component target) 21 { 22 this(target, true); 23 } 24 25 public FileDropHandler(final Component target, final boolean recursive) 26 { 27 dropListener = new DropTargetListener() 28 { 29 public void dragEnter(DropTargetDragEvent evt) 30 { 31 if(isDragOk(evt)) 32 { 33 evt.acceptDrag(DnDConstants.ACTION_COPY); 34 } 35 else 36 { 37 evt.rejectDrag(); 38 } 39 } 40 41 public void dragOver(DropTargetDragEvent evt) 42 {} 43 44 public void drop(DropTargetDropEvent evt) 45 { 46 try 47 { 48 Transferable tr = evt.getTransferable(); 49 50 if(tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) 51 { 52 evt.acceptDrop(DnDConstants.ACTION_COPY); 53 54 List fileList = (List )tr.getTransferData(DataFlavor.javaFileListFlavor); 55 56 File [] filesTemp = new File [fileList.size()]; 57 fileList.toArray(filesTemp); 58 final File [] files = filesTemp; 59 60 filesDropped(files); 61 62 evt.getDropTargetContext().dropComplete(true); 63 } 64 else 65 { 66 evt.rejectDrop(); 67 } 68 } 69 catch(IOException io) 70 { 71 io.printStackTrace(); 72 evt.rejectDrop(); 73 } 74 catch(UnsupportedFlavorException ufe) 75 { 76 ufe.printStackTrace(); 77 evt.rejectDrop(); 78 } 79 } 80 81 public void dragExit(DropTargetEvent evt) 82 { 83 } 84 85 public void dropActionChanged(DropTargetDragEvent evt) 86 { 87 if(isDragOk(evt)) 88 { 89 evt.acceptDrag(DnDConstants.ACTION_COPY); 90 } 91 else 92 { 93 evt.rejectDrag(); 94 } 95 } 96 }; 97 98 makeDropTarget(target, recursive); 99 } 100 101 private void makeDropTarget(final Component target, boolean recursive) 102 { 103 final DropTarget dt = new DropTarget(); 104 try 105 { 106 dt.addDropTargetListener(dropListener); 107 } 108 catch(TooManyListenersException e) 109 { 110 e.printStackTrace(); 111 } 112 113 target.addHierarchyListener(new HierarchyListener () 114 { 115 public void hierarchyChanged(HierarchyEvent evt) 116 { 117 Component parent = target.getParent(); 118 if(parent == null) 119 { 120 target.setDropTarget(null); 121 } 122 else 123 { 124 new DropTarget(target, dropListener); 125 } 126 } 127 }); 128 if(target.getParent() != null) 129 new DropTarget(target, dropListener); 130 131 if(recursive && (target instanceof Container)) 132 { 133 Container cont = (Container)target; 134 Component[] comps = cont.getComponents(); 135 136 for(int i = 0; i < comps.length; i++) 137 makeDropTarget(comps[i], recursive); 138 } 139 } 140 141 private boolean isDragOk(final DropTargetDragEvent evt) 142 { 143 boolean ok = false; 144 145 DataFlavor [] flavors = evt.getCurrentDataFlavors(); 146 int i = 0; 147 while(!ok && i < flavors.length) 148 { 149 if(flavors[i].equals(DataFlavor.javaFileListFlavor)) 150 ok = true; 151 i++; 152 } 153 return ok; 154 } 155 156 public abstract void filesDropped(File [] files); 157 158 public static void main(String [] args) 159 { 160 JFrame frame = new JFrame("FileDrop"); 161 final JTree tree = new JTree(); 162 frame.getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER); 163 164 new FileDropHandler(tree) 165 { 166 public void filesDropped(File [] files) 167 { 168 for(int i = 0; i < files.length; i++) 169 { 170 System.out.println("dropped " + files[i]); 171 } 172 } 173 }; 174 175 frame.setBounds(100, 100, 300, 400); 176 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 177 frame.show(); 178 } 179 } | Popular Tags |