E4:将对象从TableViewer拖动到Windows资源管理器(或操作系统特定的文件系统)



在我的Eclipse RCP应用程序中,我在TableViewer中显示一些业务数据。

我希望用户能够从表查看器中拖动一行,并将其放在windows桌面/资源管理器上。然后,Windows应该使用所选行的数据创建一个文件,我可以在DragSourceAdapter类的dragSetData(..)方法中提供这些数据。

如何实现?在表查看器上使用FileTransfer作为dragSourceSupport似乎是一种方法,因为它触发了对dragSetData()方法的调用。但是,在这种方法中,我应该创建什么对象并将其分配给"event.data"呢?

一个工作的例子将不胜感激。

我已经毫无问题地实现了相反的操作,即将文件从windows资源管理器拖动到TableViewer上,并在表中添加一行。网上有很多这样的例子,但找不到相反的例子,从eclipse拖到OS

[编辑+新需求]

所以我知道我必须在某个地方创建一个临时文件,并在dragSetData()中的event.data中设置该临时文件的名称
Q:有没有更简单的方法可以做到这一点,例如在某个位置(iun data)直接设置文件的内容而不设置临时文件?

还有另一个要求。当放置操作即将发生时,我想向用户显示一个弹出窗口,用户必须从"行"中选择要导出的"业务数据"以及将创建的文件的名称。我尝试了以下操作(目前只询问文件名),但它并没有像预期的那样工作,因为光标到达应用程序外的第一个像素时,弹出窗口就会出现。我想在下降操作发生之前显示弹出窗口
Q:有没有办法在放置操作发生之前显示这个弹出窗口,即当用户"释放"鼠标按钮时?

@Override
public void dragSetData(final DragSourceEvent event){
if (FileTransfer.getInstance().isSupportedType(event.dataType)) { 
// Will be a more complex dialog with multiple fields..
InputDialog inputDialog = new InputDialog(shell, "Please enter a file name", "File Name:", "", null);
if (inputDialog.open() != Window.OK) {
event.doit = false;
return;
}
event.data = new String[] { inputDialog.getValue() };
}
}

FileTransferevent.data是一个文件路径字符串数组。

你的DragSourceAdapter类可能看起来像:

public class MyDragSourceAdapter extends DragSourceAdapter
{
private final StructuredViewer viewer;
public MyDragSourceAdapter(final StructuredViewer viewer)
{
super();
this.viewer = viewer;
}
@Override
public void dragStart(final DragSourceEvent event)
{
IStructuredSelection selection = viewer.getStructuredSelection();
if (selection == null)
return;
// TODO check if the selection contains any files
// TODO set event.doit = false if not
}
@Override
public void dragSetData(final DragSourceEvent event)
{
if (!FileTransfer.getInstance().isSupportedType(event.dataType))
return;
IStructuredSelection selection = viewer.getStructuredSelection();
List<String> files = new ArrayList<>(selection.size());
// TODO add files in the selection to 'files'
event.data = files.toArray(new String [files.size()]);
}
}

然后用将其安装在查看器上

MyDragSourceAdapter adapter = new MyDragSourceAdapter(viewer);
viewer.addDragSupport(DND.DROP_COPY, new Transfer [] {FileTransfer.getInstance()}, adapter);

相关内容

最新更新