Skip to content

Commit 869ffcf

Browse files
committed
JetBrains Timeout Input Dialog
1 parent c78330c commit 869ffcf

File tree

1 file changed

+67
-16
lines changed

1 file changed

+67
-16
lines changed

components/ide/jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/actions/ExtendWorkspaceTimeoutAction.kt

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,58 @@ import com.intellij.openapi.diagnostic.thisLogger
1111
import io.gitpod.gitpodprotocol.api.entities.WorkspaceTimeoutDuration
1212
import io.gitpod.jetbrains.remote.GitpodManager
1313
import com.intellij.notification.NotificationType
14+
import com.intellij.openapi.ui.DialogWrapper
15+
import com.intellij.openapi.ui.ValidationInfo
16+
import javax.swing.JComponent
17+
import javax.swing.JTextField
18+
import javax.swing.JPanel
19+
import javax.swing.JComboBox
20+
import javax.swing.BoxLayout
21+
22+
fun validate(duration: String): String {
23+
try {
24+
duration.toInt()
25+
} catch (e: NumberFormatException) {
26+
throw IllegalArgumentException("Duration must be an integer")
27+
}
28+
29+
return duration
30+
}
31+
32+
class InputDurationDialog : DialogWrapper(null, true) {
33+
private val textField = JTextField(10)
34+
private val unitComboBox = JComboBox(arrayOf("minutes", "hours"))
35+
36+
init {
37+
init()
38+
title = "Set timeout duration"
39+
}
40+
41+
override fun createCenterPanel(): JComponent {
42+
val customComponent = JPanel()
43+
customComponent.layout = BoxLayout(customComponent, BoxLayout.X_AXIS)
44+
customComponent.add(textField)
45+
customComponent.add(unitComboBox)
46+
47+
textField.text = "180"
48+
49+
return customComponent
50+
}
51+
52+
override fun doValidate(): ValidationInfo? {
53+
try {
54+
validate(textField.text)
55+
return null
56+
} catch (e: IllegalArgumentException) {
57+
return ValidationInfo(e.message ?: "An unknown error has occured", textField)
58+
}
59+
}
60+
61+
fun getDuration(): String {
62+
val selectedUnit = unitComboBox.selectedItem.toString()
63+
return "${textField.text}${selectedUnit[0]}"
64+
}
65+
}
1466

1567
class ExtendWorkspaceTimeoutAction : AnAction() {
1668
private val manager = service<GitpodManager>()
@@ -21,26 +73,25 @@ class ExtendWorkspaceTimeoutAction : AnAction() {
2173
"action" to "extend-timeout"
2274
))
2375

24-
manager.client.server.setWorkspaceTimeout(workspaceInfo.workspaceId, WorkspaceTimeoutDuration.DURATION_180M.toString()).whenComplete { result, e ->
25-
var message: String
26-
var notificationType: NotificationType
27-
28-
if (e != null) {
29-
message = "Cannot extend workspace timeout: ${e.message}"
30-
notificationType = NotificationType.ERROR
31-
thisLogger().error("gitpod: failed to extend workspace timeout", e)
32-
} else {
33-
if (result.resetTimeoutOnWorkspaces.isNotEmpty()) {
34-
message = "Workspace timeout has been extended to three hours. This reset the workspace timeout for other workspaces."
35-
notificationType = NotificationType.WARNING
76+
val dialog = InputDurationDialog()
77+
if (dialog.showAndGet()) {
78+
val duration = dialog.getDuration()
79+
manager.client.server.setWorkspaceTimeout(workspaceInfo.workspaceId, duration.toString()).whenComplete { _, e ->
80+
var message: String
81+
var notificationType: NotificationType
82+
83+
if (e != null) {
84+
message = "Cannot extend workspace timeout: ${e.message}"
85+
notificationType = NotificationType.ERROR
86+
thisLogger().error("gitpod: failed to extend workspace timeout", e)
3687
} else {
37-
message = "Workspace timeout has been extended to three hours."
88+
message = "Workspace timeout has been extended to ${duration}."
3889
notificationType = NotificationType.INFORMATION
3990
}
40-
}
4191

42-
val notification = manager.notificationGroup.createNotification(message, notificationType)
43-
notification.notify(null)
92+
val notification = manager.notificationGroup.createNotification(message, notificationType)
93+
notification.notify(null)
94+
}
4495
}
4596
}
4697
}

0 commit comments

Comments
 (0)