forked from redhat-developer/intellij-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResourceNameDialog.kt
114 lines (102 loc) · 4 KB
/
ResourceNameDialog.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
/*******************************************************************************
* Copyright (c) 2023 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.intellij.kubernetes.dialogs
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.openapi.ui.ValidationInfo
import com.intellij.ui.TextFieldWithAutoCompletion
import com.intellij.ui.TextFieldWithAutoCompletionListProvider
import com.intellij.ui.components.JBLabel
import com.redhat.devtools.intellij.kubernetes.registerEscapeShortcut
import com.redhat.devtools.intellij.kubernetes.setBold
import com.redhat.devtools.intellij.kubernetes.setRootPaneBorders
import com.redhat.devtools.intellij.kubernetes.setGlassPaneResizable
import com.redhat.devtools.intellij.kubernetes.setMovable
import io.fabric8.kubernetes.api.model.HasMetadata
import net.miginfocom.swing.MigLayout
import java.awt.Point
import javax.swing.JComponent
import javax.swing.JPanel
import javax.swing.RootPaneContainer
import javax.swing.SwingConstants
class ResourceNameDialog<NAMESPACE: HasMetadata>(
project: Project,
private val kind: String,
existingNamespaces: Collection<NAMESPACE>,
private val onOk: (name: String) -> Unit,
private val location: Point?
) : DialogWrapper(project, false) {
private val title = JBLabel("Set current $kind").apply {
setBold(this)
}
private val nameTextField = TextFieldWithAutoCompletion(
project,
onLookup(existingNamespaces),
false,
null
)
private fun onLookup(projects: Collection<NAMESPACE>): TextFieldWithAutoCompletionListProvider<NAMESPACE> {
return object : TextFieldWithAutoCompletionListProvider<NAMESPACE>(projects) {
override fun getLookupString(item: NAMESPACE): String {
return item.metadata.name
}
}
}
override fun createCenterPanel(): JComponent {
return JPanel(
MigLayout("ins 4, gap 4, fillx, filly, hidemode 3")
).apply {
add(title, "gapbottom 10, span 2, wrap")
val label = JBLabel("Current $kind:", SwingConstants.LEFT)
add(label)
add(nameTextField, "growx, pushx, w min:200, wrap")
}
}
override fun init() {
super.init()
setUndecorated(true)
val dialogWindow = peer.window
val rootPane = (dialogWindow as RootPaneContainer).rootPane
registerEscapeShortcut(rootPane, ::closeImmediately, myDisposable)
setRootPaneBorders(rootPane)
setGlassPaneResizable(peer.rootPane, disposable)
setMovable(getRootPane(), title)
isResizable = false
isModal = false
if (location != null) {
setLocation(location.x, location.y)
}
setOKButtonText("Set")
}
override fun getPreferredFocusedComponent(): JComponent {
return nameTextField
}
override fun show() {
init()
super.show()
}
override fun doValidate(): ValidationInfo? {
return if (nameTextField.text.isEmpty()) {
ValidationInfo("Name mustn't be empty", nameTextField)
} else {
null
}
}
override fun doOKAction() {
super.doOKAction()
onOk.invoke(nameTextField.text)
}
private fun closeImmediately() {
if (isVisible) {
doCancelAction()
}
}
}