Go Back   SmartClient Forums > Smart GWT Technical Q&A
Wiki Register Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 12th Aug 2009, 01:05
mat.dinister mat.dinister is offline
Registered Developer
 
Join Date: Aug 2009
Posts: 3
Default smartGWT vs. MVP pattern

Hello,

the model view presenter pattern in combination with gwt, which was presented on the google i/o (http://code.google.com/intl/de-DE/ev...ractices.html), doesn't seem to work with Interfaces of smartgwt.

if I try to add a Listener on a smartgwt specific handler (eg a cellclickhandler for a listgrid) in the presenter, it seems to be completely ignored.

a sample application of how to use mvp in gwt can be found here: http://code.google.com/p/gwt-mvp-sample/

a concrete example:

in the presenter I define an interface:
Code:
public class Presenter {
	
interface Display {
HasCellClickHandlers getFoo();
}
}
in the view I implement this interface:
Code:
public HasCellClickHandlers getFoo() {
	return thelistgrid;
}
back in the presenter again I bind the view and add the cellclickhandler:
Code:
public void bindView(final Display view){
		this.view = view;
		
view.getFoo().addCellClickHandler(new CellClickHandler() {
			public void onCellClick(CellClickEvent event) {
				// do something
				
			}
		});
in this example the onCellClick event is ignored..

does anyone know how to fix this? Are those interfaces not supported in smartGWT? will they be supported in future?

best regards and thank you for your help,
mat

Last edited by mat.dinister; 12th Aug 2009 at 01:17..
Reply With Quote
  #2  
Old 12th Aug 2009, 01:36
sjivan sjivan is offline
Registered Developer
 
Join Date: May 2008
Posts: 1,534
Default

There's nothing special required to support MVP. The SmartGWT ListGrid cell handlers work just fine and the MVP simply making the appropriate calls to SmartGWT.

Have a look at this Cell Click sample.

If you're still having issues, post a simplified standalone testcase that demonstrates the issue.

Sanjiv
Reply With Quote
  #3  
Old 12th Aug 2009, 05:19
mat.dinister mat.dinister is offline
Registered Developer
 
Join Date: Aug 2009
Posts: 3
Default

thanks for your answer, but I still have issues with smartgwt and mvp (I am already working with smartgwt 1.2).

I prepared a simple demonstration:

MvpView.java
Code:
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.HasCellClickHandlers;


public class MvpView extends Composite implements MvpPresenter.Display {
	
	private final ListGrid countryGrid = new ListGrid();
	private Button myButton = new Button("save");

	public MvpView() {
        countryGrid.setAlternateRecordStyles(true);  
        countryGrid.setShowAllRecords(true);  
        ListGridField nameField = new ListGridField("countryName", "Country");  
        ListGridField capitalField = new ListGridField("capital", "Capital");  
        ListGridField continentField = new ListGridField("continent", "Continent");  
        countryGrid.setFields(nameField, capitalField, continentField);  
        countryGrid.setData(CountryData.getRecords());

//       works here but has to be in the presenter (mvp pattern)        
//        countryGrid.addCellClickHandler(new CellClickHandler() {  
//			public void onCellClick(CellClickEvent event) {
//				GWT.log("oncellclick listgrid sucessful here but should be in the presenter!", null);
//			}
//	    });
        
		RootPanel.get().add(countryGrid);
		RootPanel.get().add(myButton);
	}

	public HasCellClickHandlers getListGridCellClick() {
		return countryGrid;
	}

	public HasClickHandlers getMyButton() {
		return myButton;
	}
}
MvpPresenter:
Code:
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.smartgwt.client.widgets.grid.events.HasCellClickHandlers;

public class MvpPresenter {
	interface Display {
		HasClickHandlers getMyButton();
		HasCellClickHandlers getListGridCellClick();
	}
	
	private Display view;

	public void bindView(final Display view) {
		this.view=view;
		
		view.getMyButton().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				GWT.log("onclick button sucessful", null);
			}
		});
		
		view.getListGridCellClick().addCellClickHandler(new CellClickHandler() {  
			public void onCellClick(CellClickEvent event) {
				GWT.log("oncellclick listgrid sucessful", null);
			}
	    });
	}
}
and the entry point binds the view:
Code:
public void onModuleLoad() {
		presenter.bindView(view);	
	}
CountryRecord and CountryData as shown in the smartgwt showcase.

The problem is, still, that the CellClickHandler is ignored in the presenter, but would work in the view directly.

any suggestions?
Reply With Quote
  #4  
Old 18th Aug 2009, 01:25
mat.dinister mat.dinister is offline
Registered Developer
 
Join Date: Aug 2009
Posts: 3
Default

