Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ContainerSizeChangeDuringTablePaint.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.components.table;
  17. import java.util.Iterator;
  18. import com.vaadin.data.Container;
  19. import com.vaadin.data.Item;
  20. import com.vaadin.data.util.IndexedContainer;
  21. import com.vaadin.server.PaintException;
  22. import com.vaadin.server.PaintTarget;
  23. import com.vaadin.server.VaadinRequest;
  24. import com.vaadin.tests.components.AbstractTestUI;
  25. import com.vaadin.ui.Button;
  26. import com.vaadin.ui.Button.ClickEvent;
  27. import com.vaadin.ui.Table;
  28. public class ContainerSizeChangeDuringTablePaint extends AbstractTestUI {
  29. /**
  30. * A test {@link Table} that simply acts a hook for when Vaadin starts
  31. * painting the Table.
  32. */
  33. private static class WobblyTable extends Table {
  34. /**
  35. * A flag for the container to know when it should change the size.
  36. */
  37. boolean isBeingPainted;
  38. public WobblyTable(String caption, Container dataSource) {
  39. super(caption, dataSource);
  40. }
  41. @Override
  42. public void paintContent(PaintTarget target) throws PaintException {
  43. isBeingPainted = true;
  44. try {
  45. super.paintContent(target);
  46. } finally {
  47. isBeingPainted = false;
  48. }
  49. }
  50. }
  51. /**
  52. * A {@link Container} that can change its size while its
  53. * {@link WobblyTable} is being painted.
  54. */
  55. private static class WobblyContainer extends IndexedContainer {
  56. private WobblyTable table = null;
  57. private boolean shouldSabotageNextPaint = false;
  58. public void setWobblyTable(WobblyTable table) {
  59. this.table = table;
  60. }
  61. @Override
  62. public int size() {
  63. if (table != null && table.isBeingPainted
  64. && shouldSabotageNextPaint) {
  65. try {
  66. System.out.print("Firing item set change "
  67. + "event during Table paint... ");
  68. fireItemSetChange();
  69. System.out.println("Success!");
  70. } finally {
  71. shouldSabotageNextPaint = false;
  72. }
  73. }
  74. return super.size();
  75. }
  76. public void sabotageNextPaint() {
  77. shouldSabotageNextPaint = true;
  78. }
  79. }
  80. private static final Object PROPERTY_1 = new Object();
  81. private static final Object PROPERTY_2 = new Object();
  82. private static final Object PROPERTY_3 = new Object();
  83. @Override
  84. protected void setup(VaadinRequest request) {
  85. final WobblyContainer container = generateContainer();
  86. final WobblyTable table = createTable(container);
  87. container.setWobblyTable(table);
  88. addComponent(table);
  89. Button button = new Button(
  90. "Add an item and also trigger an ItemSetChangeEvent in Container during next Table paint",
  91. new Button.ClickListener() {
  92. @Override
  93. public void buttonClick(ClickEvent event) {
  94. // we need this to simply trigger a table paint.
  95. addItem(container, "A", "New", "Row");
  96. container.sabotageNextPaint();
  97. event.getButton().setCaption(
  98. "Event was fired successfully.");
  99. }
  100. });
  101. button.setId("addRow");
  102. addComponent(button);
  103. }
  104. private static WobblyTable createTable(IndexedContainer container) {
  105. WobblyTable t = new WobblyTable(null, container);
  106. t.setColumnHeader(PROPERTY_1, "Property 1");
  107. t.setColumnHeader(PROPERTY_2, "Property 2");
  108. t.setColumnHeader(PROPERTY_3, "Property 3");
  109. t.setPageLength(container.size() + 1);
  110. return t;
  111. }
  112. private static WobblyContainer generateContainer() {
  113. WobblyContainer c = new WobblyContainer();
  114. c.addContainerProperty(PROPERTY_1, String.class, null);
  115. c.addContainerProperty(PROPERTY_2, String.class, null);
  116. c.addContainerProperty(PROPERTY_3, String.class, null);
  117. addItem(c, "Hello", "World", "!");
  118. return c;
  119. }
  120. @SuppressWarnings("unchecked")
  121. private static void addItem(Container c, Object... properties) {
  122. Object itemId = c.addItem();
  123. Item item = c.getItem(itemId);
  124. int i = 0;
  125. Iterator<?> propIter = c.getContainerPropertyIds().iterator();
  126. while (propIter.hasNext()) {
  127. Object propertyId = propIter.next();
  128. item.getItemProperty(propertyId).setValue(properties[i]);
  129. i++;
  130. }
  131. }
  132. @Override
  133. protected String getTestDescription() {
  134. return "Container changes during the painting cycle should not lead to an IllegalStateException";
  135. }
  136. @Override
  137. protected Integer getTicketNumber() {
  138. return 12258;
  139. }
  140. }