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:

Comments:

Hi Geertjan, Thanks for this; It's certainly the kind of solution I'm looking for. I put the string in the Lookup of the AbstractNode instances and it works as expected. I'll be modifying your example to work with the Customer objects in your earlier blog post on this topic. If I can get it to work for those, then I can get it to work for the classes my application is using.

Chris

Posted by Chris Esposito on June 18, 2014 at 04:20 PM PDT #

I'now extended this example so it now supports cut / copy / paste / reorder / delete / drag / drop, using Customer objects as the beans and BeanNodes as the Node UI class. Nodes / Customers can have arbitrarily nested child hierarchies, and there is a method in the Customer class ( makeDropDecision(boolean Customer dragged, Customer dropped) ) that decides if the drop is allowed or rejected. Drop attempts using non-Customer objects are currently rejected.

If anybody wants the source, please let me know.

Posted by Chris Esposito on June 23, 2014 at 03:38 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
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
« March 2015
SunMonTueWedThuFriSat
4
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    
       
Today