Skip to content

Commit b615786

Browse files
authored
Merge pull request #38 from OneAndOlaf/master
Actually fix notifications for arrays.
2 parents fd14a7d + 14ff5ee commit b615786

6 files changed

Lines changed: 29 additions & 7 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<modelVersion>4.0.0</modelVersion>
77
<groupId>com.github.hanseter</groupId>
88
<artifactId>json-properties-fx</artifactId>
9-
<version>1.0.14</version>
9+
<version>1.0.15</version>
1010

1111
<packaging>bundle</packaging>
1212
<name>JSON Properties Editor Fx</name>

src/main/kotlin/com/github/hanseter/json/editor/actions/ArrayActions.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.github.hanseter.json.editor.extensions.EffectiveSchemaInArray
77
import com.github.hanseter.json.editor.i18n.JsonPropertiesMl
88
import com.github.hanseter.json.editor.types.SupportedType
99
import com.github.hanseter.json.editor.types.TypeModel
10+
import com.github.hanseter.json.editor.util.shallowClone
1011
import javafx.event.Event
1112
import org.everit.json.schema.ArraySchema
1213
import org.json.JSONArray
@@ -30,8 +31,12 @@ object AddToArrayAction : EditorAction {
3031
model: TypeModel<*, *>,
3132
mouseEvent: Event?
3233
): PropertiesEditResult? {
33-
val children = (model as TypeModel<JSONArray?, SupportedType.ComplexType.ArrayType>).value
34-
?: JSONArray().also {
34+
// Because this calls model.setValue instead of returning an edit result, we need to ensure
35+
// the internal value does not change before we invoke setValue, otherwise, it would return
36+
// immediately. Hence a shallow copy.
37+
val children = (model as TypeModel<JSONArray?, SupportedType.ComplexType.ArrayType>).value?.let {
38+
JSONArray().putAll(it)
39+
} ?: JSONArray().also {
3540
model.value = it
3641
}
3742
val childDefaultValue =

src/main/kotlin/com/github/hanseter/json/editor/util/BindableJsonArray.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class BindableJsonArray(parent: BindableJsonType?, private val arr: JSONArray) :
88
BindableJsonType(parent) {
99

1010
override fun updateFromChild(child: BindableJsonType, schema: EffectiveSchema<*>) {
11-
super.setValue(schema, child.getValue())
11+
super.updateAfterChange(schema, child.getValue())
1212
}
1313

1414
override fun setValueInternal(schema: EffectiveSchema<*>, value: Any?) =

src/main/kotlin/com/github/hanseter/json/editor/util/BindableJsonType.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ abstract class BindableJsonType(private val parent: BindableJsonType?) {
1717
updateAfterChange(schema.nonSyntheticAncestor!!, child.getValue())
1818
}
1919

20-
private fun updateAfterChange(
20+
protected fun updateAfterChange(
2121
schema: EffectiveSchema<*>,
2222
value: Any?,
2323
) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.github.hanseter.json.editor.util
2+
3+
import org.json.JSONArray
4+
5+
fun JSONArray.shallowClone(): JSONArray {
6+
return JSONArray().putAll(this)
7+
}
8+

src/test/kotlin/com/github/hanseter/json/editor/ArrayTest.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import org.junit.jupiter.api.extension.ExtendWith
1414
import org.testfx.framework.junit5.ApplicationExtension
1515
import org.testfx.framework.junit5.Start
1616
import org.testfx.util.WaitForAsyncUtils
17+
import java.util.concurrent.atomic.AtomicInteger
1718

1819
@ExtendWith(ApplicationExtension::class)
1920
class ArrayTest {
@@ -26,19 +27,24 @@ class ArrayTest {
2627

2728
@Test
2829
fun updateArrayValue() {
30+
val updateCounter = AtomicInteger(0)
31+
2932
val schema = JSONObject(
3033
"""{"type":"object","properties":{"bar":{"type":"array",
3134
"items":{"type":"string"}
3235
}}}"""
3336
)
3437
val json = JSONObject("""{"bar":["hello", "world"]}""")
35-
editor.display("1", "1", json, schema) { it }
38+
editor.display("1", "1", json, schema) { it.also { updateCounter.incrementAndGet() } }
3639
val itemTable = editor.getItemTable()
3740
val arrayEntry = itemTable.root.children[0].findChildWithKey("bar")!!
3841
val textField = arrayEntry.children.first().value.createControl()!!.control as TextField
3942
assertThat(textField.text, `is`("hello"))
4043
textField.text = "bye bye"
44+
45+
WaitForAsyncUtils.waitForFxEvents()
4146
assertThat(json.getJSONArray("bar").getString(0), `is`("bye bye"))
47+
assertThat(updateCounter.get(), `is`(1))
4248
}
4349

4450
@Test
@@ -116,13 +122,15 @@ class ArrayTest {
116122

117123
@Test
118124
fun arrayElementCanBeAddedByButtonPress() {
125+
val updateCounter = AtomicInteger(0)
126+
119127
val schema = JSONObject(
120128
"""{"type":"object","properties":{"bar":{"type":"array",
121129
"items":{"type":"string"}
122130
}}}"""
123131
)
124132
val data = JSONObject()
125-
editor.display("1", "1", data, schema) { it }
133+
editor.display("1", "1", data, schema) { it.also { updateCounter.incrementAndGet() } }
126134
val itemTable = editor.getItemTable()
127135
val arrayEntry = itemTable.root.children[0].findChildWithKey("bar")!!
128136
val addButton = arrayEntry.value.createActions()!!.children[1] as Button
@@ -139,6 +147,7 @@ class ArrayTest {
139147
}
140148
WaitForAsyncUtils.waitForFxEvents()
141149
assertThat(data.getJSONArray("bar").toList(), contains(null, null, null))
150+
assertThat(updateCounter.get(), `is`(3))
142151
}
143152

144153
@Test

0 commit comments

Comments
 (0)