/
RemoteConnectionView.kt
168 lines (154 loc) · 6.49 KB
/
RemoteConnectionView.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package org.chorusmc.chorus.views.remoteconnection
import javafx.css.PseudoClass
import javafx.geometry.Pos
import javafx.scene.Scene
import javafx.scene.control.*
import javafx.scene.image.Image
import javafx.scene.input.MouseEvent
import javafx.scene.layout.HBox
import javafx.scene.layout.VBox
import javafx.stage.Stage
import org.chorusmc.chorus.Chorus
import org.chorusmc.chorus.connection.Password
import org.chorusmc.chorus.connection.RemoteConnection
import org.chorusmc.chorus.nodes.control.NumericTextField
import org.chorusmc.chorus.theme.Themes
import org.chorusmc.chorus.util.config
import org.chorusmc.chorus.util.toObservableList
import org.chorusmc.chorus.util.translate
/**
* @author Gio
*/
open class RemoteConnectionView(private val name: String, defaultPort: Int, setting: String, private val psw: Password) {
private var ips = emptyMap<String, Pair<String, String>>()
private val ip = ComboBox<String>()
private val username = TextField()
private val password = PasswordField()
private val port = NumericTextField()
val connectButton = Button()
private val filesBox = VBox()
val stage = Stage()
lateinit var onSelect: Runnable
lateinit var selectedPath: String
var title = "Chorus - $name"
set(value) {
stage.title = value
}
fun onConfirm(unit: (String, String, Int, String) -> Unit) = connectButton.setOnAction {
unit(ip.selectionModel.selectedItem, username.text, port.text.toInt(), password.text)
}
var onBrowse: (Pair<String, String>) -> Unit = {}
init {
config[setting].split("\\n").forEach {
with(it.split("|")) {
if(size == 2) {
ips += this[0] to (this[1] to defaultPort.toString())
} else if(size == 3) {
ips += this[0] to (this[1] to this[2])
}
}
}
}
fun show() {
val root = VBox()
root.styleClass += "pane"
val scene = Scene(root, 800.0, 400.0)
scene.stylesheets.addAll(Themes.byConfig().path[0], "/assets/styles/global.css")
ip.selectionModel.selectedItemProperty().addListener {_ ->
val pair = ips[ip.selectionModel.selectedItem]!!
username.text = pair.first
port.text = pair.second
password.requestFocus()
}
ip.promptText = translate("${name.toLowerCase()}.no_server")
ip.items = ips.keys.toList().toObservableList()
if(ip.items.size > 0) ip.selectionModel.selectFirst() else ip.isDisable = true
ip.styleClass += "ip-box"
username.promptText = translate("remote.username")
username.styleClass += "username-field"
password.text = psw.psw
password.promptText = translate("remote.password")
password.styleClass += "password-field"
port.promptText = translate("remote.port")
port.styleClass += "username-field"
port.prefWidth = 40.0
port.alignment = Pos.CENTER
username.setOnAction {
connectButton.fire()
}
port.disableProperty().bind(ip.selectionModel.selectedItemProperty().isNull)
username.disableProperty().bind(ip.selectionModel.selectedItemProperty().isNull)
password.setOnAction {
connectButton.fire()
}
password.disableProperty().bind(ip.selectionModel.selectedItemProperty().isNull)
connectButton.text = translate("remote.connect")
connectButton.styleClass += "connect-button"
connectButton.disableProperty().bind(
ip.selectionModel.selectedItemProperty().isNull
.or(username.textProperty().isEmpty)
.or(password.textProperty().isEmpty)
.or(port.textProperty().isEmpty)
)
port.setOnAction {
connectButton.fire()
}
val addressHbox = HBox(ip)
if(ip.selectionModel.selectedItem != null) addressHbox.children.addAll(username, password, port, connectButton)
addressHbox.styleClass += "sftp-address-box"
addressHbox.alignment = Pos.CENTER
addressHbox.spacing = 15.0
addressHbox.prefHeight = 75.0
addressHbox.minHeight = addressHbox.prefHeight
addressHbox.prefWidthProperty().bind(root.prefWidthProperty())
val scrollpane = ScrollPane(filesBox)
scrollpane.prefWidthProperty().bind(scene.widthProperty())
scrollpane.prefHeightProperty().bind(scene.heightProperty())
scrollpane.hbarPolicy = ScrollPane.ScrollBarPolicy.NEVER
filesBox.styleClass += "files-box"
filesBox.prefWidthProperty().bind(scrollpane.prefWidthProperty())
val mainVbox = VBox(addressHbox, scrollpane)
root.children += mainVbox
stage.minWidth = scene.width
stage.minHeight = scene.height
stage.title = title
stage.scene = scene
stage.icons += Image(Chorus::class.java.getResourceAsStream("/assets/images/icon.png"))
stage.show()
password.requestFocus()
}
fun close() = stage.close()
fun clear() = filesBox.children.clear()
fun generateFiles(connection: RemoteConnection, loc: String = connection.home) {
filesBox.children.clear()
var location = ""
if(loc.endsWith("/..")) {
val parts = loc.split("/")
(0 until parts.size - 2).forEach {
location += parts[it] + "/"
}
if(parts.size - 3 > 0) {
location = location.substring(0, location.length - 1)
}
} else location = loc
val files = try {
connection.getFiles(location)
} catch(e: Exception) {
location = connection.home
connection.getFiles(location)
}
title = "Chorus - $name [$location]"
files.filter {it.filename != "."}.sortedBy {it.filename}.sortedBy {!it.isDir}.forEach {
if(!(location == "/" && it.filename == "..")) {
val button = RemoteConnectionButton(it.filename, "$location${if(location.endsWith("/")) "" else "/"}${it.filename}", this, connection, it.isDir)
button.prefWidthProperty().bind(filesBox.prefWidthProperty())
button.addEventFilter(MouseEvent.MOUSE_PRESSED) {
filesBox.children.filtered {it != button}.forEach {
it.pseudoClassStateChanged(PseudoClass.getPseudoClass("focused"), false)
}
}
filesBox.children += button
}
}
}
}