@@ -22,6 +22,7 @@ import com.coder.gateway.sdk.getOS
22
22
import com.coder.gateway.sdk.toURL
23
23
import com.coder.gateway.sdk.v2.models.Workspace
24
24
import com.coder.gateway.sdk.withPath
25
+ import com.intellij.ide.ActivityTracker
25
26
import com.intellij.ide.BrowserUtil
26
27
import com.intellij.ide.IdeBundle
27
28
import com.intellij.openapi.Disposable
@@ -85,9 +86,6 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
85
86
WorkspaceStatusColumnInfo (" Status" )
86
87
)
87
88
88
- private val startWorkspaceAction = StartWorkspaceAction ()
89
- private val stopWorkspaceAction = StopWorkspaceAction ()
90
-
91
89
private var tableOfWorkspaces = TableView (listTableModelOfWorkspaces).apply {
92
90
setEnableAntialiasing(true )
93
91
rowSelectionAllowed = true
@@ -103,24 +101,13 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
103
101
setSelectionMode(ListSelectionModel .SINGLE_SELECTION )
104
102
selectionModel.addListSelectionListener {
105
103
enableNextButtonCallback(selectedObject != null && selectedObject?.agentStatus == RUNNING )
106
- when (selectedObject?.agentStatus) {
107
- RUNNING -> {
108
- startWorkspaceAction.isEnabled = false
109
- stopWorkspaceAction.isEnabled = true
110
- }
111
-
112
- STOPPED , FAILED -> {
113
- startWorkspaceAction.isEnabled = true
114
- stopWorkspaceAction.isEnabled = false
115
- }
116
-
117
- else -> {
118
- disableAllWorkspaceActions()
119
- }
120
- }
104
+ updateWorkspaceActions()
121
105
}
122
106
}
123
107
108
+ private val startWorkspaceAction = StartWorkspaceAction ()
109
+ private val stopWorkspaceAction = StopWorkspaceAction ()
110
+
124
111
private val toolbar = ToolbarDecorator .createDecorator(tableOfWorkspaces)
125
112
.disableAddAction()
126
113
.disableRemoveAction()
@@ -178,7 +165,7 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
178
165
withContext(Dispatchers .IO ) {
179
166
try {
180
167
coderClient.startWorkspace(workspace.workspaceID, workspace.workspaceName)
181
- startWorkspaceAction.isEnabled = false
168
+ loadWorkspaces()
182
169
} catch (e: WorkspaceResponseException ) {
183
170
logger.warn(" Could not build workspace ${workspace.name} , reason: $e " )
184
171
}
@@ -196,7 +183,7 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
196
183
withContext(Dispatchers .IO ) {
197
184
try {
198
185
coderClient.stopWorkspace(workspace.workspaceID, workspace.workspaceName)
199
- stopWorkspaceAction.isEnabled = false
186
+ loadWorkspaces()
200
187
} catch (e: WorkspaceResponseException ) {
201
188
logger.warn(" Could not stop workspace ${workspace.name} , reason: $e " )
202
189
}
@@ -206,14 +193,29 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
206
193
}
207
194
}
208
195
209
- private fun disableAllWorkspaceActions () {
210
- startWorkspaceAction.isEnabled = false
211
- stopWorkspaceAction.isEnabled = false
212
- }
213
-
214
196
override fun onInit (wizardModel : CoderWorkspacesWizardModel ) {
215
197
enableNextButtonCallback(false )
216
- disableAllWorkspaceActions()
198
+ updateWorkspaceActions()
199
+ }
200
+
201
+ private fun updateWorkspaceActions () {
202
+ when (tableOfWorkspaces.selectedObject?.agentStatus) {
203
+ RUNNING -> {
204
+ startWorkspaceAction.isEnabled = false
205
+ stopWorkspaceAction.isEnabled = true
206
+ }
207
+
208
+ STOPPED , FAILED -> {
209
+ startWorkspaceAction.isEnabled = true
210
+ stopWorkspaceAction.isEnabled = false
211
+ }
212
+
213
+ else -> {
214
+ startWorkspaceAction.isEnabled = false
215
+ stopWorkspaceAction.isEnabled = false
216
+ }
217
+ }
218
+ ActivityTracker .getInstance().inc()
217
219
}
218
220
219
221
private fun loginAndLoadWorkspace () {
@@ -272,7 +274,7 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
272
274
token = wizardModel.token
273
275
}
274
276
ProgressManager .getInstance().run (authTask)
275
- loadWorkspaces ()
277
+ triggerWorkspacePolling ()
276
278
}
277
279
278
280
private fun askToken (): String? {
@@ -296,32 +298,32 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
296
298
}
297
299
}
298
300
299
- private fun loadWorkspaces () {
301
+ private fun triggerWorkspacePolling () {
300
302
poller?.cancel()
301
303
302
304
poller = cs.launch {
303
305
while (isActive) {
304
- val workspaceList = withContext(Dispatchers .IO ) {
305
- try {
306
- return @withContext coderClient.workspaces().collectAgents()
307
- } catch (e: Exception ) {
308
- logger.error(" Could not retrieve workspaces for ${coderClient.me.username} on ${coderClient.coderURL} . Reason: $e " )
309
- emptyList()
310
- }
311
- }
306
+ loadWorkspaces()
307
+ delay(5000 )
308
+ }
309
+ }
310
+ }
312
311
313
- val selectedWorkspace = withContext(Dispatchers .Main ) {
314
- tableOfWorkspaces.selectedObject
315
- }
312
+ private suspend fun loadWorkspaces () {
313
+ val workspaceList = withContext(Dispatchers .IO ) {
314
+ try {
315
+ return @withContext coderClient.workspaces().collectAgents()
316
+ } catch (e: Exception ) {
317
+ logger.error(" Could not retrieve workspaces for ${coderClient.me.username} on ${coderClient.coderURL} . Reason: $e " )
318
+ emptyList()
319
+ }
320
+ }
316
321
317
- // if we just run the update on the main dispatcher, the code will block because it cant get some AWT locks
318
- ApplicationManager .getApplication().invokeLater {
319
- listTableModelOfWorkspaces.updateItems(workspaceList)
320
- if (selectedWorkspace != null ) {
321
- tableOfWorkspaces.selectItem(selectedWorkspace)
322
- }
323
- }
324
- delay(5000 )
322
+ withContext(Dispatchers .Main ) {
323
+ val selectedWorkspace = tableOfWorkspaces.selectedObject?.name
324
+ listTableModelOfWorkspaces.items = workspaceList
325
+ if (selectedWorkspace != null ) {
326
+ tableOfWorkspaces.selectItem(selectedWorkspace)
325
327
}
326
328
}
327
329
}
@@ -510,15 +512,15 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
510
512
}
511
513
}
512
514
513
- private fun ListTableModel <WorkspaceAgentModel>.updateItems ( workspaces : Collection < WorkspaceAgentModel > ) {
514
- while ( this .rowCount > 0 ) this .removeRow( 0 )
515
- this .addRows(workspaces)
516
- }
517
-
518
- private fun TableView<WorkspaceAgentModel>. selectItem ( workspace : WorkspaceAgentModel ) {
519
- this .items.forEachIndexed { index, workspaceAgentModel ->
520
- if (workspaceAgentModel.name == workspace.name)
521
- this .setRowSelectionInterval(index, index)
515
+ private fun TableView <WorkspaceAgentModel>.selectItem ( workspaceName : String? ) {
516
+ if (workspaceName != null ) {
517
+ this .items.forEachIndexed { index, workspaceAgentModel ->
518
+ if (workspaceAgentModel.name == workspaceName) {
519
+ selectionModel.addSelectionInterval(convertRowIndexToView(index), convertRowIndexToView(index))
520
+ // fix cell selection case
521
+ columnModel.selectionModel.addSelectionInterval( 0 , columnCount - 1 )
522
+ }
523
+ }
522
524
}
523
525
}
524
526
0 commit comments