1 19 20 package org.netbeans.modules.openfile; 21 22 import java.awt.datatransfer.DataFlavor ; 23 import java.awt.datatransfer.Transferable ; 24 import java.awt.datatransfer.UnsupportedFlavorException ; 25 import java.awt.dnd.DropTargetDragEvent ; 26 import java.awt.dnd.DropTargetDropEvent ; 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.net.URI ; 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.StringTokenizer ; 34 import java.util.logging.Level ; 35 import java.util.logging.Logger ; 36 import org.openide.ErrorManager; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileUtil; 39 import org.openide.windows.ExternalDropHandler; 40 41 45 public class DefaultExternalDropHandler extends ExternalDropHandler { 46 47 public boolean canDrop(DropTargetDragEvent e) { 48 return canDrop( e.getCurrentDataFlavors() ); 49 } 50 51 public boolean canDrop(DropTargetDropEvent e) { 52 return canDrop( e.getCurrentDataFlavors() ); 53 } 54 55 boolean canDrop( DataFlavor [] flavors ) { 56 for( int i=0; null != flavors && i<flavors.length; i++ ) { 57 if( DataFlavor.javaFileListFlavor.equals( flavors[i] ) 58 || getUriListDataFlavor().equals( flavors[i] ) ) { 59 60 return true; 61 } 62 } 63 return false; 64 } 65 66 public boolean handleDrop(DropTargetDropEvent e) { 67 Transferable t = e.getTransferable(); 68 if( null == t ) 69 return false; 70 List fileList = getFileList( t ); 71 72 if( null != fileList && !fileList.isEmpty() ) { 73 for( Iterator i=fileList.iterator(); i.hasNext(); ) { 74 openFile( (File )i.next() ); 75 } 76 return true; 77 } 78 return false; 79 } 80 81 List getFileList( Transferable t ) { 82 try { 83 if( t.isDataFlavorSupported( DataFlavor.javaFileListFlavor ) ) { 84 return (List )t.getTransferData( DataFlavor.javaFileListFlavor ); 86 } else if( t.isDataFlavorSupported( getUriListDataFlavor() ) ) { 87 String uriList = (String )t.getTransferData( getUriListDataFlavor() ); 89 return textURIListToFileList( uriList ); 90 } 91 } catch( UnsupportedFlavorException ex ) { 92 ErrorManager.getDefault().notify( ErrorManager.INFORMATIONAL, ex ); 93 } catch( IOException ex ) { 94 Logger.getLogger(DefaultExternalDropHandler.class.getName()).log(Level.FINE, null, ex); 96 } 97 return null; 98 } 99 100 void openFile( File file ) { 101 FileObject fo = FileUtil.toFileObject( FileUtil.normalizeFile( file ) ); 102 OpenFile.open(fo, -1); 103 } 104 105 private static DataFlavor uriListDataFlavor; 106 107 DataFlavor getUriListDataFlavor() { 108 if( null == uriListDataFlavor ) { 109 try { 110 uriListDataFlavor = new DataFlavor ("text/uri-list;class=java.lang.String"); 111 } catch( ClassNotFoundException cnfE ) { 112 throw new AssertionError (cnfE); 114 } 115 } 116 return uriListDataFlavor; 117 } 118 119 List textURIListToFileList( String data ) { 120 List list = new ArrayList (1); 121 for( StringTokenizer st = new StringTokenizer (data, "\r\n"); 122 st.hasMoreTokens();) { 123 String s = st.nextToken(); 124 if( s.startsWith("#") ) { 125 continue; 127 } 128 try { 129 URI uri = new URI (s); 130 File file = new File (uri); 131 list.add( file ); 132 } catch( java.net.URISyntaxException e ) { 133 } catch( IllegalArgumentException e ) { 135 } 137 } 138 return list; 139 } 140 } 141 | Popular Tags |