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.

PropertyValueChange.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package com.vaadin.tests.components.table;
  2. import com.vaadin.data.Container;
  3. import com.vaadin.data.Property;
  4. import com.vaadin.data.Property.ValueChangeEvent;
  5. import com.vaadin.data.Property.ValueChangeListener;
  6. import com.vaadin.data.util.IndexedContainer;
  7. import com.vaadin.tests.components.TestBase;
  8. import com.vaadin.ui.AbstractSelect.NewItemHandler;
  9. import com.vaadin.ui.Button;
  10. import com.vaadin.ui.ComboBox;
  11. import com.vaadin.ui.Component;
  12. import com.vaadin.ui.DefaultFieldFactory;
  13. import com.vaadin.ui.Field;
  14. import com.vaadin.ui.Label;
  15. import com.vaadin.ui.Table;
  16. import com.vaadin.ui.Table.ColumnGenerator;
  17. import com.vaadin.ui.TableFieldFactory;
  18. public class PropertyValueChange extends TestBase {
  19. @Override
  20. protected String getDescription() {
  21. return "Property value change should only update absolutely "
  22. + "needed cells. Tables have common datasource. The first is "
  23. + "editable, second one has data in disabled fields, the lastone "
  24. + "is plain table that directly shows data. Use first table and "
  25. + "combobox/sync button to send changed values to server and evaluate "
  26. + "given uidl responses.";
  27. }
  28. @Override
  29. protected Integer getTicketNumber() {
  30. return 2823;
  31. }
  32. private IndexedContainer container;
  33. // Also use column generator in test, to ensure it is possible to build
  34. // columns that update automatically.
  35. ColumnGenerator multiplier = new ColumnGenerator() {
  36. private int getMultipliedValue(Property<Integer> p) {
  37. int i = p.getValue().intValue();
  38. return i * 3;
  39. }
  40. @Override
  41. public Component generateCell(Table source, Object itemId,
  42. Object columnId) {
  43. final Label l = new Label();
  44. @SuppressWarnings("unchecked")
  45. final Property<Integer> integer = source.getContainerProperty(
  46. itemId, "integer");
  47. l.setValue(String.valueOf(getMultipliedValue(integer)));
  48. // we must hook value change listener to ensure updates in all use
  49. // cases (eg. edit mode)
  50. if (integer instanceof Property.ValueChangeNotifier) {
  51. Property.ValueChangeNotifier notifier = (Property.ValueChangeNotifier) integer;
  52. notifier.addListener(new ValueChangeListener() {
  53. @Override
  54. public void valueChange(ValueChangeEvent event) {
  55. l.setValue(String.valueOf(getMultipliedValue(integer)));
  56. }
  57. });
  58. }
  59. return l;
  60. }
  61. };
  62. TableFieldFactory ff = new MyFieldFactory();
  63. @Override
  64. public void setup() {
  65. container = new IndexedContainer();
  66. container.addContainerProperty("text", String.class, "sampletext");
  67. container.addContainerProperty("integer", Integer.class, 5);
  68. container.addItem();
  69. container.addItem();
  70. Table t1 = new Table(
  71. "Editable table with bells and wistles. See description.");
  72. t1.setDescription("Opening combobox should never fire table"
  73. + " refresh (for this table). Update from textfield "
  74. + "(integer) may be sent to server however. The readonly table"
  75. + " my refresh, but not this one.");
  76. t1.setPageLength(0);
  77. t1.setContainerDataSource(container);
  78. t1.addGeneratedColumn("integer x 3", multiplier);
  79. t1.setTableFieldFactory(ff);
  80. t1.setEditable(true);
  81. t1.setId("editortable");
  82. Table t2 = new Table(
  83. "A clone of table1, but disabled. Properties are in components.");
  84. t2.setDescription("This table is in editable mode."
  85. + " Updates to common datasource should not affect redraw for this "
  86. + "table. Only the components inside table should get updated.");
  87. t2.setTableFieldFactory(ff);
  88. t2.setEditable(true);
  89. t2.setEnabled(false);
  90. t2.setContainerDataSource(container);
  91. t2.addGeneratedColumn("integer x 3", multiplier);
  92. t2.setPageLength(0);
  93. t2.setId("disabled table");
  94. Table reader = new Table("Reader table");
  95. reader.setDescription("This table should be redrawn on container changes as container data is "
  96. + "displayed directly in cells.");
  97. reader.setContainerDataSource(container);
  98. reader.addGeneratedColumn("integer x 3", multiplier);
  99. reader.setPageLength(0);
  100. reader.setId("reader table");
  101. getLayout().addComponent(t1);
  102. getLayout().addComponent(t2);
  103. getLayout().addComponent(reader);
  104. getLayout().addComponent(new Button("Sync!"));
  105. }
  106. }
  107. class MyFieldFactory extends DefaultFieldFactory {
  108. IndexedContainer texts = new IndexedContainer();
  109. public MyFieldFactory() {
  110. texts.addItem("sampletext");
  111. texts.addItem("foo");
  112. texts.addItem("bar");
  113. for (int i = 0; i < 100; i++) {
  114. texts.addItem("foo" + 1);
  115. }
  116. }
  117. @Override
  118. public Field<?> createField(Container container, Object itemId,
  119. Object propertyId, Component uiContext) {
  120. if (propertyId.equals("text")) {
  121. // replace text fields with comboboxes
  122. final ComboBox cb = new ComboBox() {
  123. };
  124. cb.setContainerDataSource(texts);
  125. cb.setNewItemsAllowed(true);
  126. cb.setNewItemHandler(new NewItemHandler() {
  127. @Override
  128. public void addNewItem(String newItemCaption) {
  129. texts.addItem(newItemCaption);
  130. cb.setValue(newItemCaption);
  131. }
  132. });
  133. return cb;
  134. }
  135. return super.createField(container, itemId, propertyId, uiContext);
  136. }
  137. }