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/hamsterrage1 Feb 22 '24
That seems reasonable to me. The only issues I can see are in the details, and not with the actual methodology. For instance, your setProductModel() re-initializes the incoming parameter - which isn't going to hurt because the references are passed by value and it only does it when the incoming parameter is Null - but why do it that way?
Does the same thing without messing with the method parameter, so it feels a little cleaner and less ambiguous to me.
However:
Is a little more troublesome. Why have two Buttons, then?
I think the user expectation is that "Quit" will toss the changes, and "Save" commits them. I think this also goes back to your original design question, too.
You have two choices:
Personally, I'd go with option 2. DirtyFX is great, and it's super easy to use. Intsead of calling SimpleStringProperty(), you call DirtyStringProperty() and then you have access to reset() on each Property. You can also use the isDirty() method to control whether your "Save" Button is disabled, which is nice.
I'm assuming that there's a database update somewhere in there, too. It's not clear where this is happening - in the dependent interactor or the master? That might change the architecture a little bit too.