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.8KB

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