]>
Commit | Line | Data |
---|---|---|
a13899c5 | 1 | /** @file\r |
2 | \r | |
3 | The file is used to override JTree to provides customized interfaces \r | |
4 | \r | |
5 | Copyright (c) 2006, Intel Corporation\r | |
6 | All rights reserved. This program and the accompanying materials\r | |
7 | are licensed and made available under the terms and conditions of the BSD License\r | |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | package org.tianocore.frameworkwizard.common.ui;\r | |
17 | \r | |
18 | import javax.swing.JTree;\r | |
19 | import javax.swing.tree.DefaultMutableTreeNode;\r | |
20 | import javax.swing.tree.DefaultTreeModel;\r | |
21 | import javax.swing.tree.TreeNode;\r | |
22 | import javax.swing.tree.TreePath;\r | |
23 | \r | |
79cb6fdb | 24 | import org.tianocore.frameworkwizard.common.Identifications.Identification;\r |
a13899c5 | 25 | \r |
26 | /**\r | |
27 | The class is used to override JTree to provides customized interfaces \r | |
28 | It extends JTree\r | |
a13899c5 | 29 | \r |
30 | **/\r | |
31 | public class ITree extends JTree {\r | |
32 | ///\r | |
33 | /// Define class Serial Version UID\r | |
34 | ///\r | |
35 | private static final long serialVersionUID = -7907086164518295327L;\r | |
09ef9242 | 36 | \r |
a13899c5 | 37 | //\r |
38 | // Define class members\r | |
39 | //\r | |
40 | DefaultTreeModel treeModel = null;\r | |
41 | \r | |
42 | /**\r | |
43 | This is the default constructor\r | |
44 | \r | |
45 | **/\r | |
46 | public ITree() {\r | |
47 | super();\r | |
48 | }\r | |
49 | \r | |
50 | /**\r | |
51 | This is the overrided constructor\r | |
52 | Init class members with input data\r | |
53 | \r | |
54 | @param iDmtRoot The root node of the tree\r | |
55 | \r | |
56 | **/\r | |
57 | public ITree(IDefaultMutableTreeNode iDmtRoot) {\r | |
58 | super(iDmtRoot);\r | |
09ef9242 | 59 | treeModel = (DefaultTreeModel) this.getModel();\r |
a13899c5 | 60 | }\r |
61 | \r | |
62 | /**\r | |
63 | Get category of selected node\r | |
64 | \r | |
65 | @return The category of selected node\r | |
66 | \r | |
67 | **/\r | |
68 | public int getSelectCategory() {\r | |
69 | int intCategory = 0;\r | |
70 | TreePath path = this.getSelectionPath();\r | |
71 | IDefaultMutableTreeNode node = (IDefaultMutableTreeNode) path.getLastPathComponent();\r | |
72 | intCategory = node.getCategory();\r | |
73 | return intCategory;\r | |
74 | }\r | |
75 | \r | |
a13899c5 | 76 | /**\r |
77 | Add input node as child node for current selected node\r | |
78 | \r | |
79 | @param strNewNode The name of the node which need be added\r | |
80 | \r | |
81 | **/\r | |
82 | public void addNode(String strNewNode) {\r | |
83 | DefaultMutableTreeNode parentNode = null;\r | |
84 | DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(strNewNode);\r | |
85 | newNode.setAllowsChildren(true);\r | |
86 | TreePath parentPath = this.getSelectionPath();\r | |
87 | \r | |
88 | /**\r | |
89 | * Get parent node of new node\r | |
90 | */\r | |
91 | parentNode = (DefaultMutableTreeNode) (parentPath.getLastPathComponent());\r | |
92 | \r | |
93 | /**\r | |
94 | * Insert new node\r | |
95 | */\r | |
96 | treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());\r | |
97 | this.scrollPathToVisible(new TreePath(newNode.getPath()));\r | |
98 | }\r | |
99 | \r | |
100 | /**\r | |
101 | Add input node as child node for current selected node\r | |
102 | \r | |
103 | @param newNode The node need be added\r | |
104 | \r | |
105 | **/\r | |
106 | public void addNode(IDefaultMutableTreeNode newNode) {\r | |
107 | IDefaultMutableTreeNode parentNode = null;\r | |
108 | newNode.setAllowsChildren(true);\r | |
109 | TreePath parentPath = this.getSelectionPath();\r | |
110 | parentNode = (IDefaultMutableTreeNode) (parentPath.getLastPathComponent());\r | |
111 | treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());\r | |
112 | this.scrollPathToVisible(new TreePath(newNode.getPath()));\r | |
113 | }\r | |
09ef9242 | 114 | \r |
a13899c5 | 115 | /**\r |
09ef9242 | 116 | Add input node as child node for current selected node\r |
117 | \r | |
118 | @param newNode The node need be added\r | |
119 | \r | |
120 | **/\r | |
121 | public void addNode(IDefaultMutableTreeNode parentNode, IDefaultMutableTreeNode newNode) {\r | |
122 | treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());\r | |
123 | this.scrollPathToVisible(new TreePath(newNode.getPath()));\r | |
124 | }\r | |
a13899c5 | 125 | \r |
126 | /**\r | |
127 | Remove the selected node\r | |
128 | \r | |
129 | @param strRemovedNode\r | |
130 | \r | |
131 | **/\r | |
132 | public void removeSelectedNode() {\r | |
133 | TreePath treePath = this.getSelectionPath();\r | |
134 | removeNodeByPath(treePath);\r | |
135 | }\r | |
09ef9242 | 136 | \r |
a13899c5 | 137 | /**\r |
09ef9242 | 138 | Remove the node by tree path\r |
139 | \r | |
140 | @param strRemovedNode\r | |
141 | \r | |
142 | **/\r | |
a13899c5 | 143 | public void removeNodeByPath(TreePath treePath) {\r |
144 | if (treePath != null) {\r | |
145 | DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();\r | |
146 | TreeNode parent = (TreeNode) selectionNode.getParent();\r | |
147 | if (parent != null) {\r | |
148 | treeModel.removeNodeFromParent(selectionNode);\r | |
149 | }\r | |
150 | }\r | |
151 | }\r | |
152 | \r | |
153 | /**\r | |
09ef9242 | 154 | Return a node by input tree path\r |
155 | \r | |
156 | @param treePath\r | |
157 | @return\r | |
a13899c5 | 158 | \r |
159 | **/\r | |
09ef9242 | 160 | public IDefaultMutableTreeNode getNodeByPath(TreePath treePath) {\r |
161 | if (treePath != null) {\r | |
162 | IDefaultMutableTreeNode selectionNode = (IDefaultMutableTreeNode) treePath.getLastPathComponent();\r | |
163 | return selectionNode;\r | |
164 | }\r | |
165 | return null;\r | |
166 | }\r | |
167 | \r | |
168 | /**\r | |
169 | Remove all child nodes under current node\r | |
170 | \r | |
171 | **/\r | |
a13899c5 | 172 | public void removeNodeChildrenByPath(TreePath treePath) {\r |
173 | if (treePath != null) {\r | |
174 | DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();\r | |
175 | for (int index = currentNode.getChildCount() - 1; index > -1; index--) {\r | |
09ef9242 | 176 | treeModel.removeNodeFromParent((DefaultMutableTreeNode) currentNode.getChildAt(index));\r |
a13899c5 | 177 | }\r |
178 | }\r | |
179 | }\r | |
180 | \r | |
181 | /**\r | |
182 | Remove all nodes of the tree\r | |
183 | \r | |
184 | **/\r | |
185 | public void removeAllNode() {\r | |
186 | DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) treeModel.getRoot();\r | |
187 | rootNode.removeAllChildren();\r | |
188 | treeModel.reload();\r | |
189 | }\r | |
09ef9242 | 190 | \r |
a13899c5 | 191 | public IDefaultMutableTreeNode getSelectNode() {\r |
192 | TreePath treepath = this.getSelectionPath();\r | |
193 | IDefaultMutableTreeNode selectionNode = null;\r | |
194 | if (treepath != null) {\r | |
195 | selectionNode = (IDefaultMutableTreeNode) treepath.getLastPathComponent();\r | |
196 | }\r | |
197 | return selectionNode;\r | |
198 | }\r | |
09ef9242 | 199 | \r |
a13899c5 | 200 | public IDefaultMutableTreeNode getNodeById(IDefaultMutableTreeNode node, Identification id) {\r |
201 | for (int index = 0; index < node.getChildCount(); index++) {\r | |
202 | IDefaultMutableTreeNode iNode = (IDefaultMutableTreeNode) node.getChildAt(index);\r | |
203 | if (iNode.getId().equals(id)) {\r | |
204 | return iNode;\r | |
205 | }\r | |
206 | }\r | |
207 | return null;\r | |
208 | }\r | |
09ef9242 | 209 | \r |
a13899c5 | 210 | public IDefaultMutableTreeNode getNodeById(IDefaultMutableTreeNode node, Identification id, int category) {\r |
211 | for (int index = 0; index < node.getChildCount(); index++) {\r | |
212 | IDefaultMutableTreeNode iNode = (IDefaultMutableTreeNode) node.getChildAt(index);\r | |
213 | if (iNode.getId().equals(id) && iNode.getCategory() == category) {\r | |
214 | return iNode;\r | |
215 | }\r | |
92e29378 | 216 | IDefaultMutableTreeNode childNode = getNodeById(iNode, id, category);\r |
217 | if (childNode != null) {\r | |
218 | return childNode;\r | |
219 | }\r | |
a13899c5 | 220 | }\r |
221 | return null;\r | |
222 | }\r | |
09ef9242 | 223 | \r |
a13899c5 | 224 | public TreePath getPathOfNode(IDefaultMutableTreeNode node) {\r |
92e29378 | 225 | if (node != null) {\r |
226 | TreePath treePath = new TreePath(treeModel.getPathToRoot(node));\r | |
09ef9242 | 227 | return treePath;\r |
92e29378 | 228 | }\r |
229 | return null;\r | |
a13899c5 | 230 | }\r |
231 | }\r |