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.

SetDataSourceWithPropertyIds.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package com.vaadin.tests.components.table;
  2. import java.io.Serializable;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import com.vaadin.server.VaadinRequest;
  6. import com.vaadin.shared.ui.MarginInfo;
  7. import com.vaadin.tests.components.AbstractReindeerTestUI;
  8. import com.vaadin.ui.Button;
  9. import com.vaadin.ui.Label;
  10. import com.vaadin.v7.data.util.BeanItemContainer;
  11. import com.vaadin.v7.ui.Table;
  12. import com.vaadin.v7.ui.Table.CacheUpdateException;
  13. public class SetDataSourceWithPropertyIds extends AbstractReindeerTestUI {
  14. @Override
  15. protected String getTestDescription() {
  16. return "It should be possible to set a dataSource without generating columns that were never intended to be visible.<br>"
  17. + "First initialization happens before the table is attached.";
  18. }
  19. @Override
  20. protected Integer getTicketNumber() {
  21. return 10419;
  22. }
  23. private static final String TABLE_NAME = "JOBS";
  24. private static final String[] PK_COLUMN_NAMES = { "JOB_ID" };
  25. private static final String SEQUENCE_NAME = "";
  26. private static final String VERSION_COLUMN_NAME = "";
  27. Table table = new Table();
  28. BeanItemContainer<JobsBean> jobContainer = new BeanItemContainer(
  29. JobsBean.class);
  30. Label label = new Label();
  31. @Override
  32. protected void setup(VaadinRequest request) {
  33. getLayout().setSpacing(true);
  34. getLayout().setMargin(new MarginInfo(true, false, false, false));
  35. Button button = new Button("Toggle editability");
  36. button.setId("button");
  37. button.addClickListener(event -> refreshTable());
  38. label.setSizeUndefined();
  39. label.setId("label");
  40. table.setId("table");
  41. refreshTable();
  42. addComponent(button);
  43. addComponent(label);
  44. addComponent(table);
  45. }
  46. private void refreshTable() {
  47. // error only occurs when table is editable and already attached
  48. table.setEditable(table.getParent() == null || !table.isEditable());
  49. jobContainer.removeAllItems();
  50. jobContainer.addAll(getBeanList());
  51. try {
  52. table.setContainerDataSource(jobContainer);
  53. table.setVisibleColumns("jobId");
  54. label.setValue("no Exception");
  55. } catch (CacheUpdateException e) {
  56. List<String> propertyIds = new ArrayList<>();
  57. propertyIds.add("jobId");
  58. table.setContainerDataSource(jobContainer, propertyIds);
  59. label.setValue("Exception caught");
  60. }
  61. }
  62. private List<JobsBean> getBeanList() {
  63. List<JobsBean> list = new ArrayList<>();
  64. JobsBean jobsBean = new JobsBean();
  65. jobsBean.setJobId("1");
  66. list.add(jobsBean);
  67. return list;
  68. }
  69. public class JobsBean<T> implements Serializable {
  70. private static final long serialVersionUID = 1932918476339138393L;
  71. protected String jobId;
  72. protected String jobTitle;
  73. protected Long minSalary;
  74. protected Long maxSalary;
  75. private T auxiliaryData;
  76. public T getAuxiliaryData() {
  77. return auxiliaryData;
  78. }
  79. public void setAuxiliaryData(final T pAuxiliaryData) {
  80. auxiliaryData = pAuxiliaryData;
  81. }
  82. public String getTableName() {
  83. return TABLE_NAME;
  84. }
  85. public String[] getPrimaryKeyColumnNames() {
  86. return PK_COLUMN_NAMES;
  87. }
  88. public String getSequenceName() {
  89. return SEQUENCE_NAME;
  90. }
  91. public String getVersionColumnName() {
  92. return VERSION_COLUMN_NAME;
  93. }
  94. public String getJobId() {
  95. return jobId;
  96. }
  97. public void setJobId(final String pJobId) {
  98. jobId = pJobId;
  99. }
  100. public String getJobTitle() {
  101. return jobTitle;
  102. }
  103. public void setJobTitle(final String pJobTitle) {
  104. jobTitle = pJobTitle;
  105. }
  106. public Long getMinSalary() {
  107. return minSalary;
  108. }
  109. public void setMinSalary(final Long pMinSalary) {
  110. minSalary = pMinSalary;
  111. }
  112. public Long getMaxSalary() {
  113. return maxSalary;
  114. }
  115. public void setMaxSalary(final Long pMaxSalary) {
  116. maxSalary = pMaxSalary;
  117. }
  118. @Override
  119. public boolean equals(Object pObject) {
  120. if (this == pObject) {
  121. return true;
  122. }
  123. if (!(pObject instanceof JobsBean)) {
  124. return false;
  125. }
  126. JobsBean other = (JobsBean) pObject;
  127. return getJobId().equals(other.getJobId());
  128. }
  129. @Override
  130. public int hashCode() {
  131. return getJobId().hashCode();
  132. }
  133. }
  134. }