[platform-dev] Re: Weakly listening in a Node hierarchy with user collapsing the tree

  • From: Ernest C Lötter < >
  • To:
  • Subject: [platform-dev] Re: Weakly listening in a Node hierarchy with user collapsing the tree
  • Date: Thu, 19 Sep 2013 23:37:58 +0200

Thanks Tim, your solution sketch nudged me towards a solution, which turned into a not completely horrible solution. For documentation purposes and the sake of someone else being interested in this in the future, here is the process.

I made the interface

public interface NodeExpansion {
    void nodeExpanded();
    void nodeCollapsed();
}

as a capability to put in the Lookup of my Nodes - so that they could do custom tasks when collapse/expand occurs. By subclassing my BeanTreeView (which I'm doing anyway already), one has access to the protected tree (JTree) field of BeanTreeView. Now, we make a reusable listener class:

public class NodeExpansionListener implements TreeExpansionListener {
    @Override
    public void treeExpanded(TreeExpansionEvent event) {
      Node node = getNode(event);
        if (node != null) {
for (NodeExpansion ne : node.getLookup().lookupAll(NodeExpansion.class)) {
                ne.nodeExpanded();
            }
        }
    }

    @Override
    public void treeCollapsed(TreeExpansionEvent event) {
        Node node = getNode(event);
        if (node != null) {
for (NodeExpansion ne : node.getLookup().lookupAll(NodeExpansion.class)) {
                ne.nodeCollapsed();
            }
        }
    }

    private static Node getNode(TreeExpansionEvent treeExpansionEvent) {
        try {
return Visualizer.findNode(treeExpansionEvent.getPath().getLastPathComponent());
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
        return null;
    }
}

Returning to my subclass of BeanTreeView, I introduce

private final NodeExpansionListener nodeExpansionListener = new NodeExpansionListener();

and ensure that it is added/removed from my tree using

@Override
public void addNotify() {
    super.addNotify();
    tree.addTreeExpansionListener(nodeExpansionListener);
}

@Override
public void removeNotify() {
    super.removeNotify();
tree.removeTreeExpansionListener(nodeExpansionListener);
}

At this point we are done, and I can put instances of NodeExpansion in my Nodes' Lookups, and they could do Children re-initialization as Tim suggested.

Along the way, even bugzilla entries of more than 10 years old were useful reading here, see for example https://netbeans.org/bugzilla/show_bug.cgi?id=35399 (leading to my discovery of Visualizer).

Best regards,
Ernest



On 18/09/13 23:03, Tim Boudreau wrote:
Elegantly probably doesn't exist. You've got BeanTreeView which is a subclass of JScrollPane, and you could grab the JTree by calling getViewportView(), but the tree nodes are actually instances of VisualizerNode (a thread-safety layer on top of nodes). If you can do all of that and somehow get a reference to the Node that was collapsed, or at least its lookup's contents from there, you could put an interface with one method, onCollapse(), which replaces the children, and call it when you receive collapse notification. Far from elegant, but should work if it's critical to discard stuff.

-Tim





[platform-dev] Weakly listening in a Node hierarchy with user collapsing the tree

Ernest C Lötter 09/17/2013

[platform-dev] Re: Weakly listening in a Node hierarchy with user collapsing the tree

Tim Boudreau 09/17/2013

[platform-dev] Re: Weakly listening in a Node hierarchy with user collapsing the tree

Ernest C Lötter 09/18/2013

[platform-dev] Re: Weakly listening in a Node hierarchy with user collapsing the tree

Tim Boudreau 09/18/2013

[platform-dev] Re: Weakly listening in a Node hierarchy with user collapsing the tree

Ernest C Lötter 09/19/2013

Project Features

Project Links

About this Project

Platform was started in November 2009, is owned by Antonin Nebuzelsky, and has 149 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20160708.bf2ac18). © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close