@@ -48,6 +48,7 @@ open class FileTreeNode() {
4848
4949 companion object {
5050
51+ var packageNameToDirs = true
5152 private val TAG = FileTreeNode ::class .java.simpleName
5253 private val sPathSplitPattern = Pattern .compile(" [./]" )
5354 private val sPlaceholderPattern = Pattern .compile(" \\ $\\ {([A-Za-z0-9_\\ -]+)}" )
@@ -76,12 +77,19 @@ open class FileTreeNode() {
7677 fun removeFromParent (): Boolean {
7778 if (parent != null ) {
7879 parent!! .labeledChildren.remove(getLabel())
79- parent!! .children.remove(this )
80+ parent!! .realChildren.remove(this )
81+ parent = null
8082 return true
8183 }
8284 return false
8385 }
8486
87+ /* *
88+ * add child to this node
89+ *
90+ * @param child The child need be add
91+ * @param override Weather override the node with the same name and type
92+ */
8593 fun addChild (child : FileTreeNode , override : Boolean = false): Boolean {
8694 if (hasChild(child.getLabel())) {
8795 if (override ) {
@@ -92,13 +100,13 @@ open class FileTreeNode() {
92100 }
93101 }
94102 child.parent = this
95- children .add(child)
103+ realChildren .add(child)
96104 labeledChildren[child.getLabel()] = child
97105 return true
98106 }
99107
100108 fun hasChild (name : String , isDir : Boolean ): Boolean {
101- children .forEach {
109+ realChildren .forEach {
102110 if (it.name == name && isDir == it.isDir) {
103111 return true
104112 }
@@ -122,7 +130,6 @@ open class FileTreeNode() {
122130 }
123131
124132 fun getRealName (fileName : String = this.name): String {
125-
126133 return fileName.replacePlaceholder(getPlaceholderInherit(), ! isDir)
127134 }
128135
@@ -190,7 +197,7 @@ open class FileTreeNode() {
190197 fun traversal (block : (it: FileTreeNode , depth: Int ) -> Unit , depth : Int = 0) {
191198 if (! isDir) return
192199
193- children .forEach {
200+ realChildren .forEach {
194201 block(it, depth)
195202 it.traversal(block, depth + 1 )
196203 }
@@ -238,43 +245,60 @@ open class FileTreeNode() {
238245 return this == parent || parent == null
239246 }
240247
248+ /* *
249+ * build file tree
250+ * replace placeholder in name
251+ * if this is directory and name is a path or package name
252+ * the name will expand to several dirs
253+ */
241254 fun build () {
242255
243- name = getRealName()
244256 if (! isDir) {
245257 return
246258 }
259+ name = getRealName()
247260 if (! isRoot()) {
248- val dir = name.split(sPathSplitPattern).toMutableList()
261+ val dir = if (packageNameToDirs) {
262+ name.split(sPathSplitPattern)
263+ } else {
264+ name.split(" /" )
265+ }.filter {
266+ it.isNotBlank()
267+ }.toMutableList()
268+
249269 expandDirs(dir)
250270 }
251- children .forEach {
271+ realChildren .forEach {
252272 it.build()
253273 }
254274 }
255275
256- private fun expandDirs (dirs : List <String >) {
276+ /* *
277+ * expand the dir list
278+ * all the children of this will move the new dir
279+ */
280+ private fun expandDirs (dirs : MutableList <String >) {
257281 if (dirs.isEmpty() || dirs.size == 1 ) {
258282 return
259283 }
260284 this .name = dirs.first()
261- dirs.drop (0 )
285+ dirs.removeAt (0 )
262286
263- var node: FileTreeNode ? = null
264287 var preNode: FileTreeNode = this
265288 var newChild: FileTreeNode
289+
290+ val oldChildren = realChildren.toMutableList()
291+ oldChildren.forEach {
292+ it.removeFromParent()
293+ }
266294 dirs.forEach {
267295 newChild = FileTreeNode (preNode, it, true )
268- if (node == null ) {
269- node = newChild
270- }
271296 preNode.addChild(newChild)
272297 preNode = newChild
273298 }
274- preNode.children = children
275- children.clear()
276- labeledChildren.clear()
277- realChildren.clear()
299+ oldChildren.forEach {
300+ preNode.addChild(it)
301+ }
278302 }
279303
280304 fun getAllPlaceholderInTree (): List <String > {
@@ -297,6 +321,11 @@ open class FileTreeNode() {
297321 return result
298322 }
299323
324+ /* *
325+ * get the tree graph of node inherit
326+ *
327+ * @return The tree graph of node
328+ */
300329 fun getTreeGraph (): String {
301330 return getNodeGraph().toString()
302331 }
0 commit comments