001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.openide.explorer;
042:
043: import java.awt.datatransfer.DataFlavor;
044: import java.awt.datatransfer.Transferable;
045: import java.awt.datatransfer.UnsupportedFlavorException;
046: import java.io.IOException;
047: import java.util.ArrayList;
048: import java.util.List;
049: import java.util.logging.Level;
050: import java.util.logging.Logger;
051: import org.openide.util.datatransfer.ExTransferable;
052: import org.openide.util.datatransfer.ExTransferable.Multi;
053: import org.openide.util.datatransfer.MultiTransferObject;
054:
055: /**
056: * Utilities to handle drag and drop events to/from other applications
057: *
058: * @author S. Aubrecht
059: */
060: public class ExternalDragAndDrop {
061:
062: private ExternalDragAndDrop() {
063: }
064:
065: /**
066: * The default Transferable implementation for multi-object drag and drop operations is
067: * ExTransferable.Multi. However it uses a custom DataFlavor which prevents drag and drop
068: * of multiple files from the IDE to other applications.
069: * This method checks whether the given Multi instance contains objects that support
070: * DataFlavor.javaFileListFlavor and adds a separate Transferable instance for them.
071: *
072: * @param multi Multi transferable
073: *
074: * @return The original Multi transferable if none of the inner transferables supports
075: * javaFileListFlavor. Otherwise it returns a new ExTransferable with the original Multi
076: * transferable and an additional Transferable with javaFileListFlavor that aggregates
077: * all file objects from the Multi instance.
078: *
079: */
080: public static Transferable maybeAddExternalFileDnd(Multi multi) {
081: Transferable res = multi;
082: try {
083: MultiTransferObject mto = (MultiTransferObject) multi
084: .getTransferData(ExTransferable.multiFlavor);
085: final ArrayList fileList = new ArrayList(mto.getCount());
086: for (int i = 0; i < mto.getCount(); i++) {
087: if (mto.isDataFlavorSupported(i,
088: DataFlavor.javaFileListFlavor)) {
089: List list = (List) mto.getTransferData(i,
090: DataFlavor.javaFileListFlavor);
091: fileList.addAll(list);
092: }
093: }
094: if (!fileList.isEmpty()) {
095: ExTransferable fixed = ExTransferable.create(multi);
096: fixed.put(new ExTransferable.Single(
097: DataFlavor.javaFileListFlavor) {
098: protected Object getData() throws IOException,
099: UnsupportedFlavorException {
100: return fileList;
101: }
102: });
103: res = fixed;
104: }
105: } catch (UnsupportedFlavorException ex) {
106: Logger.getLogger(ExternalDragAndDrop.class.getName()).log(
107: Level.INFO, null, ex);
108: } catch (IOException ex) {
109: Logger.getLogger(ExternalDragAndDrop.class.getName()).log(
110: Level.INFO, null, ex);
111: }
112: return res;
113: }
114: }
|