[feedback] added RadioButtonGroupBlock

GitOrigin-RevId: 8b5bb644e297d02037ae76ff65c3ca5afe70c48c
This commit is contained in:
Petr Kudriavtsev
2023-10-20 20:34:11 +02:00
committed by intellij-monorepo-bot
parent 16df83ae83
commit ed4fb70c3e
2 changed files with 81 additions and 0 deletions

View File

@@ -44,6 +44,8 @@ dialog.feedback.combobox.required=Choose a value
dialog.feedback.checkboxGroup.other.placeholder=Other
dialog.feedback.checkboxGroup.require.not.empty=Choose options or write your own
dialog.feedback.radioGroup.require.not.empty=Choose an option
notification.request.feedback.action.respond.text=Respond
notification.request.feedback.action.dont.show.text=Don't show again
notification.thanks.feedback.title=Thank you for your feedback

View File

@@ -0,0 +1,79 @@
package com.intellij.platform.feedback.dialog.uiBlocks
import com.intellij.openapi.ui.validation.WHEN_STATE_CHANGED
import com.intellij.openapi.util.NlsContexts
import com.intellij.platform.feedback.impl.bundle.CommonFeedbackBundle
import com.intellij.ui.components.JBRadioButton
import com.intellij.ui.dsl.builder.*
import kotlinx.serialization.json.JsonObjectBuilder
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
class RadioButtonGroupBlock(
@NlsContexts.Label private val myGroupLabel: String,
private val myItemsData: List<CheckBoxItemData>,
private val myJsonGroupName: String) : FeedbackBlock, TextDescriptionProvider, JsonDataProvider {
private var requireAnswer = false
override fun addToPanel(panel: Panel) {
val allButtons: ArrayList<JBRadioButton> = arrayListOf()
panel.apply {
buttonsGroup(indent = false) {
row {
label(myGroupLabel)
.bold()
.errorOnApply(CommonFeedbackBundle.message("dialog.feedback.radioGroup.require.not.empty")) {
return@errorOnApply requireAnswer && allButtons.all { !it.isSelected }
}.apply {
validationRequestor { parentDisposable, validate ->
allButtons.forEach {
WHEN_STATE_CHANGED.invoke(it).subscribe(parentDisposable, validate)
}
}
}
}.bottomGap(BottomGap.NONE)
myItemsData.forEachIndexed { i, itemData ->
row {
radioButton(itemData.label).bindSelected(
{ myItemsData[i].property },
{ myItemsData[i].property = it }
).applyToComponent {
allButtons.add(this)
}
}.apply {
if (i == myItemsData.size - 1) {
this.bottomGap(BottomGap.MEDIUM)
}
}
}
}
}
}
override fun collectBlockTextDescription(stringBuilder: StringBuilder) {
stringBuilder.apply {
appendLine(myGroupLabel)
myItemsData.forEach { itemData ->
appendLine(" ${itemData.label} - ${itemData.property}")
}
appendLine()
}
}
override fun collectBlockDataToJson(jsonObjectBuilder: JsonObjectBuilder) {
jsonObjectBuilder.apply {
put(myJsonGroupName, buildJsonObject {
myItemsData.forEach { itemData ->
put(itemData.jsonElementName, itemData.property)
}
})
}
}
fun requireAnswer(): RadioButtonGroupBlock {
requireAnswer = true
return this
}
}