edvin / tornadofx2

TornadoFX 2.0
Apache License 2.0
156 stars 41 forks source link

CLassCastException when binding in ItemViewModel #54

Open tyb51 opened 2 years ago

tyb51 commented 2 years ago

I have been working on an application for use during brainsurgery using this amazing framework for about a year now. BUt I have now encountered an issue that I have not been able to solve.

I made this custom implementation of an ObservableMapProperty which is backed by a reversed map (value,keys) with some custom impelmentations class ReversableObservableMapProperty<K, V?> (map: ObservableMap<K,V?> = FXCollections.observableMap<K,V?>(hashMapOf())) : SimpleMapProperty<K, V?>(map)

I have a Model : class MappingModel() { val parcelHexagonMapProperty = ReversableObservableMapProperty<ParcelPoint,Hexagon?>() val parcelHexagonMap by parcelHexagonMapProperty }

And and ItemViewModel: class MappingModelView: ItemViewModel<MappingModel>() { val parcelHexagonMap = bind(MappingModel::parcelHexagonMapProperty) }

On build however it throws the following error: java.lang.ClassCastException: class tornadofx.BindingAwareSimpleMapProperty cannot be cast to class com.sasdashboard.ui.utils.ReversableObservableMap (tornadofx.BindingAwareSimpleMapProperty and com.sasdashboard.ui.utils.ReversableObservableMap are in unnamed module of loader 'app') at line val parcelHexagonMap = bind(MappingModel::parcelHexagonMapProperty)

Any idea what causes the issue? Is it due to my custom MapProperty not being accessible by TornadoFx? Might it be due to kotlin version (as I saw in another similar issue #12 . Any ideas on how to resolve it. I rely a lot on this easy binding of properties and delegation of Mapfunctions.

tyb51 commented 2 years ago

I currently fixed it by creating a custom: class BindingAwareReversableObservableMapProperty<S, T>(viewModel: ViewModel, name: String?) : ReversableObservableMap<S, T>(viewModel, name) {

and

inline fun <reified PropertyType : T, reified T: ReversableObservableMap<*,*>,reified V: ItemViewModel<M>, reified M : Any, ResultType : PropertyType> V.bindReversableMap(property: KProperty1<M, PropertyType>, autocommit: Boolean = false, forceObjectProperty: Boolean = false, defaultValue: T? = null): ResultType {

But its really just a lot of boiler plate. Any other ideas on how a neater implementation would look like?