Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

40
LINES

< > BotCompany Repo | #1025669 // jDynamicTree with node wrapping [dev.]

JavaX fragment (include)

import javax.swing.tree.*;

static <A, B extends Cl<A>> JTree jDynamicTree(A root, IF1<A, B> makeChildren, O... _) {
  ret jDynamicTree(root, toF1(makeChildren), _);
}

static <A, B extends Cl<A>> JTree jDynamicTree(final A root, final F1<A, B> makeChildren, fO... _) {
  // optional wrapping/unwrapping of nodes for nicer printing
  optPar IF1 wrapNode;
  optPar IF1 unwrapNode;
    
  ret swing(func -> JTree {
    DefaultMutableTreeNode rootNode = new(callFOrKeep(wrapNode, root));
    rootNode.add(new DefaultMutableTreeNode(jDynamicTree_dummy()));
    final JTree tree = new JTree(rootNode);
    tree.setShowsRootHandles(true);
    tree.collapseRow(0);
    final bool keepChildrenOnCollapse = optParam(_, 'keepChildrenOnCollapse, false);
    bool makeChildrenIsFast = boolParam makeChildrenIsFast(_) || makeChildren == null;
    bool debug = boolPar debug(_);
    
    class jDynamicTree_Listener implements TreeWillExpandListener {
      public void treeWillCollapse(TreeExpansionEvent e) {}
      public void treeWillExpand(TreeExpansionEvent e) { pcall {
        if (debug) print("treeWillExpand");
        DefaultMutableTreeNode node = cast e.getPath().getLastPathComponent();
        if (!keepChildrenOnCollapse || jDynamicTree_isInDummyState(node)) {
          L<A> l = asList(callF(makeChildren, callFOrKeep(unwrapNode, (A) node.getUserObject())));
          if (makeChildrenIsFast
            ? replaceTreeNodeChildren_withDummyChild2(node, l, jDynamicTree_dummy(), makeChildren)
            : replaceTreeNodeChildren_withDummyChild(node, l, jDynamicTree_dummy()))
            ((DefaultTreeModel) tree.getModel()).nodeStructureChanged(node);
        }
      }}
    }
    tree.addTreeWillExpandListener(new jDynamicTree_Listener);
    
    ret tree;
  });
}

Author comment

Began life as a copy of #1016044

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

-
Snippet ID: #1025669
Snippet name: jDynamicTree with node wrapping [dev.]
Eternal ID of this version: #1025669/1
Text MD5: 172bcaf8488c4379414a12414e20bea8
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-10-12 21:56:54
Source code size: 1808 bytes / 40 lines
Pitched / IR pitched: No / No
Views / Downloads: 170 / 203
Referenced in: