Skip to content

Commit 77787a8

Browse files
committed
Add generic function to ask for input
1 parent 8d60046 commit 77787a8

File tree

1 file changed

+58
-48
lines changed

1 file changed

+58
-48
lines changed

src/main/kotlin/com/coder/gateway/CoderRemoteConnectionHandle.kt

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,49 @@ class CoderRemoteConnectionHandle {
103103
companion object {
104104
val logger = Logger.getInstance(CoderRemoteConnectionHandle::class.java.simpleName)
105105

106+
/**
107+
* Generic function to ask for input.
108+
*/
109+
@JvmStatic
110+
fun ask(comment: String, isError: Boolean, link: Pair<String, String>?, default: String?): String? {
111+
var inputFromUser: String? = null
112+
ApplicationManager.getApplication().invokeAndWait({
113+
lateinit var inputTextField: JBTextField
114+
val panel = panel {
115+
row {
116+
if (link != null) browserLink(link.first, link.second)
117+
inputTextField = textField()
118+
.applyToComponent {
119+
text = default ?: ""
120+
minimumSize = Dimension(520, -1)
121+
}.component
122+
}.layout(RowLayout.PARENT_GRID)
123+
row {
124+
cell() // To align with the text box.
125+
cell(
126+
ComponentPanelBuilder.createCommentComponent(comment, false, -1, true)
127+
.applyIf(isError) {
128+
apply {
129+
foreground = UIUtil.getErrorForeground()
130+
}
131+
}
132+
)
133+
}.layout(RowLayout.PARENT_GRID)
134+
}
135+
AppIcon.getInstance().requestAttention(null, true)
136+
if (!dialog(
137+
CoderGatewayBundle.message("gateway.connector.view.login.token.dialog"),
138+
panel = panel,
139+
focusedComponent = inputTextField
140+
).showAndGet()
141+
) {
142+
return@invokeAndWait
143+
}
144+
inputFromUser = inputTextField.text
145+
}, ModalityState.any())
146+
return inputFromUser
147+
}
148+
106149
/**
107150
* Open a dialog for providing the token. Show any existing token so the
108151
* user can validate it if a previous connection failed. If we are not
@@ -131,60 +174,27 @@ class CoderRemoteConnectionHandle {
131174
existingToken = t
132175
}
133176
}
134-
var tokenFromUser: String? = null
135-
ApplicationManager.getApplication().invokeAndWait({
136-
lateinit var sessionTokenTextField: JBTextField
137-
val panel = panel {
138-
row {
139-
browserLink(
140-
CoderGatewayBundle.message("gateway.connector.view.login.token.label"),
141-
getTokenUrl.toString()
142-
)
143-
sessionTokenTextField = textField()
144-
.applyToComponent {
145-
text = existingToken
146-
minimumSize = Dimension(520, -1)
147-
}.component
148-
}.layout(RowLayout.PARENT_GRID)
149-
row {
150-
cell() // To align with the text box.
151-
cell(
152-
ComponentPanelBuilder.createCommentComponent(
153-
CoderGatewayBundle.message(
154-
if (isRetry) "gateway.connector.view.workspaces.token.rejected"
155-
else if (tokenSource == TokenSource.CONFIG) "gateway.connector.view.workspaces.token.injected"
156-
else if (existingToken.isNotBlank()) "gateway.connector.view.workspaces.token.comment"
157-
else "gateway.connector.view.workspaces.token.none"
158-
),
159-
false,
160-
-1,
161-
true
162-
).applyIf(isRetry) {
163-
apply {
164-
foreground = UIUtil.getErrorForeground()
165-
}
166-
}
167-
)
168-
}.layout(RowLayout.PARENT_GRID)
169-
}
170-
AppIcon.getInstance().requestAttention(null, true)
171-
if (!dialog(
172-
CoderGatewayBundle.message("gateway.connector.view.login.token.dialog"),
173-
panel = panel,
174-
focusedComponent = sessionTokenTextField
175-
).showAndGet()
176-
) {
177-
return@invokeAndWait
178-
}
179-
tokenFromUser = sessionTokenTextField.text
180-
}, ModalityState.any())
177+
val tokenFromUser = ask(
178+
CoderGatewayBundle.message(
179+
if (isRetry) "gateway.connector.view.workspaces.token.rejected"
180+
else if (tokenSource == TokenSource.CONFIG) "gateway.connector.view.workspaces.token.injected"
181+
else if (existingToken.isNotBlank()) "gateway.connector.view.workspaces.token.comment"
182+
else "gateway.connector.view.workspaces.token.none"
183+
),
184+
isRetry,
185+
Pair(
186+
CoderGatewayBundle.message("gateway.connector.view.login.token.label"),
187+
getTokenUrl.toString()
188+
),
189+
existingToken,
190+
)
181191
if (tokenFromUser.isNullOrBlank()) {
182192
return null
183193
}
184194
if (tokenFromUser != existingToken) {
185195
tokenSource = TokenSource.USER
186196
}
187-
return Pair(tokenFromUser!!, tokenSource)
197+
return Pair(tokenFromUser, tokenSource)
188198
}
189199
}
190200
}

0 commit comments

Comments
 (0)