Skip to content

Commit

Permalink
feat: supports importing hosts from CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
hstyi committed Feb 22, 2025
1 parent 1f392c5 commit 4308415
Show file tree
Hide file tree
Showing 16 changed files with 225 additions and 54 deletions.
4 changes: 4 additions & 0 deletions THIRDPARTY
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ commons-text 1.13.0
Apache License 2.0
https://github.com/apache/commons-text/blob/master/LICENSE.txt

commons-csv 1.13.0
Apache License 2.0
https://github.com/apache/commons-csv/blob/master/LICENSE.txt

eddsa 0.3.0
Creative Commons Zero v1.0 Universal
https://github.com/str4d/ed25519-java/blob/master/LICENSE.txt
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ dependencies {
implementation(libs.commons.codec)
implementation(libs.commons.io)
implementation(libs.commons.lang3)
implementation(libs.commons.csv)
implementation(libs.commons.net)
implementation(libs.commons.text)
implementation(libs.commons.compress)
Expand Down
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ trove4j = "1.0.20200330"
kotlinx-serialization-json = "1.8.0"
commons-codec = "1.18.0"
commons-lang3 = "3.17.0"
commons-csv = "1.13.0"
commons-net = "3.11.1"
commons-text = "1.13.0"
commons-compress = "1.27.1"
Expand Down Expand Up @@ -41,7 +42,7 @@ rhino = "1.7.15"
delight-rhino-sandbox = "0.0.17"
testcontainers = "1.20.4"
mixpanel = "1.5.3"
jSerialComm="2.11.0"
jSerialComm = "2.11.0"

[libraries]
kotlinx-coroutines-swing = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-swing", version.ref = "kotlinx-coroutines" }
Expand All @@ -53,6 +54,7 @@ tinylog-impl = { group = "org.tinylog", name = "tinylog-impl", version.ref = "ti
commons-codec = { group = "commons-codec", name = "commons-codec", version.ref = "commons-codec" }
commons-net = { group = "commons-net", name = "commons-net", version.ref = "commons-net" }
commons-lang3 = { group = "org.apache.commons", name = "commons-lang3", version.ref = "commons-lang3" }
commons-csv = { group = "org.apache.commons", name = "commons-csv", version.ref = "commons-csv" }
commons-text = { group = "org.apache.commons", name = "commons-text", version.ref = "commons-text" }
commons-compress = { group = "org.apache.commons", name = "commons-compress", version.ref = "commons-compress" }
pty4j = { group = "org.jetbrains.pty4j", name = "pty4j", version.ref = "pty4j" }
Expand Down
14 changes: 5 additions & 9 deletions src/main/kotlin/app/termora/HostManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ class HostManager private constructor() {
fun addHost(host: Host) {
assertEventDispatchThread()
database.addHost(host)
setHost(host)
if (host.deleted) {
hosts.entries.removeIf { it.value.id == host.id || it.value.parentId == host.id }
} else {
hosts[host.id] = host
}
}

/**
Expand All @@ -39,12 +43,4 @@ class HostManager private constructor() {
return hosts[id]
}


/**
* 仅修改缓存中的
*/
fun setHost(host: Host) {
assertEventDispatchThread()
hosts[host.id] = host
}
}
50 changes: 46 additions & 4 deletions src/main/kotlin/app/termora/HostTreeNode.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package app.termora

import javax.swing.tree.DefaultMutableTreeNode
import javax.swing.tree.TreeNode

class HostTreeNode(host: Host) : DefaultMutableTreeNode(host) {
companion object {
private val hostManager get() = HostManager.getInstance()
}

/**
* 如果要重新赋值,记得修改 [Host.updateDate] 否则下次取出时可能时缓存的
*/
var host: Host
get() = hostManager.getHost((userObject as Host).id) ?: userObject as Host
set(value) {
setUserObject(value)
hostManager.setHost(value)
get() {
val cacheHost = hostManager.getHost((userObject as Host).id)
val myHost = userObject as Host
if (cacheHost == null) {
return myHost
}
return if (cacheHost.updateDate > myHost.updateDate) cacheHost else myHost
}
set(value) = setUserObject(value)

val folderCount
get() = children().toList().count { if (it is HostTreeNode) it.host.protocol == Protocol.Folder else false }
Expand All @@ -32,6 +40,29 @@ class HostTreeNode(host: Host) : DefaultMutableTreeNode(host) {
return children
}

fun childrenNode(): List<HostTreeNode> {
return children?.map { it as HostTreeNode } ?: emptyList()
}


/**
* 深度克隆
* @param scopes 克隆的范围
*/
fun clone(scopes: Set<Protocol> = emptySet()): HostTreeNode {
val newNode = clone() as HostTreeNode
deepClone(newNode, this, scopes)
return newNode
}

private fun deepClone(newNode: HostTreeNode, oldNode: HostTreeNode, scopes: Set<Protocol> = emptySet()) {
for (child in oldNode.childrenNode()) {
if (scopes.isNotEmpty() && !scopes.contains(child.host.protocol)) continue
val newChildNode = child.clone() as HostTreeNode
deepClone(newChildNode, child, scopes)
newNode.add(newChildNode)
}
}

override fun clone(): Any {
val newNode = HostTreeNode(host)
Expand All @@ -40,6 +71,17 @@ class HostTreeNode(host: Host) : DefaultMutableTreeNode(host) {
return newNode
}

override fun isNodeChild(aNode: TreeNode?): Boolean {
if (aNode is HostTreeNode) {
for (node in childrenNode()) {
if (node.host == aNode.host) {
return true
}
}
}
return super.isNodeChild(aNode)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand Down
Loading

0 comments on commit 4308415

Please sign in to comment.