KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > openide > explorer > ExternalDragAndDrop


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 package org.netbeans.modules.openide.explorer;
20
21 import java.awt.datatransfer.DataFlavor JavaDoc;
22 import java.awt.datatransfer.Transferable JavaDoc;
23 import java.awt.datatransfer.UnsupportedFlavorException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import org.openide.util.datatransfer.ExTransferable;
30 import org.openide.util.datatransfer.ExTransferable.Multi;
31 import org.openide.util.datatransfer.MultiTransferObject;
32
33 /**
34  * Utilities to handle drag and drop events to/from other applications
35  *
36  * @author S. Aubrecht
37  */

38 public class ExternalDragAndDrop {
39     
40     private ExternalDragAndDrop() {
41     }
42     
43     /**
44      * The default Transferable implementation for multi-object drag and drop operations is
45      * ExTransferable.Multi. However it uses a custom DataFlavor which prevents drag and drop
46      * of multiple files from the IDE to other applications.
47      * This method checks whether the given Multi instance contains objects that support
48      * DataFlavor.javaFileListFlavor and adds a separate Transferable instance for them.
49      *
50      * @param multi Multi transferable
51      *
52      * @return The original Multi transferable if none of the inner transferables supports
53      * javaFileListFlavor. Otherwise it returns a new ExTransferable with the original Multi
54      * transferable and an additional Transferable with javaFileListFlavor that aggregates
55      * all file objects from the Multi instance.
56      *
57      */

58     public static Transferable JavaDoc maybeAddExternalFileDnd( Multi multi ) {
59         Transferable JavaDoc res = multi;
60         try {
61             MultiTransferObject mto = (MultiTransferObject) multi.getTransferData(ExTransferable.multiFlavor);
62             final ArrayList JavaDoc fileList = new ArrayList JavaDoc( mto.getCount() );
63             for( int i=0; i<mto.getCount(); i++ ) {
64                 if( mto.isDataFlavorSupported( i, DataFlavor.javaFileListFlavor ) ) {
65                     List JavaDoc list = (List JavaDoc)mto.getTransferData( i, DataFlavor.javaFileListFlavor );
66                     fileList.addAll( list );
67                 }
68             }
69             if( !fileList.isEmpty() ) {
70                 ExTransferable fixed = ExTransferable.create( multi );
71                 fixed.put( new ExTransferable.Single( DataFlavor.javaFileListFlavor ) {
72                     protected Object JavaDoc getData() throws IOException JavaDoc, UnsupportedFlavorException JavaDoc {
73                         return fileList;
74                     }
75                 });
76                 res = fixed;
77             }
78         } catch (UnsupportedFlavorException JavaDoc ex) {
79             Logger.getLogger(ExternalDragAndDrop.class.getName()).log(Level.INFO, null, ex);
80         } catch (IOException JavaDoc ex) {
81             Logger.getLogger(ExternalDragAndDrop.class.getName()).log(Level.INFO, null, ex);
82         }
83         return res;
84     }
85 }
86
Popular Tags