Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

GridEditorCustomField.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.components.grid;
  17. import java.util.HashSet;
  18. import java.util.Set;
  19. import com.vaadin.annotations.Theme;
  20. import com.vaadin.data.Binder;
  21. import com.vaadin.server.VaadinRequest;
  22. import com.vaadin.server.data.ListDataProvider;
  23. import com.vaadin.server.data.Query;
  24. import com.vaadin.tests.components.AbstractTestUIWithLog;
  25. import com.vaadin.tests.fieldgroup.ComplexPerson;
  26. import com.vaadin.ui.Button;
  27. import com.vaadin.ui.ComboBox;
  28. import com.vaadin.ui.Component;
  29. import com.vaadin.ui.CustomField;
  30. import com.vaadin.ui.Grid;
  31. import com.vaadin.ui.HorizontalLayout;
  32. import com.vaadin.ui.TextField;
  33. @Theme("tests-valo-disabled-animations")
  34. public class GridEditorCustomField extends AbstractTestUIWithLog {
  35. private static final String LAST_NAME_IDENTIFIER = "lastName";
  36. private static final String FIRST_NAME_IDENTIFIER = "firstName";
  37. private static final String ADDRESS_CITY_IDENTIFIER = "address.city";
  38. @Override
  39. protected void setup(VaadinRequest request) {
  40. Grid<ComplexPerson> grid = createGrid();
  41. ListDataProvider<ComplexPerson> dataProvider = ComplexPerson
  42. .createDataProvider(100);
  43. grid.setDataProvider(dataProvider);
  44. Set<String> cities = new HashSet<>();
  45. dataProvider.fetch(new Query<>()).forEach(person -> {
  46. cities.add(person.getAddress().getCity());
  47. });
  48. CustomCitySelect cityEditor = new CustomCitySelect(
  49. cities.toArray(new String[cities.size()]));
  50. TextField firstNameField = new TextField();
  51. TextField lastNameField = new TextField();
  52. Binder<ComplexPerson> binder = new Binder<>();
  53. binder.bind(firstNameField, ComplexPerson::getFirstName,
  54. ComplexPerson::setFirstName);
  55. binder.bind(lastNameField, ComplexPerson::getLastName,
  56. ComplexPerson::setLastName);
  57. binder.bind(cityEditor, person -> person.getAddress().getCity(),
  58. (person, city) -> person.getAddress().setCity(city));
  59. grid.getEditor().setBinder(binder);
  60. grid.getColumn(ADDRESS_CITY_IDENTIFIER).setEditorComponent(cityEditor);
  61. grid.getColumn(FIRST_NAME_IDENTIFIER)
  62. .setEditorComponent(firstNameField);
  63. grid.getColumn(LAST_NAME_IDENTIFIER).setEditorComponent(lastNameField);
  64. addComponent(grid);
  65. }
  66. private Grid<ComplexPerson> createGrid() {
  67. Grid<ComplexPerson> grid = new Grid<>();
  68. grid.setWidth("800px");
  69. grid.addColumn(FIRST_NAME_IDENTIFIER, person -> person.getFirstName())
  70. .setCaption("First Name");
  71. grid.addColumn(LAST_NAME_IDENTIFIER, person -> person.getLastName())
  72. .setCaption("Last Name");
  73. grid.addColumn(ADDRESS_CITY_IDENTIFIER,
  74. person -> person.getAddress().getCity())
  75. .setCaption("City Name");
  76. grid.getEditor().setEnabled(true);
  77. return grid;
  78. }
  79. public static class CustomCitySelect extends CustomField<String> {
  80. private HorizontalLayout fieldLayout;
  81. private String[] values;
  82. private ComboBox<String> cityComboBox;
  83. private String cachedValue;
  84. public CustomCitySelect(String... values) {
  85. this.values = values;
  86. }
  87. @Override
  88. protected Component initContent() {
  89. fieldLayout = new HorizontalLayout();
  90. fieldLayout.setWidth("100%");
  91. cityComboBox = new ComboBox<>();
  92. cityComboBox.setItems(values);
  93. if (cachedValue != null) {
  94. cityComboBox.setValue(cachedValue);
  95. cachedValue = null;
  96. }
  97. fieldLayout.addComponent(cityComboBox);
  98. fieldLayout.setExpandRatio(cityComboBox, 1.0f);
  99. Button addCountryButton = new Button("New");
  100. fieldLayout.addComponent(addCountryButton);
  101. setFocusDelegate(cityComboBox);
  102. return fieldLayout;
  103. }
  104. @Override
  105. public String getValue() {
  106. if (cityComboBox == null) {
  107. return null;
  108. }
  109. return cityComboBox.getValue();
  110. }
  111. @Override
  112. protected void doSetValue(String value) {
  113. if (cityComboBox == null) {
  114. getContent();
  115. }
  116. cityComboBox.setValue(value);
  117. }
  118. }
  119. }