Java JTree Node sort(DefaultMutableTreeNode node)

Here you can find the source of sort(DefaultMutableTreeNode node)

Description

Method by Adrian: [ <a href="http://stackoverflow.com/a/15704264/5620200">StackOverflow</a> ] & Mike: [ <a href= "http://stackoverflow.com/questions/1542170/arranging-nodes-in-a-jtree"> StackOverflow</a> ]

License

Open Source License

Parameter

Parameter Description
node a parameter

Declaration

@SuppressWarnings("unchecked")
public static DefaultMutableTreeNode sort(DefaultMutableTreeNode node) 

Method Source Code


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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;

public class Main {
    /**/*from ww  w .j av  a  2 s . c  o m*/
     * Method by Adrian: [
     * <a href="http://stackoverflow.com/a/15704264/5620200">StackOverflow</a> ]
     * & Mike: [ <a href=
     * "http://stackoverflow.com/questions/1542170/arranging-nodes-in-a-jtree">
     * StackOverflow</a> ]
     * 
     * @param node
     * @return
     */
    @SuppressWarnings("unchecked")
    public static DefaultMutableTreeNode sort(DefaultMutableTreeNode node) {
        List<DefaultMutableTreeNode> children = Collections.list(node.children());
        List<String> orgCnames = new ArrayList<String>();
        List<String> cNames = new ArrayList<String>();
        DefaultMutableTreeNode temParent = new DefaultMutableTreeNode();
        for (DefaultMutableTreeNode child : children) {
            DefaultMutableTreeNode ch = (DefaultMutableTreeNode) child;
            temParent.insert(ch, 0);
            String uppser = ch.toString().toUpperCase();
            // Not dependent on package name, so if duplicates are found
            // they will later on be confused. Adding this is of
            // very little consequence and fixes the issue.
            if (cNames.contains(uppser)) {
                uppser += "$COPY";
            }
            cNames.add(uppser);
            orgCnames.add(uppser);
            if (!child.isLeaf()) {
                sort(child);
            }
        }
        Collections.sort(cNames);
        for (String name : cNames) {
            int indx = orgCnames.indexOf(name);
            int insertIndex = node.getChildCount();
            node.insert(children.get(indx), insertIndex);
        }
        // Fixing folder placement
        for (int i = 0; i < node.getChildCount() - 1; i++) {
            DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(i);
            for (int j = i + 1; j <= node.getChildCount() - 1; j++) {
                DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) node.getChildAt(j);
                if (!prevNode.isLeaf() && child.isLeaf()) {
                    node.insert(child, j);
                    node.insert(prevNode, i);
                }
            }
        }
        return node;
    }
}

Related

  1. recursiveTreeNode(DefaultMutableTreeNode node)
  2. refreshNode(final JTree tree, final TreeNode node)
  3. RemoveChildren(DefaultMutableTreeNode node)
  4. searchForNode(TreeNode node, String query)
  5. setTreeSelectedNode(JTree tree, TreeNode node)
  6. sortNode(DefaultMutableTreeNode parent, Comparator comparator)
  7. sortTreeDepthFirst(DefaultMutableTreeNode root, Comparator comparator)
  8. sortTreeNode(DefaultMutableTreeNode root)
  9. sortTreeNodesAlphaNumeric(final DefaultMutableTreeNode node)