TreeViewer选择和剪贴板

时间:2011-12-14 01:27:34

标签: java swt jface

是否可以将多个对象(代表TreeViewer个选项)放入剪贴板?这是我的Cut代码。

static public void Cut(EssentialData dataInst)
{
    IStructuredSelection selection = (IStructuredSelection)dataInst.getTreeViewer().getSelection();

    if(selection == null)
    {
        System.err.println("selection received was null");
        return;
    }

    Object[] objects = selection.toArray();

    //because setContents requires a Transfer object for every object sent
    List<Transfer> typesList = new ArrayList<>();

    for(int i = 0; i < objects.length; i++)
        typesList.add(FileEntryTransfer.getInstance());

    final Clipboard cb = new Clipboard(Display.getCurrent());
    cb.setContents(objects, typesList.toArray(new Transfer[]{}));
    cb.dispose();

    for(int i = 0; i < objects.length; i++)
        dataInst.getFileManager().removeEntry((FileEntry)objects[i]);

    dataInst.getTreeViewer().refresh(false);
}

我要做的是将多个选项(FileEntry的实例)放入剪贴板。

FileEntryTransfer是我的自定义ByteArrayTransfer课程,我也用于DND。它运行良好,但我注意到setContents()实际上只发送了一个FileEntry对象,而不是一个对象数组(这就是我想要的)。

有什么想法吗?

[编辑]感谢vanaprogeja,我用

解决了这个问题
cb.setContents(new Object[]{ objects }, new Transfer[] { FileEntryTransfer.getInstance() });

1 个答案:

答案 0 :(得分:2)

怎么样:

cb.setContents(new Object[]{ objects },
    new Transfer[] { FileEntryTransfer.getInstance() });