如何在拖拽目标TransferHandler中找到拖拽源组件?

发布于 2024-09-05 16:42:12 字数 505 浏览 1 评论 0原文

问题是关于Java 5和拖放。

有没有办法确定拖放目标TransferHandler中的拖动源组件

我需要在我的 canImport(..)importData(..) 方法中知道这一点,以便对传输相同类型或 的数据采取不同的操作数据风味

我目前实现这一目标的方法是劫持 DataFlavor 的“人类可读字段”并设置一个字符串操作,结果如下:

DataFlavor localCopyFooFlavor = new DataFlavor(Foo.class, "COPY");
DataFlavor localEditFooFlavor = new DataFlavor(Foo.class, "EDIT");

然后使用此字段来决定要采取什么操作。 我很确定这是错误的。

谢谢。

Question is regarding Java 5 and Drag n Drop.

Is there a way to determine the drag source component in the drop target TransferHandler?

I need to know this in my canImport(..) or importData(..) method in order to take different actions for transfer data of the same type or DataFlavor.

The way I have achieved this currently is to hijack the DataFlavor 'Human Readable Field' and set a string action, resulting something like this:

DataFlavor localCopyFooFlavor = new DataFlavor(Foo.class, "COPY");
DataFlavor localEditFooFlavor = new DataFlavor(Foo.class, "EDIT");

Then use this field to decide what action to take.
I pretty sure this is wrong.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑾夏年华 2024-09-12 16:42:16

想通了。

关键是为每个放置目标组件创建一个 DataFlavor。所以这里我有 4 个接受 foo 类型的 drop 的目标、3 个按钮和 1 个面板。

fooCopyButtonFlavor   = new DataFlavor(FooCopyButtonTransferData.class,   "Foo 'Copy Button' Transfer Data");
fooEditButtonFlavor   = new DataFlavor(FooEditButtonTransferData.class,   "Foo Entry 'Edit Button' Transfer Data");
fooDeleteButtonFlavor = new DataFlavor(FooDeleteButtonTransferData.class, "Foo Entry 'Delete Button' Transfer Data");
fooDialogPanelFlavor  = new DataFlavor(FooDialogPanelTransferData.class, "Foo Entry 'Dialog Panel' Transfer Data")

我决定结束我的 Foo 状态课程。

并使这个包装器实现一个我命名为 TransferDataStrategy 的自定义接口。现在这样做可以让我在 importData(...) 函数中针对相同的风格执行不同的操作。

public interface TransferDataStrategy<MODEL>
{
    MODEL getModel();

    OptionStrategy getOptionStrategy();
}

我的 Transferable 实现(可以被认为是拖动的源)现在可以通过返回不同的 DataFlavor (或 Drop Target Component)来驱动放置时发生的情况策略。

public class SourceOneTransferOperation implements Transferable
{
    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException
    {
        if (flavor.equals(FooDataFlavor.fooCopyButtonFlavor)) {
            TransferDataStrategy<Foo> tds = new FooCopyAAA(model);
            return tds;
        }
     ...
    }
}

注意下面的源二也支持 FooCopyButtonFlavor,但返回不同的策略。

public class SourceTwoTransferOperation implements Transferable
{
    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException
    {
        if (flavor.equals(FooDataFlavor.fooCopyButtonFlavor)) {
            TransferDataStrategy<Foo> tds = new FooCopyBBB(model);
            return tds;
        }
     ...
    }
}

这很难解释,但希望它能有所帮助。

Figured it out.

The key is to create a DataFlavor per drop target component. So here I have 4 targets which accept drops of type foo, 3 buttons and 1 panel.

fooCopyButtonFlavor   = new DataFlavor(FooCopyButtonTransferData.class,   "Foo 'Copy Button' Transfer Data");
fooEditButtonFlavor   = new DataFlavor(FooEditButtonTransferData.class,   "Foo Entry 'Edit Button' Transfer Data");
fooDeleteButtonFlavor = new DataFlavor(FooDeleteButtonTransferData.class, "Foo Entry 'Delete Button' Transfer Data");
fooDialogPanelFlavor  = new DataFlavor(FooDialogPanelTransferData.class, "Foo Entry 'Dialog Panel' Transfer Data")

I decided to wrap up my Foo state class.

And make this wrapper implements a custom interface I named TransferDataStrategy. Doing this now allows me to perform different actions in the importData(...) function for the same flavors.

public interface TransferDataStrategy<MODEL>
{
    MODEL getModel();

    OptionStrategy getOptionStrategy();
}

My Transferable implementations (which can be thought of as the SOURCE of the drag) can now drive what happens on drop for the same DataFlavor (or Drop Target Component) by returning different strategies.

public class SourceOneTransferOperation implements Transferable
{
    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException
    {
        if (flavor.equals(FooDataFlavor.fooCopyButtonFlavor)) {
            TransferDataStrategy<Foo> tds = new FooCopyAAA(model);
            return tds;
        }
     ...
    }
}

Notice Source Two Below is also supports FooCopyButtonFlavor, but returns a different strategy.

public class SourceTwoTransferOperation implements Transferable
{
    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException
    {
        if (flavor.equals(FooDataFlavor.fooCopyButtonFlavor)) {
            TransferDataStrategy<Foo> tds = new FooCopyBBB(model);
            return tds;
        }
     ...
    }
}

This is quite hard to explain but hopefully it might help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文