Results 1 to 5 of 5
  1. #1
    tomba is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Unhappy drag and drop - disable intra component drop

    Hi guys,
    I have two JFileChoosers to which I've attached custom TransferHandlers. I'd like to allow the user to drag from one file chooser to the other, but not from one chooser to itself. I can't find anywhere in the api to find the source of a drag, so I'm not sure that this can be done.

    I can disallow drops completely, or allow drops in either, but not only from one to the other.

    Any suggestions would be greatly appreciated.

    Thank you.

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    Suppose both fileChoosers are viewing the same directory; do you want to be able to drag from one to the other??
    If so, why prohibit dropping from a fileChooser onto itself?
    If not, the code could check whether the origin and destination directories differ.

  3. #3
    tomba is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    The two file choosers will be looking at different file systems. One of them the local drive, and one of them a remote drive, like an ftp client. So there isn't a chance of them looking at the same directory.

    Ideally I'd like to know in the canImport method if the drop is allowed so the cursor can accurately represent the allowed actions.
    Last edited by tomba; 12-08-2009 at 10:13 PM.

  4. #4
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    (Caveat: I've never written a drag-n-drop app.)

    Can you create two new DataFlavor objects and associate one with data from the first file choosers and the other with the second?

    Here's a JFileChooser subclass with an addDataFlavor method:

    Java Code:
    package Test;
    
    import java.io.*;
    import javax.swing.*;
    import java.awt.datatransfer.*;
    import javax.swing.filechooser.FileSystemView;
    
    /**
     * A JFileChooser to which additional DataFlavor values can be added.
     * Such values might be used in identifying
     * which JFileChooser is the source of a Drag-n-Drop.
     *
     * @author zweibieren
     *
     * a JComponent implements getTransferHandler
     * a TransferHandler implements createTransferable
     * a Transferable implements getDataFlavors
     * so to add a data flavor we have to subclass TransferHandler and Transferable
     */
    public class FlavorfulFileChooser extends JFileChooser {
    	DataFlavor [] flavors;
    
    	public FlavorfulFileChooser(File currentDirectory, FileSystemView fsv) {
            super(currentDirectory, fsv);
    		FFTransferHandler newTH = new FFTransferHandler();
    		//  get initial flavors list
    		Transferable baseT = newTH.createTransferable(this);
    		flavors = baseT.getTransferDataFlavors();
    		if (flavors == null)
    			flavors = new DataFlavor[0];
    		setTransferHandler(newTH);
    	}
    
    	public void addDataFlavor (DataFlavor newFlav) {
    		int n = flavors.length;
    		DataFlavor [] old = flavors;
    		flavors = new DataFlavor[n + 1];
    		System.arraycopy(old, 0, flavors, 0, n);
    		flavors[n] = newFlav;
    	}
    
    	class FFTransferHandler extends TransferHandler {
    		public FFTransferHandler () {
    			super();
    		}
    		@Override
    		public Transferable createTransferable(JComponent c) {
    			return new FFTransferable(super.createTransferable(c));
    		}
    		@Override
    		public int getSourceActions(JComponent c) { return COPY; }
    
    	}
    	class FFTransferable implements Transferable {
    		Transferable baseT;
    		FFTransferable(Transferable base) {
    			baseT = base;
    		}
    		public DataFlavor[] getTransferDataFlavors() {
    			return flavors;
    		}
    		public boolean isDataFlavorSupported(DataFlavor flavor) {
    			// unwarranted assumption: uses getTransferDataFlavors
    			// return baseT.isDataFlavorSupported(flavor);
    			for (DataFlavor df : flavors)
    				if (df.equals(flavor)) return true;
    			return false;
    		}
    		public Object getTransferData(DataFlavor flavor)
                           throws UnsupportedFlavorException,
                                  IOException {
    			return baseT.getTransferData(flavor);
    		}
    	}
    
    	void printDebugInfo(String msg) {
    		System.out.println();
    		System.out.println(msg);
    		FFTransferHandler th = (FFTransferHandler) getTransferHandler();
    		System.out.println("TransferHandler is class " + th.getClass().getCanonicalName());
    		Transferable t = th.createTransferable(this);
    		System.out.println("Transferable is class " + t.getClass().getCanonicalName());
    		DataFlavor [] flavs = t.getTransferDataFlavors();
    		System.out.print("   flavors:");
    		for (DataFlavor df : flavs)
    			System.out.print("  " + df.getHumanPresentableName());
    		System.out.println();
    	}
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
    		FlavorfulFileChooser fffc 
    				= new FlavorfulFileChooser(new File("."), 
    				FileSystemView.getFileSystemView());
    		fffc.printDebugInfo("initial");
    		fffc.addDataFlavor(new DataFlavor(File.class, "tag"));
    		fffc.printDebugInfo("after adding flavor tag");
    		fffc.addDataFlavor(new DataFlavor(File.class, "tagtoo"));
    		fffc.printDebugInfo("after adding flavor tagtoo");
    	}
    }
    }

  5. #5
    tomba is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    Thank you very much, zweibieren. That seems to work well.

    Best wishes!

Similar Threads

  1. Drag and Drop
    By carderne in forum New To Java
    Replies: 0
    Last Post: 08-31-2009, 09:18 AM
  2. Drag and Drop(URGENT)
    By simmi in forum AWT / Swing
    Replies: 5
    Last Post: 07-10-2009, 11:16 AM
  3. Drag and drop
    By thayalan in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2009, 03:04 PM
  4. Drag and drop
    By abhivenugopal in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-30-2008, 02:10 PM
  5. Replies: 3
    Last Post: 06-07-2007, 05:52 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •