Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ColumnConnector.java 6.3KB

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