Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

GridIndexedContainerInsertSelect.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package com.vaadin.v7.tests.components.grid.basicfeatures.server;
  2. import com.vaadin.annotations.Widgetset;
  3. import com.vaadin.server.VaadinRequest;
  4. import com.vaadin.tests.components.AbstractReindeerTestUI;
  5. import com.vaadin.ui.Button;
  6. import com.vaadin.v7.data.Item;
  7. import com.vaadin.v7.data.sort.Sort;
  8. import com.vaadin.v7.data.util.AbstractInMemoryContainer;
  9. import com.vaadin.v7.data.util.IndexedContainer;
  10. import com.vaadin.v7.ui.CheckBox;
  11. import com.vaadin.v7.ui.Grid;
  12. import com.vaadin.v7.ui.HorizontalLayout;
  13. import com.vaadin.v7.ui.VerticalLayout;
  14. @Widgetset("com.vaadin.v7.Vaadin7WidgetSet")
  15. public class GridIndexedContainerInsertSelect extends AbstractReindeerTestUI {
  16. private static final String INFO_COLUMN_PATTERN = "InsertedIndex: %d ItemUid: %d SelectedRowIndex: %d SelectedItemUid: %s";
  17. private static final String COLUMN1 = "Column1";
  18. private static final String COLUMN2 = "Column2";
  19. private int currInsertionIndex = 4;
  20. private VerticalLayout layout;
  21. private Grid grid;
  22. private IndexedContainer container;
  23. protected void setup(VaadinRequest vaadinRequest) {
  24. layout = new VerticalLayout();
  25. layout.setSizeFull();
  26. initGrid();
  27. initData(container);
  28. selectFirstRow();
  29. CheckBox checkBox = new CheckBox("Select row after insert", true);
  30. Button.ClickListener clickListener = e -> {
  31. addNewItem();
  32. selectOnlyLastItemIfRequested(checkBox);
  33. currInsertionIndex++;
  34. };
  35. Button button = new Button("Add row after selected row!",
  36. clickListener);
  37. HorizontalLayout inputBox = new HorizontalLayout();
  38. inputBox.addComponents(checkBox, button);
  39. layout.addComponents(grid, inputBox);
  40. addComponent(layout);
  41. }
  42. private void initGrid() {
  43. grid = new Grid();
  44. grid.setSizeFull();
  45. container = new IndexedContainer();
  46. container.addContainerProperty(COLUMN1, String.class, null);
  47. container.addContainerProperty(COLUMN2, String.class, null);
  48. grid.setContainerDataSource(container);
  49. }
  50. private void selectFirstRow() {
  51. grid.select("1");
  52. }
  53. private void initData(IndexedContainer container) {
  54. createRowItem("1", "Item 1", "ItemCol2 1", container);
  55. createRowItem("2", "Item 2", "ItemCol2 2", container);
  56. createRowItem("3", "Item 3", "ItemCol2 3", container);
  57. }
  58. private void selectOnlyLastItemIfRequested(CheckBox checkBox) {
  59. if (checkBox.getValue()) {
  60. grid.select(currInsertionIndex + "");
  61. }
  62. }
  63. private void addNewItem() {
  64. final Object selectedRow = grid.getSelectedRow();
  65. int currentIndex = container.indexOfId(selectedRow);
  66. int insertIndex = currentIndex + 1;
  67. final Item thirdItem = container.addItemAt(insertIndex,
  68. currInsertionIndex + "");
  69. setRowData(thirdItem, "Item " + currInsertionIndex,
  70. getInfoColumnContent(selectedRow, currentIndex, insertIndex));
  71. }
  72. private void setRowData(Item thirdItem, String column1Data,
  73. String column2Data) {
  74. thirdItem.getItemProperty(COLUMN1).setValue(column1Data);
  75. thirdItem.getItemProperty(COLUMN2).setValue(column2Data);
  76. }
  77. private String getInfoColumnContent(Object selectedRow, int currentIndex,
  78. int insertIndex) {
  79. return String.format(INFO_COLUMN_PATTERN, insertIndex,
  80. currInsertionIndex, currentIndex, selectedRow);
  81. }
  82. private void createRowItem(String id, String column1, String column2,
  83. AbstractInMemoryContainer<Object, Object, Item> container) {
  84. Item firstRow = container.addItem(id);
  85. setRowData(firstRow, column1, column2);
  86. }
  87. }