mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
UI DSL: add applyIfEnabled() method to avoid saving the value of a field when it's disabled
GitOrigin-RevId: 6eab56a9d9edfb186d70a19645383c1f969eca74
This commit is contained in:
committed by
intellij-monorepo-bot
parent
8d41a0ee7b
commit
6e6d49014c
@@ -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<T : JComponent> {
|
||||
fun onReset(callback: () -> Unit): CellBuilder<T>
|
||||
fun onIsModified(callback: () -> Boolean): CellBuilder<T>
|
||||
|
||||
/**
|
||||
* 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<T>
|
||||
|
||||
fun <V> withBinding(
|
||||
componentGet: (T) -> V,
|
||||
componentSet: (T, V) -> Unit,
|
||||
modelBinding: PropertyBinding<V>
|
||||
): CellBuilder<T> {
|
||||
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<T : JComponent> {
|
||||
withValidationOnApply { if (callback(it)) ValidationInfo(message, it) else null }
|
||||
return this
|
||||
}
|
||||
|
||||
@ApiStatus.Internal
|
||||
fun shouldSaveOnApply(): Boolean
|
||||
}
|
||||
|
||||
fun <T : JTextComponent> CellBuilder<T>.validateTextOnInput(callback: (String) -> String?): CellBuilder<T> {
|
||||
|
||||
@@ -418,6 +418,8 @@ class CellBuilderImpl<T : JComponent> internal constructor(
|
||||
private val row: MigLayoutRow,
|
||||
override val component: T
|
||||
) : CellBuilder<T>, CheckboxCellBuilder, ScrollPaneCellBuilder {
|
||||
private var applyIfEnabled = false
|
||||
|
||||
override fun comment(text: String, maxLineLength: Int): CellBuilder<T> {
|
||||
row.addCommentRow(component, text, maxLineLength)
|
||||
return this
|
||||
@@ -463,6 +465,16 @@ class CellBuilderImpl<T : JComponent> internal constructor(
|
||||
return this
|
||||
}
|
||||
|
||||
override fun applyIfEnabled(): CellBuilder<T> {
|
||||
applyIfEnabled = true
|
||||
return this
|
||||
}
|
||||
|
||||
override fun shouldSaveOnApply(): Boolean {
|
||||
if (applyIfEnabled && !component.isEnabled) return false
|
||||
return true
|
||||
}
|
||||
|
||||
override fun actsAsLabel() {
|
||||
builder.updateComponentConstraints(component) { spanX = 1 }
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user