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).

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... _) {
  ret swing(func -> JTree {
    optPar IF1<A, O> valueToText;
    DefaultMutableTreeNode rootNode = new(root);
    rootNode.add(new DefaultMutableTreeNode(jDynamicTree_dummy()));
    final JTree tree = new JTree(rootNode) {
      public S convertValueToText(O value, bool selected, bool expanded, bool leaf, int row, bool hasFocus) {
        if (valueToText != null) {
          O userObject = ((DefaultMutableTreeNode) value).getUserObject();
          pcall { ret str(valueToText.get((A) userObject)); }
        }
        ret super.convertValueToText(value, selected, expanded, leaf, row, hasFocus);
      }
    };
    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, (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 #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: 583 / 766
Version history: 30 change(s)
Referenced in: [show references]