the related ticket on the issue tracker can be found here:
http://code.google.com/p/smartgwt/is...ary%20Reporter
Reply With Quote
  #5  
Old 18th Aug 2009, 05:35
gavrikvetal gavrikvetal is offline
Registered Developer
 
Join Date: Apr 2009
Posts: 31
Default

Hi All
For mvp concepts i created some several classes which are implemented HasClickHandlers

This is stub for IButton

Code:
import ClickHandlerImpl;

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;

public class IButton extends com.smartgwt.client.widgets.IButton implements HasClickHandlers {

  public HandlerRegistration addClickHandler(ClickHandler clickHandler) {
    return super.addClickHandler(new ClickHandlerImpl(clickHandler));
  }
  
}
Here ClickHandlerImpl is some implementation of GWT ClickHandler like

Code:
public class ClickEventImpl extends ClickEvent {
}
After this You can use IButton as GWT HasClickHandler component in your Presenter class

Code:
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;

public class Presenter {

  public interface Display {
    HasClickHandlers getTestButton();
  }
  
  public void bindDisplay(Display d) {
    d.getTestButton().addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        // do something
      }
    });
  }
  
}
and below some Display implementation
Code:
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.RootPanel;


public class View implements Presenter.Display {
  
  private IButton testButton;
  
  public View() {
    testButton = new IButton("Test");
    RootPanel.get().add(testButton);
  }

  public HasClickHandlers getTestButton() {
    return testButton;
  }
  
}
I implemented several components such IButton, ImgButton, TreeGrid, ListGrid, ect.
Likely you can implement HasValue<T>, HasSelectionHandlers<T>, ect.

I hope GmartGWT team will implement GWT Has... interfaces in their components.
Thanks
Reply With Quote
  #6  
Old 18th Aug 2009, 17:26
sjivan sjivan is offline
Registered Developer
 
Join Date: May 2008
Posts: 1,534
Default

gavrikvetal,
I'm not sure what you're trying to do here? The SmartGWT components do implement various handlers and there's absolutely no need to adapt the IButton's HasClickHandlers with com.google.gwt.event.dom.client.HasClickHandlers.

You can implement MVP with any *Handler as illustrated in mat.dinister's sample.

Sanjiv
Reply With Quote
  #7  
Old 28th Aug 2009, 14:24
naim naim is offline
Registered Developer
 
Join Date: Aug 2009
Posts: 2
Default mvp

Hi Sjivan,
I'm currently working on a gwt app that we are porting over to smartgwt and I'm experiencing the same problems when trying to use the MVP pattern with an itemButton.
if I add a listener directly to the button an event is fired but if i had a listener in my presenter class it is ignored. I've read above that you have fixed this problem and was wondering if you could direct me to the fix or the patch.

regards Naim
Reply With Quote
  #8  
Old 28th Aug 2009, 14:59
sjivan sjivan is offline
Registered Developer
 
Join Date: May 2008
Posts: 1,534
Default

The issue mat.dinister posted was resolved in SVN. Can you try running using the latest build from SVN from here : http://www.smartclient.com/smartgwt/builds/

If you're still having issues, please post a standalone testcase. Note that I'm planning on releasing SmartGWT 1.2.1 which incorporates some minor bugfixes and module refactoring pretty soon (perhaps this weekend).

Sanjiv
Reply With Quote
  #9  
Old 30th Aug 2009, 14:36
naim naim is offline
Registered Developer
 
Join Date: Aug 2009
Posts: 2
Default

Hi Sjivan,
Thanks for the quick response I'm now currently using the latest build and the issue seems to be resolved thanks again .
Reply With Quote
  #10  
Old 28th Sep 2009, 08:33
netname netname is offline
Registered Developer
 
Join Date: Dec 2008
Posts: 16
Default MVP pattern and GWT Interfaces

I checked 1.3 and I do not see a way to represent a DynamicForm, a Grid, a TextItem, etc., as a single interface in the Presenter. It seems to be that you still need to create custom interfaces - and derived types of these SmartGWT componentes that implement these interfaces -.

Has anybody using the MVP pattern found a better way?

To clarify, in MVP you do not want the implementation classes (such as DynamicForm) to appear in the Presenter - they should just be present on the View -. O/w you cannot create standard JUnit cases (you have to use GWTTestCase). Thus, you need interfaces that can be used to replace these components in the Presenter.

I would go further and state that SmartGWT - as a project based on GWT -should try to follow/implement as much as possible the same interfaces that GWT defines. That way, we can be sure that in the future any changes/improvements to GWT will not imply a huge refactoring for SmartGWT....nor for our projects based on SmartGWT.

Thanks

Last edited by netname; 28th Sep 2009 at 08:38..
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search


© 2010,2011 Isomorphic Software. All Rights Reserved