VLayout layout = new VLayout(15);
layout.setWidth(650);
layout.setAutoHeight();
final ListGrid countryGrid = new ListGrid();
countryGrid.setLeaveScrollbarGap(true);
countryGrid.setCanFreezeFields(true);
countryGrid.setCanGroupBy(true);
countryGrid.setWidth100();
countryGrid.setHeight(224);
countryGrid.setDataSource(CountryXmlDS.getInstance());
countryGrid.setAutoFetchData(true);
//allow users to add formula and summary fields
//accessible in the grid header context menu
countryGrid.setCanAddFormulaFields(true);
countryGrid.setCanAddSummaryFields(true);
ListGridField countryCodeField = new ListGridField("countryCode", "Flag", 50);
countryCodeField.setAlign(Alignment.CENTER);
countryCodeField.setType(ListGridFieldType.IMAGE);
countryCodeField.setImageURLPrefix("flags/16/");
countryCodeField.setImageURLSuffix(".png");
countryCodeField.setCanSort(false);
ListGridField nameField = new ListGridField("countryName", "Country");
ListGridField capitalField = new ListGridField("capital", "Capital");
ListGridField populationField = new ListGridField("population", "Population");
populationField.setCellFormatter(new CellFormatter() {
public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
if (value == null) return null;
try {
NumberFormat nf = NumberFormat.getFormat("0,000");
return nf.format(((Number) value).longValue());
} catch (Exception e) {
return value.toString();
}
}
});
ListGridField areaField = new ListGridField("area", "Area (km²)");
areaField.setType(ListGridFieldType.INTEGER);
areaField.setCellFormatter(new CellFormatter() {
public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
if (value == null) return null;
String val = null;
try {
NumberFormat nf = NumberFormat.getFormat("0,000");
val = nf.format(((Number) value).longValue());
} catch (Exception e) {
return value.toString();
}
return val + "km²";
}
});
countryGrid.setFields(countryCodeField, nameField, capitalField, populationField, areaField);
//create another grid to display the preference name and viewState string
final ListGrid preferecesGrid = new ListGrid();
preferecesGrid.setHeight(300);
preferecesGrid.setWrapCells(true);
preferecesGrid.setFixedRecordHeights(false);
preferecesGrid.setCanEdit(true);
preferecesGrid.setEmptyMessage("No Saved Preferences");
preferecesGrid.setSelectionType(SelectionStyle.SINGLE);
preferecesGrid.setCanRemoveRecords(true);
final ListGridField name = new ListGridField("name", "Preference");
ListGridField viewState = new ListGridField("viewState", "View State String");
viewState.setEditorType(new TextAreaItem());
viewState.setCellFormatter(new CellFormatter() {
@Override
public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
//prettier display
return ((String) value).replace("\\r", "");
}
});
preferecesGrid.setFields(name, viewState);
preferecesGrid.setAutoFetchData(true);
//create a "preferences" DataSource to bind to SelectItem and Preferences ListGrid
final DataSource preferencesDS = new DataSource();
DataSourceIntegerField pkField = new DataSourceIntegerField("pk");
pkField.setHidden(true);
pkField.setPrimaryKey(true);
DataSourceTextField preferenceField = new DataSourceTextField("name", "Name");
DataSourceTextField stateField = new DataSourceTextField("viewState", "View State");
preferencesDS.setFields(pkField, preferenceField, stateField);
preferencesDS.setClientOnly(true);
preferecesGrid.setDataSource(preferencesDS);
ToolStripButton formulaButton = new ToolStripButton("Formula Builder", "crystal/oo/sc_insertformula.png");
formulaButton.setAutoFit(true);
formulaButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
countryGrid.addFormulaField();
}
});
ToolStripButton summaryBuilder = new ToolStripButton("Summary Builder", "crystal/16/apps/tooloptions.png");
summaryBuilder.setAutoFit(true);
summaryBuilder.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
countryGrid.addSummaryField();
}
});
final SelectItem preferenceSelectItem = new SelectItem("name");
preferenceSelectItem.setTitle("Preference");
ListGrid pickListProperties = new ListGrid();
pickListProperties.setEmptyMessage("No Saved Preferences");
preferenceSelectItem.setPickListProperties(pickListProperties);
preferenceSelectItem.setOptionDataSource(preferencesDS);
//apply the selected preference from the SelectItem
preferenceSelectItem.addChangedHandler(new ChangedHandler() {