Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

NativeSelect.java 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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.SingleSelection;
  21. import com.vaadin.server.communication.data.typed.TypedDataGenerator;
  22. import com.vaadin.ui.components.AbstractListing;
  23. import elemental.json.JsonObject;
  24. public class NativeSelect<T> extends AbstractListing<T> {
  25. private DataSource<T> dataSource;
  26. private Function<T, String> nameProvider = T::toString;
  27. public NativeSelect() {
  28. setSelectionModel(new SingleSelection<>());
  29. addDataGenerator(new TypedDataGenerator<T>() {
  30. @Override
  31. public void generateData(T data, JsonObject jsonObject) {
  32. jsonObject.put("n", nameProvider.apply(data));
  33. }
  34. @Override
  35. public void destroyData(T data) {
  36. }
  37. });
  38. }
  39. public NativeSelect(DataSource<T> dataSource) {
  40. this();
  41. setDataSource(dataSource);
  42. }
  43. @Override
  44. public void setDataSource(DataSource<T> data) {
  45. dataSource = data;
  46. if (dataSource != null) {
  47. setDataProvider(DataProvider.create(dataSource, this));
  48. } else {
  49. setDataProvider(null);
  50. }
  51. }
  52. @Override
  53. public DataSource<T> getDataSource() {
  54. return dataSource;
  55. }
  56. }