diff --git a/src/main/kotlin/org/http4k/intellij/step/ChoiceView.kt b/src/main/kotlin/org/http4k/intellij/step/ChoiceView.kt index 4c8f70f..9349207 100644 --- a/src/main/kotlin/org/http4k/intellij/step/ChoiceView.kt +++ b/src/main/kotlin/org/http4k/intellij/step/ChoiceView.kt @@ -3,27 +3,34 @@ package org.http4k.intellij.step import org.http4k.intellij.wizard.Answer import org.http4k.intellij.wizard.Option import org.http4k.intellij.wizard.Step -import java.awt.BorderLayout -import java.awt.BorderLayout.CENTER -import java.awt.BorderLayout.NORTH -import java.awt.Component.LEFT_ALIGNMENT +import java.awt.BorderLayout.SOUTH import java.awt.GridLayout import javax.swing.ButtonGroup import javax.swing.JPanel import javax.swing.JRadioButton fun ChoiceView(choice: Step.Choice, parent: JPanel, onComplete: OnComplete): JPanel { - val panel = JPanel().apply { - layout = BorderLayout() - alignmentX = LEFT_ALIGNMENT - add(choice.label.label().bold(), NORTH) - } + val childAnswers = mutableListOf() + var selected = choice.options.first { it.default } - return panel.apply { - val nextButton = NextButton(false) + val panel = QuestionPanel(choice.label, false) + + panel.nextButton.apply { + addActionListener { + when { + selected.steps.isEmpty() -> + onComplete(choice.answersFor(selected, childAnswers)) - var selected = choice.options.first { it.default } + else -> parent.add(ChildStepsView(selected.steps, parent) { + onComplete(choice.answersFor(selected, childAnswers.toList() + it)) + }) + } + parent.remove(panel) + parent.revalidate() + } + } + return panel.apply { val buttonGroup = ButtonGroup() val optionsPanel = JPanel().apply { @@ -42,24 +49,8 @@ fun ChoiceView(choice: Step.Choice, parent: JPanel, onComplete: OnComplete): JPa optionsPanel.add(OptionBox(button, option)) } - panel.add(optionsPanel, CENTER) - - val childAnswers = mutableListOf() - - add(nextButton.apply { - addActionListener { - when { - selected.steps.isEmpty() -> - onComplete(choice.answersFor(selected, childAnswers)) + panel.add(optionsPanel, SOUTH) - else -> parent.add(ChildStepsView(selected.steps, parent) { - onComplete(choice.answersFor(selected, childAnswers.toList() + it)) - }) - } - parent.remove(panel) - parent.revalidate() - } - }, nextLocation) } } diff --git a/src/main/kotlin/org/http4k/intellij/step/InputView.kt b/src/main/kotlin/org/http4k/intellij/step/InputView.kt index af72a9b..ff1d673 100644 --- a/src/main/kotlin/org/http4k/intellij/step/InputView.kt +++ b/src/main/kotlin/org/http4k/intellij/step/InputView.kt @@ -2,19 +2,14 @@ package org.http4k.intellij.step import org.http4k.intellij.wizard.Answer import org.http4k.intellij.wizard.Step -import java.awt.BorderLayout import java.awt.Component.LEFT_ALIGNMENT import javax.swing.JPanel import javax.swing.JTextField fun InputView(input: Step.Input, parent: JPanel, onComplete: OnComplete): JPanel { - val panel = JPanel().apply { - layout = BorderLayout() - alignmentX = LEFT_ALIGNMENT - add(input.label.label().apply { alignmentX = LEFT_ALIGNMENT }) - } + + val panel = QuestionPanel(input.label, true) return panel.apply { - val nextButton = NextButton() val selection = JTextField().apply { alignmentX = LEFT_ALIGNMENT text = input.default @@ -22,12 +17,12 @@ fun InputView(input: Step.Input, parent: JPanel, onComplete: OnComplete): JPanel } add(selection) - add(nextButton.apply { + panel.nextButton.apply { addActionListener { onComplete(listOf(Answer.Text(input.label, listOf(selection.text)))) parent.remove(panel) parent.revalidate() } - }, nextLocation) + } } } diff --git a/src/main/kotlin/org/http4k/intellij/step/MultiChoiceView.kt b/src/main/kotlin/org/http4k/intellij/step/MultiChoiceView.kt index 403b558..ba45bf1 100644 --- a/src/main/kotlin/org/http4k/intellij/step/MultiChoiceView.kt +++ b/src/main/kotlin/org/http4k/intellij/step/MultiChoiceView.kt @@ -2,20 +2,14 @@ package org.http4k.intellij.step import org.http4k.intellij.wizard.Answer import org.http4k.intellij.wizard.Step -import java.awt.BorderLayout import java.awt.BorderLayout.CENTER -import java.awt.BorderLayout.NORTH -import java.awt.Component.LEFT_ALIGNMENT import java.awt.GridLayout import javax.swing.JCheckBox import javax.swing.JPanel fun MultiChoiceView(multiChoice: Step.MultiChoice, parent: JPanel, onComplete: OnComplete): JPanel { - val panel = JPanel().apply { - layout = BorderLayout() - alignmentX = LEFT_ALIGNMENT - add(multiChoice.label.label(), NORTH) - } + + val panel = QuestionPanel(multiChoice.label, true) return panel.apply { val selected = multiChoice.options.filter { it.default }.toMutableSet() @@ -37,7 +31,7 @@ fun MultiChoiceView(multiChoice: Step.MultiChoice, parent: JPanel, onComplete: O panel.add(optionsPanel, CENTER) - add(NextButton(true).apply { + panel.nextButton.apply { addActionListener { onComplete( listOf( @@ -51,7 +45,7 @@ fun MultiChoiceView(multiChoice: Step.MultiChoice, parent: JPanel, onComplete: O parent.remove(panel) parent.revalidate() } - }, nextLocation) + } } } diff --git a/src/main/kotlin/org/http4k/intellij/step/util.kt b/src/main/kotlin/org/http4k/intellij/step/util.kt index 20daa1c..f571c67 100644 --- a/src/main/kotlin/org/http4k/intellij/step/util.kt +++ b/src/main/kotlin/org/http4k/intellij/step/util.kt @@ -3,7 +3,12 @@ package org.http4k.intellij.step import java.awt.BorderLayout.EAST import java.awt.Component.LEFT_ALIGNMENT import java.awt.Font.BOLD +import javax.swing.Box +import javax.swing.BoxLayout +import javax.swing.BoxLayout.X_AXIS +import javax.swing.BoxLayout.Y_AXIS import javax.swing.JLabel +import javax.swing.JPanel const val nextLocation = EAST @@ -18,3 +23,21 @@ fun JLabel.bold() = apply { fun JLabel.header() = bold().apply { font = font.deriveFont(20f) } + + +class QuestionPanel(question: String, nextEnabled: Boolean) : JPanel() { + val nextButton = NextButton(nextEnabled) + + init { + layout = BoxLayout(this, Y_AXIS) + alignmentX = LEFT_ALIGNMENT + + add( + JPanel().apply { + layout = BoxLayout(this, X_AXIS) + add(question.label().bold()) + add(nextButton) + }) + add(Box.createVerticalStrut(10)) + } +}