X

Geertjan's Blog

  • March 1, 2013

No Expansion Icon When No Children (Part 1)

Geertjan Wielenga
Product Manager

Sometimes you have Nodes with an expansion icon, i.e., the "plus" sign, even though the Object doesn't have Children. Once the user tries to expand the Node, the "plus" sign disappears, and no Children are shown. Kind of misleading because the user thought that there would be Children because the expansion icon was shown. Would be nicer if there were to be no expansion icon if the Object has no Children.

Here's how to solve that, via Children.createLazy:

public class MyNode extends AbstractNode {
public MyNode(NodeKey key) {
super(Children.createLazy(new MyCallable(key)), Lookups.singleton(key));
setDisplayName(key.toString());
}
private static class MyCallable implements Callable<Children> {
private final NodeKey key;
private MyCallable(NodeKey key) {
this.key = key;
}
@Override
public Children call() throws Exception {
//Check, somehow, that your key has children,
//e.g., create "hasChildren" on the object
//to look in the database to see whether
//the object has children;
//if it doesn't have children, return a leaf:
if (!key.hasChildren()) {
return Children.LEAF;
} else {
return Children.create(new MyChildFactory(key), true);
}
}
}
}

Now see part 2.

Join the discussion

Comments ( 4 )
  • Michael Bishop Monday, May 6, 2013

    Hello,

    I recently implemented this solution. The explorer tree appeared correctly, but no longer updates correctly; adding and removing nodes don't seem to be triggering the refresh() method in the factory class. For instance, when adding a new child, the underlying data model updates and I can confirm the child is added, but nothing happens in the tree.

    My factory class is unchanged and everything is the same except inserting a custom Callable as illustrated above. Is there something extra that needs to be done to make the factory classes do their updates as before?


  • Geertjan Friday, May 24, 2013

    Hi Michael, have you solved this problem? Sorry for not getting back to this sooner.


  • Geertjan Friday, May 24, 2013

    PS: What do you mean by "adding a new child", i.e., how are you adding a new child? A small sample app that demonstrates the problem will be the first step to solving this.


  • Geertjan Sunday, May 26, 2013
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.