Java JTree Node copyDMTreeNode(DefaultMutableTreeNode out, DefaultMutableTreeNode in)

Here you can find the source of copyDMTreeNode(DefaultMutableTreeNode out, DefaultMutableTreeNode in)

Description

Makes a deep copy of a gives DMTreeNode.

License

Apache License

Parameter

Parameter Description
out DeepCopy of TreeNode
in Input TreeNode

Declaration

public static void copyDMTreeNode(DefaultMutableTreeNode out,
        DefaultMutableTreeNode in) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w.  ja  va2s .c  o  m*/
 * Created on 26.04.2005
 *
 * Copyright 2005-2006 Daniel Jacobi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

import javax.swing.tree.*;

public class Main {
    /**
     * Makes a deep copy of a gives DMTreeNode. The new TreeNode is constructed 
     * below the new root <i>out</i>.
     * 
     * @param out DeepCopy of TreeNode
     * @param in Input TreeNode
     */
    public static void copyDMTreeNode(DefaultMutableTreeNode out,
            DefaultMutableTreeNode in) {
        int curDepth = 0;
        DefaultMutableTreeNode cur = null;
        DefaultMutableTreeNode new_node = null;
        DefaultMutableTreeNode curDepthNode = null;

        // skip old root
        cur = in;
        curDepth = cur.getLevel();
        curDepthNode = out;

        // reconstruct tree
        cur = (DefaultMutableTreeNode) cur.getNextNode();
        while (cur != null) {
            new_node = (DefaultMutableTreeNode) cur.clone();

            if (cur.getLevel() > curDepth) { // new child
                curDepthNode.add(new_node);
            } else if (cur.getLevel() < curDepth) { // new Parent
                ((DefaultMutableTreeNode) curDepthNode.getParent()
                        .getParent()).add(new_node);
            } else { // new 'brother'
                ((DefaultMutableTreeNode) curDepthNode.getParent())
                        .add(new_node);
            }

            // save last node info
            curDepthNode = new_node;
            curDepth = cur.getLevel();

            // next node
            cur = (DefaultMutableTreeNode) cur.getNextNode();
        }
    }
}

Related

  1. addCloneNode(DefaultMutableTreeNode srcNode, DefaultMutableTreeNode root)
  2. ApplyFilter(DefaultMutableTreeNode node, String filter)
  3. cloneTreeNode(DefaultMutableTreeNode srcNode)
  4. collapseSubTree(JTree tree, DefaultMutableTreeNode startNode, DefaultTreeModel model)
  5. contains(final JTree tree, final TreeNode node)
  6. createFolderNode(Object userObject)
  7. createSubTreeIfNecessary(TreeModel model, DefaultMutableTreeNode parent, String childName)
  8. expandAll(JTree tree, DefaultMutableTreeNode root)
  9. expandAll(JTree tree, DefaultMutableTreeNode treeNode)