Index: openide/src/org/openide/explorer/view/DragDropUtilities.java =================================================================== RCS file: /cvs/openide/src/org/openide/explorer/view/DragDropUtilities.java,v retrieving revision 1.32 diff -u -r1.32 DragDropUtilities.java --- openide/src/org/openide/explorer/view/DragDropUtilities.java 4 Feb 2005 07:12:16 -0000 1.32 +++ openide/src/org/openide/explorer/view/DragDropUtilities.java 21 Apr 2005 10:02:54 -0000 @@ -174,33 +174,29 @@ Transferable[] tArray = new Transferable[nodes.length]; //System.out.println("Sel count: " + nodes.length); // NOI18N for (int i = 0; i < nodes.length; i++) { - Clipboard c = getClipboard(); - if (c instanceof ExClipboard) { - ExClipboard cb = (ExClipboard)c; - if ((dragAction & DnDConstants.ACTION_MOVE) != 0) { - tArray[i] = cb.convert (nodes[i].clipboardCut()); - //System.out.println("Clipboard CUT for node: "+nodes[0]); - } else { - tArray[i] = cb.convert (nodes[i].clipboardCopy()); - //System.out.println("Clipboard COPY for node: "+nodes[0]); - } + if ((dragAction & DnDConstants.ACTION_MOVE) != 0) { + tArray[i] = nodes[i].clipboardCut(); + //System.out.println("Clipboard CUT for node: "+nodes[0]); } else { - // In case of standalone library we cannot do - // conversion here. Is this ok? - if ((dragAction & DnDConstants.ACTION_MOVE) != 0) { - tArray[i] = nodes[i].clipboardCut(); - //System.out.println("Clipboard CUT for node: "+nodes[0]); - } else { - tArray[i] = nodes[i].clipboardCopy(); - //System.out.println("Clipboard COPY for node: "+nodes[0]); - } + tArray[i] = nodes[i].drag (); + //System.out.println("Clipboard COPY for node: "+nodes[0]); } } + + Transferable result; + if (tArray.length == 1) // only one node, so return regular single transferable - return tArray[0]; - // enclose the transferables into multi transferable - return new Multi(tArray); + result = tArray[0]; + else + // enclose the transferables into multi transferable + result = new Multi(tArray); + + Clipboard c = getClipboard(); + if (c instanceof ExClipboard) + return ((ExClipboard) c).convert(result); + else + return result; } /** Returns transferable of given node Index: openide/test/unit/src/org/openide/explorer/view/DragDropUtilitiesTest.java =================================================================== RCS file: openide/test/unit/src/org/openide/explorer/view/DragDropUtilitiesTest.java diff -N openide/test/unit/src/org/openide/explorer/view/DragDropUtilitiesTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/test/unit/src/org/openide/explorer/view/DragDropUtilitiesTest.java 21 Apr 2005 10:02:55 -0000 @@ -0,0 +1,219 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun + * Microsystems, Inc. All Rights Reserved. + */ + +package org.openide.explorer.view; + +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.StringSelection; +import junit.framework.*; + +import java.awt.*; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.Transferable; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.awt.dnd.DnDConstants; +import java.awt.dnd.DragSource; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.awt.image.ColorModel; +import java.io.IOException; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.ArrayList; +import java.util.TreeSet; +import javax.swing.JMenuItem; +import javax.swing.JPopupMenu; +import javax.swing.SwingUtilities; + +import org.openide.DialogDisplayer; +import org.openide.ErrorManager; +import org.openide.NotifyDescriptor; +import org.openide.NotifyDescriptor.Message; +import org.openide.awt.JPopupMenuPlus; +import org.openide.nodes.Node; +import org.openide.util.Lookup; +import org.openide.util.NbBundle; +import org.openide.util.UserCancelException; +import org.openide.util.Utilities; +import org.openide.util.datatransfer.ExClipboard; +import org.openide.util.datatransfer.ExTransferable; +import org.openide.util.datatransfer.MultiTransferObject; +import org.openide.util.datatransfer.PasteType; +import org.openide.util.datatransfer.ExTransferable.Multi; + +/** + * + * @author Jaroslav Tulach + */ +public class DragDropUtilitiesTest extends org.netbeans.junit.NbTestCase { + static { + System.setProperty ("org.openide.util.Lookup", "org.openide.explorer.view.DragDropUtilitiesTest$Lkp"); + } + + + private Lkp lookup; + + public DragDropUtilitiesTest (String testName) { + super (testName); + } + + protected void setUp () throws Exception { + Lookup l = Lookup.getDefault (); + assertEquals (Lkp.class, l.getClass ()); + lookup = (Lkp)l; + lookup.last = null; + } + + protected void tearDown () throws Exception { + } + + public void testGetNodeTransferableForSingleNodeCopy () throws Exception { + N node = new N (); + + Transferable t = DragDropUtilities.getNodeTransferable (node, org.openide.nodes.NodeTransfer.DND_COPY); + + assertEquals ("One call to copy", 1, node.copy); + assertEquals ("Also one call to drag which delegates to copy", 1, node.drag); + assertEquals ("No call to cut", 0, node.cut); + assertNotNull ("Call to convertor", lookup.last); + assertTrue ("StringSelection got to ExClipboard convertor", lookup.last instanceof StringSelection); + } + + public void testGetNodeTransferableForSingleNodeCut () throws Exception { + N node = new N (); + + Transferable t = DragDropUtilities.getNodeTransferable (node, org.openide.nodes.NodeTransfer.DND_MOVE); + + assertEquals ("One call to cut", 1, node.cut); + assertEquals ("No call to drag", 0, node.drag); + assertEquals ("No call to copy", 0, node.copy); + assertNotNull ("Call to convertor", lookup.last); + assertTrue ("StringSelection got to ExClipboard convertor", lookup.last instanceof StringSelection); + } + + public void testMultiTransferableForCopy () throws Exception { + N node = new N (); + N n2 = new N (); + N[] arr = { node, n2 }; + + Transferable t = DragDropUtilities.getNodeTransferable (arr, org.openide.nodes.NodeTransfer.DND_COPY); + + assertEquals ("One call to copy", 1, node.copy); + assertEquals ("One call to copy on n2", 1, n2.copy); + assertEquals ("Also one call to drag which delegates to copy", 1, node.drag); + assertEquals ("Also one call to drag which delegates to copy on n2", 1, n2.drag); + assertEquals ("No call to cut", 0, node.cut); + assertEquals ("No call to cut", 0, n2.cut); + + assertNotNull ("Call to convertor", lookup.last); + assertTrue ("multi flavor supported", lookup.last.isDataFlavorSupported (ExTransferable.multiFlavor)); + Object obj = lookup.last.getTransferData (ExTransferable.multiFlavor); + if (!( obj instanceof MultiTransferObject)) { + fail ("It should be MultiTransferObject: " + obj); + } + MultiTransferObject m = (MultiTransferObject)obj; + + assertEquals ("Two in multi", 2, m.getCount ()); + assertTrue ("Is string", m.getTransferData (0, DataFlavor.stringFlavor) instanceof String); + assertTrue ("Is string2", m.getTransferData (1, DataFlavor.stringFlavor) instanceof String); + } + + public void testMultiTransferableForCut () throws Exception { + N node = new N (); + N n2 = new N (); + N[] arr = { node, n2 }; + + Transferable t = DragDropUtilities.getNodeTransferable (arr, org.openide.nodes.NodeTransfer.DND_MOVE); + + assertEquals ("One call to cut ", 1, node.cut); + assertEquals ("One call to cut on n2", 1, n2.cut); + assertEquals ("No to drag", 0, node.drag); + assertEquals ("No to drag on n2", 0, n2.drag); + assertEquals ("No call to copy", 0, node.copy); + assertEquals ("No call to copy on n2", 0, n2.copy); + + assertNotNull ("Call to convertor", lookup.last); + assertTrue ("multi flavor supported", lookup.last.isDataFlavorSupported (ExTransferable.multiFlavor)); + Object obj = lookup.last.getTransferData (ExTransferable.multiFlavor); + if (!( obj instanceof MultiTransferObject)) { + fail ("It should be MultiTransferObject: " + obj); + } + MultiTransferObject m = (MultiTransferObject)obj; + + assertEquals ("Two in multi", 2, m.getCount ()); + assertTrue ("Is string", m.getTransferData (0, DataFlavor.stringFlavor) instanceof String); + assertTrue ("Is string2", m.getTransferData (1, DataFlavor.stringFlavor) instanceof String); + } + + + + private static class N extends org.openide.nodes.AbstractNode { + public int copy; + public int cut; + public int drag; + public Transferable ret = new StringSelection ("A text"); + + public N () { + super (org.openide.nodes.Children.LEAF); + } + + public Transferable clipboardCut () throws IOException { + cut++; + return ret; + } + + public Transferable clipboardCopy () throws IOException { + copy++; + return ret; + } + + public Transferable drag () throws IOException { + drag++; + return super.drag (); + } + } + + public static final class Lkp extends org.openide.util.lookup.AbstractLookup + implements ExClipboard.Convertor { + public Transferable last; + + public Lkp () { + this (new org.openide.util.lookup.InstanceContent ()); + } + + protected Lkp (org.openide.util.lookup.InstanceContent ic) { + super (ic); + ic.add (new MyClipboard (this)); + } + + public Transferable convert (Transferable t) { + last = t; + return t; + } + } + + private static final class MyClipboard extends ExClipboard { + private Lkp lookup; + + public MyClipboard (Lkp lookup) { + super ("Empty"); + this.lookup = lookup; + } + + public Convertor[] getConvertors () { + return new Convertor[] { lookup }; + } + } +}