-
Notifications
You must be signed in to change notification settings - Fork 379
/
ui.kt
67 lines (56 loc) · 1.97 KB
/
ui.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
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
package org.rust.ide.refactoring.extractStructFields
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.openapi.ui.ValidationInfo
import com.intellij.ui.components.JBTextField
import com.intellij.ui.dsl.builder.panel
import org.jetbrains.annotations.TestOnly
import org.rust.RsBundle
import org.rust.ide.refactoring.isValidRustVariableIdentifier
import org.rust.openapiext.fullWidthCell
import org.rust.openapiext.isUnitTestMode
import javax.swing.JComponent
private var MOCK: ExtractFieldsUi? = null
fun showExtractStructFieldsDialog(project: Project): String? {
val chooser = if (isUnitTestMode) {
MOCK ?: error("You should set mock ui via `withMockExtractFieldsUi`")
} else {
ExtractFieldsDialog(project)
}
return chooser.selectStructName(project)
}
@TestOnly
fun withMockExtractFieldsUi(mockUi: ExtractFieldsUi, action: () -> Unit) {
MOCK = mockUi
try {
action()
} finally {
MOCK = null
}
}
interface ExtractFieldsUi {
fun selectStructName(project: Project): String?
}
private class ExtractFieldsDialog(project: Project) : DialogWrapper(project, false), ExtractFieldsUi {
private val input: JBTextField = JBTextField()
init {
super.init()
title = RsBundle.message("action.Rust.RsExtractStructFields.choose.name.dialog.title")
}
override fun doValidate(): ValidationInfo? {
if (!isValidRustVariableIdentifier(input.text)) {
return ValidationInfo(RsBundle.message("action.Rust.RsExtractStructFields.choose.name.dialog.invalid.name"), input)
}
return null
}
override fun createCenterPanel(): JComponent =
panel {
row { fullWidthCell(input).focused() }
}
override fun selectStructName(project: Project): String? =
if (showAndGet()) input.text else null
}