r/JavaFX • u/sonnyDev80 • Jan 20 '24
Help Encaspulating-Encapsulated Scenario in MVCI pattern by PragmaticCoding
I'm trying to use MVCI pattern by PragmaticCoding, in particular the Encaspulating-Encapsulated Scenario, but I'm stuck on the adding/editing part.
Maybe I'm doing something wrong or I'm missing something or I didn't understand the case.
--Edit--
First of all, to clarify I post the GUI I've built

--Edit End--
In the Encaspulating Scene, I built a ViewBuilder with a Search Textbox, a TableView and 3 buttons for adding/editing/remove items from the table.
The Encaspulating Model I'm passing to the ViewBuilder is done by:
- StringProperty searchProperty => the binding to textbox
- ObservableList<ProductModel> list => the list on which the TableView is populated
- ObjectProperty<ProductModel> selectedProductProperty => the binding to the selected record by the user in the TableView
So, the TableView is based on ProductModel (that is backed to a POJO Product class used by the DAO to interact with the db...), but ProductModel actually belongs to the Encapsulated Scene: this sounds strange even to me, but I couldn't make it better at the moment.
Maybe this could the first mistake, but, please read on to understand what I wanted to do.
So, I bound the selectionModelProperty of the TableView to the selectedProductProperty of the Encaspulating Model via this piece of code:
model.selectedProductProperty().bind(table.selectionModelProperty().getValue().selectedItemProperty());In this way, I thought I could "share" the selected item with the Encapsulated Controller, passing selectedProductProperty to the constructor.
I thought...but then many questions came to me, and I tried different things but now I 'm stuck.
ProductModel is a complex object made up by 6 properties, but, as you can imagine, they can grow in the future.
Do I have to bind each of them to their counterparts in ProductModel?
Is there a way to bind directly the passed object to the Encapsulated Model, being able to manage the null value when no selection is made in the TableView?
I searched and read a lot, but nothing found.
Anyone can help and/or explain how to do?
View all comments
Show parent comment
1
u/sonnyDev80 Feb 24 '24
That's great because I would like to adhere to your pattern as much as I can.
I also think that this code
is better than mine: thanks for pointing that out!
Yes, and to answer your last question: yes, the "Save" button commits to a SQLite db behind the hood.
The save operation is going to happen in the dependent interactor.
The quit operation simply ask the user if s/he wants to exit and doesn't perform anything else.
So in both cases, when the popup closes, the master view is going to update itself refreshing data from the database (look at interactor.lookup() above...).
I know this could be not very efficient on large dataset, but this particular table of the database is going to be very small (some hundreds of records at maximum).
I'll keep in mind DirtyFX and the other answers you gave before, because I think they could be useful for other parts of the project.
Now I can continue, but I'm sure I will ask you something else in the near future.
Thank you so much!