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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.client.widgets.Grid.HeaderCell;
  23. import com.vaadin.shared.data.DataCommunicatorConstants;
  24. import com.vaadin.shared.ui.Connect;
  25. import com.vaadin.shared.ui.grid.ColumnState;
  26. import elemental.json.JsonObject;
  27. import elemental.json.JsonValue;
  28. /**
  29. * A connector class for columns of the Grid component.
  30. *
  31. * @author Vaadin Ltd
  32. * @since 8.0
  33. */
  34. @Connect(com.vaadin.ui.Grid.Column.class)
  35. public class ColumnConnector extends AbstractExtensionConnector {
  36. public abstract static class CustomColumn
  37. extends Column<Object, JsonObject> {
  38. private final String connectorId;
  39. CustomColumn(String connectorId) {
  40. this.connectorId = connectorId;
  41. }
  42. public String getConnectorId() {
  43. return connectorId;
  44. }
  45. @Override
  46. protected void setDefaultHeaderContent(HeaderCell cell) {
  47. // NO-OP, Server takes care of header contents.
  48. }
  49. }
  50. private CustomColumn column;
  51. /* This parent is needed because it's no longer available in onUnregister */
  52. private GridConnector parent;
  53. @Override
  54. protected void extend(ServerConnector target) {
  55. parent = getParent();
  56. column = new CustomColumn(getConnectorId()) {
  57. @Override
  58. public Object getValue(JsonObject row) {
  59. final JsonObject rowData = row
  60. .getObject(DataCommunicatorConstants.DATA);
  61. if (rowData.hasKey(getConnectorId())) {
  62. final JsonValue columnValue = rowData.get(getConnectorId());
  63. return getRendererConnector().decode(columnValue);
  64. }
  65. return null;
  66. }
  67. };
  68. // Initially set a renderer
  69. updateRenderer();
  70. getParent().addColumn(column, getState().internalId);
  71. }
  72. @SuppressWarnings("unchecked")
  73. private AbstractRendererConnector<Object> getRendererConnector() {
  74. return (AbstractRendererConnector<Object>) getState().renderer;
  75. }
  76. @OnStateChange("caption")
  77. void updateCaption() {
  78. column.setHeaderCaption(getState().caption);
  79. }
  80. @OnStateChange("sortable")
  81. void updateSortable() {
  82. column.setSortable(getState().sortable);
  83. }
  84. @OnStateChange("renderer")
  85. void updateRenderer() {
  86. column.setRenderer(getRendererConnector().getRenderer());
  87. getParent().onColumnRendererChanged(column);
  88. }
  89. @OnStateChange("hidingToggleCaption")
  90. void updateHidingToggleCaption() {
  91. column.setHidingToggleCaption(getState().hidingToggleCaption);
  92. }
  93. @OnStateChange("hidden")
  94. void updateHidden() {
  95. column.setHidden(getState().hidden);
  96. }
  97. @OnStateChange("hidable")
  98. void updateHidable() {
  99. column.setHidable(getState().hidable);
  100. }
  101. @OnStateChange("resizable")
  102. void updateResizable() {
  103. column.setResizable(getState().resizable);
  104. }
  105. @OnStateChange("width")
  106. void updateWidth() {
  107. column.setWidth(getState().width);
  108. }
  109. @OnStateChange("minWidth")
  110. void updateMinWidth() {
  111. column.setMinimumWidth(getState().minWidth);
  112. }
  113. @OnStateChange("minimumWidthFromContent")
  114. void updateMinimumWidthFromContent() {
  115. column.setMinimumWidthFromContent(getState().minimumWidthFromContent);
  116. }
  117. @OnStateChange("maxWidth")
  118. void updateMaxWidth() {
  119. column.setMaximumWidth(getState().maxWidth);
  120. }
  121. @OnStateChange("expandRatio")
  122. void updateExpandRatio() {
  123. column.setExpandRatio(getState().expandRatio);
  124. }
  125. @OnStateChange("editable")
  126. void updateEditable() {
  127. column.setEditable(getState().editable);
  128. }
  129. @Override
  130. public void onUnregister() {
  131. super.onUnregister();
  132. if (parent.getParent() != null) {
  133. // If the grid itself was unregistered there is no point in spending
  134. // time to remove columns (and have problems with frozen columns)
  135. // before throwing everything away
  136. parent.removeColumn(column);
  137. parent = null;
  138. }
  139. column = null;
  140. }
  141. @Override
  142. public GridConnector getParent() {
  143. return (GridConnector) super.getParent();
  144. }
  145. @Override
  146. public ColumnState getState() {
  147. return (ColumnState) super.getState();
  148. }
  149. }