3 The file is used to override JTree to provides customized interfaces
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
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.
16 package org
.tianocore
.frameworkwizard
.common
.ui
;
18 import javax
.swing
.JTree
;
19 import javax
.swing
.tree
.DefaultMutableTreeNode
;
20 import javax
.swing
.tree
.DefaultTreeModel
;
21 import javax
.swing
.tree
.TreeNode
;
22 import javax
.swing
.tree
.TreePath
;
24 import org
.tianocore
.frameworkwizard
.common
.Identifications
.Identification
;
27 The class is used to override JTree to provides customized interfaces
31 public class ITree
extends JTree
{
33 /// Define class Serial Version UID
35 private static final long serialVersionUID
= -7907086164518295327L;
38 // Define class members
40 DefaultTreeModel treeModel
= null;
43 This is the default constructor
51 This is the overrided constructor
52 Init class members with input data
54 @param iDmtRoot The root node of the tree
57 public ITree(IDefaultMutableTreeNode iDmtRoot
) {
59 treeModel
= (DefaultTreeModel
) this.getModel();
63 Get category of selected node
65 @return The category of selected node
68 public int getSelectCategory() {
70 TreePath path
= this.getSelectionPath();
71 IDefaultMutableTreeNode node
= (IDefaultMutableTreeNode
) path
.getLastPathComponent();
72 intCategory
= node
.getCategory();
77 Add input node as child node for current selected node
79 @param strNewNode The name of the node which need be added
82 public void addNode(String strNewNode
) {
83 DefaultMutableTreeNode parentNode
= null;
84 DefaultMutableTreeNode newNode
= new DefaultMutableTreeNode(strNewNode
);
85 newNode
.setAllowsChildren(true);
86 TreePath parentPath
= this.getSelectionPath();
89 * Get parent node of new node
91 parentNode
= (DefaultMutableTreeNode
) (parentPath
.getLastPathComponent());
96 treeModel
.insertNodeInto(newNode
, parentNode
, parentNode
.getChildCount());
97 this.scrollPathToVisible(new TreePath(newNode
.getPath()));
101 Add input node as child node for current selected node
103 @param newNode The node need be added
106 public void addNode(IDefaultMutableTreeNode newNode
) {
107 IDefaultMutableTreeNode parentNode
= null;
108 newNode
.setAllowsChildren(true);
109 TreePath parentPath
= this.getSelectionPath();
110 parentNode
= (IDefaultMutableTreeNode
) (parentPath
.getLastPathComponent());
111 treeModel
.insertNodeInto(newNode
, parentNode
, parentNode
.getChildCount());
112 this.scrollPathToVisible(new TreePath(newNode
.getPath()));
116 Add input node as child node for current selected node
118 @param newNode The node need be added
121 public void addNode(IDefaultMutableTreeNode parentNode
, IDefaultMutableTreeNode newNode
) {
122 treeModel
.insertNodeInto(newNode
, parentNode
, parentNode
.getChildCount());
123 this.scrollPathToVisible(new TreePath(newNode
.getPath()));
127 Remove the selected node
129 @param strRemovedNode
132 public void removeSelectedNode() {
133 TreePath treePath
= this.getSelectionPath();
134 removeNodeByPath(treePath
);
138 Remove the node by tree path
140 @param strRemovedNode
143 public void removeNodeByPath(TreePath treePath
) {
144 if (treePath
!= null) {
145 DefaultMutableTreeNode selectionNode
= (DefaultMutableTreeNode
) treePath
.getLastPathComponent();
146 TreeNode parent
= (TreeNode
) selectionNode
.getParent();
147 if (parent
!= null) {
148 treeModel
.removeNodeFromParent(selectionNode
);
154 Return a node by input tree path
160 public IDefaultMutableTreeNode
getNodeByPath(TreePath treePath
) {
161 if (treePath
!= null) {
162 IDefaultMutableTreeNode selectionNode
= (IDefaultMutableTreeNode
) treePath
.getLastPathComponent();
163 return selectionNode
;
169 Remove all child nodes under current node
172 public void removeNodeChildrenByPath(TreePath treePath
) {
173 if (treePath
!= null) {
174 DefaultMutableTreeNode currentNode
= (DefaultMutableTreeNode
) treePath
.getLastPathComponent();
175 for (int index
= currentNode
.getChildCount() - 1; index
> -1; index
--) {
176 treeModel
.removeNodeFromParent((DefaultMutableTreeNode
) currentNode
.getChildAt(index
));
182 Remove all nodes of the tree
185 public void removeAllNode() {
186 DefaultMutableTreeNode rootNode
= (DefaultMutableTreeNode
) treeModel
.getRoot();
187 rootNode
.removeAllChildren();
191 public IDefaultMutableTreeNode
getSelectNode() {
192 TreePath treepath
= this.getSelectionPath();
193 IDefaultMutableTreeNode selectionNode
= null;
194 if (treepath
!= null) {
195 selectionNode
= (IDefaultMutableTreeNode
) treepath
.getLastPathComponent();
197 return selectionNode
;
200 public IDefaultMutableTreeNode
getNodeById(IDefaultMutableTreeNode node
, Identification id
) {
201 for (int index
= 0; index
< node
.getChildCount(); index
++) {
202 IDefaultMutableTreeNode iNode
= (IDefaultMutableTreeNode
) node
.getChildAt(index
);
203 if (iNode
.getId().equals(id
)) {
210 public IDefaultMutableTreeNode
getNodeById(IDefaultMutableTreeNode node
, Identification id
, int category
) {
211 for (int index
= 0; index
< node
.getChildCount(); index
++) {
212 IDefaultMutableTreeNode iNode
= (IDefaultMutableTreeNode
) node
.getChildAt(index
);
213 if (iNode
.getId().equals(id
) && iNode
.getCategory() == category
) {
216 IDefaultMutableTreeNode childNode
= getNodeById(iNode
, id
, category
);
217 if (childNode
!= null) {
224 public TreePath
getPathOfNode(IDefaultMutableTreeNode node
) {
226 TreePath treePath
= new TreePath(treeModel
.getPathToRoot(node
));