-
Notifications
You must be signed in to change notification settings - Fork 496
/
MySqlConnectionDialog.kt
89 lines (83 loc) · 2.61 KB
/
MySqlConnectionDialog.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
package app.cash.sqldelight.dialects.mysql.ide
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.openapi.ui.ValidationInfo
import com.intellij.ui.layout.GrowPolicy.MEDIUM_TEXT
import com.intellij.ui.layout.ValidationInfoBuilder
import com.intellij.ui.layout.applyToComponent
import com.intellij.ui.layout.panel
import javax.swing.JComponent
import javax.swing.JTextField
internal class MySqlConnectionDialog(
project: Project,
private var connectionName: String? = null,
internal var host: String = "localhost",
internal var port: String = "3306",
internal var databaseName: String = "",
internal var username: String = "",
internal var password: String = "",
) : DialogWrapper(project) {
internal val connectionKey get() = connectionName ?: "Default MySQL"
init {
title = "Create MySQL Connection"
init()
}
override fun createCenterPanel(): JComponent {
return panel {
row("Connection Name") {
textField(
getter = { connectionKey },
setter = { connectionName = it },
).withValidationOnApply(validateNonEmpty(connectionNameNonEmpty))
.growPolicy(MEDIUM_TEXT)
.applyToComponent {
if (connectionName != null) this.isEditable = false
}
}
row("Host") {
textField(
getter = { host },
setter = { host = it },
).withValidationOnApply(validateNonEmpty(hostNonEmpty))
.growPolicy(MEDIUM_TEXT)
}
row("Port") {
textField(
getter = { port },
setter = { port = it },
).withValidationOnApply(validateNonEmpty(portNonEmpty))
.growPolicy(MEDIUM_TEXT)
}
row("Database Name") {
textField(
getter = { databaseName },
setter = { databaseName = it },
)
.growPolicy(MEDIUM_TEXT)
}
row("Username") {
textField(
getter = { username },
setter = { username = it },
)
.growPolicy(MEDIUM_TEXT)
}
row("Password") {
textField(
getter = { password },
setter = { password = it },
)
.growPolicy(MEDIUM_TEXT)
}
}
}
companion object {
private const val connectionNameNonEmpty = "You must supply a connection name."
private const val hostNonEmpty = "You must supply a host."
private const val portNonEmpty = "You must supply a port."
}
}
private fun validateNonEmpty(message: String): ValidationInfoBuilder.(JTextField) -> ValidationInfo? =
{
if (it.text.isNullOrEmpty()) error(message) else null
}