Friday Jun 13, 2014

How To Know Which Node You Dropped On

A great question from Chris Esposito from Boeing, who wants to know how to know on which Node a drop has taken place. For the context, read this blog entry. For the answer, as almost always in the NetBeans Platform: "Use the Lookup." I.e., put the object, in this case a String representing a name, into the Lookup of the Node on which you're going to be dropping. Then, when you do the drop on a Node, you can check what is in its Lookup. Or, expressed in code:

@Override
protected Node createNodeForKey(String name) {
    Node node = new AbstractNode(Children.LEAF, Lookups.singleton(name)) {
        @Override
        public PasteType getDropType(Transferable t, int arg1, int arg2) {
            final Node draggedNode = NodeTransfer.node(t, arg1);
            return new PasteType() {
                @Override
                public Transferable paste() throws IOException {
                    names.add(draggedNode.getDisplayName());
                    refresh(true);
                    String theDraggedNode = draggedNode.getDisplayName();
                    String theNodeOnWhichTheDropOccurred = getLookup().lookup(String.class);
                    String message = "You dropped "
                            + theDraggedNode + " on "
                            + theNodeOnWhichTheDropOccurred + "!";
                    StatusDisplayer.getDefault().setStatusText(message);
                    return null;
                }
            };
        }
    };
    node.setDisplayName(name);
    return node;
}

So, now, instead of simply adding the dropped Node to the end of the list, you can add it right after the Node which you dropped it on:

@Override
protected Node createNodeForKey(String name) {
    Node node = new AbstractNode(Children.LEAF, Lookups.singleton(name)) {
        @Override
        public PasteType getDropType(Transferable t, int arg1, int arg2) {
            final Node draggedNode = NodeTransfer.node(t, arg1);
            return new PasteType() {
                @Override
                public Transferable paste() throws IOException {
                    String theDraggedNode = draggedNode.getDisplayName();
                    String theNodeOnWhichTheDropOccurred = getLookup().lookup(String.class);
                    int i = names.indexOf(theNodeOnWhichTheDropOccurred);
                    names.add(i+1, draggedNode.getDisplayName());
                    refresh(true);
                    String message = "You dropped "
                            + theDraggedNode + " on "
                            + theNodeOnWhichTheDropOccurred + "!";
                    StatusDisplayer.getDefault().setStatusText(message);
                    return null;
                }
            };
        }
    };
    node.setDisplayName(name);
    return node;
}

Result:

About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« June 2014 »
SunMonTueWedThuFriSat
14
15
19
28
29
     
       
Today