Skip to content

Commit

Permalink
feat: show patches earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
MC-XiaoHei committed Jul 11, 2024
1 parent db2738b commit 21033f8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,39 +29,31 @@ 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<String, String> = mutableMapOf()
val patchesInfo: MutableMap<PatchType, PatchesInfo> = mutableMapOf()
val properties = Properties()
val configPath: Path = Paths.get(project.guessProjectDir()?.path ?: ".", LEAVESKNIFE_CONFIG_FILE)
val patchesList = mutableMapOf<PatchType,MutableList<String>>(
PatchType.SERVER to mutableListOf(),
PatchType.API to mutableListOf(),
PatchType.GENERATED_API to mutableListOf()
val patchesList = mutableMapOf<PatchType, CopyOnWriteArrayList<String>>(
PatchType.SERVER to CopyOnWriteArrayList(),
PatchType.API to CopyOnWriteArrayList(),
PatchType.GENERATED_API to CopyOnWriteArrayList()
)

private fun onEnable() {
println("Enabling plugin")
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<String>()
val newPatchesList = CopyOnWriteArrayList<String>()
File(path).list()?.forEach {
newPatchesList.add(it)
}
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)) }
}
}
}
}

0 comments on commit 21033f8

Please sign in to comment.