Skip to content

Commit 58296ed

Browse files
authored
Merge pull request #39 from OneAndOlaf/improvement/change-customization-object
Allow changing the customization object after creating the editor.
2 parents 008a431 + 89eeac9 commit 58296ed

6 files changed

Lines changed: 60 additions & 11 deletions

File tree

.sdkmanrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Enable auto-env through the sdkman_auto_env config
22
# Add key=value pairs of SDKs to use below
3-
java=21.0.4-tem
3+
java=17.0.10-tem

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.17</version>
9+
<version>1.0.18</version>
1010

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

src/main/kotlin/com/github/hanseter/json/editor/JsonPropertiesEditor.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class JsonPropertiesEditor @JvmOverloads constructor(
2525
private val readOnly: Boolean = false,
2626
viewOptions: ViewOptions = ViewOptions(),
2727
actions: List<EditorAction> = listOf(ResetToDefaultAction, ResetToNullAction),
28-
private val customizationObject: CustomizationObject = DefaultCustomizationObject,
28+
customizationObject: CustomizationObject = DefaultCustomizationObject,
2929
additionalValidators: List<Validator> = emptyList()
3030
) : StackPane() {
3131
var referenceProposalProvider: IdReferenceProposalProvider =
@@ -53,6 +53,17 @@ class JsonPropertiesEditor @JvmOverloads constructor(
5353
}
5454
}
5555

56+
var customizationObject: CustomizationObject = customizationObject
57+
set(value) {
58+
field = value
59+
60+
Platform.runLater {
61+
idsToPanes.values.forEach {
62+
it.rebuildControlTree()
63+
}
64+
}
65+
}
66+
5667
private val actions =
5768
actions + PreviewAction(
5869
{ referenceProposalProvider },
@@ -121,7 +132,7 @@ class JsonPropertiesEditor @JvmOverloads constructor(
121132
title, objId, obj,
122133
schema,
123134
readOnly,
124-
resolutionScope, customizationObject, callback
135+
resolutionScope, callback
125136
)
126137
pane.fillData(obj)
127138
idsToPanes[objId] = pane
@@ -211,7 +222,6 @@ class JsonPropertiesEditor @JvmOverloads constructor(
211222
private fun createTitledPaneForSchema(
212223
title: String, objId: String, data: JSONObject,
213224
rawSchema: JSONObject, readOnly: Boolean, resolutionScope: URI?,
214-
customizationObject: CustomizationObject,
215225
callback: OnEditCallback
216226
): JsonPropertiesPane =
217227
JsonPropertiesPane(
@@ -225,7 +235,7 @@ class JsonPropertiesEditor @JvmOverloads constructor(
225235
actions,
226236
{ validators },
227237
viewOptions,
228-
customizationObject,
238+
{ customizationObject },
229239
callback
230240
)
231241

src/main/kotlin/com/github/hanseter/json/editor/JsonPropertiesPane.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class JsonPropertiesPane(
3232
private val actions: List<EditorAction>,
3333
private val validators: () -> List<Validator>,
3434
viewOptions: ViewOptions,
35-
private val customizationObject: CustomizationObject,
35+
private val customizationObject: () -> CustomizationObject,
3636
private val changeListener: JsonPropertiesEditor.OnEditCallback
3737
) {
3838
val treeItem: FilterableTreeItem<TreeItemData> =
@@ -80,7 +80,7 @@ class JsonPropertiesPane(
8080
createControlTree()
8181
}
8282

83-
private fun rebuildControlTree() {
83+
fun rebuildControlTree() {
8484

8585
val uiState = saveUiState()
8686

src/main/kotlin/com/github/hanseter/json/editor/ui/FilterableTreeItem.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ class ControlTreeItemData(
121121
private val actions: List<EditorAction>,
122122
private val actionHandler: (Event, EditorAction, TypeControl) -> Unit,
123123
private val objId: String,
124-
private val customizationObject: CustomizationObject
124+
private val customizationObject: () -> CustomizationObject
125125
) : TreeItemData {
126126
private val changeListeners: MutableList<(TreeItemData) -> Unit> = mutableListOf()
127127

128128
override val title: String
129-
get() = customizationObject.getTitle(typeControl.model, typeControl.model.schema.title)
129+
get() = customizationObject().getTitle(typeControl.model, typeControl.model.schema.title)
130130

131131
override val description: String?
132-
get() = customizationObject.getDescription(typeControl.model, typeControl.model.schema.description)
132+
get() = customizationObject().getDescription(typeControl.model, typeControl.model.schema.description)
133133

134134
override val required: Boolean
135135
get() = typeControl.model.schema.required

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,43 @@ class CustomizationObjectTest {
106106

107107
}
108108

109+
@Test
110+
fun `customization can be changed after creation`() {
111+
val schema = JSONObject("""
112+
{
113+
"type":"object",
114+
"properties": {
115+
"foo": {
116+
"type": "string"
117+
},
118+
"notFoo": {
119+
"type": "string"
120+
}
121+
}
122+
}""")
123+
val editor = JsonPropertiesEditor()
124+
125+
val json = JSONObject("""{"foo":""}""")
126+
editor.display("1", "1", json, schema) { it }
127+
128+
val fooCell = editor.getKeyCellInTable("foo")
129+
130+
MatcherAssert.assertThat((fooCell.graphic as Labeled).text, Matchers.`is`("foo"))
131+
132+
editor.customizationObject = object : CustomizationObject {
133+
134+
override fun getTitle(model: TypeModel<*, *>, defaultTitle: String): String {
135+
if (model.schema.pointer == listOf("foo")) {
136+
return "bar"
137+
}
138+
return defaultTitle
139+
}
140+
141+
}
142+
143+
WaitForAsyncUtils.waitForFxEvents()
144+
val fooCellNew = editor.getKeyCellInTable("bar")
145+
MatcherAssert.assertThat((fooCellNew.graphic as Labeled).text, Matchers.`is`("bar"))
146+
}
147+
109148
}

0 commit comments

Comments
 (0)