From 46af9a44b2440f0b65b5312f435fbec43640996e Mon Sep 17 00:00:00 2001 From: hstyi Date: Sat, 4 Jan 2025 13:02:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=96=B0=E5=A2=9E/?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BB=E6=9C=BA=E6=97=B6=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=20(#10)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/app/termora/DialogWrapper.kt | 16 ++++-- src/main/kotlin/app/termora/HostDialog.kt | 54 +++++++++++++++++-- src/main/resources/i18n/messages.properties | 4 ++ .../resources/i18n/messages_zh_CN.properties | 3 ++ .../resources/i18n/messages_zh_TW.properties | 3 ++ 5 files changed, 71 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/app/termora/DialogWrapper.kt b/src/main/kotlin/app/termora/DialogWrapper.kt index f674aa8..d533771 100644 --- a/src/main/kotlin/app/termora/DialogWrapper.kt +++ b/src/main/kotlin/app/termora/DialogWrapper.kt @@ -71,17 +71,23 @@ abstract class DialogWrapper(owner: Window?) : JDialog(owner) { BorderFactory.createMatteBorder(1, 0, 0, 0, DynamicColor.BorderColor), BorderFactory.createEmptyBorder(8, 12, 8, 12) ) - - val okButton = createJButtonForAction(createOkAction()) box.add(Box.createHorizontalGlue()) - box.add(createJButtonForAction(CancelAction())) - box.add(Box.createHorizontalStrut(8)) - box.add(okButton) + val actions = createActions() + for (i in actions.size - 1 downTo 0) { + box.add(createJButtonForAction(actions[i])) + if (i != 0) { + box.add(Box.createHorizontalStrut(8)) + } + } return box } + protected open fun createActions(): List { + return listOf(createOkAction(), CancelAction()) + } + protected open fun createOkAction(): AbstractAction { return OkAction() } diff --git a/src/main/kotlin/app/termora/HostDialog.kt b/src/main/kotlin/app/termora/HostDialog.kt index 5c2989c..4c7356e 100644 --- a/src/main/kotlin/app/termora/HostDialog.kt +++ b/src/main/kotlin/app/termora/HostDialog.kt @@ -1,12 +1,13 @@ package app.termora +import org.apache.commons.lang3.exception.ExceptionUtils +import org.apache.sshd.client.SshClient +import org.apache.sshd.client.session.ClientSession import java.awt.BorderLayout import java.awt.Dimension import java.awt.Window -import javax.swing.BorderFactory -import javax.swing.JComponent -import javax.swing.JPanel -import javax.swing.UIManager +import java.awt.event.ActionEvent +import javax.swing.* class HostDialog(owner: Window, host: Host? = null) : DialogWrapper(owner) { private val pane = if (host != null) EditHostOptionsPane(host) else HostOptionsPane() @@ -33,6 +34,51 @@ class HostDialog(owner: Window, host: Host? = null) : DialogWrapper(owner) { return panel } + override fun createActions(): List { + return listOf(createOkAction(), createTestConnectionAction(), CancelAction()) + } + + private fun createTestConnectionAction(): AbstractAction { + return object : AnAction(I18n.getString("termora.new-host.test-connection")) { + override fun actionPerformed(e: ActionEvent) { + if (!pane.validateFields()) { + return + } + + putValue(NAME, "${I18n.getString("termora.new-host.test-connection")}...") + SwingUtilities.invokeLater { + testConnection(pane.getHost()) + putValue(NAME, I18n.getString("termora.new-host.test-connection")) + } + + } + } + } + + + private fun testConnection(host: Host) { + if (host.protocol != Protocol.SSH) { + OptionPane.showMessageDialog(this, I18n.getString("termora.new-host.test-connection-successful")) + return + } + + var client: SshClient? = null + var session: ClientSession? = null + try { + client = SshClients.openClient(host) + session = SshClients.openSession(host, client) + OptionPane.showMessageDialog(this, I18n.getString("termora.new-host.test-connection-successful")) + } catch (e: Exception) { + OptionPane.showMessageDialog( + this, ExceptionUtils.getRootCauseMessage(e), + messageType = JOptionPane.ERROR_MESSAGE + ) + } finally { + session?.close() + client?.close() + } + + } override fun doOKAction() { if (!pane.validateFields()) { diff --git a/src/main/resources/i18n/messages.properties b/src/main/resources/i18n/messages.properties index 7c122ed..2fe7166 100644 --- a/src/main/resources/i18n/messages.properties +++ b/src/main/resources/i18n/messages.properties @@ -140,6 +140,10 @@ termora.new-host.tunneling.add=Add termora.new-host.tunneling.edit=${termora.keymgr.edit} termora.new-host.tunneling.delete=${termora.remove} +termora.new-host.test-connection=Test Connection +termora.new-host.test-connection-successful=Connection successful + + # Key manager termora.keymgr.title=Key Manager diff --git a/src/main/resources/i18n/messages_zh_CN.properties b/src/main/resources/i18n/messages_zh_CN.properties index cb97e4d..ab2fccb 100644 --- a/src/main/resources/i18n/messages_zh_CN.properties +++ b/src/main/resources/i18n/messages_zh_CN.properties @@ -124,6 +124,9 @@ termora.new-host.terminal.env=环境 +termora.new-host.test-connection=测试连接 +termora.new-host.test-connection-successful=连接成功 + termora.new-host.tunneling=隧道 termora.new-host.tunneling.table.name=名称 termora.new-host.tunneling.table.type=类型 diff --git a/src/main/resources/i18n/messages_zh_TW.properties b/src/main/resources/i18n/messages_zh_TW.properties index 141adac..e1ea859 100644 --- a/src/main/resources/i18n/messages_zh_TW.properties +++ b/src/main/resources/i18n/messages_zh_TW.properties @@ -119,6 +119,9 @@ termora.new-host.terminal.startup-commands=啟動命令 termora.new-host.terminal.heartbeat-interval=心跳間隔 termora.new-host.terminal.env=環境 +termora.new-host.test-connection=測試連接 +termora.new-host.test-connection-successful=連線成功 + termora.new-host.tunneling=隧道 termora.new-host.tunneling.table.name=名稱 termora.new-host.tunneling.table.type=類型