feat: 支持新增/修改主机时测试连接 (#10)

This commit is contained in:
hstyi
2025-01-04 13:02:46 +08:00
committed by hstyi
parent babc440841
commit 46af9a44b2
5 changed files with 71 additions and 9 deletions

View File

@@ -71,17 +71,23 @@ abstract class DialogWrapper(owner: Window?) : JDialog(owner) {
BorderFactory.createMatteBorder(1, 0, 0, 0, DynamicColor.BorderColor), BorderFactory.createMatteBorder(1, 0, 0, 0, DynamicColor.BorderColor),
BorderFactory.createEmptyBorder(8, 12, 8, 12) BorderFactory.createEmptyBorder(8, 12, 8, 12)
) )
val okButton = createJButtonForAction(createOkAction())
box.add(Box.createHorizontalGlue()) 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 return box
} }
protected open fun createActions(): List<AbstractAction> {
return listOf(createOkAction(), CancelAction())
}
protected open fun createOkAction(): AbstractAction { protected open fun createOkAction(): AbstractAction {
return OkAction() return OkAction()
} }

View File

@@ -1,12 +1,13 @@
package app.termora 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.BorderLayout
import java.awt.Dimension import java.awt.Dimension
import java.awt.Window import java.awt.Window
import javax.swing.BorderFactory import java.awt.event.ActionEvent
import javax.swing.JComponent import javax.swing.*
import javax.swing.JPanel
import javax.swing.UIManager
class HostDialog(owner: Window, host: Host? = null) : DialogWrapper(owner) { class HostDialog(owner: Window, host: Host? = null) : DialogWrapper(owner) {
private val pane = if (host != null) EditHostOptionsPane(host) else HostOptionsPane() 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 return panel
} }
override fun createActions(): List<AbstractAction> {
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() { override fun doOKAction() {
if (!pane.validateFields()) { if (!pane.validateFields()) {

View File

@@ -140,6 +140,10 @@ termora.new-host.tunneling.add=Add
termora.new-host.tunneling.edit=${termora.keymgr.edit} termora.new-host.tunneling.edit=${termora.keymgr.edit}
termora.new-host.tunneling.delete=${termora.remove} termora.new-host.tunneling.delete=${termora.remove}
termora.new-host.test-connection=Test Connection
termora.new-host.test-connection-successful=Connection successful
# Key manager # Key manager
termora.keymgr.title=Key Manager termora.keymgr.title=Key Manager

View File

@@ -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=隧道
termora.new-host.tunneling.table.name=名称 termora.new-host.tunneling.table.name=名称
termora.new-host.tunneling.table.type=类型 termora.new-host.tunneling.table.type=类型

View File

@@ -119,6 +119,9 @@ termora.new-host.terminal.startup-commands=啟動命令
termora.new-host.terminal.heartbeat-interval=心跳間隔 termora.new-host.terminal.heartbeat-interval=心跳間隔
termora.new-host.terminal.env=環境 termora.new-host.terminal.env=環境
termora.new-host.test-connection=測試連接
termora.new-host.test-connection-successful=連線成功
termora.new-host.tunneling=隧道 termora.new-host.tunneling=隧道
termora.new-host.tunneling.table.name=名稱 termora.new-host.tunneling.table.name=名稱
termora.new-host.tunneling.table.type=類型 termora.new-host.tunneling.table.type=類型