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.

Grid.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.ArrayList;
  18. import java.util.Collections;
  19. import java.util.Comparator;
  20. import java.util.HashSet;
  21. import java.util.List;
  22. import java.util.Set;
  23. import java.util.function.Function;
  24. import com.vaadin.shared.data.sort.SortDirection;
  25. import com.vaadin.shared.tokka.ui.components.grid.GridServerRpc;
  26. import com.vaadin.tokka.server.communication.data.DataSource;
  27. import com.vaadin.tokka.server.communication.data.KeyMapper;
  28. import com.vaadin.tokka.server.communication.data.SingleSelection;
  29. import com.vaadin.tokka.server.communication.data.SortOrder;
  30. import com.vaadin.tokka.ui.components.AbstractListing;
  31. public class Grid<T> extends AbstractListing<T> {
  32. private KeyMapper<Column<T, ?>> columnKeys = new KeyMapper<>();
  33. private Set<Column<T, ?>> columnSet = new HashSet<>();
  34. private List<SortOrder<Column<T, ?>>> sortOrder = new ArrayList<>();
  35. public Grid() {
  36. setSelectionModel(new SingleSelection<T>());
  37. registerRpc(new GridServerRpc() {
  38. @Override
  39. public void setSortOrder(List<String> columnIds,
  40. List<SortDirection> directions) {
  41. assert columnIds.size() == directions.size() : "Column and sort direction counts don't match.";
  42. sortOrder.clear();
  43. if (columnIds.size() == 0) {
  44. // Somehow there's suddenly no sort order anymore..
  45. getDataCommunicator().setBackEndSorting(
  46. Collections.emptyList());
  47. getDataCommunicator().setInMemorySorting(null);
  48. return;
  49. }
  50. for (int i = 0; i < columnIds.size(); ++i) {
  51. Column<T, ?> column = columnKeys.get(columnIds.get(i));
  52. sortOrder.add(new SortOrder<>(column, directions.get(i)));
  53. }
  54. updateSortOrder();
  55. }
  56. });
  57. }
  58. public <V> Column<T, V> addColumn(String caption, Function<T, V> getter) {
  59. Column<T, V> c = new Column<T, V>(caption, getter);
  60. c.extend(this);
  61. c.setCommunicationId(columnKeys.key(c));
  62. columnSet.add(c);
  63. addDataGenerator(c);
  64. return c;
  65. }
  66. public void removeColumn(Column<T, ?> column) {
  67. if (columnSet.remove(column)) {
  68. columnKeys.remove(column);
  69. removeDataGenerator(column);
  70. column.remove();
  71. }
  72. }
  73. @Override
  74. public void setDataSource(DataSource<T, ?> data) {
  75. super.setDataSource(data);
  76. updateSortOrder();
  77. }
  78. protected void updateSortOrder() {
  79. if (getDataCommunicator().getDataSource() == null) {
  80. return;
  81. }
  82. if (getDataCommunicator().getDataSource().isInMemory()) {
  83. Comparator<T> comparator = sortOrder
  84. .stream()
  85. .map(order -> order.getSorted().getComparator(
  86. order.getDirection()))
  87. .reduce((x, y) -> 0, Comparator::thenComparing);
  88. getDataCommunicator().setInMemorySorting(comparator);
  89. } else {
  90. List<SortOrder<String>> sortProperties = new ArrayList<>();
  91. sortOrder
  92. .stream()
  93. .map(order -> order.getSorted().getSortOrder(
  94. order.getDirection()))
  95. .forEach(sortProperties::addAll);
  96. getDataCommunicator().setBackEndSorting(sortProperties);
  97. }
  98. }
  99. }