Skip to content

Commit f9c8573

Browse files
committed
Break out workspaces table
I want to write a unit test for the selection logic.
1 parent c138d09 commit f9c8573

File tree

1 file changed

+33
-38
lines changed

1 file changed

+33
-38
lines changed

src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,9 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
108108

109109
private var tfUrl: JTextField? = null
110110
private var cbExistingToken: JCheckBox? = null
111-
private var listTableModelOfWorkspaces = ListTableModel<WorkspaceAgentModel>(
112-
WorkspaceIconColumnInfo(""),
113-
WorkspaceNameColumnInfo("Name"),
114-
WorkspaceTemplateNameColumnInfo("Template"),
115-
WorkspaceVersionColumnInfo("Version"),
116-
WorkspaceStatusColumnInfo("Status")
117-
)
118111

119112
private val notificationBanner = NotificationBanner()
120-
private var tableOfWorkspaces = TableView(listTableModelOfWorkspaces).apply {
113+
private var tableOfWorkspaces = WorkspacesTable().apply {
121114
setEnableAntialiasing(true)
122115
rowSelectionAllowed = true
123116
columnSelectionAllowed = false
@@ -348,7 +341,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
348341
}
349342

350343
override fun onInit(wizardModel: CoderWorkspacesWizardModel) {
351-
listTableModelOfWorkspaces.items = emptyList()
344+
tableOfWorkspaces.listTableModel.items = emptyList()
352345
if (localWizardModel.coderURL.isNotBlank() && localWizardModel.token != null) {
353346
triggerWorkspacePolling(true)
354347
} else {
@@ -454,7 +447,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
454447
// Clear out old deployment details.
455448
poller?.cancel()
456449
tableOfWorkspaces.setEmptyState("Connecting to $deploymentURL...")
457-
listTableModelOfWorkspaces.items = emptyList()
450+
tableOfWorkspaces.listTableModel.items = emptyList()
458451

459452
// Authenticate and load in a background process with progress.
460453
// TODO: Make this cancelable.
@@ -677,7 +670,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
677670
}
678671
withContext(Dispatchers.Main) {
679672
val selectedWorkspace = tableOfWorkspaces.selectedObject?.name
680-
listTableModelOfWorkspaces.items = ws.toList()
673+
tableOfWorkspaces.listTableModel.items = ws.toList()
681674
if (selectedWorkspace != null) {
682675
tableOfWorkspaces.selectItem(selectedWorkspace)
683676
}
@@ -764,7 +757,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
764757
else CoderCLIManager.getDataDir(),
765758
settings.binarySource,
766759
)
767-
cliManager.configSsh(listTableModelOfWorkspaces.items)
760+
cliManager.configSsh(tableOfWorkspaces.items)
768761

769762
logger.info("Opening IDE and Project Location window for ${workspace.name}")
770763
return true
@@ -776,6 +769,18 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
776769
cs.cancel()
777770
}
778771

772+
companion object {
773+
val logger = Logger.getInstance(CoderWorkspacesStepView::class.java.simpleName)
774+
}
775+
}
776+
777+
class WorkspacesTableModel : ListTableModel<WorkspaceAgentModel>(
778+
WorkspaceIconColumnInfo(""),
779+
WorkspaceNameColumnInfo("Name"),
780+
WorkspaceTemplateNameColumnInfo("Template"),
781+
WorkspaceVersionColumnInfo("Version"),
782+
WorkspaceStatusColumnInfo("Status")
783+
) {
779784
private class WorkspaceIconColumnInfo(columnName: String) : ColumnInfo<WorkspaceAgentModel, String>(columnName) {
780785
override fun valueOf(workspace: WorkspaceAgentModel?): String? {
781786
return workspace?.templateName
@@ -803,7 +808,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
803808
}
804809
}
805810

806-
private inner class WorkspaceNameColumnInfo(columnName: String) : ColumnInfo<WorkspaceAgentModel, String>(columnName) {
811+
private class WorkspaceNameColumnInfo(columnName: String) : ColumnInfo<WorkspaceAgentModel, String>(columnName) {
807812
override fun valueOf(workspace: WorkspaceAgentModel?): String? {
808813
return workspace?.name
809814
}
@@ -822,15 +827,16 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
822827
text = value
823828
}
824829

825-
font = RelativeFont.BOLD.derive(this@CoderWorkspacesStepView.tableOfWorkspaces.tableHeader.font)
830+
font = RelativeFont.BOLD.derive(table.tableHeader.font)
826831
border = JBUI.Borders.empty(0, 8)
827832
return this
828833
}
829834
}
830835
}
831836
}
832837

