diff --git a/src/main/java/tornadofx/FX.kt b/src/main/java/tornadofx/FX.kt index b33315153..66bfc7c9a 100644 --- a/src/main/java/tornadofx/FX.kt +++ b/src/main/java/tornadofx/FX.kt @@ -18,6 +18,7 @@ import javafx.scene.layout.Pane import javafx.scene.layout.VBox import javafx.stage.Stage import javafx.stage.Window +import tornadofx.FX.Companion.dicontainer import tornadofx.FX.Companion.inheritParamHolder import tornadofx.FX.Companion.inheritScopeHolder import tornadofx.FX.Companion.stylesheets @@ -176,6 +177,7 @@ class FX { object : SimpleObjectProperty<(Class?) -> String>({ it?.name ?: "Messages" }) { override fun invalidated() = loadMessages() } + /** * Provides the name of the Resource Bundle for a given Component Class. * A `null` value may be passed to represent the global bundle. @@ -409,6 +411,7 @@ fun removeStylesheet(stylesheetType: KClass) { fun setInScope(value: T, scope: Scope = FX.defaultScope, kclass: KClass = value.javaClass.kotlin) = FX.getComponents(scope).put(kclass, value) + @Suppress("UNCHECKED_CAST") fun Scope.set(vararg value: T) = value.associateByTo(FX.getComponents(this)) { it::class } @@ -419,6 +422,7 @@ inline fun find(scope: Scope = FX.defaultScope, params: inline fun find(scope: Scope = FX.defaultScope, vararg params: Pair<*, Any?>): T = find(scope, params.toMap()) fun find(type: KClass, scope: Scope = FX.defaultScope, vararg params: Pair<*, Any?>): T = find(type, scope, params.toMap()) + @Suppress("UNCHECKED_CAST") fun find(type: KClass, scope: Scope = FX.defaultScope, params: Map<*, Any?>? = null): T { val useScope = FX.fixedScopes[type] ?: scope @@ -431,7 +435,7 @@ fun find(type: KClass, scope: Scope = FX.defaultScope, params if (!components.containsKey(type as KClass)) { synchronized(FX.lock) { if (!components.containsKey(type)) { - val cmp = type.java.newInstance() + val cmp = dicontainer?.getInstance(type) ?: type.java.newInstance() (cmp as? UIComponent)?.init() // if cmp.scope overrode the scope, inject into that instead if (cmp is Component && cmp.scope != useScope) { @@ -442,11 +446,11 @@ fun find(type: KClass, scope: Scope = FX.defaultScope, params } } val cmp = components[type] as T - cmp.paramsProperty?.value = stringKeyedMap + cmp.paramsProperty.value = stringKeyedMap return cmp } - val cmp = type.java.newInstance() + val cmp = dicontainer?.newInstance(type) ?: type.java.newInstance() cmp.paramsProperty.value = stringKeyedMap (cmp as? Fragment)?.init() @@ -459,12 +463,15 @@ fun find(type: KClass, scope: Scope = FX.defaultScope, params interface DIContainer { fun getInstance(type: KClass): T + fun newInstance(type: KClass): T = type.java.newInstance() + fun getInstance(type: KClass, name: String): T { throw AssertionError("Injector is not configured, so bean of type $type with name $name can not be resolved") } } inline fun DIContainer.getInstance() = getInstance(T::class) +inline fun DIContainer.newInstance() = newInstance(T::class) inline fun DIContainer.getInstance(name: String) = getInstance(T::class, name) /**