Skip to content

Commit

Permalink
View changes
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Sep 1, 2024
1 parent 0b0f147 commit 23f4cf3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 48 deletions.
49 changes: 20 additions & 29 deletions src/main/kotlin/org/http4k/intellij/step/ChoiceView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Answer>()
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 {
Expand All @@ -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<Answer>()

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)
}
}

Expand Down
13 changes: 4 additions & 9 deletions src/main/kotlin/org/http4k/intellij/step/InputView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,27 @@ 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
addActionListener { nextButton.isEnabled = text.isNotEmpty() }
}

add(selection)
add(nextButton.apply {
panel.nextButton.apply {
addActionListener {
onComplete(listOf(Answer.Text(input.label, listOf(selection.text))))
parent.remove(panel)
parent.revalidate()
}
}, nextLocation)
}
}
}
14 changes: 4 additions & 10 deletions src/main/kotlin/org/http4k/intellij/step/MultiChoiceView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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(
Expand All @@ -51,7 +45,7 @@ fun MultiChoiceView(multiChoice: Step.MultiChoice, parent: JPanel, onComplete: O
parent.remove(panel)
parent.revalidate()
}
}, nextLocation)
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/main/kotlin/org/http4k/intellij/step/util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
}
}

0 comments on commit 23f4cf3

Please sign in to comment.