1+ package com.github.xepozz.php_dump.panel
2+
3+ import com.github.xepozz.php_dump.actions.RunDumpTokensCommandAction
4+ import com.github.xepozz.php_dump.nonBlocking
5+ import com.github.xepozz.php_dump.services.OpcacheSettingsTreeDumperService
6+ import com.github.xepozz.php_dump.stubs.any_tree.AnyNodeList
7+ import com.github.xepozz.php_dump.stubs.any_tree.AnyRootNode
8+ import com.github.xepozz.php_dump.stubs.any_tree.AnyTreeStructure
9+ import com.github.xepozz.php_dump.stubs.any_tree.LeafNode
10+ import com.github.xepozz.php_dump.tree.RootNode
11+ import com.github.xepozz.php_dump.tree.TokensTreeStructure
12+ import com.intellij.ide.util.treeView.AbstractTreeStructure
13+ import com.intellij.openapi.Disposable
14+ import com.intellij.openapi.actionSystem.ActionManager
15+ import com.intellij.openapi.actionSystem.DefaultActionGroup
16+ import com.intellij.openapi.fileEditor.FileEditorManager
17+ import com.intellij.openapi.project.Project
18+ import com.intellij.openapi.ui.SimpleToolWindowPanel
19+ import com.intellij.ui.TreeUIHelper
20+ import com.intellij.ui.components.JBScrollPane
21+ import com.intellij.ui.tree.AsyncTreeModel
22+ import com.intellij.ui.tree.StructureTreeModel
23+ import com.intellij.ui.treeStructure.Tree
24+ import com.intellij.util.ui.tree.TreeUtil
25+ import kotlinx.coroutines.runBlocking
26+ import org.jdesktop.swingx.VerticalLayout
27+ import java.awt.BorderLayout
28+ import javax.swing.JPanel
29+ import javax.swing.JProgressBar
30+ import javax.swing.SwingUtilities
31+ import javax.swing.tree.DefaultMutableTreeNode
32+ import javax.swing.tree.DefaultTreeModel
33+
34+ class OpcacheSettingsPanel (private val project : Project ) :
35+ SimpleToolWindowPanel (false , false ),
36+ RefreshablePanel , Disposable {
37+ private val progressBar = JProgressBar ()
38+
39+ private val treeModel = StructureTreeModel (TokensTreeStructure (RootNode (null )), this )
40+ private val tree = Tree (DefaultTreeModel (DefaultMutableTreeNode ())).apply {
41+ setModel(AsyncTreeModel (treeModel, this @OpcacheSettingsPanel))
42+ isRootVisible = true
43+ showsRootHandles = true
44+
45+ TreeUIHelper .getInstance()
46+ .installTreeSpeedSearch(this , { path ->
47+ val treeNode = path.lastPathComponent as ? DefaultMutableTreeNode
48+ val tokenNode = treeNode?.userObject as ? LeafNode
49+
50+ tokenNode?.node?.value
51+ }, true )
52+ }
53+ val service: OpcacheSettingsTreeDumperService = project.getService(OpcacheSettingsTreeDumperService ::class .java)
54+
55+
56+ init {
57+ treeModel.invalidateAsync()
58+
59+ createToolbar()
60+ createContent()
61+
62+ SwingUtilities .invokeLater { refreshData() }
63+ }
64+
65+ fun createToolbar () {
66+ val actionGroup = DefaultActionGroup ().apply {
67+ add(RunDumpTokensCommandAction (service, " Dump Tree" ))
68+ addSeparator()
69+ }
70+
71+ val actionToolbar = ActionManager .getInstance().createActionToolbar(" Tree Toolbar" , actionGroup, false )
72+ actionToolbar.targetComponent = this
73+
74+ val toolBarPanel = JPanel (VerticalLayout ()).apply {
75+ add(
76+ JPanel (VerticalLayout ()).apply {
77+ add(createRefreshButton { refreshData() })
78+ add(createExpandsAll(tree))
79+ add(createCollapseAll(tree))
80+ }
81+ )
82+
83+ add(actionToolbar.component)
84+ }
85+ // searchTextField.addDocumentListener(this)
86+
87+ toolbar = toolBarPanel
88+ }
89+
90+ private fun createContent () {
91+ val responsivePanel = JPanel (BorderLayout ())
92+ responsivePanel.add(progressBar, BorderLayout .NORTH )
93+ responsivePanel.add(JBScrollPane (tree))
94+
95+ setContent(responsivePanel)
96+ }
97+
98+ private fun refreshData () {
99+ progressBar.setIndeterminate(true )
100+ progressBar.isVisible = true
101+ tree.emptyText.text = " Loading..."
102+
103+
104+ project.nonBlocking({ getViewData() }) { result ->
105+ tree.emptyText.text = " Nothing to show"
106+ rebuildTree(result)
107+
108+ progressBar.setIndeterminate(false )
109+ progressBar.isVisible = false
110+ }
111+ }
112+
113+ private fun rebuildTree (list : AnyNodeList ? ) {
114+ val treeModel = StructureTreeModel <AbstractTreeStructure >(AnyTreeStructure (AnyRootNode (list)), this )
115+ tree.setModel(AsyncTreeModel (treeModel, this ))
116+ tree.setRootVisible(false )
117+ treeModel.invalidateAsync()
118+
119+ TreeUtil .expandAll(tree)
120+ }
121+
122+ private fun getViewData (): AnyNodeList {
123+ val result = AnyNodeList ()
124+ val editor = FileEditorManager .getInstance(project).selectedTextEditor ? : return result
125+ val virtualFile = editor.virtualFile ? : return result
126+
127+ val runBlocking = runBlocking { service.dump(virtualFile) }
128+ println (" result is $runBlocking " )
129+
130+ return runBlocking as ? AnyNodeList ? : result
131+ }
132+
133+ override fun refresh (project : Project , type : RefreshType ) {
134+ refreshData()
135+ }
136+
137+ override fun dispose () {
138+ }
139+ }
0 commit comments