CellTable MVP

1,050 views
Skip to first unread message

rlhawk1

unread,
Jan 14, 2011, 8:56:35 PM1/14/11
to Google Web Toolkit
I'm trying to create a CellTable where when you click on a row, it
goes to another screen, and I'm trying to keep a correct MVP
architecture.

I have two questions. First, do I initialize my CellTable in the
view, or the presenter?

Right now I have something like this in my view code, but with more
columns.

TextColumn<LightOwner> nameColumn = new TextColumn<LightOwner>() {
public String getValue(LightOwner o) {
return o.getName();
}
};
ownersTable.addColumn(nameColumn, "Name");

It works fine, but is it okay for the view to be knowing about my
LightOwner type like that.

Secondly, I need it to bring up the owner edit screen for the id of
the row clicked when they click on a row. Reading around, it /sounds/
like I need to use the SelectionModel class for this, but I'm unsure
rather that should be in the presenter or view also.

By their example, I need to add code something like this:

final SingleSelectionModel<String> selectionModel = new
SingleSelectionModel<String>();
ownersTable.setSelectionModel(selectionModel);
selectionModel.addSelectionChangeHandler(new
SelectionChangeEvent.Handler() {
public void onSelectionChange(SelectionChangeEvent event) {
String selected = selectionModel.getSelectedObject();
if (selected != null) {
// Go to edit screen with selected.getId();
}
}
});

But what I'm used to is having a HasClickHandlers in the view and
attaching the event in the presenter. How am I supposed to use
SingleSelectionModel with MVP? Or am I going about this totally wrong?

David Chandler

unread,
Jan 16, 2011, 1:53:36 AM1/16/11
to google-we...@googlegroups.com
Hi Raymond,

I've taken a stab at this in the listwidget sample project. I wanted the view screen to be its own Place for bookmarking, so I created a HyperlinkCell and use utility methods in RequestFactory and PlaceHistoryMapper to create a URL from an ID / Place. Here's the relevant code from ListsViewImpl (arguably doesn't really belong in the view, but I haven't moved it out yet):

Column<ItemListProxy, Hyperlink> linkColumn = new Column<ItemListProxy, Hyperlink>(new HyperlinkCell())

{

@Override

public Hyperlink getValue(ItemListProxy list)

{

String proxyToken = clientFactory.getRequestFactory().getHistoryToken(list.stableId());

String historyToken = clientFactory.getHistoryMapper().getToken(new EditListPlace(proxyToken));

Hyperlink h = new Hyperlink(list.getName(),historyToken);

return h;

}

};

cellTable.addColumn(linkColumn, "Edit");


and HyperlinkCell:


public class HyperlinkCell extends AbstractCell<Hyperlink>

{

@Override

public void render(com.google.gwt.cell.client.Cell.Context context,

Hyperlink h, SafeHtmlBuilder sb)

{

                // WARNING Make sure there's no user-provided content in your link!

sb.append(SafeHtmlUtils.fromTrustedString(h.toString()));

}

}


To decode the URL, use the companion method in RequestFactory:

final ListwidgetRequestFactory req = clientFactory.getRequestFactory();

EntityProxyId<ItemListProxy> proxyId = req.getProxyId(this.itemListToken);


All this is necessary only if you want the detail screen to be a bookmarkable Place; otherwise, you can use a SelectionModel as shown in http://gwt.google.com/samples/Showcase/Showcase.html#!CwCellList

I'm inclined to put SelectionModel in the presenter since it's a model, not a widget. And CellTable et al implement non-widget interfaces so you can safely reference them in the view interface like this:

HasData<SomeProxy> getSomeProxyDisplay();


then in your presenter:
view.getSomeProxyDisplay().setSelectionModel(...);

The listwidget sample project is at http://code.google.com/p/listwidget/

HTH,
/dmc



--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.




--
David Chandler
Developer Programs Engineer, Google Web Toolkit
w: http://code.google.com/
b: http://googlewebtoolkit.blogspot.com/
t: @googledevtools

rlhawk1

unread,
Jan 17, 2011, 5:16:35 PM1/17/11
to Google Web Toolkit
Thank you so much! Using what you said I was able to get it working
in just a few minutes. I'm using your sample project to help arrange
some other stuff better as well.

I probably need to work on my event handling rather than just using
History.newItem(), but here's the code I have in my presenter now.

// In my constructor.
selectionModel = new SingleSelectionModel<LightOwner>();
display.getOwnerTable().setSelectionModel(selectionModel);

// In the function I use for setting up all event handling.
selectionModel.addSelectionChangeHandler(new
SelectionChangeEvent.Handler() {
public void onSelectionChange(SelectionChangeEvent event) {
LightOwner selected = selectionModel.getSelectedObject();
if (selected != null) {
History.newItem("edit-owner/"+selected.getId());
}
}
});

Thanks again!
> bookmarkable Place; otherwise, you can use a SelectionModel as shown inhttp://gwt.google.com/samples/Showcase/Showcase.html#!CwCellList
>
> I'm inclined to put SelectionModel in the presenter since it's a model, not
> a widget. And CellTable et al implement non-widget interfaces so you can
> safely reference them in the view interface like this:
>
>    HasData<SomeProxy> getSomeProxyDisplay();
>
> then in your presenter:
> view.getSomeProxyDisplay().setSelectionModel(...);
>
> The listwidget sample project is athttp://code.google.com/p/listwidget/
> > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsubs cr...@googlegroups.com>
> > .
Reply all
Reply to author
Forward
0 new messages