2013-04-26 32 views
1

我有一个SWT应用程序与一堆图形元素。我希望用户能够将元素拖放到他们的桌面/ Windows资源管理器/ OS X Finder。当他们放下元素时,我需要将它放到的路径,以便我可以在该位置创建一个代表元素的文件。SWT拖动到资源管理器(Windows)或Finder(OS X)

我不认为我可以使用FileTransfer,因为没有源文件。有一个源对象可以创建一个文件,但只有一次它知道把它放在哪里。

下面内置的是我试图实现的一个简单示例,它有一个带有要从中拖出的标签的文本框。如果用户拖动到某个文件夹或文件,我想获取它们拖到的路径。如果他们拖到一个文件,我想用文本框中的任何内容替换该文件的内容。如果他们拖到一个文件夹,我想用文本框中的任何内容创建一个名为“TestFile”的文件。

import org.eclipse.swt.SWT; 
import org.eclipse.swt.dnd.*; 
import org.eclipse.swt.layout.*; 
import org.eclipse.swt.widgets.*; 

public class DesktopDragExample { 

public static void main(String[] args) { 
    // put together the SWT main loop 
    final Display display = Display.getDefault(); 
    display.syncExec(new Runnable() { 
     @Override 
     public void run() { 
      Shell shell = new Shell(display, SWT.SHELL_TRIM); 
      initializeGui(shell); 

      //open the shell 
      shell.open(); 

      //run the event loop 
      while (!shell.isDisposed()) { 
       if (!display.readAndDispatch()) { 
        display.sleep(); 
       } 
      } 
     } 
    }); 
} 

// create the gui 
private static void initializeGui(Composite parent) { 
    GridLayout layout = new GridLayout(2, false); 
    parent.setLayout(layout); 

    // make the instructions label 
    Label infoLbl = new Label(parent, SWT.WRAP); 
    GridData gd = new GridData(); 
    gd.grabExcessHorizontalSpace = true; 
    gd.horizontalAlignment = SWT.FILL; 
    gd.horizontalSpan = 2; 
    infoLbl.setLayoutData(gd); 
    infoLbl.setText(
      "You should be able to drag to the desktop, Windows Explorer, or OS X Finder.\n" + 
      "If you drag to a file, it will replace the contents of that file with the contents of the text box.\n" + 
      "If you drag to a folder, it will create a file named 'TestFile' whose contents are whatever is in the text box."); 

    // make the text element 
    final Text text = new Text(parent, SWT.SINGLE | SWT.BORDER); 
    gd = new GridData(); 
    gd.grabExcessHorizontalSpace = true; 
    gd.horizontalAlignment = SWT.FILL; 
    text.setLayoutData(gd); 

    // make the label element 
    Label label = new Label(parent, SWT.NONE); 
    label.setText("Drag me"); 

    // listener for drags 
    DragSourceListener dragListener = new DragSourceListener() { 
     @Override 
     public void dragStart(DragSourceEvent e) { 
      e.detail = DND.DROP_COPY; 
     } 

     @Override 
     public void dragFinished(DragSourceEvent e) { 
      System.out.println("--dragFinished--"); 
      System.out.println("e.data=" + e.data); 
     } 

     @Override 
     public void dragSetData(DragSourceEvent e) { 
      System.out.println("--dragSetData--"); 
      System.out.println("e.data=" + e.data); 
     } 
    }; 


    // the DragSource 
    DragSource dragSource = new DragSource(label, DND.DROP_COPY); 
    dragSource.setTransfer(new Transfer[]{FileTransfer.getInstance()}); 
    dragSource.addDragListener(dragListener); 
} 

private static void draggedTo(String path, String textBoxContents) { 
    System.out.println("Dragged the contents '" + textBoxContents + "' to '" + path + "'"); 
} 

} 

这里有一些其他人同样的问题,不过貌似无解至今: Drag from SWT to Desktop..want destination path as String

回答

0

做到这一点是通过创建一个临时文件,然后使用文件传输的唯一途径。我怀疑这就是你必须在本机代码中做的事情。我会看看我是否有足够的时间来绘制样本...

+1

如何创建临时文件的成本很高?例如,假设被拖动的对象是一台速度较慢的FTP服务器上的1GB文件。如果我可以创建一个临时文件,在DragStart中开始写入,然后在拖动开始后继续写入,那么这可能会起作用......但我真的希望能够自己控制复制:例如,如果用户决定取消,我想知道我可以停止写入临时文件。 – 2013-04-29 20:27:44

+1

如果有一些方法来使用临时文件作为跟踪,那将是很酷的... – 2013-04-29 20:34:09

+0

@NedTwigg我很确定你可以在Linux/Mac上使用管道(不熟悉Windows) - 但这可能需要本地代码。 – Eugene 2013-04-29 20:59:35

相关问题