/** @file
 
 The file is used to create tree view sections
 
 Copyright (c) 2006, Intel Corporation
 All rights reserved. This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php
 
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 
 **/
package org.tianocore.frameworkwizard.platform.ui;
import java.awt.GridLayout;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;


public class DynamicTree extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    protected DefaultMutableTreeNode rootNode;
    protected DefaultTreeModel treeModel;
    protected JTree tree;
    private Toolkit toolkit = Toolkit.getDefaultToolkit();

    public DynamicTree() {
        super(new GridLayout(1,0));
        
        rootNode = new DefaultMutableTreeNode("Sections");
        treeModel = new DefaultTreeModel(rootNode);
        treeModel.addTreeModelListener(new MyTreeModelListener());

        tree = new JTree(treeModel);
        DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
        renderer.setOpenIcon(new ImageIcon("junk.gif"));
        renderer.setClosedIcon(new ImageIcon("junk.gif"));
        renderer.setLeafIcon(new ImageIcon("junk.gif"));
        tree.setEditable(true);
        tree.getSelectionModel().setSelectionMode
                (TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.setShowsRootHandles(true);

        JScrollPane scrollPane = new JScrollPane(tree);
        add(scrollPane);
    }

    /** Remove all nodes except the root node. */
    public void clear() {
        rootNode.removeAllChildren();
        treeModel.reload();
    }

    public String getCurrentNodeText() {
        DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
        if (currentNode != null){
            return (String)currentNode.getUserObject();
        }
        return null;
    }
    /** Remove the currently selected node. */
    public void removeCurrentNode() {
        TreePath currentSelection = tree.getSelectionPath();
        if (currentSelection != null) {
            DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)
                         (currentSelection.getLastPathComponent());
            MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());
            if (parent != null) {
                treeModel.removeNodeFromParent(currentNode);
                return;
            }
        } 

        // Either there was no selection, or the root was selected.
        toolkit.beep();
    }

    /** Add child to the currently selected node. */
    public DefaultMutableTreeNode addObject(Object child) {
        DefaultMutableTreeNode parentNode = null;
        TreePath parentPath = tree.getSelectionPath();

        if (parentPath == null) {
            parentNode = rootNode;
        } else {
            parentNode = (DefaultMutableTreeNode)
                         (parentPath.getLastPathComponent());
        }

        return addObject(parentNode, child, true);
    }

    public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
                                            Object child) {
        return addObject(parent, child, false);
    }

    public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
                                            Object child, 
                                            boolean shouldBeVisible) {
        DefaultMutableTreeNode childNode = 
                new DefaultMutableTreeNode(child);

        if (parent == null) {
            parent = rootNode;
        }

        treeModel.insertNodeInto(childNode, parent, 
                                 parent.getChildCount());

        //Make sure the user can see the lovely new node.
        if (shouldBeVisible) {
            tree.scrollPathToVisible(new TreePath(childNode.getPath()));
        }
        return childNode;
    }

    class MyTreeModelListener implements TreeModelListener {
        public void treeNodesChanged(TreeModelEvent e) {
            DefaultMutableTreeNode node;
            node = (DefaultMutableTreeNode)
                     (e.getTreePath().getLastPathComponent());

            /*
             * If the event lists children, then the changed
             * node is the child of the node we've already
             * gotten.  Otherwise, the changed node and the
             * specified node are the same.
             */
            try {
                int index = e.getChildIndices()[0];
                node = (DefaultMutableTreeNode)
                       (node.getChildAt(index));
            } catch (NullPointerException exc) {}

            System.out.println("The user has finished editing the node.");
            System.out.println("New value: " + node.getUserObject());
        }
        public void treeNodesInserted(TreeModelEvent e) {
        }
        public void treeNodesRemoved(TreeModelEvent e) {
        }
        public void treeStructureChanged(TreeModelEvent e) {
        }
    }
}



@@ -616,63 +616,8 @@
 ut;%0A
-import java.awt.Toolkit;%0A%0Aimport javax.swing.ImageIcon;
 %0Aimp
@@ -644,40 +644,8 @@
 el;%0A
-import javax.swing.JScrollPane;%0A
 impo
@@ -753,266 +753,13 @@
 Tree
-CellRenderer;%0Aimport javax.swing.tree.DefaultTreeModel;%0Aimport javax.swing.tree.MutableTreeNode;%0Aimport javax.swing.tree.TreePath;%0Aimport javax.swing.tree.TreeSelectionModel;%0Aimport javax.swing.event.TreeModelEvent;%0Aimport javax.swing.event.TreeModelListener
+Model
 ;%0A%0A%0A
