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.

TableWithContainerRequiringEqualsForItemId.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package com.vaadin.tests.components.table;
  2. import java.util.Date;
  3. import com.vaadin.server.VaadinRequest;
  4. import com.vaadin.tests.components.AbstractReindeerTestUI;
  5. import com.vaadin.tests.util.Log;
  6. import com.vaadin.ui.Button;
  7. import com.vaadin.ui.Component;
  8. import com.vaadin.v7.data.util.BeanContainer;
  9. import com.vaadin.v7.data.util.BeanItem;
  10. import com.vaadin.v7.ui.Table;
  11. import com.vaadin.v7.ui.themes.Reindeer;
  12. public class TableWithContainerRequiringEqualsForItemId
  13. extends AbstractReindeerTestUI {
  14. private MyEntityContainer container = new MyEntityContainer();
  15. private Log log = new Log(10);
  16. public static class MyEntityContainer
  17. extends BeanContainer<Long, MyEntity> {
  18. public MyEntityContainer() {
  19. super(MyEntity.class);
  20. setBeanIdResolver(
  21. new BeanIdResolver<Long, TableWithContainerRequiringEqualsForItemId.MyEntity>() {
  22. @Override
  23. public Long getIdForBean(MyEntity bean) {
  24. // Return a new instance every time to ensure Table
  25. // can
  26. // handle it
  27. return new Long(bean.getId());
  28. }
  29. });
  30. }
  31. @Override
  32. public Long getIdByIndex(int index) {
  33. // Explicitly get the id using the resolver to make sure the
  34. // instance does not stay the same
  35. BeanItem<MyEntity> beanItem = getItem(super.getIdByIndex(index));
  36. return getBeanIdResolver().getIdForBean(beanItem.getBean());
  37. }
  38. }
  39. @Override
  40. protected void setup(VaadinRequest request) {
  41. Table t = new Table("Table with 1000 item");
  42. t.addGeneratedColumn("Actions", new Table.ColumnGenerator() {
  43. @Override
  44. public Component generateCell(final Table source,
  45. final Object itemId, final Object columnId) {
  46. Button tripFolderLink = new Button("Button" + itemId);
  47. tripFolderLink.addClickListener(event -> log.log("Button "
  48. + event.getButton().getCaption() + " clicked"));
  49. tripFolderLink.setStyleName(Reindeer.BUTTON_SMALL);
  50. return tripFolderLink;
  51. }
  52. });
  53. for (int i = 0; i < 1000; i++) {
  54. MyEntity myEntity = new MyEntity(i + "st");
  55. myEntity.setCreated(
  56. new Date(new Date().getTime() - 24 * 60 * 60 * 1000L));
  57. myEntity.setId(i);
  58. container.addBean(myEntity);
  59. }
  60. t.setContainerDataSource(container);
  61. t.setVisibleColumns("id", "created", "name", "Actions");
  62. addComponent(t);
  63. addComponent(log);
  64. t.sort(new Object[] { "id" }, new boolean[] { false });
  65. }
  66. @Override
  67. protected String getTestDescription() {
  68. return "Test that verifies that Table works correctly with containers which do not return the same instance of the itemId object but instead requires an itemId.equals(otherItemId) check";
  69. }
  70. @Override
  71. protected Integer getTicketNumber() {
  72. return 8712;
  73. }
  74. public static class MyEntity {
  75. private long id;
  76. private String name;
  77. private Date created;
  78. public MyEntity() {
  79. }
  80. public MyEntity(String string) {
  81. name = string;
  82. }
  83. public String getName() {
  84. return name;
  85. }
  86. public Date getCreated() {
  87. return created;
  88. }
  89. public void setCreated(Date created) {
  90. this.created = created;
  91. }
  92. public long getId() {
  93. return id;
  94. }
  95. public void setId(long id) {
  96. this.id = id;
  97. }
  98. }
  99. }