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.

Column.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.tokka.ui.components.grid;
  17. import java.util.Arrays;
  18. import java.util.Comparator;
  19. import java.util.List;
  20. import java.util.function.Function;
  21. import java.util.stream.Collectors;
  22. import java.util.stream.Stream;
  23. import com.vaadin.server.AbstractExtension;
  24. import com.vaadin.shared.data.sort.SortDirection;
  25. import com.vaadin.shared.tokka.data.DataProviderConstants;
  26. import com.vaadin.shared.tokka.ui.components.grid.ColumnState;
  27. import com.vaadin.tokka.server.communication.data.SortOrder;
  28. import com.vaadin.tokka.server.communication.data.TypedDataGenerator;
  29. import elemental.json.Json;
  30. import elemental.json.JsonObject;
  31. public class Column<T, V> extends AbstractExtension implements
  32. TypedDataGenerator<T> {
  33. private Function<T, V> getter;
  34. private Function<SortDirection, Stream<SortOrder<String>>> sortOrderProvider;
  35. Column(String caption, Function<T, V> getter) {
  36. this.getter = getter;
  37. getState().caption = caption;
  38. getState().sortable = true;
  39. }
  40. @Override
  41. public void generateData(T data, JsonObject jsonObject) {
  42. assert getState(false).communicationId != null : "No communication ID set for column "
  43. + getState(false).caption;
  44. if (!jsonObject.hasKey(DataProviderConstants.DATA)) {
  45. jsonObject.put(DataProviderConstants.DATA, Json.createObject());
  46. }
  47. JsonObject obj = jsonObject.getObject(DataProviderConstants.DATA);
  48. // TODO: Renderers
  49. obj.put(getState(false).communicationId, getter.apply(data).toString());
  50. }
  51. @Override
  52. public void destroyData(T data) {
  53. }
  54. public ColumnState getState() {
  55. return getState(true);
  56. }
  57. public ColumnState getState(boolean markAsDirty) {
  58. return (ColumnState) super.getState(markAsDirty);
  59. }
  60. void extend(Grid<T> grid) {
  61. super.extend(grid);
  62. }
  63. void setCommunicationId(String key) {
  64. getState().communicationId = key;
  65. }
  66. /**
  67. * Sets whether the user can sort this Column or not.
  68. *
  69. * @param sortable
  70. * is column sortable
  71. * @return the column
  72. */
  73. public Column<T, V> setSortable(boolean sortable) {
  74. getState().sortable = sortable;
  75. return this;
  76. }
  77. /**
  78. * Returns the state of sorting for this Column.
  79. *
  80. * @return {@code true} if column can be sorted by user; {@code false} if
  81. * not
  82. */
  83. public boolean isSortable() {
  84. return getState(false).sortable;
  85. }
  86. public String getCaption() {
  87. return getState(false).caption;
  88. }
  89. Comparator<T> getComparator(SortDirection sortDirection) {
  90. Comparator<T> c = new Comparator<T>() {
  91. @SuppressWarnings("unchecked")
  92. @Override
  93. public int compare(T o1, T o2) {
  94. Comparable<V> comp = (Comparable<V>) getter.apply(o1);
  95. return comp.compareTo(getter.apply(o2));
  96. }
  97. };
  98. return sortDirection == SortDirection.ASCENDING ? c : c.reversed();
  99. }
  100. public Column<T, V> setSortProperty(String... properties) {
  101. sortOrderProvider = dir -> Arrays.asList(properties).stream()
  102. .map(s -> new SortOrder<>(s, dir));
  103. return this;
  104. }
  105. public Column<T, V> setSortBuilder(
  106. Function<SortDirection, Stream<SortOrder<String>>> provider) {
  107. sortOrderProvider = provider;
  108. return this;
  109. }
  110. public List<SortOrder<String>> getSortOrder(SortDirection direction) {
  111. return sortOrderProvider.apply(direction).collect(Collectors.toList());
  112. }
  113. }