You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GridRefreshRow.java 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.vaadin.tests.components.grid;
  2. import com.vaadin.annotations.Theme;
  3. import com.vaadin.server.VaadinRequest;
  4. import com.vaadin.tests.components.AbstractTestUIWithLog;
  5. import com.vaadin.tests.util.Person;
  6. import com.vaadin.tests.util.PersonContainer;
  7. import com.vaadin.ui.Button;
  8. import com.vaadin.ui.CheckBox;
  9. import com.vaadin.ui.Component;
  10. import com.vaadin.ui.HorizontalLayout;
  11. import com.vaadin.v7.ui.Grid;
  12. import com.vaadin.v7.ui.Grid.CellReference;
  13. import com.vaadin.v7.ui.Grid.CellStyleGenerator;
  14. import com.vaadin.v7.ui.Grid.RowReference;
  15. import com.vaadin.v7.ui.Grid.RowStyleGenerator;
  16. @Theme("valo")
  17. public class GridRefreshRow extends AbstractTestUIWithLog {
  18. private PersonContainer container;
  19. private Grid grid;
  20. private boolean styles[] = { false, false, false };
  21. @Override
  22. protected void setup(VaadinRequest request) {
  23. getPage().getStyles()
  24. .add(".rowstyle td {background-color: lightgreen !important;}");
  25. getPage().getStyles()
  26. .add("td.cellstyle {background-color: lightblue !important;}");
  27. container = PersonContainer.createWithTestData(100);
  28. container.addNestedContainerBean("address");
  29. grid = new Grid(container);
  30. grid.setWidth("800px");
  31. grid.setRowStyleGenerator(new RowStyleGenerator() {
  32. @Override
  33. public String getStyle(RowReference row) {
  34. int index = container.indexOfId(row.getItemId());
  35. if (index < 3 && styles[index]) {
  36. return "rowstyle";
  37. }
  38. return null;
  39. }
  40. });
  41. grid.setCellStyleGenerator(new CellStyleGenerator() {
  42. @Override
  43. public String getStyle(CellReference cell) {
  44. int index = container.indexOfId(cell.getItemId());
  45. if (index < 3 && styles[index]
  46. && "email".equals(cell.getPropertyId())) {
  47. return "cellstyle";
  48. }
  49. return null;
  50. }
  51. });
  52. addComponent(grid);
  53. addComponents(new HorizontalLayout(update(0), update(1), update(2)));
  54. Button refresh10 = new Button("Refresh 0-9", event -> grid
  55. .refreshRows(container.getItemIds(0, 9).toArray()));
  56. refresh10.setId("refresh10");
  57. addComponents(
  58. new HorizontalLayout(refresh(0), refresh(1), refresh(2),
  59. new Button("Refresh non-existant",
  60. event -> grid.refreshRows("foobar"))),
  61. refresh10);
  62. addComponents(new HorizontalLayout(style(0), style(1), style(2)));
  63. }
  64. private Component style(final int i) {
  65. final CheckBox checkBox = new CheckBox("Style for " + i);
  66. checkBox.addValueChangeListener(
  67. event -> styles[i] = checkBox.getValue());
  68. checkBox.setId("style" + i);
  69. return checkBox;
  70. }
  71. private Component update(final int i) {
  72. Button button = new Button("Update " + i, event -> {
  73. Person p = container.getIdByIndex(i);
  74. p.setFirstName("!" + p.getFirstName());
  75. });
  76. button.setId("update" + i);
  77. return button;
  78. }
  79. protected Component refresh(final int i) {
  80. Button button = new Button("Refresh row " + i,
  81. event -> grid.refreshRows(container.getIdByIndex(i)));
  82. button.setId("refresh" + i);
  83. return button;
  84. }
  85. }