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

45
LINES

< > BotCompany Repo | #1016044 // jDynamicTree - JTree with children made on demand, simpler functions

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3072L/19K).

1  
import javax.swing.tree.*;
2  
3  
static <A, B extends Cl<A>> JTree jDynamicTree(A root, IF1<A, B> makeChildren, O... _) {
4  
  ret jDynamicTree(root, toF1(makeChildren), _);
5  
}
6  
7  
static <A, B extends Cl<A>> JTree jDynamicTree(final A root, final F1<A, B> makeChildren, fO... _) {
8  
  ret swing(func -> JTree {
9  
    optPar IF1<A, O> valueToText;
10  
    DefaultMutableTreeNode rootNode = new(root);
11  
    rootNode.add(new DefaultMutableTreeNode(jDynamicTree_dummy()));
12  
    final JTree tree = new JTree(rootNode) {
13  
      public S convertValueToText(O value, bool selected, bool expanded, bool leaf, int row, bool hasFocus) {
14  
        if (valueToText != null) {
15  
          O userObject = ((DefaultMutableTreeNode) value).getUserObject();
16  
          pcall { ret str(valueToText.get((A) userObject)); }
17  
        }
18  
        ret super.convertValueToText(value, selected, expanded, leaf, row, hasFocus);
19  
      }
20  
    };
21  
    tree.setShowsRootHandles(true);
22  
    tree.collapseRow(0);
23  
    final bool keepChildrenOnCollapse = optParam(_, 'keepChildrenOnCollapse, false);
24  
    bool makeChildrenIsFast = boolParam makeChildrenIsFast(_) || makeChildren == null;
25  
    bool debug = boolPar debug(_);
26  
    
27  
    class jDynamicTree_Listener implements TreeWillExpandListener {
28  
      public void treeWillCollapse(TreeExpansionEvent e) {}
29  
      public void treeWillExpand(TreeExpansionEvent e) { pcall {
30  
        if (debug) print("treeWillExpand");
31  
        DefaultMutableTreeNode node = cast e.getPath().getLastPathComponent();
32  
        if (!keepChildrenOnCollapse || jDynamicTree_isInDummyState(node)) {
33  
          L<A> l = asList(callF(makeChildren, (A) node.getUserObject()));
34  
          if (makeChildrenIsFast
35  
            ? replaceTreeNodeChildren_withDummyChild2(node, l, jDynamicTree_dummy(), makeChildren)
36  
            : replaceTreeNodeChildren_withDummyChild(node, l, jDynamicTree_dummy()))
37  
            ((DefaultTreeModel) tree.getModel()).nodeStructureChanged(node);
38  
        }
39  
      }}
40  
    }
41  
    tree.addTreeWillExpandListener(new jDynamicTree_Listener);
42  
    
43  
    ret tree;
44  
  });
45  
}

Author comment

Began life as a copy of #1008652

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1016044
Snippet name: jDynamicTree - JTree with children made on demand, simpler functions
Eternal ID of this version: #1016044/31
Text MD5: bcb90d81329eaec36aec63208f66de32
Transpilation MD5: 4da20ae5df146798c3545b71c1edc3d4
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-05-23 22:31:39
Source code size: 2078 bytes / 45 lines
Pitched / IR pitched: No / No
Views / Downloads: 589 / 774
Version history: 30 change(s)
Referenced in: [show references]