Java JTree Node insertStrAsNodeLexi(DefaultTreeModel treeModel, String child, DefaultMutableTreeNode parent, Boolean childIsFile)

Here you can find the source of insertStrAsNodeLexi(DefaultTreeModel treeModel, String child, DefaultMutableTreeNode parent, Boolean childIsFile)

Description

insert Str As Node Lexi

License

Open Source License

Declaration

public static void insertStrAsNodeLexi(DefaultTreeModel treeModel, String child, DefaultMutableTreeNode parent,
            Boolean childIsFile) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class Main {
    public static void insertStrAsNodeLexi(DefaultTreeModel treeModel, String child, DefaultMutableTreeNode parent,
            Boolean childIsFile) {
        DefaultMutableTreeNode currNode;
        DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);
        if (childIsFile)
            childNode.setAllowsChildren(false);
        else/*  ww  w.  jav a 2s  .co m*/
            childNode.setAllowsChildren(true);
        Boolean parentIsFile;
        boolean inserted = false;
        for (int i = 0; i < parent.getChildCount(); i++) {
            currNode = (DefaultMutableTreeNode) parent.getChildAt(i);
            parentIsFile = !currNode.getAllowsChildren();
            if ((currNode.getUserObject().toString().compareTo(child) > 0)
                    && (parentIsFile.compareTo(childIsFile) >= 0)) { // dirs before files
                treeModel.insertNodeInto(childNode, parent, i);
                inserted = true;
                break;
            }
        }
        if (!inserted)
            treeModel.insertNodeInto(childNode, parent, parent.getChildCount());
    }
}

Related

  1. getPathStringForNode(DefaultMutableTreeNode node)
  2. getSubTree(TreeNode root, String name)
  3. hasOnlyLeafs(JTree tree, Object node)
  4. initializeExpandedPathsBeforeChange(JTree tree, DefaultMutableTreeNode root)
  5. insertNodeInAlphabeticalOrder(DefaultMutableTreeNode childNode, DefaultMutableTreeNode parentNode, DefaultTreeModel model)
  6. isAncestor(final TreeNode node, final TreeNode parent)
  7. isChildNode(DefaultMutableTreeNode parent, DefaultMutableTreeNode child)
  8. isMovingUp(DefaultMutableTreeNode ntarget, DefaultMutableTreeNode nsource)
  9. isNodeAsStrChild(DefaultMutableTreeNode parent, String child)