mirror of
https://github.com/TermoraDev/termora.git
synced 2026-01-16 02:12:58 +08:00
feat: 支持新增/修改主机时测试连接 (#10)
This commit is contained in:
@@ -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<AbstractAction> {
|
||||
return listOf(createOkAction(), CancelAction())
|
||||
}
|
||||
|
||||
protected open fun createOkAction(): AbstractAction {
|
||||
return OkAction()
|
||||
}
|
||||
|
||||
@@ -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<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() {
|
||||
if (!pane.validateFields()) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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=类型
|
||||
|
||||
@@ -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=類型
|
||||
|
||||
Reference in New Issue
Block a user