Wizard: rework UI components for settings
#KT-35712 fixed
This commit is contained in:
+32
@@ -0,0 +1,32 @@
|
||||
package org.jetbrains.kotlin.tools.projectWizard.wizard.ui.components
|
||||
|
||||
import com.intellij.ui.components.JBCheckBox
|
||||
import com.intellij.ui.components.JBTextField
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.ValuesReadingContext
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.SettingValidator
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.textField
|
||||
|
||||
class CheckboxComponent(
|
||||
valuesReadingContext: ValuesReadingContext,
|
||||
labelText: String? = null,
|
||||
initialValue: Boolean? = null,
|
||||
validator: SettingValidator<Boolean>? = null,
|
||||
onValueUpdate: (Boolean) -> Unit = {}
|
||||
) : UIComponent<Boolean>(
|
||||
valuesReadingContext,
|
||||
labelText,
|
||||
validator,
|
||||
onValueUpdate
|
||||
) {
|
||||
override val uiComponent: JBCheckBox = JBCheckBox(labelText, initialValue ?: false).apply {
|
||||
addChangeListener {
|
||||
fireValueUpdated(this@apply.isSelected)
|
||||
}
|
||||
}
|
||||
|
||||
override fun updateUiValue(newValue: Boolean) = safeUpdateUi {
|
||||
uiComponent.isSelected = newValue
|
||||
}
|
||||
|
||||
override fun getUiValue(): Boolean = uiComponent.isSelected
|
||||
}
|
||||
+20
-68
@@ -3,46 +3,34 @@ package org.jetbrains.kotlin.tools.projectWizard.wizard.ui.components
|
||||
import com.intellij.openapi.ui.ComboBox
|
||||
import com.intellij.ui.ColoredListCellRenderer
|
||||
import com.intellij.ui.SimpleTextAttributes
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.ValuesReadingContext
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.SettingValidator
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.ValidationResult
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.ValuesReadingContext
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.settingValidator
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.Component
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.panel
|
||||
import java.awt.BorderLayout
|
||||
import javax.swing.JComponent
|
||||
import org.jetbrains.kotlin.tools.projectWizard.settings.DisplayableSettingItem
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.setting.ValidationIndicator
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
||||
import java.awt.event.ItemEvent
|
||||
import javax.swing.DefaultComboBoxModel
|
||||
import javax.swing.Icon
|
||||
import javax.swing.JList
|
||||
|
||||
class DropDownComponent<T : DisplayableSettingItem>(
|
||||
private val valuesReadingContext: ValuesReadingContext,
|
||||
private val initialValues: List<T> = emptyList(),
|
||||
initialValues: List<T> = emptyList(),
|
||||
labelText: String? = null,
|
||||
private val filter: (T) -> Boolean = { true },
|
||||
filter: (T) -> Boolean = { true },
|
||||
private val validator: SettingValidator<T> = settingValidator { ValidationResult.OK },
|
||||
private val iconProvider: (T) -> Icon? = { null },
|
||||
private val onAnyValueUpdate: (T) -> Unit = {}
|
||||
) : Component() {
|
||||
onValueUpdate: (T) -> Unit = {}
|
||||
) : UIComponent<T>(
|
||||
valuesReadingContext,
|
||||
labelText,
|
||||
validator,
|
||||
onValueUpdate
|
||||
) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
|
||||
private val validationIndicator = ValidationIndicator(defaultText = labelText, showText = true)
|
||||
|
||||
private var allowFireEventWhenValueUpdated: Boolean = true
|
||||
|
||||
private fun withoutActionFiring(action: () -> Unit) {
|
||||
val initialAllowFireEventWhenValueUpdated = allowFireEventWhenValueUpdated
|
||||
allowFireEventWhenValueUpdated = false
|
||||
action()
|
||||
allowFireEventWhenValueUpdated = initialAllowFireEventWhenValueUpdated
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private val comboBox = ComboBox<T>().apply {
|
||||
override val uiComponent: ComboBox<T> = ComboBox<T>(
|
||||
initialValues.filter(filter).toTypedArray<DisplayableSettingItem>() as Array<T>
|
||||
).apply {
|
||||
renderer = object : ColoredListCellRenderer<T>() {
|
||||
override fun customizeCellRenderer(
|
||||
list: JList<out T>,
|
||||
@@ -69,55 +57,19 @@ class DropDownComponent<T : DisplayableSettingItem>(
|
||||
}
|
||||
}
|
||||
|
||||
addItemListener { e ->
|
||||
if (e?.stateChange == ItemEvent.SELECTED) {
|
||||
value = e.item as T
|
||||
addItemListener { event ->
|
||||
if (event?.stateChange == ItemEvent.SELECTED) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
(event.item as? T)?.let(::fireValueUpdated)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onInit() {
|
||||
super.onInit()
|
||||
updateValues(initialValues)
|
||||
}
|
||||
|
||||
fun updateValues(newValues: List<T>) {
|
||||
val newValuesFiltered = newValues.filter(filter)
|
||||
val oldValue = comboBox.selectedItem
|
||||
withoutActionFiring {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
comboBox.model = DefaultComboBoxModel(newValuesFiltered.toTypedArray<DisplayableSettingItem>() as Array<T>)
|
||||
}
|
||||
|
||||
if (oldValue !in newValuesFiltered) {
|
||||
newValuesFiltered.firstOrNull()?.let { newValue ->
|
||||
value = newValuesFiltered.first()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override val component: JComponent = panel {
|
||||
add(validationIndicator, BorderLayout.NORTH)
|
||||
add(comboBox, BorderLayout.CENTER)
|
||||
override fun updateUiValue(newValue: T) = safeUpdateUi {
|
||||
uiComponent.selectedItem = newValue
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
var value: T
|
||||
get() = comboBox.selectedItem as T
|
||||
set(value) {
|
||||
comboBox.selectedItem = value
|
||||
fireValueChanged(value)
|
||||
}
|
||||
|
||||
private fun fireValueChanged(newValue: T) {
|
||||
validate(newValue)
|
||||
if (allowFireEventWhenValueUpdated) {
|
||||
onAnyValueUpdate(newValue)
|
||||
}
|
||||
}
|
||||
|
||||
fun validate(value: T = this.value) {
|
||||
validationIndicator.validationState = validator.validate(valuesReadingContext, value)
|
||||
}
|
||||
override fun getUiValue(): T = uiComponent.selectedItem as T
|
||||
}
|
||||
|
||||
|
||||
+21
-37
@@ -6,24 +6,32 @@ import com.intellij.openapi.ui.TextFieldWithBrowseButton
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.StringValidator
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.ValidationResult
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.ValuesReadingContext
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.SettingValidator
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.settingValidator
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.Component
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.panel
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.setting.ValidationIndicator
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.textField
|
||||
import java.awt.BorderLayout
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import javax.swing.Icon
|
||||
import javax.swing.JComponent
|
||||
|
||||
class PathFieldComponent(
|
||||
private val valuesReadingContext: ValuesReadingContext,
|
||||
private val initialText: String = "",
|
||||
labelText: String? = "",
|
||||
private val validator: StringValidator = org.jetbrains.kotlin.tools.projectWizard.core.entity.settingValidator { ValidationResult.OK },
|
||||
private val onSuccessValueUpdate: (String) -> Unit = {},
|
||||
private val onAnyValueUpdate: (String) -> Unit = {}
|
||||
) : Component() {
|
||||
private val validationIndicator = ValidationIndicator(defaultText = labelText, showText = true)
|
||||
private val pathField = TextFieldWithBrowseButton(
|
||||
textField(initialText, this::fireValueChanged)
|
||||
valuesReadingContext: ValuesReadingContext,
|
||||
labelText: String? = null,
|
||||
initialValue: Path? = null,
|
||||
validator: SettingValidator<Path>? = null,
|
||||
onValueUpdate: (Path) -> Unit = {}
|
||||
) : UIComponent<Path>(
|
||||
valuesReadingContext,
|
||||
labelText,
|
||||
validator,
|
||||
onValueUpdate
|
||||
) {
|
||||
override val uiComponent: TextFieldWithBrowseButton = TextFieldWithBrowseButton(
|
||||
textField(initialValue?.toString().orEmpty()) { path -> fireValueUpdated(Paths.get(path)) }
|
||||
).apply {
|
||||
addBrowseFolderListener(
|
||||
TextBrowseFolderListener(
|
||||
@@ -33,33 +41,9 @@ class PathFieldComponent(
|
||||
)
|
||||
}
|
||||
|
||||
override val component: JComponent = panel {
|
||||
add(validationIndicator, BorderLayout.NORTH)
|
||||
add(pathField, BorderLayout.CENTER)
|
||||
override fun updateUiValue(newValue: Path)= safeUpdateUi {
|
||||
uiComponent.text = newValue.toString()
|
||||
}
|
||||
|
||||
|
||||
var value: String
|
||||
get() = pathField.text
|
||||
set(value) {
|
||||
pathField.text = value
|
||||
fireValueChanged(value)
|
||||
}
|
||||
|
||||
private fun fireValueChanged(text: String) {
|
||||
validate(text)
|
||||
if (validationIndicator.validationState == ValidationResult.OK) {
|
||||
onSuccessValueUpdate(text)
|
||||
}
|
||||
onAnyValueUpdate(text)
|
||||
}
|
||||
|
||||
override fun onInit() {
|
||||
super.onInit()
|
||||
validate(initialText)
|
||||
}
|
||||
|
||||
private fun validate(text: String) {
|
||||
validationIndicator.validationState = validator.validate(valuesReadingContext, text)
|
||||
}
|
||||
override fun getUiValue(): Path = Paths.get(uiComponent.text)
|
||||
}
|
||||
+17
-44
@@ -1,54 +1,27 @@
|
||||
package org.jetbrains.kotlin.tools.projectWizard.wizard.ui.components
|
||||
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.StringValidator
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.ValidationResult
|
||||
import com.intellij.ui.components.JBTextField
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.ValuesReadingContext
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.settingValidator
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.Component
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.panel
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.setting.ValidationIndicator
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.SettingValidator
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.textField
|
||||
import java.awt.BorderLayout
|
||||
import javax.swing.JComponent
|
||||
|
||||
class TextFieldComponent(
|
||||
private val valuesReadingContext: ValuesReadingContext,
|
||||
private val initialText: String = "",
|
||||
labelText: String? = "",
|
||||
private val validator: StringValidator = settingValidator { ValidationResult.OK },
|
||||
private val onSuccessValueUpdate: (String) -> Unit = {},
|
||||
private val onAnyValueUpdate: (String) -> Unit = {}
|
||||
) : Component() {
|
||||
private val validationIndicator = ValidationIndicator(defaultText = labelText, showText = true)
|
||||
private val textField = textField(initialText, this::fireValueChanged)
|
||||
valuesReadingContext: ValuesReadingContext,
|
||||
labelText: String? = null,
|
||||
initialValue: String? = null,
|
||||
validator: SettingValidator<String>? = null,
|
||||
onValueUpdate: (String) -> Unit = {}
|
||||
) : UIComponent<String>(
|
||||
valuesReadingContext,
|
||||
labelText,
|
||||
validator,
|
||||
onValueUpdate
|
||||
) {
|
||||
override val uiComponent: JBTextField = textField(initialValue.orEmpty(), ::fireValueUpdated)
|
||||
|
||||
override val component: JComponent = panel {
|
||||
add(validationIndicator, BorderLayout.NORTH)
|
||||
add(textField, BorderLayout.CENTER)
|
||||
override fun updateUiValue(newValue: String) = safeUpdateUi {
|
||||
uiComponent.text = newValue
|
||||
}
|
||||
|
||||
|
||||
var value: String
|
||||
get() = textField.text
|
||||
set(value) {
|
||||
textField.text = value
|
||||
fireValueChanged(value)
|
||||
}
|
||||
|
||||
private fun fireValueChanged(text: String) {
|
||||
validate(text)
|
||||
if (validationIndicator.validationState == ValidationResult.OK) {
|
||||
onSuccessValueUpdate(text)
|
||||
}
|
||||
onAnyValueUpdate(text)
|
||||
}
|
||||
|
||||
override fun onInit() {
|
||||
super.onInit()
|
||||
validate(initialText)
|
||||
}
|
||||
|
||||
private fun validate(text: String) {
|
||||
validationIndicator.validationState = validator.validate(valuesReadingContext, text)
|
||||
}
|
||||
override fun getUiValue(): String = uiComponent.text
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.tools.projectWizard.wizard.ui.components
|
||||
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.ValuesReadingContext
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.Setting
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.SettingType
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.SettingValidator
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.DynamicComponent
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.panel
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.setting.ValidationIndicator
|
||||
import java.awt.BorderLayout
|
||||
import javax.swing.JComponent
|
||||
|
||||
abstract class UIComponent<V : Any>(
|
||||
private val valuesReadingContext: ValuesReadingContext,
|
||||
labelText: String? = null,
|
||||
private val validator: SettingValidator<V>? = null,
|
||||
private val onValueUpdate: (V) -> Unit = {}
|
||||
) : DynamicComponent(valuesReadingContext) {
|
||||
private val validationIndicator = if (validator != null)
|
||||
ValidationIndicator(defaultText = labelText, showText = true)
|
||||
else null
|
||||
|
||||
protected abstract val uiComponent: JComponent
|
||||
|
||||
abstract fun updateUiValue(newValue: V)
|
||||
abstract fun getUiValue(): V
|
||||
|
||||
private var allowEventFiring = true
|
||||
|
||||
protected fun fireValueUpdated(value: V) {
|
||||
if (allowEventFiring) {
|
||||
onValueUpdate(value)
|
||||
}
|
||||
validate(value)
|
||||
}
|
||||
|
||||
override fun onInit() {
|
||||
super.onInit()
|
||||
validate(getUiValue())
|
||||
}
|
||||
|
||||
final override val component: JComponent by lazy(LazyThreadSafetyMode.NONE) {
|
||||
panel {
|
||||
validationIndicator?.let { add(it, BorderLayout.NORTH) }
|
||||
add(uiComponent, BorderLayout.CENTER)
|
||||
}
|
||||
}
|
||||
|
||||
protected fun safeUpdateUi(updater: () -> Unit) {
|
||||
val allowEventFiringSaved = allowEventFiring
|
||||
allowEventFiring = false
|
||||
updater()
|
||||
allowEventFiring = allowEventFiringSaved
|
||||
}
|
||||
|
||||
fun validate(value: V) {
|
||||
if (validator == null) return
|
||||
if (validationIndicator == null) return
|
||||
validationIndicator.validationState = validator.validate(valuesReadingContext, value)
|
||||
}
|
||||
}
|
||||
|
||||
fun <V: Any> UIComponent<V>.valueForSetting(setting: Setting<V, SettingType<V>>): V =
|
||||
setting.defaultValue ?: getUiValue()
|
||||
+7
-9
@@ -11,39 +11,37 @@ import org.jetbrains.kotlin.tools.projectWizard.plugins.buildSystem.BuildSystemP
|
||||
import org.jetbrains.kotlin.tools.projectWizard.plugins.buildSystem.BuildSystemType
|
||||
import org.jetbrains.kotlin.tools.projectWizard.plugins.kotlin.KotlinPlugin
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.components.DropDownComponent
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.components.valueForSetting
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.panel
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.setting.SettingComponent
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.setting.UIComponentDelegatingSettingComponent
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.setting.ValidationIndicator
|
||||
import java.awt.BorderLayout
|
||||
|
||||
|
||||
class BuildSystemTypeSettingComponent(
|
||||
private val valuesReadingContext: ValuesReadingContext
|
||||
) : SettingComponent<BuildSystemType, DropDownSettingType<BuildSystemType>>(
|
||||
) : UIComponentDelegatingSettingComponent<BuildSystemType, DropDownSettingType<BuildSystemType>>(
|
||||
BuildSystemPlugin::type.reference,
|
||||
valuesReadingContext
|
||||
) {
|
||||
override val validationIndicator: ValidationIndicator? = null
|
||||
private val listComponent = DropDownComponent(
|
||||
override val uiComponent: DropDownComponent<BuildSystemType> = DropDownComponent(
|
||||
valuesReadingContext,
|
||||
setting.type.values,
|
||||
labelText = "Build System",
|
||||
filter = { value -> setting.type.filter(valuesReadingContext, reference, value) },
|
||||
validator = setting.validator,
|
||||
iconProvider = BuildSystemType::icon,
|
||||
onAnyValueUpdate = { value = it }
|
||||
onValueUpdate = { value = it }
|
||||
).asSubComponent()
|
||||
|
||||
override val component = panel {
|
||||
add(listComponent.component, BorderLayout.CENTER)
|
||||
}
|
||||
|
||||
override fun onValueUpdated(reference: SettingReference<*, *>?) {
|
||||
super.onValueUpdated(reference)
|
||||
if (reference == KotlinPlugin::projectKind.reference) {
|
||||
listComponent.component.updateUI()
|
||||
uiComponent.component.updateUI()
|
||||
}
|
||||
listComponent.validate()
|
||||
value?.let(uiComponent::validate)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -24,7 +24,7 @@ class ModuleSettingsComponent(valuesReadingContext: ValuesReadingContext) : Dyna
|
||||
private val nameField = TextFieldComponent(
|
||||
valuesReadingContext,
|
||||
labelText = "Name",
|
||||
onAnyValueUpdate = { value ->
|
||||
onValueUpdate = { value ->
|
||||
module?.name = value
|
||||
eventManager.fireListeners(null)
|
||||
},
|
||||
@@ -52,7 +52,7 @@ class ModuleSettingsComponent(valuesReadingContext: ValuesReadingContext) : Dyna
|
||||
}
|
||||
|
||||
private fun updateModule(module: Module) {
|
||||
nameField.value = module.name
|
||||
nameField.updateUiValue(module.name)
|
||||
nameField.component.isVisible = module.kind != ModuleKind.target
|
||||
|| module.configurator.moduleType != ModuleType.common
|
||||
|
||||
|
||||
+70
-106
@@ -1,69 +1,59 @@
|
||||
package org.jetbrains.kotlin.tools.projectWizard.wizard.ui.setting
|
||||
|
||||
import com.intellij.openapi.ui.ComboBox
|
||||
import com.intellij.ui.components.JBCheckBox
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.ValuesReadingContext
|
||||
import org.jetbrains.kotlin.tools.projectWizard.settings.DisplayableSettingItem
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.*
|
||||
import org.jetbrains.kotlin.tools.projectWizard.settings.DisplayableSettingItem
|
||||
import org.jetbrains.kotlin.tools.projectWizard.settings.version.Version
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.components.DropDownComponent
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.components.PathFieldComponent
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.components.TextFieldComponent
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.components.*
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.label
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.panel
|
||||
import java.awt.BorderLayout
|
||||
import java.awt.event.ItemEvent
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import javax.swing.DefaultComboBoxModel
|
||||
import javax.swing.JComponent
|
||||
|
||||
|
||||
sealed class DefaultSettingComponent<V : Any, T : SettingType<V>>(
|
||||
reference: SettingReference<V, T>,
|
||||
valuesReadingContext: ValuesReadingContext
|
||||
) : SettingComponent<V, T>(reference, valuesReadingContext) {
|
||||
companion object {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun <V : Any, T : SettingType<V>> create(
|
||||
setting: SettingReference<V, T>,
|
||||
valuesReadingContext: ValuesReadingContext
|
||||
): DefaultSettingComponent<V, T> = when (setting.type) {
|
||||
VersionSettingType::class ->
|
||||
VersionSettingComponent(
|
||||
setting as SettingReference<Version, VersionSettingType>,
|
||||
valuesReadingContext
|
||||
) as DefaultSettingComponent<V, T>
|
||||
BooleanSettingType::class ->
|
||||
BooleanSettingComponent(
|
||||
setting as SettingReference<Boolean, BooleanSettingType>,
|
||||
valuesReadingContext
|
||||
) as DefaultSettingComponent<V, T>
|
||||
DropDownSettingType::class ->
|
||||
DropdownSettingComponent(
|
||||
setting as SettingReference<DisplayableSettingItem, DropDownSettingType<DisplayableSettingItem>>,
|
||||
valuesReadingContext
|
||||
) as DefaultSettingComponent<V, T>
|
||||
StringSettingType::class ->
|
||||
StringSettingComponent(
|
||||
setting as SettingReference<String, StringSettingType>,
|
||||
valuesReadingContext
|
||||
) as DefaultSettingComponent<V, T>
|
||||
PathSettingType::class ->
|
||||
PathSettingComponent(
|
||||
setting as SettingReference<Path, PathSettingType>,
|
||||
valuesReadingContext
|
||||
) as DefaultSettingComponent<V, T>
|
||||
else -> TODO(setting.type.qualifiedName!!)
|
||||
}
|
||||
object DefaultSettingComponent {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun <V : Any, T : SettingType<V>> create(
|
||||
setting: SettingReference<V, T>,
|
||||
valuesReadingContext: ValuesReadingContext
|
||||
): SettingComponent<V, T> = when (setting.type) {
|
||||
VersionSettingType::class ->
|
||||
VersionSettingComponent(
|
||||
setting as SettingReference<Version, VersionSettingType>,
|
||||
valuesReadingContext
|
||||
) as SettingComponent<V, T>
|
||||
BooleanSettingType::class ->
|
||||
BooleanSettingComponent(
|
||||
setting as SettingReference<Boolean, BooleanSettingType>,
|
||||
valuesReadingContext
|
||||
) as SettingComponent<V, T>
|
||||
DropDownSettingType::class ->
|
||||
DropdownSettingComponent(
|
||||
setting as SettingReference<DisplayableSettingItem, DropDownSettingType<DisplayableSettingItem>>,
|
||||
valuesReadingContext
|
||||
) as SettingComponent<V, T>
|
||||
StringSettingType::class ->
|
||||
StringSettingComponent(
|
||||
setting as SettingReference<String, StringSettingType>,
|
||||
valuesReadingContext
|
||||
) as SettingComponent<V, T>
|
||||
PathSettingType::class ->
|
||||
PathSettingComponent(
|
||||
setting as SettingReference<Path, PathSettingType>,
|
||||
valuesReadingContext
|
||||
) as SettingComponent<V, T>
|
||||
else -> TODO(setting.type.qualifiedName!!)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class VersionSettingComponent(
|
||||
reference: SettingReference<Version, VersionSettingType>,
|
||||
valuesReadingContext: ValuesReadingContext
|
||||
) : DefaultSettingComponent<Version, VersionSettingType>(reference, valuesReadingContext) {
|
||||
) : SettingComponent<Version, VersionSettingType>(reference, valuesReadingContext) {
|
||||
private val settingLabel = label(setting.title)
|
||||
private val comboBox = ComboBox<Version>().apply {
|
||||
addItemListener { e ->
|
||||
@@ -98,94 +88,68 @@ class VersionSettingComponent(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class DropdownSettingComponent(
|
||||
reference: SettingReference<DisplayableSettingItem, DropDownSettingType<DisplayableSettingItem>>,
|
||||
private val valuesReadingContext: ValuesReadingContext
|
||||
) : DefaultSettingComponent<DisplayableSettingItem, DropDownSettingType<DisplayableSettingItem>>(
|
||||
) : UIComponentDelegatingSettingComponent<DisplayableSettingItem, DropDownSettingType<DisplayableSettingItem>>(
|
||||
reference,
|
||||
valuesReadingContext
|
||||
) {
|
||||
private val dropDownComponent = DropDownComponent(
|
||||
valuesReadingContext,
|
||||
setting.type.values,
|
||||
override val uiComponent = DropDownComponent(
|
||||
valuesReadingContext = valuesReadingContext,
|
||||
initialValues = setting.type.values,
|
||||
validator = setting.validator,
|
||||
filter = { value -> setting.type.filter(valuesReadingContext, reference, value) },
|
||||
labelText = setting.title,
|
||||
onAnyValueUpdate = { newValue ->
|
||||
value = newValue
|
||||
}
|
||||
onValueUpdate = { newValue -> value = newValue }
|
||||
).asSubComponent()
|
||||
|
||||
override val validationIndicator: ValidationIndicator? = null
|
||||
override val component: JComponent = dropDownComponent.component
|
||||
}
|
||||
|
||||
class BooleanSettingComponent(
|
||||
reference: SettingReference<Boolean, BooleanSettingType>,
|
||||
valuesReadingContext: ValuesReadingContext
|
||||
) : DefaultSettingComponent<Boolean, BooleanSettingType>(reference, valuesReadingContext) {
|
||||
private val checkBox = JBCheckBox(setting.title).apply {
|
||||
addChangeListener {
|
||||
value = this@apply.isSelected
|
||||
}
|
||||
}
|
||||
|
||||
override fun onValueUpdated(reference: SettingReference<*, *>?) {
|
||||
super.onValueUpdated(reference)
|
||||
checkBox.isSelected = value ?: false
|
||||
}
|
||||
|
||||
override val validationIndicator = ValidationIndicator(showText = false)
|
||||
|
||||
override val component: JComponent by lazy(LazyThreadSafetyMode.NONE) {
|
||||
panel {
|
||||
// TODO add validation indicator
|
||||
add(checkBox, BorderLayout.CENTER)
|
||||
}
|
||||
}
|
||||
) : UIComponentDelegatingSettingComponent<Boolean, BooleanSettingType>(
|
||||
reference,
|
||||
valuesReadingContext
|
||||
) {
|
||||
override val uiComponent = CheckboxComponent(
|
||||
valuesReadingContext = valuesReadingContext,
|
||||
labelText = setting.title,
|
||||
initialValue = null,
|
||||
validator = setting.validator,
|
||||
onValueUpdate = { newValue -> value = newValue }
|
||||
).asSubComponent()
|
||||
}
|
||||
|
||||
class StringSettingComponent(
|
||||
reference: SettingReference<String, StringSettingType>,
|
||||
valuesReadingContext: ValuesReadingContext,
|
||||
showLabel: Boolean = true
|
||||
) : DefaultSettingComponent<String, StringSettingType>(reference, valuesReadingContext) {
|
||||
private val textFieldComponent = TextFieldComponent(
|
||||
valuesReadingContext,
|
||||
setting.defaultValue.orEmpty(),
|
||||
) : UIComponentDelegatingSettingComponent<String, StringSettingType>(
|
||||
reference,
|
||||
valuesReadingContext
|
||||
) {
|
||||
override val uiComponent = TextFieldComponent(
|
||||
valuesReadingContext = valuesReadingContext,
|
||||
initialValue = null,
|
||||
labelText = if (showLabel) setting.title else null,
|
||||
validator = setting.validator,
|
||||
onAnyValueUpdate = {
|
||||
value = it
|
||||
}
|
||||
onValueUpdate = { newValue -> value = newValue }
|
||||
).asSubComponent()
|
||||
|
||||
override val validationIndicator: ValidationIndicator? = null
|
||||
|
||||
override fun onInit() {
|
||||
super.onInit()
|
||||
textFieldComponent.value = value.orEmpty()
|
||||
}
|
||||
|
||||
override val component = textFieldComponent.component
|
||||
}
|
||||
|
||||
class PathSettingComponent(
|
||||
reference: SettingReference<Path, PathSettingType>,
|
||||
valuesReadingContext: ValuesReadingContext
|
||||
) : DefaultSettingComponent<Path, PathSettingType>(reference, valuesReadingContext) {
|
||||
private val pathFieldComponent = PathFieldComponent(
|
||||
valuesReadingContext,
|
||||
"",
|
||||
setting.title,
|
||||
validator = settingValidator { path ->
|
||||
setting.validator.validate(this, Paths.get(path))
|
||||
},
|
||||
onAnyValueUpdate = {
|
||||
value = Paths.get(it)
|
||||
}
|
||||
) : UIComponentDelegatingSettingComponent<Path, PathSettingType>(
|
||||
reference,
|
||||
valuesReadingContext
|
||||
) {
|
||||
override val uiComponent = PathFieldComponent(
|
||||
valuesReadingContext = valuesReadingContext,
|
||||
labelText = setting.title,
|
||||
validator = settingValidator { path -> setting.validator.validate(this, path) },
|
||||
onValueUpdate = { newValue -> value = newValue }
|
||||
).asSubComponent()
|
||||
|
||||
override val validationIndicator: ValidationIndicator? = null
|
||||
|
||||
override val component = pathFieldComponent.component
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.tools.projectWizard.wizard.ui.setting
|
||||
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.ValuesReadingContext
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.SettingReference
|
||||
import org.jetbrains.kotlin.tools.projectWizard.core.entity.SettingType
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.components.UIComponent
|
||||
import org.jetbrains.kotlin.tools.projectWizard.wizard.ui.components.valueForSetting
|
||||
import javax.swing.JComponent
|
||||
|
||||
abstract class UIComponentDelegatingSettingComponent<V : Any, T : SettingType<V>>(
|
||||
reference: SettingReference<V, T>,
|
||||
valuesReadingContext: ValuesReadingContext
|
||||
) : SettingComponent<V, T>(reference, valuesReadingContext) {
|
||||
abstract val uiComponent: UIComponent<V>
|
||||
|
||||
// As there is one in UIComponent
|
||||
override val validationIndicator: ValidationIndicator? = null
|
||||
|
||||
override fun onInit() {
|
||||
super.onInit()
|
||||
if (value == null) {
|
||||
if (value == null) {
|
||||
value = uiComponent.valueForSetting(setting)
|
||||
}
|
||||
}
|
||||
value?.let(uiComponent::updateUiValue)
|
||||
}
|
||||
|
||||
override val component: JComponent by lazy(LazyThreadSafetyMode.NONE) { uiComponent.component }
|
||||
}
|
||||
Reference in New Issue
Block a user