super.onFirstShowPage();
final PScrollPanel scroll = new PScrollPanel();
final PFlowPanel layout = new PFlowPanel();
final PFlexTable formContainer = new PFlexTable();
final PSimplePanel listContainer = new PSimplePanel();
layout.add(formContainer);
final PButton add = new PButton("Add");
add.addClickHandler(new PClickHandler() {
@Override
public void onClick(final PClickEvent event) {
final Integer i = Integer.parseInt(line.getText());
final Pony father = dataGrid.getVisibleItem(i);
final List<Pony> copy = new ArrayList<Pony>();
for (int j = 0; j < 3; j++) {
final Pony p = new Pony(father.getId(), "Copy-" + father.getName(), father.getAge(), father.getRace());
copy.add(p);
}
dataGrid.insertSubList(father, copy);
}
});
final PButton remove = new PButton("Remove");
remove.addClickHandler(new PClickHandler() {
@Override
public void onClick(final PClickEvent event) {
final Integer i = Integer.parseInt(line.getText());
final Pony father = dataGrid.getVisibleItem(i);
dataGrid.removeSubList(father);
}
});
layout.add(line);
layout.add(add);
layout.add(remove);
layout.add(listContainer);
scroll.setWidget(layout);
examplePanel.setWidget(scroll);
// Register handler
addHandler(SubmitFormEvent.TYPE, this);
final Pager<Pony> pager = new Pager<Pony>(new DefaultPagerView());
dataGrid = new DataGridActivity<Pony>(new DefaultSimpleListView());
final DefaultActionSelectorView actionSelectorView = new DefaultActionSelectorView();
final DefaultInfoSelectorView infoSelectorView = new DefaultInfoSelectorView();
final CompositeSelectorView selectorView = new CompositeSelectorView(actionSelectorView, infoSelectorView);
final Selector<Pony> selector = new Selector<Pony>(selectorView);
final RemoteDataProvider<Pony> dataProvider = new RemoteDataProvider<Pony>(pager, dataGrid) {
@Override
protected List<Pony> getData(final Query query) {
final Result<List<Pony>> result = new FindPonysCommand(query).execute();
final List<Pony> data = result.getData();
final int fullSize = result.getFullSize();
pager.process(fullSize);
selector.reset();
selector.setPageSize(data.size());
selector.setFullSize(fullSize);
return data;
}
@Override
protected List<Pony> getFullData(final Query query) {
return new FindPonysCommand(query).execute().getData();
}
};
final DataGridColumnDescriptor<Pony, Pony> selectColumnDescriptor = new DataGridColumnDescriptor<Pony, Pony>();
selectColumnDescriptor.setHeaderCellRenderer(new StringHeaderCellRenderer("Select"));
selectColumnDescriptor.setValueProvider(new IdentityValueProvider<Pony>());
final CellRenderer<Pony> selectCellRenderer = new CellRenderer<Pony>() {
@Override
public IsPWidget render(final int row, final Pony value) {
final SelectorCheckBox<Pony> selectorCheckBox = new SelectorCheckBox<Pony>();
selectorCheckBox.setData(value);
selectorCheckBox.addSelectableListener(selector);
selector.registerSelectable(selectorCheckBox);
selectorCheckBox.addValueChangeHandler(new PValueChangeHandler<Boolean>() {
@Override
public void onValueChange(final PValueChangeEvent<Boolean> event) {
if (event.getValue()) {
selectorCheckBox.onCheck();
dataGrid.selectRow(value);
} else {
selectorCheckBox.onUncheck();
dataGrid.unSelectRow(value);
}
}
});
return selectorCheckBox;
}
};
selectColumnDescriptor.setCellRenderer(selectCellRenderer);
selectColumnDescriptor.setSubCellRenderer(selectCellRenderer);
final DataGridColumnDescriptor<Pony, Pony> descriptor = new DataGridColumnDescriptor<Pony, Pony>();
descriptor.setHeaderCellRenderer(new HeaderCellRenderer() {
@Override
public IsPWidget render() {
return new PLabel();
}
});
final DataGridColumnDescriptor<Pony, String> nameColumnDescriptor = new DataGridColumnDescriptor<Pony, String>();
final ComplexHeaderCellRenderer nameHeaderCellRender = new FilterableHeaderCellRenderer("Name", new StringTextBoxFormField(), "name");
nameHeaderCellRender.addFilterListener(dataProvider);
nameColumnDescriptor.setHeaderCellRenderer(nameHeaderCellRender);
nameColumnDescriptor.setValueProvider(new BeanValueProvider<Pony, String>("name"));
nameColumnDescriptor.setCellRenderer(new LabelCellRenderer<String>());
nameColumnDescriptor.setSubCellRenderer(new LabelCellRenderer<String>());
nameColumnDescriptor.setSubCellRenderer(new LabelCellRenderer<String>());
final PListBox ageListBox = new PListBox(true);
for (int i = 0; i < 30; i++)
ageListBox.addItem(i + " year", i);
final DataGridColumnDescriptor<Pony, String> ageColumnDescriptor = new DataGridColumnDescriptor<Pony, String>();
ageColumnDescriptor.setValueProvider(new BeanValueProvider<Pony, String>("age"));
final ComplexHeaderCellRenderer ageHeaderCellRender = new ComplexHeaderCellRenderer("Age", new ListBoxFormField<Integer>(ageListBox), "age");
ageHeaderCellRender.addFilterListener(dataProvider);
ageColumnDescriptor.setHeaderCellRenderer(ageHeaderCellRender);
ageColumnDescriptor.setCellRenderer(new LabelCellRenderer<String>());
ageColumnDescriptor.setSubCellRenderer(new LabelCellRenderer<String>());
final DataGridColumnDescriptor<Pony, String> raceColumnDescriptor = new DataGridColumnDescriptor<Pony, String>();
raceColumnDescriptor.setValueProvider(new BeanValueProvider<Pony, String>("race"));
final ComplexHeaderCellRenderer raceHeaderCellRender = new ComplexHeaderCellRenderer("Race", new StringTextBoxFormField(), "race");
raceHeaderCellRender.addFilterListener(dataProvider);
raceColumnDescriptor.setHeaderCellRenderer(raceHeaderCellRender);
raceColumnDescriptor.setCellRenderer(new LabelCellRenderer<String>());
raceColumnDescriptor.setSubCellRenderer(new LabelCellRenderer<String>());
dataGrid.addDataGridColumnDescriptor(selectColumnDescriptor);
dataGrid.addDataGridColumnDescriptor(nameColumnDescriptor);
dataGrid.addDataGridColumnDescriptor(ageColumnDescriptor);
dataGrid.addDataGridColumnDescriptor(raceColumnDescriptor);
dataProvider.registerHasCriteria(ageHeaderCellRender);
dataProvider.registerHasCriteria(nameHeaderCellRender);
dataProvider.registerHasCriteria(raceHeaderCellRender);
formContainer.setWidget(0, 0, actionSelectorView);
final PButton refresh = new PButton("Refresh");
refresh.addClickHandler(new PClickHandler() {
@Override
public void onClick(final PClickEvent event) {
dataProvider.onPageChange(0);
}
});
formContainer.setWidget(0, 1, refresh);
final PButton addPonyButton = new PButton("Create new pony");
addPonyButton.addClickHandler(new PClickHandler() {
@Override
public void onClick(final PClickEvent clickEvent) {
showCreatePonyPopup();
}
});
addPonyButton.addStyleName(PonySDKTheme.BUTTON_GREEN);
formContainer.setWidget(0, 2, addPonyButton);
formContainer.setWidget(0, 3, pager.asWidget());
formContainer.setWidget(0, 4, infoSelectorView);
// Build create pony form
buildCreatePonyActivity();
dataProvider.onPageChange(0);