|
| 1 | +package com.coder.gateway.views.steps |
| 2 | + |
| 3 | +import com.intellij.openapi.Disposable |
| 4 | +import com.intellij.openapi.wm.impl.welcomeScreen.WelcomeScreenUIManager |
| 5 | +import com.intellij.ui.dsl.builder.AlignX |
| 6 | +import com.intellij.ui.dsl.builder.RightGap |
| 7 | +import com.intellij.ui.dsl.builder.panel |
| 8 | +import com.intellij.util.ui.JBUI |
| 9 | +import com.intellij.util.ui.components.BorderLayoutPanel |
| 10 | +import com.jetbrains.gateway.api.GatewayUI |
| 11 | +import javax.swing.JButton |
| 12 | + |
| 13 | +/** |
| 14 | + * Wrapper around all wizard steps. This view takes you from configuring a URL |
| 15 | + * to connecting to a workspace. |
| 16 | + */ |
| 17 | +class CoderWizardView( |
| 18 | + private val onFinish: (data: Map<String, String>) -> Unit, |
| 19 | +) : BorderLayoutPanel(), Disposable { |
| 20 | + private lateinit var previousButton: JButton |
| 21 | + private lateinit var nextButton: JButton |
| 22 | + |
| 23 | + // These are not in a list because the types of one step lead into the types |
| 24 | + // of the next and it would not be possible to do that with a generic type |
| 25 | + // on a list. It could possibly be refactored to have steps point to their |
| 26 | + // own next step which might be cleaner, but this works. |
| 27 | + private val step1 = CoderWorkspacesStepView { nextButton.isEnabled = it } |
| 28 | + private val step2 = CoderWorkspaceStepView { nextButton.isEnabled = it } |
| 29 | + private var current: CoderWizardStep? = null |
| 30 | + |
| 31 | + private val buttons = panel { |
| 32 | + separator(background = WelcomeScreenUIManager.getSeparatorColor()) |
| 33 | + row { |
| 34 | + label("").resizableColumn().align(AlignX.FILL).gap(RightGap.SMALL) |
| 35 | + previousButton = button("") { previous() } |
| 36 | + .align(AlignX.RIGHT).gap(RightGap.SMALL) |
| 37 | + .applyToComponent { background = WelcomeScreenUIManager.getMainAssociatedComponentBackground() }.component |
| 38 | + nextButton = button("") { next() } |
| 39 | + .align(AlignX.RIGHT) |
| 40 | + .applyToComponent { background = WelcomeScreenUIManager.getMainAssociatedComponentBackground() }.component |
| 41 | + } |
| 42 | + }.apply { |
| 43 | + background = WelcomeScreenUIManager.getMainAssociatedComponentBackground() |
| 44 | + border = JBUI.Borders.empty(0, 16) |
| 45 | + } |
| 46 | + |
| 47 | + init { |
| 48 | + background = WelcomeScreenUIManager.getMainAssociatedComponentBackground() |
| 49 | + addToBottom(buttons) |
| 50 | + setStep(step1) |
| 51 | + step1.init() |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Replace the current step with the new one. |
| 56 | + */ |
| 57 | + private fun setStep(step: CoderWizardStep) { |
| 58 | + current?.apply { |
| 59 | + remove(component) |
| 60 | + stop() |
| 61 | + } |
| 62 | + current = step |
| 63 | + step.apply { |
| 64 | + addToCenter(component.apply { |
| 65 | + background = WelcomeScreenUIManager.getMainAssociatedComponentBackground() |
| 66 | + border = JBUI.Borders.empty(0, 16) |
| 67 | + }) |
| 68 | + nextButton.text = nextActionText |
| 69 | + previousButton.text = previousActionText |
| 70 | + nextButton.isEnabled = false |
| 71 | + updateUI() |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private fun previous() { |
| 76 | + when(current) { |
| 77 | + is CoderWorkspacesStepView -> { |
| 78 | + GatewayUI.getInstance().reset() |
| 79 | + dispose() |
| 80 | + } |
| 81 | + is CoderWorkspaceStepView -> { |
| 82 | + setStep(step1) |
| 83 | + step1.init() |
| 84 | + } |
| 85 | + null -> throw Error("Unexpected null step") |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private fun next() { |
| 90 | + when(current) { |
| 91 | + is CoderWorkspacesStepView -> { |
| 92 | + setStep(step2) |
| 93 | + step2.init(step1.data()) |
| 94 | + } |
| 95 | + is CoderWorkspaceStepView -> { |
| 96 | + onFinish(step2.data()) |
| 97 | + GatewayUI.getInstance().reset() |
| 98 | + dispose() |
| 99 | + } |
| 100 | + null -> throw Error("Unexpected null step") |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + override fun dispose() { |
| 105 | + step1.dispose() |
| 106 | + step2.dispose() |
| 107 | + } |
| 108 | +} |
0 commit comments