@@ -995,67 +995,8 @@
 ree;
-%0A    private Toolkit toolkit = Toolkit.getDefaultToolkit();
 %0A%0A  
@@ -1077,4301 +1077,20 @@
     
-rootNode = new DefaultMutableTreeNode(%22Sections%22);%0A        treeModel = new DefaultTreeModel(rootNode);%0A        treeModel.addTreeModelListener(new MyTreeModelListener());%0A%0A        tree = new JTree(treeModel);%0A        DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();%0A        renderer.setOpenIcon(new ImageIcon(%22junk.gif%22));%0A        renderer.setClosedIcon(new ImageIcon(%22junk.gif%22));%0A        renderer.setLeafIcon(new ImageIcon(%22junk.gif%22));%0A        tree.setEditable(true);%0A        tree.getSelectionModel().setSelectionMode%0A                (TreeSelectionModel.SINGLE_TREE_SELECTION);%0A        tree.setShowsRootHandles(true);%0A%0A        JScrollPane scrollPane = new JScrollPane(tree);%0A        add(scrollPane);%0A    %7D%0A%0A    /** Remove all nodes except the root node. */%0A    public void clear() %7B%0A        rootNode.removeAllChildren();%0A        treeModel.reload();%0A    %7D%0A%0A    public String getCurrentNodeText() %7B%0A        DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();%0A        if (currentNode != null)%7B%0A            return (String)currentNode.getUserObject();%0A        %7D%0A        return null;%0A    %7D%0A    /** Remove the currently selected node. */%0A    public void removeCurrentNode() %7B%0A        TreePath currentSelection = tree.getSelectionPath();%0A        if (currentSelection != null) %7B%0A            DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)%0A                         (currentSelection.getLastPathComponent());%0A            MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());%0A            if (parent != null) %7B%0A                treeModel.removeNodeFromParent(currentNode);%0A                return;%0A            %7D%0A        %7D %0A%0A        // Either there was no selection, or the root was selected.%0A        toolkit.beep();%0A    %7D%0A%0A    /** Add child to the currently selected node. */%0A    public DefaultMutableTreeNode addObject(Object child) %7B%0A        DefaultMutableTreeNode parentNode = null;%0A        TreePath parentPath = tree.getSelectionPath();%0A%0A        if (parentPath == null) %7B%0A            parentNode = rootNode;%0A        %7D else %7B%0A            parentNode = (DefaultMutableTreeNode)%0A                         (parentPath.getLastPathComponent());%0A        %7D%0A%0A        return addObject(parentNode, child, true);%0A    %7D%0A%0A    public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,%0A                                            Object child) %7B%0A        return addObject(parent, child, false);%0A    %7D%0A%0A    public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,%0A                                            Object child, %0A                                            boolean shouldBeVisible) %7B%0A        DefaultMutableTreeNode childNode = %0A                new DefaultMutableTreeNode(child);%0A%0A        if (parent == null) %7B%0A            parent = rootNode;%0A        %7D%0A%0A        treeModel.insertNodeInto(childNode, parent, %0A                                 parent.getChildCount());%0A%0A        //Make sure the user can see the lovely new node.%0A        if (shouldBeVisible) %7B%0A            tree.scrollPathToVisible(new TreePath(childNode.getPath()));%0A        %7D%0A        return childNode;%0A    %7D%0A%0A    class MyTreeModelListener implements TreeModelListener %7B%0A        public void treeNodesChanged(TreeModelEvent e) %7B%0A            DefaultMutableTreeNode node;%0A            node = (DefaultMutableTreeNode)%0A                     (e.getTreePath().getLastPathComponent());%0A%0A            /*%0A             * If the event lists children, then the changed%0A             * node is the child of the node we've already%0A             * gotten.  Otherwise, the changed node and the%0A             * specified node are the same.%0A             */%0A            try %7B%0A                int index = e.getChildIndices()%5B0%5D;%0A                node = (DefaultMutableTreeNode)%0A                       (node.getChildAt(index));%0A            %7D catch (NullPointerException exc) %7B%7D%0A%0A            System.out.println(%22The user has finished editing the node.%22);%0A            System.out.println(%22New value: %22 + node.getUserObject());%0A        %7D%0A        public void treeNodesInserted(TreeModelEvent e) %7B%0A        %7D%0A        public void treeNodesRemoved(TreeModelEvent e) %7B%0A        %7D%0A        public void treeStructureChanged(TreeModelEvent e) %7B%0A        %7D
 %0A    %7D
+%0A%0A    
 %0A%7D%0A%0A

remove some unused code.