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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.v7.tests.components.grid;
  17. import java.util.HashSet;
  18. import java.util.Set;
  19. import com.vaadin.server.VaadinRequest;
  20. import com.vaadin.tests.components.AbstractTestUIWithLog;
  21. import com.vaadin.tests.fieldgroup.ComplexPerson;
  22. import com.vaadin.ui.Button;
  23. import com.vaadin.ui.Component;
  24. import com.vaadin.ui.HorizontalLayout;
  25. import com.vaadin.v7.ui.ComboBox;
  26. import com.vaadin.v7.ui.CustomField;
  27. import com.vaadin.v7.ui.Grid;
  28. public class GridEditorCustomField extends AbstractTestUIWithLog {
  29. @Override
  30. protected void setup(VaadinRequest request) {
  31. Grid grid = new PersonTestGrid(100);
  32. grid.setWidth("800px");
  33. grid.setColumns("firstName", "lastName", "address.city");
  34. grid.setEditorEnabled(true);
  35. Set<String> cities = new HashSet<>();
  36. for (Object o : grid.getContainerDataSource().getItemIds()) {
  37. ComplexPerson p = (ComplexPerson) o;
  38. cities.add(p.getAddress().getCity());
  39. }
  40. CustomCitySelect cityEditor = new CustomCitySelect(
  41. cities.toArray(new String[cities.size()]));
  42. grid.getColumn("address.city").setEditorField(cityEditor);
  43. addComponent(grid);
  44. }
  45. public static class CustomCitySelect extends CustomField<String> {
  46. private HorizontalLayout fieldLayout;
  47. private String[] values;
  48. private ComboBox cityComboBox;
  49. public CustomCitySelect(String... values) {
  50. this.values = values;
  51. }
  52. @Override
  53. protected Component initContent() {
  54. fieldLayout = new HorizontalLayout();
  55. fieldLayout.setWidth("100%");
  56. cityComboBox = new ComboBox();
  57. for (String value : values) {
  58. cityComboBox.addItem(value);
  59. }
  60. fieldLayout.addComponent(cityComboBox);
  61. fieldLayout.setExpandRatio(cityComboBox, 1.0f);
  62. Button addCountryButton = new Button("New");
  63. fieldLayout.addComponent(addCountryButton);
  64. return fieldLayout;
  65. }
  66. @Override
  67. public Class<String> getType() {
  68. return String.class;
  69. }
  70. @Override
  71. protected void setInternalValue(String newValue) {
  72. super.setInternalValue(newValue);
  73. if (cityComboBox == null) {
  74. return;
  75. }
  76. cityComboBox.setValue(newValue);
  77. }
  78. @Override
  79. public String getInternalValue() {
  80. if (cityComboBox == null) {
  81. return null;
  82. }
  83. return (String) cityComboBox.getValue();
  84. }
  85. }
  86. }