It is possible to add multiple Grids to a single UiBinder xml file. There are multiple ways this can be done. I've listed 3 below, but I'm sure there are even more ways this can work.
One approach would be to create the Grid instances in the owner class and use a @UiField(provided = true). This tells UiBinder to use the instances the owner class creates rather than instantiating a new Grid instances. That would look something like this:
Code:
@UiField(provided = true) Grid<Stock> grid1;
@UiField(provided = true) Grid<Stock> grid2;
public Widget asWidget() {
grid1 = new Grid(....);
grid2 = new Grid(....);
return uiBinder.createAndBindUi(this);
}
The XML would then reference those fields with the ui:field attribute:
Code:
<grid:Grid ui:field="grid1" />
...
<grid:Grid ui:field="grid2" />
If you're wanting more of the Widget creation in the XML, then you can always provide the required Grid constructor parameters in a similar manner.
Code:
@UiField(provided = true) ColumnModel<Stock> columnModel1;
@UiField(provided = true) ListStore<Stock> store1;
@UiField(provided = true) GridView<Stock> gridView1;
Then, in the XML, you'll have to use <ui:with> elements.
Code:
<ui:with type="com.sencha.gxt.widget.core.client.grid.ColumnModel" field="columnModel1" />
....
<grid:Grid cm="{columnModel1}" store="{store1}" view="{gridView1}" />
If you want to continue to use the @UiFactory as you described, you'll have to pass some kind of indicator as a method parameter. This can be an enum, boolean, String, or whatever you choose. Here's an example of a @UiFactory for multiple ColumnModel instances using an integer to decide which one to create. (note that using <ui:attributes> inside <ui:with> requires GWT 2.5.0, or if you're using GWT 2.4.0 you would use the uibinder-bridge jar to get this feature)
Code:
<ui:with type="com.sencha.gxt.widget.core.client.grid.ColumnModel" field="columnModel1">
<ui:attributes which="1" />
</ui:with>
<ui:with type="com.sencha.gxt.widget.core.client.grid.ColumnModel" field="columnModel2">
<ui:attributes which="2" />
</ui:with>
In the owner class, you'd have the following factory method:
Code:
@UiFactory
public ColumnModel<Stock> createColumnModel(int which) {
switch(which) {
case 1:
ColumnModel cm = ...
return cm;
case 2:
// alternatively delegate to private methods
return createColumnModelForGrid2();
}
}