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.

GridEditorCustomField.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package com.vaadin.tests.components.grid;
  2. import com.vaadin.annotations.Theme;
  3. import com.vaadin.data.provider.ListDataProvider;
  4. import com.vaadin.data.provider.Query;
  5. import com.vaadin.server.VaadinRequest;
  6. import com.vaadin.tests.components.AbstractTestUIWithLog;
  7. import com.vaadin.tests.fieldgroup.ComplexPerson;
  8. import com.vaadin.ui.Button;
  9. import com.vaadin.ui.ComboBox;
  10. import com.vaadin.ui.Component;
  11. import com.vaadin.ui.CustomField;
  12. import com.vaadin.ui.Grid;
  13. import com.vaadin.ui.HorizontalLayout;
  14. import com.vaadin.ui.TextField;
  15. @Theme("tests-valo-disabled-animations")
  16. public class GridEditorCustomField extends AbstractTestUIWithLog {
  17. private static final String LAST_NAME_IDENTIFIER = "lastName";
  18. private static final String FIRST_NAME_IDENTIFIER = "firstName";
  19. private static final String ADDRESS_CITY_IDENTIFIER = "address.city";
  20. @Override
  21. protected void setup(VaadinRequest request) {
  22. ListDataProvider<ComplexPerson> dataProvider = ComplexPerson
  23. .createDataProvider(100);
  24. String[] cities = dataProvider.fetch(new Query<>())
  25. .map(person -> person.getAddress().getCity()).distinct()
  26. .toArray(String[]::new);
  27. CustomCitySelect cityEditor = new CustomCitySelect(cities);
  28. TextField firstNameField = new TextField();
  29. TextField lastNameField = new TextField();
  30. Grid<ComplexPerson> grid = new Grid<>();
  31. grid.setWidth("800px");
  32. grid.addColumn(person -> person.getFirstName())
  33. .setId(FIRST_NAME_IDENTIFIER).setCaption("First Name")
  34. .setEditorComponent(firstNameField,
  35. ComplexPerson::setFirstName);
  36. grid.addColumn(person -> person.getLastName())
  37. .setId(LAST_NAME_IDENTIFIER).setCaption("Last Name")
  38. .setEditorComponent(lastNameField, ComplexPerson::setLastName);
  39. grid.addColumn(person -> person.getAddress().getCity())
  40. .setId(ADDRESS_CITY_IDENTIFIER).setCaption("City Name")
  41. .setEditorComponent(cityEditor,
  42. (person, city) -> person.getAddress().setCity(city));
  43. grid.getEditor().setEnabled(true);
  44. grid.setDataProvider(dataProvider);
  45. addComponent(grid);
  46. }
  47. public static class CustomCitySelect extends CustomField<String> {
  48. private HorizontalLayout fieldLayout;
  49. private String[] values;
  50. private ComboBox<String> cityComboBox;
  51. private String cachedValue;
  52. public CustomCitySelect(String... values) {
  53. this.values = values;
  54. }
  55. @Override
  56. protected Component initContent() {
  57. fieldLayout = new HorizontalLayout();
  58. fieldLayout.setWidth("100%");
  59. cityComboBox = new ComboBox<>();
  60. cityComboBox.setItems(values);
  61. if (cachedValue != null) {
  62. cityComboBox.setValue(cachedValue);
  63. cachedValue = null;
  64. }
  65. fieldLayout.addComponent(cityComboBox);
  66. fieldLayout.setExpandRatio(cityComboBox, 1.0f);
  67. Button addCountryButton = new Button("New");
  68. fieldLayout.addComponent(addCountryButton);
  69. setFocusDelegate(cityComboBox);
  70. return fieldLayout;
  71. }
  72. @Override
  73. public String getValue() {
  74. if (cityComboBox == null) {
  75. return null;
  76. }
  77. return cityComboBox.getValue();
  78. }
  79. @Override
  80. protected void doSetValue(String value) {
  81. if (cityComboBox == null) {
  82. getContent();
  83. }
  84. cityComboBox.setValue(value);
  85. }
  86. }
  87. }