e54c7f0b6c21ff20a33118001d8a1bf0c0926ea2
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / platform / ui / DynamicTree.java
1 /** @file
2
3 The file is used to create tree view sections
4
5 Copyright (c) 2006, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15 package org.tianocore.frameworkwizard.platform.ui;
16 import java.awt.GridLayout;
17 import java.awt.Toolkit;
18
19 import javax.swing.ImageIcon;
20 import javax.swing.JPanel;
21 import javax.swing.JScrollPane;
22 import javax.swing.JTree;
23 import javax.swing.tree.DefaultMutableTreeNode;
24 import javax.swing.tree.DefaultTreeCellRenderer;
25 import javax.swing.tree.DefaultTreeModel;
26 import javax.swing.tree.MutableTreeNode;
27 import javax.swing.tree.TreePath;
28 import javax.swing.tree.TreeSelectionModel;
29 import javax.swing.event.TreeModelEvent;
30 import javax.swing.event.TreeModelListener;
31
32
33 public class DynamicTree extends JPanel {
34 /**
35 *
36 */
37 private static final long serialVersionUID = 1L;
38 protected DefaultMutableTreeNode rootNode;
39 protected DefaultTreeModel treeModel;
40 protected JTree tree;
41 private Toolkit toolkit = Toolkit.getDefaultToolkit();
42
43 public DynamicTree() {
44 super(new GridLayout(1,0));
45
46 rootNode = new DefaultMutableTreeNode("Sections");
47 treeModel = new DefaultTreeModel(rootNode);
48 treeModel.addTreeModelListener(new MyTreeModelListener());
49
50 tree = new JTree(treeModel);
51 DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
52 renderer.setOpenIcon(new ImageIcon("junk.gif"));
53 renderer.setClosedIcon(new ImageIcon("junk.gif"));
54 renderer.setLeafIcon(new ImageIcon("junk.gif"));
55 tree.setEditable(true);
56 tree.getSelectionModel().setSelectionMode
57 (TreeSelectionModel.SINGLE_TREE_SELECTION);
58 tree.setShowsRootHandles(true);
59
60 JScrollPane scrollPane = new JScrollPane(tree);
61 add(scrollPane);
62 }
63
64 /** Remove all nodes except the root node. */
65 public void clear() {
66 rootNode.removeAllChildren();
67 treeModel.reload();
68 }
69
70 public String getCurrentNodeText() {
71 DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
72 if (currentNode != null){
73 return (String)currentNode.getUserObject();
74 }
75 return null;
76 }
77 /** Remove the currently selected node. */
78 public void removeCurrentNode() {
79 TreePath currentSelection = tree.getSelectionPath();
80 if (currentSelection != null) {
81 DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)
82 (currentSelection.getLastPathComponent());
83 MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());
84 if (parent != null) {
85 treeModel.removeNodeFromParent(currentNode);
86 return;
87 }
88 }
89
90 // Either there was no selection, or the root was selected.
91 toolkit.beep();
92 }
93
94 /** Add child to the currently selected node. */
95 public DefaultMutableTreeNode addObject(Object child) {
96 DefaultMutableTreeNode parentNode = null;
97 TreePath parentPath = tree.getSelectionPath();
98
99 if (parentPath == null) {
100 parentNode = rootNode;
101 } else {
102 parentNode = (DefaultMutableTreeNode)
103 (parentPath.getLastPathComponent());
104 }
105
106 return addObject(parentNode, child, true);
107 }
108
109 public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
110 Object child) {
111 return addObject(parent, child, false);
112 }
113
114 public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
115 Object child,
116 boolean shouldBeVisible) {
117 DefaultMutableTreeNode childNode =
118 new DefaultMutableTreeNode(child);
119
120 if (parent == null) {
121 parent = rootNode;
122 }
123
124 treeModel.insertNodeInto(childNode, parent,
125 parent.getChildCount());
126
127 //Make sure the user can see the lovely new node.
128 if (shouldBeVisible) {
129 tree.scrollPathToVisible(new TreePath(childNode.getPath()));
130 }
131 return childNode;
132 }
133
134 class MyTreeModelListener implements TreeModelListener {
135 public void treeNodesChanged(TreeModelEvent e) {
136 DefaultMutableTreeNode node;
137 node = (DefaultMutableTreeNode)
138 (e.getTreePath().getLastPathComponent());
139
140 /*
141 * If the event lists children, then the changed
142 * node is the child of the node we've already
143 * gotten. Otherwise, the changed node and the
144 * specified node are the same.
145 */
146 try {
147 int index = e.getChildIndices()[0];
148 node = (DefaultMutableTreeNode)
149 (node.getChildAt(index));
150 } catch (NullPointerException exc) {}
151
152 System.out.println("The user has finished editing the node.");
153 System.out.println("New value: " + node.getUserObject());
154 }
155 public void treeNodesInserted(TreeModelEvent e) {
156 }
157 public void treeNodesRemoved(TreeModelEvent e) {
158 }
159 public void treeStructureChanged(TreeModelEvent e) {
160 }
161 }
162 }
163
164