summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/ui/ITree.java
blob: 252c34ff72a1e754572dc62850377420261bede1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/** @file
 
 The file is used to override JTree to provides customized interfaces 
 
 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.common.ui;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import org.tianocore.frameworkwizard.common.Identifications.Identification;

/**
 The class is used to override JTree to provides customized interfaces 
 It extends JTree

 **/
public class ITree extends JTree {
    ///
    /// Define class Serial Version UID
    ///
    private static final long serialVersionUID = -7907086164518295327L;

    //
    // Define class members
    //
    DefaultTreeModel treeModel = null;

    /**
     This is the default constructor
     
     **/
    public ITree() {
        super();
    }

    /**
     This is the overrided constructor
     Init class members with input data
     
     @param iDmtRoot The root node of the tree
     
     **/
    public ITree(IDefaultMutableTreeNode iDmtRoot) {
        super(iDmtRoot);
        treeModel = (DefaultTreeModel) this.getModel();
    }

    /**
     Get category of selected node
     
     @return The category of selected node
     
     **/
    public int getSelectCategory() {
        int intCategory = 0;
        TreePath path = this.getSelectionPath();
        IDefaultMutableTreeNode node = (IDefaultMutableTreeNode) path.getLastPathComponent();
        intCategory = node.getCategory();
        return intCategory;
    }

    /**
     Add input node as child node for current selected node
     
     @param strNewNode The name of the node which need be added
     
     **/
    public void addNode(String strNewNode) {
        DefaultMutableTreeNode parentNode = null;
        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(strNewNode);
        newNode.setAllowsChildren(true);
        TreePath parentPath = this.getSelectionPath();

        /**
         * Get parent node of new node
         */
        parentNode = (DefaultMutableTreeNode) (parentPath.getLastPathComponent());

        /**
         * Insert new node
         */
        treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());
        this.scrollPathToVisible(new TreePath(newNode.getPath()));
    }

    /**
     Add input node as child node for current selected node
     
     @param newNode The node need be added
     
     **/
    public void addNode(IDefaultMutableTreeNode newNode) {
        IDefaultMutableTreeNode parentNode = null;
        newNode.setAllowsChildren(true);
        TreePath parentPath = this.getSelectionPath();
        parentNode = (IDefaultMutableTreeNode) (parentPath.getLastPathComponent());
        treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());
        this.scrollPathToVisible(new TreePath(newNode.getPath()));
    }

    /**
     Add input node as child node for current selected node
     
     @param newNode The node need be added
     
     **/
    public void addNode(IDefaultMutableTreeNode parentNode, IDefaultMutableTreeNode newNode) {
        treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());
        this.scrollPathToVisible(new TreePath(newNode.getPath()));
    }

    /**
     Remove the selected node
     
     @param strRemovedNode
     
     **/
    public void removeSelectedNode() {
        TreePath treePath = this.getSelectionPath();
        removeNodeByPath(treePath);
    }

    /**
     Remove the node by tree path
     
     @param strRemovedNode
     
     **/
    public void removeNodeByPath(TreePath treePath) {
        if (treePath != null) {
            DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
            TreeNode parent = (TreeNode) selectionNode.getParent();
            if (parent != null) {
                treeModel.removeNodeFromParent(selectionNode);
            }
        }
    }
    
    /**
     Return a node by input tree path
    
     @param treePath
     @return
    
    **/
    public IDefaultMutableTreeNode getNodeByPath(TreePath treePath) {
        if (treePath != null) {
            IDefaultMutableTreeNode selectionNode = (IDefaultMutableTreeNode) treePath.getLastPathComponent();
            return selectionNode;
        }
        return null;
    }

    /**
     Remove all child nodes under current node
     
     **/
    public void removeNodeChildrenByPath(TreePath treePath) {
        if (treePath != null) {
            DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
            for (int index = currentNode.getChildCount() - 1; index > -1; index--) {
                treeModel.removeNodeFromParent((DefaultMutableTreeNode) currentNode.getChildAt(index));
            }
        }
    }

    /**
     Remove all nodes of the tree
     
     **/
    public void removeAllNode() {
        DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) treeModel.getRoot();
        rootNode.removeAllChildren();
        treeModel.reload();
    }

    public IDefaultMutableTreeNode getSelectNode() {
        TreePath treepath = this.getSelectionPath();
        IDefaultMutableTreeNode selectionNode = null;
        if (treepath != null) {
            selectionNode = (IDefaultMutableTreeNode) treepath.getLastPathComponent();
        }
        return selectionNode;
    }

    public IDefaultMutableTreeNode getNodeById(IDefaultMutableTreeNode node, Identification id) {
        for (int index = 0; index < node.getChildCount(); index++) {
            IDefaultMutableTreeNode iNode = (IDefaultMutableTreeNode) node.getChildAt(index);
            if (iNode.getId().equals(id)) {
                return iNode;
            }
        }
        return null;
    }

    public IDefaultMutableTreeNode getNodeById(IDefaultMutableTreeNode node, Identification id, int category) {
        for (int index = 0; index < node.getChildCount(); index++) {
            IDefaultMutableTreeNode iNode = (IDefaultMutableTreeNode) node.getChildAt(index);
            if (iNode.getId().equals(id) && iNode.getCategory() == category) {
                return iNode;
            }
            IDefaultMutableTreeNode childNode = getNodeById(iNode, id, category);
            if (childNode != null) {
                return childNode;
            }
        }
        return null;
    }

    public TreePath getPathOfNode(IDefaultMutableTreeNode node) {
        if (node != null) {
            TreePath treePath = new TreePath(treeModel.getPathToRoot(node));
            return treePath;
        }
        return null;
    }
}