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 21 '24
So, I've tried the code below and it seems to work.
In the MasterController I've put this code
where add and edit methods are the actions linked to the toolbar and model.selectedProductProperty().getValue() is the object selected in the tableView. As you can see, I instantiate the DependentController every time the user click on the toolbar.
Then in the DependentController constructor
and in the setProductModel method of DependentModel
In ProductModel, I've initialized things in this way
So, when the user click New and the model.selectedProductProperty().getValue() is null, I can load some default data: if the id=-1 I can calculate an id for the new product and make an insert in the database instead of an update.
Finally, in the dependent view, if I bind the properties of the ProductModel passed, I can see "live" changes in the tableView and then refreshing data after the popup is closed I don't bother if the user saved or quitted the editing.
Can you tell me if I did it right or not?