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
33 public class ITree
extends JTree
{
35 /// Define class Serial Version UID
37 private static final long serialVersionUID
= -7907086164518295327L;
40 // Define class members
42 DefaultTreeModel treeModel
= null;
45 This is the default constructor
53 This is the overrided constructor
54 Init class members with input data
56 @param iDmtRoot The root node of the tree
59 public ITree(IDefaultMutableTreeNode iDmtRoot
) {
61 treeModel
= (DefaultTreeModel
)this.getModel();
65 Get category of selected node
67 @return The category of selected node
70 public int getSelectCategory() {
72 TreePath path
= this.getSelectionPath();
73 IDefaultMutableTreeNode node
= (IDefaultMutableTreeNode
) path
.getLastPathComponent();
74 intCategory
= node
.getCategory();
79 Get operation of selected node
81 @return The operation of selected node
84 public int getSelectOperation() {
86 TreePath path
= this.getSelectionPath();
87 IDefaultMutableTreeNode node
= (IDefaultMutableTreeNode
) path
.getLastPathComponent();
88 intOperation
= node
.getOperation();
93 Get selectLoaction of selected node
95 @return The selectLoaction of selected node
98 public int getSelectLoaction() {
100 TreePath path
= this.getSelectionPath();
101 IDefaultMutableTreeNode node
= (IDefaultMutableTreeNode
) path
.getLastPathComponent();
102 intLocation
= node
.getLocation();
107 Main class, reserved for test
112 public static void main(String
[] args
) {
113 // TODO Auto-generated method stub
117 Add input node as child node for current selected node
119 @param strNewNode The name of the node which need be added
122 public void addNode(String strNewNode
) {
123 DefaultMutableTreeNode parentNode
= null;
124 DefaultMutableTreeNode newNode
= new DefaultMutableTreeNode(strNewNode
);
125 newNode
.setAllowsChildren(true);
126 TreePath parentPath
= this.getSelectionPath();
129 * Get parent node of new node
131 parentNode
= (DefaultMutableTreeNode
) (parentPath
.getLastPathComponent());
136 treeModel
.insertNodeInto(newNode
, parentNode
, parentNode
.getChildCount());
137 this.scrollPathToVisible(new TreePath(newNode
.getPath()));
141 Add input node as child node for current selected node
143 @param newNode The node need be added
146 public void addNode(IDefaultMutableTreeNode newNode
) {
147 IDefaultMutableTreeNode parentNode
= null;
148 newNode
.setAllowsChildren(true);
149 TreePath parentPath
= this.getSelectionPath();
150 parentNode
= (IDefaultMutableTreeNode
) (parentPath
.getLastPathComponent());
151 treeModel
.insertNodeInto(newNode
, parentNode
, parentNode
.getChildCount());
152 this.scrollPathToVisible(new TreePath(newNode
.getPath()));
156 Add input node as child node for current selected node
158 @param newNode The node need be added
161 public void addNode(IDefaultMutableTreeNode parentNode
, IDefaultMutableTreeNode newNode
) {
162 treeModel
.insertNodeInto(newNode
, parentNode
, parentNode
.getChildCount());
163 this.scrollPathToVisible(new TreePath(newNode
.getPath()));
167 Remove the selected node
169 @param strRemovedNode
172 public void removeSelectedNode() {
173 TreePath treePath
= this.getSelectionPath();
174 removeNodeByPath(treePath
);
178 Remove the node by tree path
180 @param strRemovedNode
183 public void removeNodeByPath(TreePath treePath
) {
184 if (treePath
!= null) {
185 DefaultMutableTreeNode selectionNode
= (DefaultMutableTreeNode
) treePath
.getLastPathComponent();
186 TreeNode parent
= (TreeNode
) selectionNode
.getParent();
187 if (parent
!= null) {
188 treeModel
.removeNodeFromParent(selectionNode
);
194 Remove all child nodes under current node
197 public void removeNodeChildrenByPath(TreePath treePath
) {
198 if (treePath
!= null) {
199 DefaultMutableTreeNode currentNode
= (DefaultMutableTreeNode
) treePath
.getLastPathComponent();
200 for (int index
= currentNode
.getChildCount() - 1; index
> -1; index
--) {
201 treeModel
.removeNodeFromParent((DefaultMutableTreeNode
)currentNode
.getChildAt(index
));
207 Remove all nodes of the tree
210 public void removeAllNode() {
211 DefaultMutableTreeNode rootNode
= (DefaultMutableTreeNode
) treeModel
.getRoot();
212 rootNode
.removeAllChildren();
216 public IDefaultMutableTreeNode
getSelectNode() {
217 TreePath treepath
= this.getSelectionPath();
218 IDefaultMutableTreeNode selectionNode
= null;
219 if (treepath
!= null) {
220 selectionNode
= (IDefaultMutableTreeNode
) treepath
.getLastPathComponent();
222 return selectionNode
;
225 public IDefaultMutableTreeNode
getNodeById(IDefaultMutableTreeNode node
, Identification id
) {
226 for (int index
= 0; index
< node
.getChildCount(); index
++) {
227 IDefaultMutableTreeNode iNode
= (IDefaultMutableTreeNode
) node
.getChildAt(index
);
228 if (iNode
.getId().equals(id
)) {
235 public IDefaultMutableTreeNode
getNodeById(IDefaultMutableTreeNode node
, Identification id
, int category
) {
236 for (int index
= 0; index
< node
.getChildCount(); index
++) {
237 IDefaultMutableTreeNode iNode
= (IDefaultMutableTreeNode
) node
.getChildAt(index
);
238 if (iNode
.getId().equals(id
) && iNode
.getCategory() == category
) {
241 IDefaultMutableTreeNode childNode
= getNodeById(iNode
, id
, category
);
242 if (childNode
!= null) {
249 public TreePath
getPathOfNode(IDefaultMutableTreeNode node
) {
251 TreePath treePath
= new TreePath(treeModel
.getPathToRoot(node
));