KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > openfile > DefaultExternalDropHandler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.openfile;
21
22 import java.awt.datatransfer.DataFlavor JavaDoc;
23 import java.awt.datatransfer.Transferable JavaDoc;
24 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
25 import java.awt.dnd.DropTargetDragEvent JavaDoc;
26 import java.awt.dnd.DropTargetDropEvent JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.net.URI JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.Logger JavaDoc;
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 /**
42  *
43  * @author S. Aubrecht
44  */

45 public class DefaultExternalDropHandler extends ExternalDropHandler {
46     
47     public boolean canDrop(DropTargetDragEvent JavaDoc e) {
48         return canDrop( e.getCurrentDataFlavors() );
49     }
50
51     public boolean canDrop(DropTargetDropEvent JavaDoc e) {
52         return canDrop( e.getCurrentDataFlavors() );
53     }
54
55     boolean canDrop( DataFlavor JavaDoc[] 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 JavaDoc e) {
67         Transferable JavaDoc t = e.getTransferable();
68         if( null == t )
69             return false;
70         List JavaDoc fileList = getFileList( t );
71
72         if( null != fileList && !fileList.isEmpty() ) {
73             for( Iterator JavaDoc i=fileList.iterator(); i.hasNext(); ) {
74                 openFile( (File JavaDoc)i.next() );
75             }
76             return true;
77         }
78         return false;
79     }
80
81     List JavaDoc getFileList( Transferable JavaDoc t ) {
82         try {
83             if( t.isDataFlavorSupported( DataFlavor.javaFileListFlavor ) ) {
84                 //windows & mac
85
return (List JavaDoc)t.getTransferData( DataFlavor.javaFileListFlavor );
86             } else if( t.isDataFlavorSupported( getUriListDataFlavor() ) ) {
87                 //linux
88
String JavaDoc uriList = (String JavaDoc)t.getTransferData( getUriListDataFlavor() );
89                 return textURIListToFileList( uriList );
90             }
91         } catch( UnsupportedFlavorException JavaDoc ex ) {
92             ErrorManager.getDefault().notify( ErrorManager.INFORMATIONAL, ex );
93         } catch( IOException JavaDoc ex ) {
94             // Ignore. Can be just "Owner timed out" from sun.awt.X11.XSelection.getData.
95
Logger.getLogger(DefaultExternalDropHandler.class.getName()).log(Level.FINE, null, ex);
96         }
97         return null;
98     }
99
100     void openFile( File JavaDoc file ) {
101         FileObject fo = FileUtil.toFileObject( FileUtil.normalizeFile( file ) );
102         OpenFile.open(fo, -1);
103     }
104
105     private static DataFlavor JavaDoc uriListDataFlavor;
106
107     DataFlavor JavaDoc getUriListDataFlavor() {
108         if( null == uriListDataFlavor ) {
109             try {
110                 uriListDataFlavor = new DataFlavor JavaDoc("text/uri-list;class=java.lang.String");
111             } catch( ClassNotFoundException JavaDoc cnfE ) {
112                 //cannot happen
113
throw new AssertionError JavaDoc(cnfE);
114             }
115         }
116         return uriListDataFlavor;
117     }
118
119     List JavaDoc textURIListToFileList( String JavaDoc data ) {
120         List JavaDoc list = new ArrayList JavaDoc(1);
121         for( StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(data, "\r\n");
122             st.hasMoreTokens();) {
123             String JavaDoc s = st.nextToken();
124             if( s.startsWith("#") ) {
125                 // the line is a comment (as per the RFC 2483)
126
continue;
127             }
128             try {
129                 URI JavaDoc uri = new URI JavaDoc(s);
130                 File JavaDoc file = new File JavaDoc(uri);
131                 list.add( file );
132             } catch( java.net.URISyntaxException JavaDoc e ) {
133                 // malformed URI
134
} catch( IllegalArgumentException JavaDoc e ) {
135                 // the URI is not a valid 'file:' URI
136
}
137         }
138         return list;
139     }
140 }
141
Popular Tags