Skip to content

Commit

Permalink
Clean up ComboBox usage
Browse files Browse the repository at this point in the history
  • Loading branch information
FWDekker committed Jan 3, 2024
1 parent 5887dbd commit e0d3269
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.fwdekker.randomness.ui.disableMnemonic
import com.fwdekker.randomness.ui.isEditable
import com.fwdekker.randomness.ui.loadMnemonic
import com.fwdekker.randomness.ui.withName
import com.intellij.openapi.ui.ComboBox
import com.intellij.ui.dsl.builder.Cell
import com.intellij.ui.dsl.builder.bindSelected
import com.intellij.ui.dsl.builder.panel
Expand Down Expand Up @@ -44,7 +43,7 @@ class AffixDecoratorEditor(
.bindSelected(scheme::enabled)
.also { enabledCheckBox = it }

cell(ComboBox(presets.toTypedArray()))
comboBox(presets)
.enabledIf(enabledIf?.and(enabledCheckBox.selected) ?: enabledCheckBox.selected)
.isEditable(true)
.withName(camelConcat(namePrefix, "affixDescriptor"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ data class ArrayDecorator(
/**
* The preset values for the [separator] field.
*/
val PRESET_SEPARATORS = arrayOf(", ", "; ", "\\n")
val PRESET_SEPARATORS = listOf(", ", "; ", "\\n")

/**
* The default value of the [separator] field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import com.fwdekker.randomness.ui.isEditable
import com.fwdekker.randomness.ui.loadMnemonic
import com.fwdekker.randomness.ui.withFixedWidth
import com.fwdekker.randomness.ui.withName
import com.intellij.openapi.ui.ComboBox
import com.intellij.ui.dsl.builder.BottomGap
import com.intellij.ui.dsl.builder.Cell
import com.intellij.ui.dsl.builder.bindSelected
Expand Down Expand Up @@ -86,7 +85,7 @@ class ArrayDecoratorEditor(
.bindSelected(scheme::separatorEnabled)
.also { separatorEnabledCheckBox = it.component }

cell(ComboBox(PRESET_SEPARATORS))
comboBox(PRESET_SEPARATORS)
.enabledIf(isEnabled.and(separatorEnabledCheckBox.selected))
.isEditable(true)
.withName("arraySeparator")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ data class DecimalScheme(
/**
* The preset values for the [decimalSeparator] field.
*/
val PRESET_DECIMAL_SEPARATORS = arrayOf(",", ".")
val PRESET_DECIMAL_SEPARATORS = listOf(",", ".")

/**
* The default value of the [decimalSeparator] field.
Expand All @@ -144,7 +144,7 @@ data class DecimalScheme(
/**
* The preset values for the [groupingSeparator] field.
*/
val PRESET_GROUPING_SEPARATORS = arrayOf(".", ",", "_")
val PRESET_GROUPING_SEPARATORS = listOf(".", ",", "_")

/**
* The default value of the [groupingSeparator] field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import com.fwdekker.randomness.ui.loadMnemonic
import com.fwdekker.randomness.ui.withFilter
import com.fwdekker.randomness.ui.withFixedWidth
import com.fwdekker.randomness.ui.withName
import com.intellij.openapi.ui.ComboBox
import com.intellij.ui.dsl.builder.AlignX
import com.intellij.ui.dsl.builder.Cell
import com.intellij.ui.dsl.builder.bindSelected
Expand Down Expand Up @@ -79,7 +78,7 @@ class DecimalSchemeEditor(scheme: DecimalScheme = DecimalScheme()) : SchemeEdito
}

row(Bundle("decimal.ui.format.decimal_separator_option")) {
cell(ComboBox(PRESET_DECIMAL_SEPARATORS))
comboBox(PRESET_DECIMAL_SEPARATORS)
.isEditable(true)
.withFilter(MinMaxLengthDocumentFilter(1, 1))
.withName("decimalSeparator")
Expand All @@ -95,7 +94,7 @@ class DecimalSchemeEditor(scheme: DecimalScheme = DecimalScheme()) : SchemeEdito
.bindSelected(scheme::groupingSeparatorEnabled)
.also { groupingSeparatorEnabled = it }

cell(ComboBox(PRESET_GROUPING_SEPARATORS))
comboBox(PRESET_GROUPING_SEPARATORS)
.enabledIf(groupingSeparatorEnabled.selected)
.isEditable(true)
.withFilter(MinMaxLengthDocumentFilter(1, 1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ data class IntegerScheme(
/**
* The preset values for the [groupingSeparator] descriptor.
*/
val PRESET_GROUPING_SEPARATORS = arrayOf(".", ",", "_")
val PRESET_GROUPING_SEPARATORS = listOf(".", ",", "_")

/**
* The default value of the [groupingSeparator] field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import com.fwdekker.randomness.ui.loadMnemonic
import com.fwdekker.randomness.ui.withFilter
import com.fwdekker.randomness.ui.withFixedWidth
import com.fwdekker.randomness.ui.withName
import com.intellij.openapi.ui.ComboBox
import com.intellij.ui.dsl.builder.AlignX
import com.intellij.ui.dsl.builder.bindSelected
import com.intellij.ui.dsl.builder.panel
Expand Down Expand Up @@ -89,7 +88,7 @@ class IntegerSchemeEditor(scheme: IntegerScheme = IntegerScheme()) : SchemeEdito
.bindSelected(scheme::groupingSeparatorEnabled)
.also { groupingSeparatorEnabled = it.component }

cell(ComboBox(PRESET_GROUPING_SEPARATORS))
comboBox(PRESET_GROUPING_SEPARATORS)
.enabledIf(base.hasValue { it == DECIMAL_BASE }.and(groupingSeparatorEnabled.selected))
.withName("groupingSeparator")
.isEditable(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ data class StringScheme(
/**
* The preset values for the [capitalization] field.
*/
val PRESET_CAPITALIZATION = arrayOf(
val PRESET_CAPITALIZATION = listOf(
CapitalizationMode.RETAIN,
CapitalizationMode.LOWER,
CapitalizationMode.UPPER,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package com.fwdekker.randomness.string

import com.fwdekker.randomness.Bundle
import com.fwdekker.randomness.CapitalizationMode
import com.fwdekker.randomness.SchemeEditor
import com.fwdekker.randomness.array.ArrayDecoratorEditor
import com.fwdekker.randomness.string.StringScheme.Companion.PRESET_CAPITALIZATION
import com.fwdekker.randomness.ui.UIConstants
import com.fwdekker.randomness.ui.loadMnemonic
import com.fwdekker.randomness.ui.withFixedWidth
import com.fwdekker.randomness.ui.withName
import com.fwdekker.randomness.ui.withSimpleRenderer
import com.intellij.openapi.ui.ComboBox
import com.intellij.ui.dsl.builder.AlignX
import com.intellij.ui.dsl.builder.BottomGap
import com.intellij.ui.dsl.builder.bindItem
import com.intellij.ui.dsl.builder.bindSelected
import com.intellij.ui.dsl.builder.bindText
import com.intellij.ui.dsl.builder.listCellRenderer
import com.intellij.ui.dsl.builder.panel
import com.intellij.ui.dsl.builder.toNullableProperty
import com.intellij.ui.layout.selected
Expand Down Expand Up @@ -56,8 +54,7 @@ class StringSchemeEditor(scheme: StringScheme = StringScheme()) : SchemeEditor<S
}.bottomGap(BottomGap.SMALL)

row(Bundle("string.ui.value.capitalization_option")) {
cell(ComboBox(PRESET_CAPITALIZATION))
.withSimpleRenderer(CapitalizationMode::toLocalizedString)
comboBox(PRESET_CAPITALIZATION, listCellRenderer { it.toLocalizedString() })
.withName("capitalization")
.bindItem(scheme::capitalization.toNullableProperty())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ data class TemplateReference(
/**
* The preset values for the [capitalization] field.
*/
val PRESET_CAPITALIZATION = arrayOf(
val PRESET_CAPITALIZATION = listOf(
CapitalizationMode.RETAIN,
CapitalizationMode.LOWER,
CapitalizationMode.UPPER,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package com.fwdekker.randomness.template

import com.fwdekker.randomness.Bundle
import com.fwdekker.randomness.CapitalizationMode
import com.fwdekker.randomness.SchemeEditor
import com.fwdekker.randomness.affix.AffixDecoratorEditor
import com.fwdekker.randomness.array.ArrayDecoratorEditor
import com.fwdekker.randomness.template.TemplateReference.Companion.PRESET_AFFIX_DECORATOR_DESCRIPTORS
import com.fwdekker.randomness.template.TemplateReference.Companion.PRESET_CAPITALIZATION
import com.fwdekker.randomness.ui.onResetThis
import com.fwdekker.randomness.ui.withName
import com.fwdekker.randomness.ui.withSimpleRenderer
import com.intellij.openapi.ui.ComboBox
import com.intellij.ui.ColoredListCellRenderer
import com.intellij.ui.dsl.builder.AlignX
import com.intellij.ui.dsl.builder.bindItem
import com.intellij.ui.dsl.builder.listCellRenderer
import com.intellij.ui.dsl.builder.panel
import com.intellij.ui.dsl.builder.toNullableProperty
import javax.swing.JList
Expand Down Expand Up @@ -41,8 +39,7 @@ class TemplateReferenceEditor(scheme: TemplateReference) : SchemeEditor<Template
}

row(Bundle("reference.ui.value.capitalization_option")) {
cell(ComboBox(PRESET_CAPITALIZATION))
.withSimpleRenderer(CapitalizationMode::toLocalizedString)
comboBox(PRESET_CAPITALIZATION, listCellRenderer { it.toLocalizedString() })
.withName("capitalization")
.bindItem(scheme::capitalization.toNullableProperty())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.fwdekker.randomness.ui

import com.intellij.openapi.ui.ComboBox
import com.intellij.ui.components.JBLabel
import com.intellij.ui.dsl.builder.Cell
import com.intellij.ui.dsl.builder.Panel
import com.intellij.ui.dsl.builder.TopGap
import com.intellij.ui.dsl.builder.bindItem
import com.intellij.ui.dsl.builder.columns
import com.intellij.ui.dsl.builder.toMutableProperty
import com.intellij.ui.layout.ComponentPredicate
Expand Down Expand Up @@ -115,15 +113,6 @@ fun <E> Cell<ComboBox<E>>.withFilter(filter: DocumentFilter): Cell<ComboBox<E>>
return this
}

/**
* Sets an item renderer on the [ComboBox] in this [Cell] that renders a label displaying the mapping of an item using
* [toString], and returns `this`.
*/
fun <E> Cell<ComboBox<E>>.withSimpleRenderer(toString: (E) -> String = { it.toString() }): Cell<ComboBox<E>> {
component.setRenderer { _, value, _, _, _ -> JBLabel(toString(value)) }
return this
}


/**
* A predicate that always returns [output].
Expand Down Expand Up @@ -163,12 +152,6 @@ fun Cell<ComboBox<String>>.bindCurrentText(property: KMutableProperty0<String>)
property.toMutableProperty()
)

/**
* Binds the value of the [ComboBox] in this [Cell] to [property], asserting that the value is not `null`.
*/
fun <T : Any> Cell<ComboBox<T?>>.bindItemNotNull(property: KMutableProperty0<T>) =
bindItem({ property.get() }, { property.set(it!!) })

/**
* Binds the value of the [JIntSpinner] in this [Cell] to [property].
*/
Expand Down
33 changes: 27 additions & 6 deletions src/main/kotlin/com/fwdekker/randomness/uuid/UuidSchemeEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import com.fwdekker.randomness.Bundle
import com.fwdekker.randomness.SchemeEditor
import com.fwdekker.randomness.affix.AffixDecoratorEditor
import com.fwdekker.randomness.array.ArrayDecoratorEditor
import com.fwdekker.randomness.ui.bindItemNotNull
import com.fwdekker.randomness.ui.isEditable
import com.fwdekker.randomness.ui.loadMnemonic
import com.fwdekker.randomness.ui.withName
import com.fwdekker.randomness.ui.withSimpleRenderer
import com.fwdekker.randomness.uuid.UuidScheme.Companion.PRESET_AFFIX_DECORATOR_DESCRIPTORS
import com.intellij.openapi.ui.ComboBox
import com.intellij.ui.ColoredListCellRenderer
import com.intellij.ui.SimpleTextAttributes
import com.intellij.ui.dsl.builder.AlignX
import com.intellij.ui.dsl.builder.bindItem
import com.intellij.ui.dsl.builder.bindSelected
import com.intellij.ui.dsl.builder.panel
import com.intellij.ui.dsl.builder.toNullableProperty
import javax.swing.JList


/**
Expand All @@ -26,11 +28,10 @@ class UuidSchemeEditor(scheme: UuidScheme = UuidScheme()) : SchemeEditor<UuidSch
group(Bundle("uuid.ui.value.header")) {
panel {
row(Bundle("uuid.ui.value.version.option")) {
cell(ComboBox(UuidScheme.SUPPORTED_VERSIONS.toTypedArray()))
comboBox(UuidScheme.SUPPORTED_VERSIONS, UuidVersionRenderer())
.isEditable(false)
.withSimpleRenderer { Bundle("uuid.ui.value.version.$it") }
.withName("version")
.bindItemNotNull(scheme::version)
.bindItem(scheme::version.toNullableProperty())
}

row {
Expand Down Expand Up @@ -66,4 +67,24 @@ class UuidSchemeEditor(scheme: UuidScheme = UuidScheme()) : SchemeEditor<UuidSch
init {
reset()
}


/**
* Renders a supported UUID version.
*/
private class UuidVersionRenderer : ColoredListCellRenderer<Int>() {
override fun customizeCellRenderer(
list: JList<out Int>,
value: Int?,
index: Int,
selected: Boolean,
hasFocus: Boolean,
) {
if (value == null) return

append("$value")
append(" ")
append(Bundle("uuid.ui.value.version.$value"), SimpleTextAttributes.GRAYED_ATTRIBUTES)
}
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/com/fwdekker/randomness/word/WordScheme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ data class WordScheme(
/**
* The preset values for the [capitalization] field.
*/
val PRESET_CAPITALIZATION = arrayOf(
val PRESET_CAPITALIZATION = listOf(
CapitalizationMode.RETAIN,
CapitalizationMode.LOWER,
CapitalizationMode.UPPER,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.fwdekker.randomness.word

import com.fwdekker.randomness.Bundle
import com.fwdekker.randomness.CapitalizationMode
import com.fwdekker.randomness.SchemeEditor
import com.fwdekker.randomness.affix.AffixDecoratorEditor
import com.fwdekker.randomness.array.ArrayDecoratorEditor
Expand All @@ -10,7 +9,6 @@ import com.fwdekker.randomness.ui.addChangeListenerTo
import com.fwdekker.randomness.ui.onResetThis
import com.fwdekker.randomness.ui.withFixedHeight
import com.fwdekker.randomness.ui.withName
import com.fwdekker.randomness.ui.withSimpleRenderer
import com.fwdekker.randomness.word.WordScheme.Companion.PRESET_AFFIX_DECORATOR_DESCRIPTORS
import com.fwdekker.randomness.word.WordScheme.Companion.PRESET_CAPITALIZATION
import com.intellij.openapi.Disposable
Expand All @@ -25,6 +23,7 @@ import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.util.Disposer
import com.intellij.ui.dsl.builder.AlignX
import com.intellij.ui.dsl.builder.bindItem
import com.intellij.ui.dsl.builder.listCellRenderer
import com.intellij.ui.dsl.builder.panel
import com.intellij.ui.dsl.builder.toMutableProperty
import com.intellij.ui.dsl.builder.toNullableProperty
Expand All @@ -43,9 +42,8 @@ class WordSchemeEditor(scheme: WordScheme = WordScheme()) : SchemeEditor<WordSch
lateinit var document: Document

row(Bundle("word.ui.words.insert_option")) {
cell(ComboBox(arrayOf(PRESET_ITEM) + DefaultWordList.WORD_LISTS))
comboBox(listOf(PRESET_ITEM) + DefaultWordList.WORD_LISTS, listCellRenderer { it.name })
.withName("presets")
.withSimpleRenderer(DefaultWordList::name)
.also {
it.component.addItemListener { event ->
if (event.stateChange == ItemEvent.SELECTED) {
Expand Down Expand Up @@ -87,8 +85,7 @@ class WordSchemeEditor(scheme: WordScheme = WordScheme()) : SchemeEditor<WordSch

group(Bundle("word.ui.format.header")) {
row(Bundle("word.ui.format.capitalization_option")) {
cell(ComboBox(PRESET_CAPITALIZATION))
.withSimpleRenderer(CapitalizationMode::toLocalizedString)
comboBox(PRESET_CAPITALIZATION, listCellRenderer { it.toLocalizedString() })
.withName("capitalization")
.bindItem(scheme::capitalization.toNullableProperty())
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/resources/randomness.properties
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ uuid.add_dashes=&Add dashes
uuid.error.unknown_version=Unknown UUID version '%1$s'.
uuid.ui.value.capitalization_option=Upper&case
uuid.ui.value.header=Value
uuid.ui.value.version.1=v1: Time
uuid.ui.value.version.4=v4: Random
uuid.ui.value.version.6=v6: Time, reordered
uuid.ui.value.version.7=v7: Time, epoch
uuid.ui.value.version.8=v8: Random
uuid.ui.value.version.1=Time
uuid.ui.value.version.4=Random
uuid.ui.value.version.6=Time, reordered
uuid.ui.value.version.7=Time, epoch
uuid.ui.value.version.8=Random
uuid.ui.value.version.option=Version:
word.error.empty_word_list=Enter at least one word.
word.title=Word
Expand Down

0 comments on commit e0d3269

Please sign in to comment.