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.

ColumnConnector.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2000-2016 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.connectors.grid;
  17. import com.vaadin.client.ServerConnector;
  18. import com.vaadin.client.annotations.OnStateChange;
  19. import com.vaadin.client.connectors.AbstractRendererConnector;
  20. import com.vaadin.client.extensions.AbstractExtensionConnector;
  21. import com.vaadin.client.widgets.Grid.Column;
  22. import com.vaadin.shared.data.DataCommunicatorConstants;
  23. import com.vaadin.shared.ui.Connect;
  24. import com.vaadin.shared.ui.grid.ColumnState;
  25. import elemental.json.JsonObject;
  26. import elemental.json.JsonValue;
  27. /**
  28. * A connector class for columns of the Grid component.
  29. *
  30. * @author Vaadin Ltd
  31. * @since 8.0
  32. */
  33. @Connect(com.vaadin.ui.Grid.Column.class)
  34. public class ColumnConnector extends AbstractExtensionConnector {
  35. private Column<Object, JsonObject> column;
  36. /* This parent is needed because it's no longer available in onUnregister */
  37. private GridConnector parent;
  38. @Override
  39. protected void extend(ServerConnector target) {
  40. parent = getParent();
  41. String columnId = getState().id;
  42. column = new Column<Object, JsonObject>() {
  43. @Override
  44. public Object getValue(JsonObject row) {
  45. final JsonObject rowData = row
  46. .getObject(DataCommunicatorConstants.DATA);
  47. if (rowData.hasKey(columnId)) {
  48. final JsonValue columnValue = rowData.get(columnId);
  49. return getRendererConnector().decode(columnValue);
  50. }
  51. return null;
  52. }
  53. };
  54. column.setRenderer(getRendererConnector().getRenderer());
  55. getParent().addColumn(column, columnId);
  56. }
  57. @SuppressWarnings("unchecked")
  58. private AbstractRendererConnector<Object> getRendererConnector() {
  59. return (AbstractRendererConnector<Object>) getState().renderer;
  60. }
  61. @OnStateChange("caption")
  62. void updateCaption() {
  63. column.setHeaderCaption(getState().caption);
  64. }
  65. @OnStateChange("sortable")
  66. void updateSortable() {
  67. column.setSortable(getState().sortable);
  68. }
  69. @Override
  70. public void onUnregister() {
  71. super.onUnregister();
  72. parent.removeColumn(column);
  73. column = null;
  74. }
  75. @Override
  76. public GridConnector getParent() {
  77. return (GridConnector) super.getParent();
  78. }
  79. @Override
  80. public ColumnState getState() {
  81. return (ColumnState) super.getState();
  82. }
  83. }