From 928a866fe7e1f4aff09efa5e43d10ad444700312 Mon Sep 17 00:00:00 2001 From: hstyi Date: Mon, 12 May 2025 15:37:39 +0800 Subject: [PATCH] feat: improve SFTP (#572) --- .../termora/sftp/SFTPFileSystemViewPanel.kt | 27 ++++++++++++----- .../kotlin/app/termora/sftp/SFTPTabbed.kt | 29 ++++++++----------- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/app/termora/sftp/SFTPFileSystemViewPanel.kt b/src/main/kotlin/app/termora/sftp/SFTPFileSystemViewPanel.kt index 685dfdc..e61094c 100644 --- a/src/main/kotlin/app/termora/sftp/SFTPFileSystemViewPanel.kt +++ b/src/main/kotlin/app/termora/sftp/SFTPFileSystemViewPanel.kt @@ -27,6 +27,8 @@ import java.awt.event.MouseAdapter import java.awt.event.MouseEvent import java.util.concurrent.atomic.AtomicBoolean import javax.swing.* +import javax.swing.event.TreeExpansionEvent +import javax.swing.event.TreeExpansionListener class SFTPFileSystemViewPanel( var host: Host? = null, @@ -35,17 +37,18 @@ class SFTPFileSystemViewPanel( companion object { private val log = LoggerFactory.getLogger(SFTPFileSystemViewPanel::class.java) + } - private enum class State { - Initialized, - Connecting, - Connected, - ConnectFailed, - } + enum class State { + Initialized, + Connecting, + Connected, + ConnectFailed, } @Volatile - private var state = State.Initialized + var state = State.Initialized + private set private val cardLayout = CardLayout() private val cardPanel = JPanel(cardLayout) private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) @@ -289,6 +292,16 @@ class SFTPFileSystemViewPanel( } } }) + + tree.addTreeExpansionListener(object : TreeExpansionListener { + override fun treeExpanded(event: TreeExpansionEvent) { + properties.putString("SFTPTabbed.Tree.state", TreeUtils.saveExpansionState(tree)) + } + + override fun treeCollapsed(event: TreeExpansionEvent) { + properties.putString("SFTPTabbed.Tree.state", TreeUtils.saveExpansionState(tree)) + } + }) } override fun dispose() { diff --git a/src/main/kotlin/app/termora/sftp/SFTPTabbed.kt b/src/main/kotlin/app/termora/sftp/SFTPTabbed.kt index 6fe69a1..ce1fcbc 100644 --- a/src/main/kotlin/app/termora/sftp/SFTPTabbed.kt +++ b/src/main/kotlin/app/termora/sftp/SFTPTabbed.kt @@ -5,7 +5,6 @@ import app.termora.actions.AnAction import app.termora.actions.AnActionEvent import com.formdev.flatlaf.extras.components.FlatPopupMenu import com.formdev.flatlaf.extras.components.FlatTabbedPane -import java.awt.Point import java.awt.event.MouseAdapter import java.awt.event.MouseEvent import java.util.concurrent.atomic.AtomicBoolean @@ -13,7 +12,6 @@ import javax.swing.JButton import javax.swing.JToolBar import javax.swing.SwingUtilities import javax.swing.UIManager -import kotlin.math.max @Suppress("DuplicatedCode") class SFTPTabbed(private val transportManager: TransportManager) : FlatTabbedPane(), Disposable { @@ -43,23 +41,20 @@ class SFTPTabbed(private val transportManager: TransportManager) : FlatTabbedPan private fun initEvents() { addBtn.addActionListener(object : AnAction() { override fun actionPerformed(evt: AnActionEvent) { - val dialog = NewHostTreeDialog(SwingUtilities.getWindowAncestor(tabbed)) - dialog.location = Point( - max(0, addBtn.locationOnScreen.x - dialog.width / 2 + addBtn.width / 2), - addBtn.locationOnScreen.y + max(tabHeight, addBtn.height) - ) - dialog.setFilter { it.host.protocol == Protocol.SSH } - dialog.setTreeName("SFTPTabbed.Tree") - dialog.allowMulti = true - dialog.isVisible = true - - val hosts = dialog.hosts - if (hosts.isEmpty()) return - - for (host in hosts) { - addSFTPFileSystemViewPanelTab(host) + for (i in 0 until tabCount) { + val c = getComponentAt(i) + if (c !is SFTPFileSystemViewPanel) continue + if (c.state != SFTPFileSystemViewPanel.State.Initialized) continue + selectedIndex = i + return } + // 添加一个新的 + addTab( + I18n.getString("termora.transport.sftp.select-host"), + SFTPFileSystemViewPanel(transportManager = transportManager) + ) + selectedIndex = tabCount - 1 } })