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.

NativeSelect.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2000-2014 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.ui.components.nativeselect;
  17. import java.util.function.Function;
  18. import com.vaadin.server.communication.data.typed.DataProvider;
  19. import com.vaadin.server.communication.data.typed.DataSource;
  20. import com.vaadin.server.communication.data.typed.SelectionModel;
  21. import com.vaadin.server.communication.data.typed.SingleSelection;
  22. import com.vaadin.server.communication.data.typed.TypedDataGenerator;
  23. import com.vaadin.ui.AbstractComponent;
  24. import com.vaadin.ui.components.Listing;
  25. import elemental.json.JsonObject;
  26. public class NativeSelect<T> extends AbstractComponent implements Listing<T> {
  27. private DataSource<T> dataSource;
  28. private DataProvider<T> dataProvider;
  29. private SelectionModel<T> selectionModel;
  30. private Function<T, String> nameProvider = T::toString;
  31. public NativeSelect() {
  32. internalSetSelectionModel(new SingleSelection<>());
  33. }
  34. public NativeSelect(DataSource<T> dataSource) {
  35. this();
  36. internalSetDataSource(dataSource);
  37. }
  38. @Override
  39. public void setDataSource(DataSource<T> data) {
  40. internalSetDataSource(data);
  41. }
  42. private void internalSetDataSource(DataSource<T> data) {
  43. if (dataProvider != null) {
  44. dataProvider.remove();
  45. dataProvider = null;
  46. }
  47. dataSource = data;
  48. if (dataSource != null) {
  49. dataProvider = DataProvider.create(dataSource, this);
  50. dataProvider.addDataGenerator(new TypedDataGenerator<T>() {
  51. @Override
  52. public void generateData(T data, JsonObject jsonObject) {
  53. jsonObject.put("n", nameProvider.apply(data));
  54. }
  55. @Override
  56. public void destroyData(T data) {
  57. }
  58. });
  59. }
  60. }
  61. @Override
  62. public DataSource<T> getDataSource() {
  63. return dataSource;
  64. }
  65. @Override
  66. public SelectionModel<T> getSelectionModel() {
  67. return selectionModel;
  68. }
  69. @Override
  70. public void setSelectionModel(SelectionModel<T> model) {
  71. internalSetSelectionModel(model);
  72. }
  73. private void internalSetSelectionModel(SelectionModel<T> model) {
  74. if (selectionModel != null) {
  75. selectionModel.remove();
  76. }
  77. selectionModel = model;
  78. if (model != null) {
  79. model.setParentListing(this);
  80. }
  81. }
  82. }