Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

GridConnector.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.client.tokka.connectors.components.grid;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. import com.vaadin.client.data.DataSource;
  23. import com.vaadin.client.renderers.Renderer;
  24. import com.vaadin.client.tokka.connectors.components.AbstractListingConnector;
  25. import com.vaadin.client.tokka.connectors.data.HasSelection;
  26. import com.vaadin.client.tokka.data.selection.SelectionModel;
  27. import com.vaadin.client.widget.grid.selection.ClickSelectHandler;
  28. import com.vaadin.client.widget.grid.sort.SortEvent;
  29. import com.vaadin.client.widget.grid.sort.SortHandler;
  30. import com.vaadin.client.widget.grid.sort.SortOrder;
  31. import com.vaadin.client.widgets.Grid;
  32. import com.vaadin.client.widgets.Grid.Column;
  33. import com.vaadin.shared.data.sort.SortDirection;
  34. import com.vaadin.shared.tokka.ui.components.grid.GridServerRpc;
  35. import com.vaadin.shared.ui.Connect;
  36. import elemental.json.JsonObject;
  37. @Connect(com.vaadin.tokka.ui.components.grid.Grid.class)
  38. public class GridConnector extends AbstractListingConnector implements
  39. HasSelection {
  40. /**
  41. * Class for providing selection event stuff from Grid to the new
  42. * client-side selection model. Only the absolutely necessary methods are
  43. * implemented.
  44. * <p>
  45. * TODO: This should be unified to have only one SelectionModel API.
  46. */
  47. private final class SelectionModelAdapter
  48. implements
  49. com.vaadin.client.widget.grid.selection.SelectionModel.Single<JsonObject> {
  50. private final SelectionModel model;
  51. private SelectionModelAdapter(SelectionModel selectionModel) {
  52. this.model = selectionModel;
  53. }
  54. @Override
  55. public boolean isSelected(JsonObject row) {
  56. return model != null && model.isSelected(row);
  57. }
  58. @Override
  59. public Renderer<Boolean> getSelectionColumnRenderer() {
  60. return null;
  61. }
  62. @Override
  63. public void setGrid(Grid<JsonObject> grid) {
  64. }
  65. @Override
  66. public void reset() {
  67. }
  68. @Override
  69. public Collection<JsonObject> getSelectedRows() {
  70. return null;
  71. }
  72. @Override
  73. public boolean select(JsonObject row) {
  74. if (model != null) {
  75. model.select(row);
  76. }
  77. return false;
  78. }
  79. @Override
  80. public boolean deselect(JsonObject row) {
  81. if (model != null) {
  82. model.deselect(row);
  83. }
  84. return false;
  85. }
  86. @Override
  87. public JsonObject getSelectedRow() {
  88. return null;
  89. }
  90. @Override
  91. public void setDeselectAllowed(boolean deselectAllowed) {
  92. }
  93. @Override
  94. public boolean isDeselectAllowed() {
  95. return true;
  96. }
  97. }
  98. /* Map to keep track of all added columns */
  99. private Map<Column<?, JsonObject>, String> columnToIdMap = new HashMap<>();
  100. @Override
  101. public Grid<JsonObject> getWidget() {
  102. return (Grid<JsonObject>) super.getWidget();
  103. }
  104. @Override
  105. protected void init() {
  106. super.init();
  107. new ClickSelectHandler<JsonObject>(getWidget());
  108. getWidget().addSortHandler(new SortHandler<JsonObject>() {
  109. @Override
  110. public void sort(SortEvent<JsonObject> event) {
  111. List<String> columnIds = new ArrayList<>();
  112. List<SortDirection> sortDirections = new ArrayList<>();
  113. for (SortOrder so : event.getOrder()) {
  114. if (columnToIdMap.containsKey(so.getColumn())) {
  115. columnIds.add(columnToIdMap.get(so.getColumn()));
  116. sortDirections.add(so.getDirection());
  117. }
  118. getRpcProxy(GridServerRpc.class).setSortOrder(columnIds,
  119. sortDirections);
  120. }
  121. }
  122. });
  123. }
  124. @Override
  125. public void setDataSource(DataSource<JsonObject> dataSource) {
  126. super.setDataSource(dataSource);
  127. getWidget().setDataSource(dataSource);
  128. }
  129. @Override
  130. public void setSelectionModel(final SelectionModel selectionModel) {
  131. super.setSelectionModel(selectionModel);
  132. getWidget()
  133. .setSelectionModel(new SelectionModelAdapter(selectionModel));
  134. }
  135. public void addColumn(Column<?, JsonObject> column, String id) {
  136. assert !columnToIdMap.containsKey(column)
  137. && !columnToIdMap.containsValue(id) : "Column with given id already exists.";
  138. getWidget().addColumn(column);
  139. columnToIdMap.put(column, id);
  140. }
  141. public void removeColumn(Column<?, JsonObject> column) {
  142. assert columnToIdMap.containsKey(column) : "Given Column does not exist.";
  143. getWidget().removeColumn(column);
  144. columnToIdMap.remove(column);
  145. }
  146. }