diff --git a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/ProjectStoreService.kt b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/ProjectStoreService.kt index c369ab4..1c985b0 100644 --- a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/ProjectStoreService.kt +++ b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/services/ProjectStoreService.kt @@ -6,6 +6,7 @@ import com.intellij.openapi.components.Service import com.intellij.openapi.project.Project import com.intellij.openapi.project.guessProjectDir import com.intellij.openapi.wm.ToolWindowManager +import com.jetbrains.rd.util.CopyOnWriteArrayList import java.io.File import java.nio.file.Path import java.nio.file.Paths @@ -28,16 +29,17 @@ class ProjectStoreService(private val project: Project) { ?.isAvailable = value == PluginStatus.ENABLED || value == PluginStatus.TOOLWINDOW_ENABLED } if (value == PluginStatus.ENABLED) onEnable() + if (value == PluginStatus.TOOLWINDOW_ENABLED) onToolWindowEnable() field = value } var modulePaths: MutableMap = mutableMapOf() val patchesInfo: MutableMap = mutableMapOf() val properties = Properties() val configPath: Path = Paths.get(project.guessProjectDir()?.path ?: ".", LEAVESKNIFE_CONFIG_FILE) - val patchesList = mutableMapOf>( - PatchType.SERVER to mutableListOf(), - PatchType.API to mutableListOf(), - PatchType.GENERATED_API to mutableListOf() + val patchesList = mutableMapOf>( + PatchType.SERVER to CopyOnWriteArrayList(), + PatchType.API to CopyOnWriteArrayList(), + PatchType.GENERATED_API to CopyOnWriteArrayList() ) private fun onEnable() { @@ -45,22 +47,13 @@ class ProjectStoreService(private val project: Project) { patchesInfo.forEach { patchInfoEntry -> val path = patchInfoEntry.value.path val patchType = patchInfoEntry.key - val patchesDir = File(path) - if (!patchesDir.exists()) { - println("Patches directory not found: $path") - status = PluginStatus.BROKEN_CONFIG - return - } - patchesDir.list()?.forEach { - patchesList[patchType]?.add(it) - } println("Creating watcher for $path") thread { createWatchService( Paths.get(path), StandardWatchEventKinds.ENTRY_MODIFY ) { - val newPatchesList = mutableListOf() + val newPatchesList = CopyOnWriteArrayList() File(path).list()?.forEach { newPatchesList.add(it) } @@ -71,6 +64,23 @@ class ProjectStoreService(private val project: Project) { } println(patchesList) } + + private fun onToolWindowEnable() { + println("Enabling tool window") + patchesInfo.forEach { patchInfoEntry -> + val path = patchInfoEntry.value.path + val patchType = patchInfoEntry.key + val patchesDir = File(path) + if (!patchesDir.exists()) { + println("Patches directory not found: $path") + status = PluginStatus.BROKEN_CONFIG + return + } + patchesDir.list()?.forEach { + patchesList[patchType]?.add(it) + } + } + } } enum class PluginStatus { diff --git a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/toolWindow/PatchesToolWindowFactory.kt b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/toolWindow/PatchesToolWindowFactory.kt index f657ff1..f729083 100644 --- a/src/main/kotlin/cn/xor7/xiaohei/leavesknife/toolWindow/PatchesToolWindowFactory.kt +++ b/src/main/kotlin/cn/xor7/xiaohei/leavesknife/toolWindow/PatchesToolWindowFactory.kt @@ -1,14 +1,16 @@ package cn.xor7.xiaohei.leavesknife.toolWindow +import cn.xor7.xiaohei.leavesknife.services.PatchType import cn.xor7.xiaohei.leavesknife.services.leavesknifeStoreService +import com.intellij.openapi.application.runInEdt import com.intellij.openapi.project.Project +import com.intellij.openapi.ui.SimpleToolWindowPanel import com.intellij.openapi.wm.ToolWindow import com.intellij.openapi.wm.ToolWindowFactory +import com.intellij.ui.components.JBScrollPane import com.intellij.ui.content.ContentFactory -import com.intellij.ui.dsl.builder.AlignX -import com.intellij.ui.dsl.builder.AlignY +import com.intellij.ui.dsl.builder.Align import com.intellij.ui.dsl.builder.panel -import com.intellij.ui.dsl.builder.plus import com.intellij.ui.treeStructure.Tree import javax.swing.tree.DefaultMutableTreeNode import javax.swing.tree.DefaultTreeModel @@ -30,16 +32,40 @@ class PatchesToolWindowFactory : ToolWindowFactory { class ServerPatchesToolWindow(toolWindow: ToolWindow) { private val store = toolWindow.project.leavesknifeStoreService - private val rootNode = DefaultMutableTreeNode("Root Node") + private val rootNode = DefaultMutableTreeNode() + private val patchesGroupNodes = mutableMapOf( + PatchType.SERVER to DefaultMutableTreeNode(PatchType.SERVER.name), + PatchType.API to DefaultMutableTreeNode(PatchType.API.name), + PatchType.GENERATED_API to DefaultMutableTreeNode(PatchType.GENERATED_API.name) + ) private val treeModel = DefaultTreeModel(rootNode) - fun getContent() = panel { - row { - rootNode.add(DefaultMutableTreeNode("Child Node")) - val tree = Tree(treeModel) - tree.isRootVisible = false - scrollCell(tree).align(AlignX.FILL + AlignY.TOP) - }.resizableRow() + fun getContent() = SimpleToolWindowPanel(true, true).apply { + setContent(panel { + row { + rootNode.add(patchesGroupNodes[PatchType.SERVER]) + rootNode.add(patchesGroupNodes[PatchType.API]) + rootNode.add(patchesGroupNodes[PatchType.GENERATED_API]) + val tree = Tree(treeModel) + tree.isRootVisible = false + val scrollPane = JBScrollPane(tree) + cell(scrollPane).align(Align.FILL).resizableColumn() + }.resizableRow() + row { + button("Refresh") { + println("Refreshing patches") + updateTree() + } + } + }) + } + + private fun updateTree() = runInEdt { + store.patchesList.forEach { (patchType, patches) -> + val node = patchesGroupNodes[patchType] ?: return@forEach + node.removeAllChildren() + patches.forEach { node.add(DefaultMutableTreeNode(it)) } + } } } } \ No newline at end of file