I am developing an application in Java and I am using Swing for GUI. I was going to do it using Presentation Model pattern which is introduced by Martin Fowler here: http://martinfowler.com/eaaDev/PresentationModel.html
but I really don't like when I have to deal with View within my Presentation Model class so I figure out I can do it differently.
In my approach I will have a CustomViewModel class extending Swing component model ( like TableModel for JTable or ButtonModel for JButton). This class will override method that is executed when user updates data on model. So I will do my job like updating DomainModel and then calling parents method. This way I will have data always in sync between DomainModel and ViewModel.
For Example:
class MyCustomJTableModel extends TableModel{
... overriding required methods for synchronization
}
in setup gui function I would have:
JTable table = new JTable();
MyCustomJTableModel model = new MyCustomJTableModel();
model.setDataSource( Database.getDB().getModelFor( "model.name" ) );
table.setModel( model )
View view = new View();
view.addComponent( "JTableModel", model );
ResourceHandler rH = new ResourceHandler();
rH.add( model );
So now in my Controller function I can have:
public class FileOpenCOntroller{
public void perform(ResourceHandler rH){
MyCustomTableModel model = rH.getModel("JTableModel");
model.loadDataFromDataSource( "File.xml" );
}
}
And that's it MyCustomTableModel has overriden method that is used by JTable to update it's content and having my code before the actual call to parents setModel I am updating actual Model. No need for view reference. The problem is when I need to deal with JFileChooser which don't have similar relationship as rest of components. I am talking about some ViewModel class.
Does anyone have an idea how to tacle this approach.
What I am trying to achieve is not to have reference to View in both MyCustomComponentModel and Controller. I would like to make the comunication between controller and view via set of CustomComponentModel which extends view models like TableModel, ButtonModel etc. The purpose of that is easier TDD. In this approach only Layout of things is not tested.