833-
private inner class WorkspaceTemplateNameColumnInfo(columnName: String) : ColumnInfo<WorkspaceAgentModel, String>(columnName) {
838+
private class WorkspaceTemplateNameColumnInfo(columnName: String) :
839+
ColumnInfo<WorkspaceAgentModel, String>(columnName) {
834840
override fun valueOf(workspace: WorkspaceAgentModel?): String? {
835841
return workspace?.templateName
836842
}
@@ -842,11 +848,6 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
842848
}
843849

844850
override fun getRenderer(item: WorkspaceAgentModel?): TableCellRenderer {
845-
val simpleH3 = this@CoderWorkspacesStepView.tableOfWorkspaces.tableHeader.font
846-
847-
val h3AttributesWithUnderlining = simpleH3.attributes as MutableMap<TextAttribute, Any>
848-
h3AttributesWithUnderlining[TextAttribute.UNDERLINE] = UNDERLINE_ON
849-
val underlinedH3 = JBFont.h3().deriveFont(h3AttributesWithUnderlining)
850851
return object : DefaultTableCellRenderer() {
851852
override fun getTableCellRendererComponent(table: JTable, value: Any, isSelected: Boolean, hasFocus: Boolean, row: Int, column: Int): Component {
852853
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column)
@@ -855,10 +856,13 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
855856
}
856857
border = JBUI.Borders.empty(0, 8)
857858

859+
val simpleH3 = table.tableHeader.font
858860
if (table.getClientProperty(MOUSE_OVER_TEMPLATE_NAME_COLUMN_ON_ROW) != null) {
859861
val mouseOverRow = table.getClientProperty(MOUSE_OVER_TEMPLATE_NAME_COLUMN_ON_ROW) as Int
860862
if (mouseOverRow >= 0 && mouseOverRow == row) {
861-
font = underlinedH3
863+
val h3AttributesWithUnderlining = simpleH3.attributes as MutableMap<TextAttribute, Any>
864+
h3AttributesWithUnderlining[TextAttribute.UNDERLINE] = UNDERLINE_ON
865+
font = JBFont.h3().deriveFont(h3AttributesWithUnderlining)
862866
return this
863867
}
864868
}
@@ -869,7 +873,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
869873
}
870874
}
871875

872-
private inner class WorkspaceVersionColumnInfo(columnName: String) : ColumnInfo<WorkspaceAgentModel, String>(columnName) {
876+
private class WorkspaceVersionColumnInfo(columnName: String) : ColumnInfo<WorkspaceAgentModel, String>(columnName) {
873877
override fun valueOf(workspace: WorkspaceAgentModel?): String? {
874878
return workspace?.status?.label
875879
}
@@ -881,15 +885,15 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
881885
if (value is String) {
882886
text = value
883887
}
884-
font = this@CoderWorkspacesStepView.tableOfWorkspaces.tableHeader.font
888+
font = table.tableHeader.font
885889
border = JBUI.Borders.empty(0, 8)
886890
return this
887891
}
888892
}
889893
}
890894
}
891895

892-
private inner class WorkspaceStatusColumnInfo(columnName: String) : ColumnInfo<WorkspaceAgentModel, String>(columnName) {
896+
private class WorkspaceStatusColumnInfo(columnName: String) : ColumnInfo<WorkspaceAgentModel, String>(columnName) {
893897
override fun valueOf(workspace: WorkspaceAgentModel?): String? {
894898
return workspace?.agentStatus?.label
895899
}
@@ -902,29 +906,24 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
902906

903907
override fun getRenderer(item: WorkspaceAgentModel?): TableCellRenderer {
904908
return object : DefaultTableCellRenderer() {
905-
override fun getTableCellRendererComponent(
906-
table: JTable,
907-
value: Any,
908-
isSelected: Boolean,
909-
hasFocus: Boolean,
910-
row: Int,
911-
column: Int,
912-
): Component {
909+
override fun getTableCellRendererComponent(table: JTable, value: Any, isSelected: Boolean, hasFocus: Boolean, row: Int, column: Int): Component {
913910
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column)
914911
if (value is String) {
915912
text = value
916913
foreground = WorkspaceAndAgentStatus.from(value).statusColor()
917914
toolTipText = WorkspaceAndAgentStatus.from(value).description
918915
}
919-
font = this@CoderWorkspacesStepView.tableOfWorkspaces.tableHeader.font
916+
font = table.tableHeader.font
920917
border = JBUI.Borders.empty(0, 8)
921918
return this
922919
}
923920
}
924921
}
925922
}
923+
}
926924

927-
private fun TableView<WorkspaceAgentModel>.selectItem(workspaceName: String?) {
925+
class WorkspacesTable : TableView<WorkspaceAgentModel>(WorkspacesTableModel()) {
926+
fun selectItem(workspaceName: String?) {
928927
if (workspaceName != null) {
929928
this.items.forEachIndexed { index, workspaceAgentModel ->
930929
if (workspaceAgentModel.name == workspaceName) {
@@ -935,8 +934,4 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
935934
}
936935
}
937936
}
938-
939-
companion object {
940-
val logger = Logger.getInstance(CoderWorkspacesStepView::class.java.simpleName)
941-
}
942937
}

0 commit comments

Comments
 (0)