PDA

View Full Version : Load model in grid, MVP



Lendon
4 Sep 2011, 3:33 PM
Hello. I make CRUD application for simple "User" bean. I'm trying to make my work with grid in MVP style. But problem is that i can't load data into my grid. But when i exclude gxt css-styles from html page it works and i can see ugly table without styles but filled with data! There is what i have:
My User bean:

public class User {...}
UserBeanModel interface:

@BeanModelMarker.BEAN(User.class)
public interface UserBeanModel extends BeanModelMarker {}

View:


public class GXTUsersView extends LayoutContainer implements UsersPresenter.Display
{
private ContentPanel contentPanel;
private Grid<BeanModel> usersGrid;
private List<User> users;
private ListLoader<ListLoadResult<ModelData>> loader;


public GXTUsersView()
{
users = new ArrayList<User>();
RpcProxy<List<User>> proxy = new RpcProxy<List<User>>() {
@Override

public void load(Object loadConfig, AsyncCallback<List<User>> callback) {

BeanModelLookup.get().getFactory(User.class).createModel(users);

callback.onSuccess(users);

}

};

BeanModelReader reader = new BeanModelReader();




// loader and store

loader = new BaseListLoader<ListLoadResult<ModelData>>(proxy, reader);

ListStore<BeanModel> store = new ListStore<BeanModel>(loader);




loader.load();

// column model

List<ColumnConfig> columns = new ArrayList<ColumnConfig>();

columns.add(new ColumnConfig("id", "ID", 40));

columns.add(new ColumnConfig("name", "Name", 200));

columns.add(new ColumnConfig("email", "Email", 200));

ColumnModel cm = new ColumnModel(columns);




usersGrid = new Grid<BeanModel>(store, cm);

usersGrid.setAutoExpandColumn("name");
}


public void onRender(Element parent, int index)
{
super.onRender(parent, index);

ContentPanel panel = new ContentPanel();
panel.setHeading("BeanModel Grid Example");
panel.add(usersGrid);

contentPanel = new ContentPanel();
contentPanel.add(usersGrid);
contentPanel.setWidth(400);
add(contentPanel);
}




public void setData(List<User> users)
{
this.users = users;
loader.load();
}
.....
}


Presenter:


public class UsersPresenter implements Presenter
{
...
public interface Display
{
HasListClickHandlers getList();
void setData(List<User> data);
Widget asWidget();
}

private Display display;
private List<User> users;
private UserServiceAsync rpcService;
private HandlerManager eventBus;


public UsersPresenter(Display view, UserServiceAsync rpcService, HandlerManager eventBus)
{
this.rpcService = rpcService;
display = view;
this.eventBus = eventBus;
bind();
}

public void go(HasWidgets container)
{
container.clear();
container.add(display.asWidget());
loadUsers();
}


private void loadUsers()
{
AsyncCallback<List<User>> userCallback = new AsyncCallback<List<User>>()
{
public void onFailure(Throwable caught)
{
Window.alert("Loading user list failure.");
}


public void onSuccess(List<User> result)
{
fillUsersTable(result);
}
};


rpcService.getUsers(userCallback);
}




private void fillUsersTable(List<User> result)
{
users = result;
display.setData(users);
}

}

Note that i'm using my own Grid class which extends GXT's Grid and implements HasListClickHandlers interface. That hack is done to keep up MVP pattern, because for presenter it doesn't matter what UI lib view uses. Grid class is shown below:


public class Grid<T extends ModelData>
extends com.extjs.gxt.ui.client.widget.grid.Grid<T>
implements HasListClickHandlers
{
private List<ListClickHandler> handlers;


public Grid(ListStore<T> store, ColumnModel cm)
{
super(store, cm);
handlers = new ArrayList<ListClickHandler>();
}


@Override
protected void onClick(GridEvent<T> event)
{
if (Events.OnClick.equals(event.getType()))
{
ListClickEvent listEvent = new ListClickEvent(event);
for (ListClickHandler handler: handlers)
handler.handleEvent(listEvent);
}
else
super.onClick(event);
}


public HandlerRegistration addListClickHandler(final ListClickHandler handler)
{
handlers.add(handler);
return new HandlerRegistration() {


public void removeHandler()
{
handlers.remove(handler);
}};
}
}

HasListClickHandlers interface:


public interface HasListClickHandlers extends com.google.gwt.event.shared.HasHandlers
{
HandlerRegistration addListClickHandler(ListClickHandler handler);
}

Can anybody help me ? What i'm doing wrong ?