Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing MVC - Event Propogation and Data Sharing

I am trying to apply MVC pattern in swing application. However I am facing two major issues given that you have nested hierarchy of panel e.g. Parent -> Child - > Grand Child -> Grand Grand Child.

Issue 1: How to transfer the data between controller and view when you have such a hierarchy? If I go and pass data from parent to child then there will be lots of duplication and if I change one child all the parents will require change. I don't want views to directly access data from db and I want the data to be transferred to views through controller only.

Issue 2: How to propagate the events from view to controller in such a hierarchy? I am thinking of using PropertyChangeListener. View will firePropertyChange event if any action has to be taken by controller. Controller will listen for these events and perform some action. But again if I do this for hierarchy then there will be code duplication.

I have three ideas that could be useful:

  1. To use controller for each Panel but this in this way I will end up with lots of controllers.
  2. Use Mediator design pattern that will provide the communication between views and controller.
  3. Use a Central Reciever & Notifier that will listen for all property change event from views and will notify controllers who are interested. but this will only solve my second issue:

Please refer to below diagram to have a picture of 3rd idea. In case of 2nd one, Mediator will come in center.

Please evaluate and let me know if anyone has implemented such issue in a better way.

enter image description here

like image 740
Atul Avatar asked Mar 08 '13 09:03

Atul


1 Answers

I have a suggestion for your issue 1:

You can have a ViewModel that contains a property of another View Model. And on your controller method you just need to receive the parent ViewModel because the model binder will bind all properties for you.

public class GrandChildViewModel{
    public Int32 SelectedDropDownItem { get; set; }
    public List<Foo> ListOfFoo { get; set; }
}

public class ChildViewModel{
    public String Name { get; set; }
    public Int32 Age { get; set; }
}
public class FatherViewModel{
    public ChildViewModel Child { get; set; }
    public GrandChildViewModel GrandChild { get; set; }
}

This is an example of the hierarchy. Your View will reference FatherViewModel only. And your controllers will receive FatherViewModel also. The modelBinder will automatic do its job, if you create the HTML like:

@Html.TextBoxFor(m => m.Child.Name)

It will render the input:

<input type="text" id="Child_Name" name="Child_Name" />
like image 129
Tiago B Avatar answered Sep 22 '22 12:09

Tiago B