Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ConnectorHierarchyWriter.java 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.server.communication;
  17. import java.io.IOException;
  18. import java.io.Serializable;
  19. import java.io.Writer;
  20. import java.util.Collection;
  21. import java.util.Set;
  22. import com.vaadin.server.AbstractClientConnector;
  23. import com.vaadin.server.ClientConnector;
  24. import com.vaadin.server.LegacyCommunicationManager;
  25. import com.vaadin.server.PaintException;
  26. import com.vaadin.ui.UI;
  27. import elemental.json.Json;
  28. import elemental.json.JsonArray;
  29. import elemental.json.JsonException;
  30. import elemental.json.JsonObject;
  31. import elemental.json.impl.JsonUtil;
  32. /**
  33. * Serializes a connector hierarchy to JSON.
  34. *
  35. * @author Vaadin Ltd
  36. * @since 7.1
  37. */
  38. public class ConnectorHierarchyWriter implements Serializable {
  39. /**
  40. * Writes a JSON object containing the connector hierarchy (parent-child
  41. * mappings) of the dirty connectors in the given UI.
  42. *
  43. * @param ui
  44. * The {@link UI} whose hierarchy to write.
  45. * @param writer
  46. * The {@link Writer} used to write the JSON.
  47. * @param stateUpdateConnectors
  48. * connector ids with state changes
  49. * @throws IOException
  50. * If the serialization fails.
  51. */
  52. public void write(UI ui, Writer writer, Set<String> stateUpdateConnectors)
  53. throws IOException {
  54. Collection<ClientConnector> dirtyVisibleConnectors = ui
  55. .getConnectorTracker().getDirtyVisibleConnectors();
  56. JsonObject hierarchyInfo = Json.createObject();
  57. for (ClientConnector connector : dirtyVisibleConnectors) {
  58. String connectorId = connector.getConnectorId();
  59. JsonArray children = Json.createArray();
  60. for (ClientConnector child : AbstractClientConnector
  61. .getAllChildrenIterable(connector)) {
  62. if (LegacyCommunicationManager
  63. .isConnectorVisibleToClient(child)) {
  64. children.set(children.length(), child.getConnectorId());
  65. }
  66. }
  67. // Omit for leaf nodes with state changes
  68. if (children.length() > 0
  69. || !stateUpdateConnectors.contains(connectorId)) {
  70. try {
  71. hierarchyInfo.put(connectorId, children);
  72. } catch (JsonException e) {
  73. throw new PaintException(
  74. "Failed to send hierarchy information about "
  75. + connectorId + " to the client: "
  76. + e.getMessage(), e);
  77. }
  78. }
  79. }
  80. writer.write(JsonUtil.stringify(hierarchyInfo));
  81. }
  82. }