Skip to content

Commit

Permalink
Added new inspection for debugging net state
Browse files Browse the repository at this point in the history
  • Loading branch information
yrke committed Jun 29, 2023
1 parent 0be30f1 commit 403c28a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/net/tapaal/gui/debug/Debug.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ object DEBUG {

}
})

add(object : AbstractAction("Inspect") {
override fun actionPerformed(e: ActionEvent) = InspectSpy().show()
})
}

return debugMenu
Expand Down
85 changes: 85 additions & 0 deletions src/main/java/net/tapaal/gui/debug/InspectSpy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package net.tapaal.gui.debug

import net.tapaal.gui.petrinet.editor.ConstantsPane
import pipe.gui.TAPAALGUI
import pipe.gui.petrinet.PetriNetTab
import java.awt.event.ActionEvent
import javax.swing.*
import javax.swing.tree.DefaultMutableTreeNode
import javax.swing.tree.DefaultTreeModel
import kotlin.reflect.KClass

class InspectSpy : JFrame() {

private val reloadBtn = JButton(object : AbstractAction("Reload") {
override fun actionPerformed(e: ActionEvent?) {
reload()
}
})

private val treeRoot = DefaultMutableTreeNode("root")
private val tree = JTree(treeRoot)

private fun reload() {
val m = tree.model as DefaultTreeModel
treeRoot.removeAllChildren()

val global = generateGlobal()
treeRoot.add(global)

m.reload()
tree.expandRow(0)
}

private fun <T : Any> T.getPrivateField(
field: String,
): Any {
val ref = this.javaClass.getDeclaredField(field)
ref.isAccessible = true
return ref.get(this)
}

private fun generateGlobal(): DefaultMutableTreeNode {
val global = DefaultMutableTreeNode("Global")

val constantsPane = TAPAALGUI.getCurrentTab().getPrivateField("constantsPanel") as ConstantsPane

val constants = DefaultMutableTreeNode("Constants")
val constantModel = constantsPane.getPrivateField("variablesListModel") as AbstractListModel<*>
constantModel.forEach { constants.add(DefaultMutableTreeNode(it)) }
global.add(constants)

val variables = DefaultMutableTreeNode("Variables")
val variableModel = constantsPane.getPrivateField("constantsListModel") as AbstractListModel<*>
variableModel.forEach { variables.add(DefaultMutableTreeNode(it)) }
global.add(variables)

val colorTypes = DefaultMutableTreeNode("Color Types")
val colorTypesModel = constantsPane.getPrivateField("colorTypesListModel") as AbstractListModel<*>
colorTypesModel.forEach { colorTypes.add(DefaultMutableTreeNode(it)) }
global.add(colorTypes)


return global
}


init {
contentPane.layout = BoxLayout(contentPane, BoxLayout.Y_AXIS)
contentPane.add(reloadBtn)

contentPane.add(JScrollPane(tree))

reload()
pack()

}


}

private fun <E> AbstractListModel<E>.forEach(function: (e: E)->Unit) {
for (i in 0 until this.size) {
function(this.getElementAt(i))
}
}

0 comments on commit 403c28a

Please sign in to comment.