Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Java drag & drop
Date: Fri, 3 Apr 2009 14:13:01 +0000 (UTC)
Organization: RWTH Aachen University
Lines: 68
Message-ID: <gr55hd$26l$1@fred.mathworks.com>
References: <fahm1i$5e7$1@fred.mathworks.com> <fc6889$n5i$1@fred.mathworks.com> <fc91jd$2uu$1@fred.mathworks.com> <fc93qh$d7j$1@fred.mathworks.com> <gfvdhq$13l$1@fred.mathworks.com> <gg1bn6$up$1@fred.mathworks.com> <gg46ub$o63$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1238767981 2261 172.30.248.38 (3 Apr 2009 14:13:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 3 Apr 2009 14:13:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1448465
Xref: news.mathworks.com comp.soft-sys.matlab:530059


"Travis Perry" <travisjperry@gmail.com> wrote in message <gg46ub$o63$1@fred.mathworks.com>...
> "Malcolm Lidierth" <ku.ca.lck@htreidil.mloclam> wrote in message <gg1bn6$up$1@fred.mathworks.com>...
> > There is no workaround that I know of. However, I have not tried this out on the most recent MATLAB versions so it is possible that it has been resolved.
> 
> Do you know if the source of the problem was ever determined?  I don't have the latest version of Matlab, I have the R2007A which I believe is what you were using before.   Do you see any other way of getting the file path and name from a file that has been dragged to a figure?  I have been stumped on this for quite a while.

I couldn't pinpoint the exact source of this problem either but do could locate it somewhere within Mathworks DropTargetListener which seems to pass the DropTargetDropEvent (dtde) to the Matlab callback function without accepting the drop before, so dtde.getTransferable() fails.
Since I wasn't able to solve that problem directly, I wrote a new DropTarget class "DropTargetList" which accepts the drop and stores transferable data as java.util.List in a new field. Matlab passes the DropTargetList object to the callback function as first argument:

% initialize new drop target
dnd=javaObjectEDT('DropTargetList')
dnd=handle(dnd,'callbackProperties');
set(dnd, 'DropCallback', @MyCallback);
e.setDropTarget(dnd);

function MyCallback(dropTargetList, dropTargetDropEvent)
data = dropTargetObject.getTransferData();
%...
end


Here's the java code of the DropTargetList class:

import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.util.List;
import java.io.IOException;

/* Modified DropTarget to be used for drag & drop in Matlab GUIs  
 *  
 * Dirk Engel
 * Process Systems Engineering
 * RWTH Aachen University
 */
public class DropTargetList extends DropTarget {
    
    private Transferable transferable;
    private DataFlavor acceptedDataFlavor = DataFlavor.javaFileListFlavor;
    private List<?> transferData;
    /*********************************************************************/

    public synchronized void drop(DropTargetDropEvent dtde) {
    	dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
    	super.drop(dtde);
    	
    	transferable = dtde.getTransferable();
    	try {
    		transferData = (List<?>)transferable.getTransferData(acceptedDataFlavor);
    	} catch (UnsupportedFlavorException e) {
    		return;
    	} catch (IOException e) {
    		return;
    	}
    }
    
    public void setAcceptedDataFlavor(DataFlavor flavor) {
    	acceptedDataFlavor = flavor;    }
    public DataFlavor getAcceptedDataFlavor() {
    	return acceptedDataFlavor;    }
    public Transferable getTransferable() {
    	return transferable;    }
    public List<?> getTransferData() {
    	return transferData;    }
}

Note to Yair Altman's suggestion to override the default drop target's TransferHandler(): I tried it and this also works but is much more difficult because one has to use JMI to call Matlab from Java. Also you get an error when calling "figure" from within the callback after compiling the m-file to a standalone application (very strange)

Hope this solves the DND problem once and for all.