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.

GridCustomPropertySetTest.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright 2000-2016 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.server.component.grid;
  17. import java.util.Optional;
  18. import java.util.stream.Stream;
  19. import org.junit.Assert;
  20. import org.junit.Test;
  21. import com.vaadin.data.PropertyDefinition;
  22. import com.vaadin.data.PropertySet;
  23. import com.vaadin.data.ValueProvider;
  24. import com.vaadin.server.Setter;
  25. import com.vaadin.ui.Grid;
  26. import com.vaadin.ui.Grid.Column;
  27. public class GridCustomPropertySetTest {
  28. public static class MyBeanWithoutGetters {
  29. public String str;
  30. public int number;
  31. public MyBeanWithoutGetters(String str, int number) {
  32. this.str = str;
  33. this.number = number;
  34. }
  35. }
  36. public static class GridWithCustomPropertySet
  37. extends Grid<MyBeanWithoutGetters> {
  38. private final class MyBeanPropertySet
  39. implements PropertySet<MyBeanWithoutGetters> {
  40. private PropertyDefinition<MyBeanWithoutGetters, String> strDef = new StrDefinition(
  41. this);
  42. private PropertyDefinition<MyBeanWithoutGetters, Integer> numberDef = new NumberDefinition(
  43. this);
  44. @Override
  45. public Stream<PropertyDefinition<MyBeanWithoutGetters, ?>> getProperties() {
  46. return Stream.of(strDef, numberDef);
  47. }
  48. @Override
  49. public Optional<PropertyDefinition<MyBeanWithoutGetters, ?>> getProperty(
  50. String name) {
  51. return getProperties().filter(pd -> pd.getName().equals(name))
  52. .findFirst();
  53. }
  54. }
  55. private final class StrDefinition
  56. implements PropertyDefinition<MyBeanWithoutGetters, String> {
  57. private PropertySet<MyBeanWithoutGetters> propertySet;
  58. public StrDefinition(
  59. PropertySet<MyBeanWithoutGetters> propertySet) {
  60. this.propertySet = propertySet;
  61. }
  62. @Override
  63. public ValueProvider<MyBeanWithoutGetters, String> getGetter() {
  64. return bean -> bean.str;
  65. }
  66. @Override
  67. public Optional<Setter<MyBeanWithoutGetters, String>> getSetter() {
  68. return Optional.of((bean, value) -> bean.str = value);
  69. }
  70. @Override
  71. public Class<String> getType() {
  72. return String.class;
  73. }
  74. @Override
  75. public Class<?> getPropertyHolderType() {
  76. return MyBeanWithoutGetters.class;
  77. }
  78. @Override
  79. public String getName() {
  80. return "string";
  81. }
  82. @Override
  83. public String getCaption() {
  84. return "The String";
  85. }
  86. @Override
  87. public PropertySet<MyBeanWithoutGetters> getPropertySet() {
  88. return propertySet;
  89. }
  90. }
  91. private final class NumberDefinition
  92. implements PropertyDefinition<MyBeanWithoutGetters, Integer> {
  93. private PropertySet<MyBeanWithoutGetters> propertySet;
  94. public NumberDefinition(
  95. PropertySet<MyBeanWithoutGetters> propertySet) {
  96. this.propertySet = propertySet;
  97. }
  98. @Override
  99. public ValueProvider<MyBeanWithoutGetters, Integer> getGetter() {
  100. return bean -> bean.number;
  101. }
  102. @Override
  103. public Optional<Setter<MyBeanWithoutGetters, Integer>> getSetter() {
  104. return Optional.of((bean, value) -> bean.number = value);
  105. }
  106. @Override
  107. public Class<Integer> getType() {
  108. return Integer.class;
  109. }
  110. @Override
  111. public Class<?> getPropertyHolderType() {
  112. return MyBeanWithoutGetters.class;
  113. }
  114. @Override
  115. public String getName() {
  116. return "numbah";
  117. }
  118. @Override
  119. public String getCaption() {
  120. return "The Number";
  121. }
  122. @Override
  123. public PropertySet<MyBeanWithoutGetters> getPropertySet() {
  124. return propertySet;
  125. }
  126. }
  127. public GridWithCustomPropertySet() {
  128. super();
  129. setPropertySet(new MyBeanPropertySet());
  130. }
  131. }
  132. @Test
  133. public void customPropertySet() {
  134. GridWithCustomPropertySet customGrid = new GridWithCustomPropertySet();
  135. Assert.assertEquals(0, customGrid.getColumns().size());
  136. Column<MyBeanWithoutGetters, Integer> numberColumn = (Column<MyBeanWithoutGetters, Integer>) customGrid
  137. .addColumn("numbah");
  138. Assert.assertEquals(1, customGrid.getColumns().size());
  139. Assert.assertEquals("The Number", numberColumn.getCaption());
  140. Assert.assertEquals(24, (int) numberColumn.getValueProvider()
  141. .apply(new MyBeanWithoutGetters("foo", 24)));
  142. Column<MyBeanWithoutGetters, String> stringColumn = (Column<MyBeanWithoutGetters, String>) customGrid
  143. .addColumn("string");
  144. Assert.assertEquals(2, customGrid.getColumns().size());
  145. Assert.assertEquals("The String", stringColumn.getCaption());
  146. Assert.assertEquals("foo", stringColumn.getValueProvider()
  147. .apply(new MyBeanWithoutGetters("foo", 24)));
  148. }
  149. }