Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add generic function to ask for input
  • Loading branch information
code-asher committed Aug 7, 2023
commit 77787a8b4e5af0e9cb75c73072e32a3ccb9afe43
106 changes: 58 additions & 48 deletions src/main/kotlin/com/coder/gateway/CoderRemoteConnectionHandle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,49 @@ class CoderRemoteConnectionHandle {
companion object {
val logger = Logger.getInstance(CoderRemoteConnectionHandle::class.java.simpleName)

/**
* Generic function to ask for input.
*/
@JvmStatic
fun ask(comment: String, isError: Boolean, link: Pair<String, String>?, default: String?): String? {
var inputFromUser: String? = null
ApplicationManager.getApplication().invokeAndWait({
lateinit var inputTextField: JBTextField
val panel = panel {
row {
if (link != null) browserLink(link.first, link.second)
inputTextField = textField()
.applyToComponent {
text = default ?: ""
minimumSize = Dimension(520, -1)
}.component
}.layout(RowLayout.PARENT_GRID)
row {
cell() // To align with the text box.
cell(
ComponentPanelBuilder.createCommentComponent(comment, false, -1, true)
.applyIf(isError) {
apply {
foreground = UIUtil.getErrorForeground()
}
}
)
}.layout(RowLayout.PARENT_GRID)
}
AppIcon.getInstance().requestAttention(null, true)
if (!dialog(
CoderGatewayBundle.message("gateway.connector.view.login.token.dialog"),
panel = panel,
focusedComponent = inputTextField
).showAndGet()
) {
return@invokeAndWait
}
inputFromUser = inputTextField.text
}, ModalityState.any())
return inputFromUser
}

/**
* Open a dialog for providing the token. Show any existing token so the
* user can validate it if a previous connection failed. If we are not
Expand Down Expand Up @@ -131,60 +174,27 @@ class CoderRemoteConnectionHandle {
existingToken = t
}
}
var tokenFromUser: String? = null
ApplicationManager.getApplication().invokeAndWait({
lateinit var sessionTokenTextField: JBTextField
val panel = panel {
row {
browserLink(
CoderGatewayBundle.message("gateway.connector.view.login.token.label"),
getTokenUrl.toString()
)
sessionTokenTextField = textField()
.applyToComponent {
text = existingToken
minimumSize = Dimension(520, -1)
}.component
}.layout(RowLayout.PARENT_GRID)
row {
cell() // To align with the text box.
cell(
ComponentPanelBuilder.createCommentComponent(
CoderGatewayBundle.message(
if (isRetry) "gateway.connector.view.workspaces.token.rejected"
else if (tokenSource == TokenSource.CONFIG) "gateway.connector.view.workspaces.token.injected"
else if (existingToken.isNotBlank()) "gateway.connector.view.workspaces.token.comment"
else "gateway.connector.view.workspaces.token.none"
),
false,
-1,
true
).applyIf(isRetry) {
apply {
foreground = UIUtil.getErrorForeground()
}
}
)
}.layout(RowLayout.PARENT_GRID)
}
AppIcon.getInstance().requestAttention(null, true)
if (!dialog(
CoderGatewayBundle.message("gateway.connector.view.login.token.dialog"),
panel = panel,
focusedComponent = sessionTokenTextField
).showAndGet()
) {
return@invokeAndWait
}
tokenFromUser = sessionTokenTextField.text
}, ModalityState.any())
val tokenFromUser = ask(
CoderGatewayBundle.message(
if (isRetry) "gateway.connector.view.workspaces.token.rejected"
else if (tokenSource == TokenSource.CONFIG) "gateway.connector.view.workspaces.token.injected"
else if (existingToken.isNotBlank()) "gateway.connector.view.workspaces.token.comment"
else "gateway.connector.view.workspaces.token.none"
),
isRetry,
Pair(
CoderGatewayBundle.message("gateway.connector.view.login.token.label"),
getTokenUrl.toString()
),
existingToken,
)
if (tokenFromUser.isNullOrBlank()) {
return null
}
if (tokenFromUser != existingToken) {
tokenSource = TokenSource.USER
}
return Pair(tokenFromUser!!, tokenSource)
return Pair(tokenFromUser, tokenSource)
}
}
}