Reputation: 624
I have a JTree
that I want to update its node based on a list that I have receiving from remote (some of its data is already in Tree ,so they are some repeatations)
Here is my data model that comes from remote
data class VariableInfo(
val name: String,
val type: String,
val value: String,
val children: List<VariableInfo>? = null,
)
And here is "VariableInfoTreeNode"
class VariableInfoTreeNode(
private val variableInfo: VariableInfo,
private val myParentNode: TreeNode?,
) : TreeNode {
override fun toString(): String {
return "${variableInfo.name}:${variableInfo.type} ${variableInfo.value}"
}
override fun getChildAt(childIndex: Int): TreeNode {
return _children[childIndex]
}
override fun getChildCount(): Int {
return _children.size
}
override fun getParent(): TreeNode? {
return myParentNode
}
override fun getIndex(node: TreeNode?): Int {
return _children.indexOfFirst {
if (node is VariableInfoTreeNode) {
it == node
} else {
false
}
}
}
override fun getAllowsChildren(): Boolean {
return true
}
override fun isLeaf(): Boolean {
return false
}
private val _children by lazy {
variableInfo.children?.map {
VariableInfoTreeNode(it, this)
} ?: emptyList()
}
override fun children(): Enumeration<out TreeNode> {
return Collections.enumeration(_children)
}
}
I also have a RootNode for containing VariableInfoTreeNode
nodes
class VariableInfoRootNode:TreeNode{
private var children= emptyList<VariableInfo>()
private lateinit var childrenNodes:List<VariableInfoTreeNode>
init {
refreshChildTree()
}
fun setVariableInfo(variableInfoList: List<VariableInfo>){
children=variableInfoList
refreshChildTree()
}
private fun refreshChildTree(){
childrenNodes=children.map {
VariableInfoTreeNode(it,this)
}
}
override fun getChildAt(childIndex: Int): TreeNode {
return childrenNodes[childIndex]
}
override fun getChildCount(): Int {
return childrenNodes.size
}
override fun getParent(): TreeNode? {
return null
}
override fun getIndex(node: TreeNode?): Int {
return childrenNodes.indexOfFirst {
it==node
}
}
override fun getAllowsChildren(): Boolean {
return true
}
override fun isLeaf(): Boolean {
return false
}
override fun children(): Enumeration<out TreeNode> {
return Collections.enumeration(childrenNodes)
}
}
and here is my MainTreeContainer class
class DebugBoardView : JPanel(GridLayout()) {
private val rootNode = VariableInfoRootNode()
private val treeModel = DefaultTreeModel(rootNode)
private val tree: Tree = Tree(treeModel).apply {
isRootVisible = false
}
// I call this method somewhere else and I want
// Tree to update itself and does not lose selection and expansion state
fun onVariablesUpdate(list: List<VariableInfo>) {
rootNode.setVariableInfo(list)
treeModel.nodeStructureChanged(rootNode)
}
init {
add(tree)
//periodically update with same fake data!
javax.swing.Timer(5000) {
onVariablesUpdate(fakeInfo)
}.start()
}
}
my problem is when I call DebugBoardView::onVariablesUpdate
my tree will updated but all of selected paths and expanded paths will be gone!
How can I fix that?
It's worth to mention that I am developing Intelij Idea Plugin
so any solution for this platform could be useful too
Upvotes: 2
Views: 53