Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ColumnConnector.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.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. getParent().addColumn(column, getState().internalId);
  94. }
  95. @SuppressWarnings("unchecked")
  96. private AbstractRendererConnector<Object> getRendererConnector() {
  97. return (AbstractRendererConnector<Object>) getState().renderer;
  98. }
  99. @OnStateChange("caption")
  100. void updateCaption() {
  101. column.setHeaderCaption(getState().caption);
  102. }
  103. @OnStateChange("assistiveCaption")
  104. void updateAssistiveCaption() {
  105. column.setAssistiveCaption(getState().assistiveCaption);
  106. }
  107. @OnStateChange("sortable")
  108. void updateSortable() {
  109. column.setSortable(getState().sortable);
  110. }
  111. @OnStateChange("renderer")
  112. void updateRenderer() {
  113. column.setRenderer(getRendererConnector().getRenderer());
  114. getParent().onColumnRendererChanged(column);
  115. }
  116. @OnStateChange("hidingToggleCaption")
  117. void updateHidingToggleCaption() {
  118. column.setHidingToggleCaption(getState().hidingToggleCaption);
  119. }
  120. @OnStateChange("hidden")
  121. void updateHidden() {
  122. column.setHidden(getState().hidden);
  123. }
  124. @OnStateChange("hidable")
  125. void updateHidable() {
  126. column.setHidable(getState().hidable);
  127. }
  128. @OnStateChange("resizable")
  129. void updateResizable() {
  130. column.setResizable(getState().resizable);
  131. }
  132. @OnStateChange("width")
  133. void updateWidth() {
  134. column.setWidth(getState().width);
  135. }
  136. @OnStateChange("minWidth")
  137. void updateMinWidth() {
  138. column.setMinimumWidth(getState().minWidth);
  139. }
  140. @OnStateChange("minimumWidthFromContent")
  141. void updateMinimumWidthFromContent() {
  142. column.setMinimumWidthFromContent(getState().minimumWidthFromContent);
  143. }
  144. @OnStateChange("maxWidth")
  145. void updateMaxWidth() {
  146. column.setMaximumWidth(getState().maxWidth);
  147. }
  148. @OnStateChange("expandRatio")
  149. void updateExpandRatio() {
  150. column.setExpandRatio(getState().expandRatio);
  151. }
  152. @OnStateChange("editable")
  153. void updateEditable() {
  154. column.setEditable(getState().editable);
  155. }
  156. @OnStateChange("tooltipContentMode")
  157. void updateTooltipContentMode() {
  158. column.setTooltipContentMode(getState().tooltipContentMode);
  159. }
  160. @Override
  161. public void onUnregister() {
  162. super.onUnregister();
  163. if (parent.getParent() != null) {
  164. // If the grid itself was unregistered there is no point in spending
  165. // time to remove columns (and have problems with frozen columns)
  166. // before throwing everything away
  167. parent.removeColumn(column);
  168. parent = null;
  169. }
  170. column = null;
  171. }
  172. @Override
  173. public GridConnector getParent() {
  174. return (GridConnector) super.getParent();
  175. }
  176. @Override
  177. public ColumnState getState() {
  178. return (ColumnState) super.getState();
  179. }
  180. }