fun handleProgressStateChanged()

in plugins/amazonq/codetransform/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codemodernizer/panels/managers/BuildProgressSplitterPanelManager.kt [115:327]


    fun handleProgressStateChanged(newState: TransformationStatus, plan: TransformationPlan?, jdk: JavaSdkVersion, transformType: CodeTransformType) {
        val currentState = statusTreePanel.getCurrentElements()
        val loadingPanelText: String
        // show the details panel when there are progress updates
        // otherwise it would show an empty panel
        val backendProgressStepsAvailable = (
            plan != null &&
                plan.hasTransformationSteps() &&
                haveProgressUpdates(plan)
            )

        fun maybeAddTransformationStep(stepId: ProgressStepId, string: String) {
            // don't show building or generate plan message for SQL conversions since we don't build or generate plan
            if (transformType == CodeTransformType.SQL_CONVERSION && (
                    string == message("codemodernizer.toolwindow.progress.building") ||
                        string == message("codemodernizer.toolwindow.progress.planning")
                    )
            ) {
                return
            }
            if (currentState.none { it.id == stepId }) {
                currentState.add(BuildProgressStepTreeItem(string, BuildStepStatus.WORKING, stepId))
            }
        }

        if (newState in setOf(
                TransformationStatus.ACCEPTED,
                TransformationStatus.STARTED,
                TransformationStatus.PREPARING,
            )
        ) {
            maybeAddTransformationStep(ProgressStepId.UPLOADING, message("codemodernizer.toolwindow.progress.uploading"))
            maybeAddTransformationStep(ProgressStepId.BUILDING, message("codemodernizer.toolwindow.progress.building"))
        }
        if (newState in setOf(TransformationStatus.PREPARED, newState == TransformationStatus.PLANNING)) {
            maybeAddTransformationStep(ProgressStepId.UPLOADING, message("codemodernizer.toolwindow.progress.uploading"))
            maybeAddTransformationStep(ProgressStepId.BUILDING, message("codemodernizer.toolwindow.progress.building"))
            maybeAddTransformationStep(ProgressStepId.PLANNING, message("codemodernizer.toolwindow.progress.planning"))
        }
        if (newState in setOf(
                TransformationStatus.PLANNED,
                TransformationStatus.TRANSFORMING,
            )
        ) {
            maybeAddTransformationStep(ProgressStepId.UPLOADING, message("codemodernizer.toolwindow.progress.uploading"))
            maybeAddTransformationStep(ProgressStepId.BUILDING, message("codemodernizer.toolwindow.progress.building"))
            maybeAddTransformationStep(ProgressStepId.PLANNING, message("codemodernizer.toolwindow.progress.planning"))
            maybeAddTransformationStep(ProgressStepId.TRANSFORMING, message("codemodernizer.toolwindow.progress.transforming"))
        }

        if (newState == TransformationStatus.PAUSED) {
            maybeAddTransformationStep(ProgressStepId.UPLOADING, message("codemodernizer.toolwindow.progress.uploading"))
            maybeAddTransformationStep(ProgressStepId.BUILDING, message("codemodernizer.toolwindow.progress.building"))
            maybeAddTransformationStep(ProgressStepId.PLANNING, message("codemodernizer.toolwindow.progress.planning"))
            maybeAddTransformationStep(ProgressStepId.TRANSFORMING, message("codemodernizer.toolwindow.progress.transforming"))
        }

        if (newState == TransformationStatus.RESUMED) {
            maybeAddTransformationStep(ProgressStepId.UPLOADING, message("codemodernizer.toolwindow.progress.uploading"))
            maybeAddTransformationStep(ProgressStepId.BUILDING, message("codemodernizer.toolwindow.progress.building"))
            maybeAddTransformationStep(ProgressStepId.PLANNING, message("codemodernizer.toolwindow.progress.planning"))
            maybeAddTransformationStep(ProgressStepId.TRANSFORMING, message("codemodernizer.toolwindow.progress.transforming"))
        }

        if (newState in setOf(
                TransformationStatus.COMPLETED,
                TransformationStatus.PARTIALLY_COMPLETED,
            )
        ) {
            maybeAddTransformationStep(ProgressStepId.UPLOADING, message("codemodernizer.toolwindow.progress.uploading"))
            maybeAddTransformationStep(ProgressStepId.BUILDING, message("codemodernizer.toolwindow.progress.building"))
            maybeAddTransformationStep(ProgressStepId.PLANNING, message("codemodernizer.toolwindow.progress.planning"))
            maybeAddTransformationStep(ProgressStepId.TRANSFORMING, message("codemodernizer.toolwindow.progress.transforming"))
        }

        // Figure out if we should add plan steps
        val statuses: List<BuildProgressStepTreeItem> = if (currentState.isEmpty()) {
            defaultProgressData().toList()
        } else {
            if (backendProgressStepsAvailable && plan != null) {
                currentBuildingTransformationStep = newBuildingTransformationStep
                // skip step 0 (contains supplemental info)
                newBuildingTransformationStep = plan.transformationSteps().size - 1
                val transformationPlanSteps = plan.transformationSteps()?.drop(1)?.map {
                    getUpdatedBuildProgressStepTreeItem(it)
                }
                transformationPlanSteps?.sortedBy { it.transformationStepId }
                val transformationPlanStepsMayAdded = maybeAddTransformationPlanSteps(transformationPlanSteps)
                currentState.removeAll { it.id == ProgressStepId.PLAN_STEP }
                if (transformationPlanStepsMayAdded.isNullOrEmpty()) {
                    currentState
                } else {
                    currentState + transformationPlanStepsMayAdded
                }
            } else {
                currentState
            }
        }
        val updatedStatuses = when (newState) {
            TransformationStatus.CREATED, TransformationStatus.ACCEPTED, TransformationStatus.STARTED -> {
                loadingPanelText = message("codemodernizer.toolwindow.scan_in_progress.accepted")
                statuses.update(BuildStepStatus.DONE, ProgressStepId.UPLOADING)
            }

            TransformationStatus.PREPARING -> {
                loadingPanelText = if (transformType == CodeTransformType.SQL_CONVERSION) {
                    message("codemodernizer.toolwindow.scan_in_progress.transforming")
                } else {
                    message("codemodernizer.toolwindow.scan_in_progress.building", jdk.description)
                }
                statuses.update(BuildStepStatus.DONE, ProgressStepId.UPLOADING)
            }

            TransformationStatus.PREPARED -> {
                loadingPanelText = if (transformType == CodeTransformType.SQL_CONVERSION) {
                    message("codemodernizer.toolwindow.scan_in_progress.transforming")
                } else {
                    message("codemodernizer.toolwindow.scan_in_progress.building", jdk.description)
                }
                statuses.update(BuildStepStatus.DONE, ProgressStepId.BUILDING)
            }

            TransformationStatus.PLANNING -> {
                loadingPanelText = if (transformType == CodeTransformType.SQL_CONVERSION) {
                    message("codemodernizer.toolwindow.scan_in_progress.transforming")
                } else {
                    message("codemodernizer.toolwindow.scan_in_progress.planning")
                }
                statuses.update(BuildStepStatus.DONE, ProgressStepId.BUILDING)
            }

            TransformationStatus.PLANNED -> {
                loadingPanelText = if (transformType == CodeTransformType.SQL_CONVERSION) {
                    message("codemodernizer.toolwindow.scan_in_progress.transforming")
                } else {
                    message("codemodernizer.toolwindow.scan_in_progress.planning")
                }
                statuses.update(BuildStepStatus.DONE, ProgressStepId.PLANNING)
            }

            TransformationStatus.TRANSFORMING -> {
                loadingPanelText = message("codemodernizer.toolwindow.scan_in_progress.transforming")
                statuses.update(BuildStepStatus.DONE, ProgressStepId.PLANNING)
            }

            TransformationStatus.PAUSED -> {
                loadingPanelText = message("codemodernizer.toolwindow.scan_in_progress.transforming")
                statuses.update(BuildStepStatus.DONE, ProgressStepId.PLANNING)
            }

            TransformationStatus.RESUMED -> {
                loadingPanelText = message("codemodernizer.toolwindow.scan_in_progress.transforming")
                statuses.update(BuildStepStatus.DONE, ProgressStepId.PLANNING)
            }

            TransformationStatus.TRANSFORMED -> {
                loadingPanelText = message("codemodernizer.toolwindow.scan_in_progress.transforming")
                statuses.update(BuildStepStatus.DONE, ProgressStepId.TRANSFORMING)
            }

            TransformationStatus.COMPLETED, TransformationStatus.PARTIALLY_COMPLETED -> {
                loadingPanelText = message("codemodernizer.toolwindow.scan_in_progress.transforming")
                statuses.update(BuildStepStatus.DONE, ProgressStepId.TRANSFORMING)
            }

            TransformationStatus.STOPPING -> {
                loadingPanelText = message("codemodernizer.toolwindow.scan_in_progress.stopping")
                statuses
            } // noop

            TransformationStatus.UNKNOWN_TO_SDK_VERSION -> {
                throw CodeModernizerException(message("codemodernizer.notification.warn.unknown_status_response"))
            }

            else -> {
                if (newState == TransformationStatus.STOPPED) {
                    loadingPanelText = message("codemodernizer.toolwindow.scan_in_progress.stopping")
                } else {
                    loadingPanelText = message("codemodernizer.toolwindow.scan_in_progress.failed")
                }
                statuses.map {
                    if (it.status == BuildStepStatus.WORKING) {
                        it.copy(status = BuildStepStatus.ERROR)
                    } else {
                        it
                    }
                }
            }
        }
        statusTreePanel.renderTree(updatedStatuses)
        if (backendProgressStepsAvailable && newState != TransformationStatus.FAILED) {
            if (this.secondComponent == loadingPanel) {
                this.remove(loadingPanel)
                setProgressStepsDefaultUI()
            }
            if (plan != null) {
                buildProgressStepDetailsPanel.setTransformationPlan(plan)
                // automatically jump to the next step's right details' panel only when the current step is finished and next step starts
                if (newBuildingTransformationStep == 1 || currentBuildingTransformationStep != newBuildingTransformationStep) {
                    buildProgressStepDetailsPanel.updateListData(newBuildingTransformationStep)
                }
            }
            repaint()
            revalidate()
        } else if (newState == TransformationStatus.STOPPED || newState == TransformationStatus.FAILED) {
            setSplitPanelStopView()
            buildProgressStepDetailsPanel.setStopView(newState)
            revalidate()
            repaint()
        } else {
            loadingPanel.updateProgressIndicatorText(loadingPanelText)
        }
    }