X

Geertjan's Blog

  • June 13, 2014

How To Know Which Node You Dropped On

Geertjan Wielenga
Product Manager

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:

Join the discussion

Comments ( 2 )
  • Chris Esposito Wednesday, June 18, 2014

    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


  • Chris Esposito Monday, June 23, 2014

    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.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.