From 6e6d49014c90c68e27d2a12ddb0743194596fcfb Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 5 Sep 2019 20:53:25 +0200 Subject: [PATCH] UI DSL: add applyIfEnabled() method to avoid saving the value of a field when it's disabled GitOrigin-RevId: 6eab56a9d9edfb186d70a19645383c1f969eca74 --- .../src/com/intellij/ui/layout/Cell.kt | 13 +++++++++++-- .../ui/layout/migLayout/MigLayoutRow.kt | 12 ++++++++++++ .../intellij/ui/layout/UiDslBindingsTest.kt | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/ui/layout/Cell.kt b/platform/platform-impl/src/com/intellij/ui/layout/Cell.kt index f501005cb1e4..8b272196028e 100644 --- a/platform/platform-impl/src/com/intellij/ui/layout/Cell.kt +++ b/platform/platform-impl/src/com/intellij/ui/layout/Cell.kt @@ -19,6 +19,7 @@ import com.intellij.openapi.vfs.VirtualFile import com.intellij.ui.* import com.intellij.ui.components.* import com.intellij.util.ui.UIUtil +import org.jetbrains.annotations.ApiStatus import java.awt.Component import java.awt.event.ActionEvent import java.awt.event.ActionListener @@ -90,14 +91,19 @@ interface CellBuilder { fun onReset(callback: () -> Unit): CellBuilder fun onIsModified(callback: () -> Boolean): CellBuilder + /** + * If this method is called, the value of the component will be stored to the backing property only if the component is enabled. + */ + fun applyIfEnabled(): CellBuilder + fun withBinding( componentGet: (T) -> V, componentSet: (T, V) -> Unit, modelBinding: PropertyBinding ): CellBuilder { - onApply { modelBinding.set(componentGet(component)) } + onApply { if (shouldSaveOnApply()) modelBinding.set(componentGet(component)) } onReset { componentSet(component, modelBinding.get()) } - onIsModified { componentGet(component) != modelBinding.get() } + onIsModified { shouldSaveOnApply() && componentGet(component) != modelBinding.get() } return this } @@ -108,6 +114,9 @@ interface CellBuilder { withValidationOnApply { if (callback(it)) ValidationInfo(message, it) else null } return this } + + @ApiStatus.Internal + fun shouldSaveOnApply(): Boolean } fun CellBuilder.validateTextOnInput(callback: (String) -> String?): CellBuilder { diff --git a/platform/platform-impl/src/com/intellij/ui/layout/migLayout/MigLayoutRow.kt b/platform/platform-impl/src/com/intellij/ui/layout/migLayout/MigLayoutRow.kt index ab3dc1c98955..676cd06d78ae 100644 --- a/platform/platform-impl/src/com/intellij/ui/layout/migLayout/MigLayoutRow.kt +++ b/platform/platform-impl/src/com/intellij/ui/layout/migLayout/MigLayoutRow.kt @@ -418,6 +418,8 @@ class CellBuilderImpl internal constructor( private val row: MigLayoutRow, override val component: T ) : CellBuilder, CheckboxCellBuilder, ScrollPaneCellBuilder { + private var applyIfEnabled = false + override fun comment(text: String, maxLineLength: Int): CellBuilder { row.addCommentRow(component, text, maxLineLength) return this @@ -463,6 +465,16 @@ class CellBuilderImpl internal constructor( return this } + override fun applyIfEnabled(): CellBuilder { + applyIfEnabled = true + return this + } + + override fun shouldSaveOnApply(): Boolean { + if (applyIfEnabled && !component.isEnabled) return false + return true + } + override fun actsAsLabel() { builder.updateComponentConstraints(component) { spanX = 1 } } diff --git a/platform/platform-tests/testSrc/com/intellij/ui/layout/UiDslBindingsTest.kt b/platform/platform-tests/testSrc/com/intellij/ui/layout/UiDslBindingsTest.kt index f45f7b1703bc..eb47662cfb21 100644 --- a/platform/platform-tests/testSrc/com/intellij/ui/layout/UiDslBindingsTest.kt +++ b/platform/platform-tests/testSrc/com/intellij/ui/layout/UiDslBindingsTest.kt @@ -11,6 +11,7 @@ import javax.swing.JTextField class UiDslBindingsTest : BasePlatformTestCase() { private var booleanValue = false private var intValue = 0 + private var stringValue = "" fun testRadioButtonWithBooleanBinding() { booleanValue = false @@ -101,4 +102,21 @@ class UiDslBindingsTest : BasePlatformTestCase() { radioButtons[0].isSelected = true assertFalse(textField.isEnabled) } + + fun testApplyIfEnabled() { + lateinit var textField: JTextField + stringValue = "" + val dialogPanel = panel { + row { + textField(::stringValue).applyIfEnabled().also { textField = it.component } + } + } + textField.text = "abc" + dialogPanel.apply() + assertEquals("abc", stringValue) + textField.isEnabled = false + textField.text = "def" + dialogPanel.apply() + assertEquals("abc", stringValue) + } }