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

LegacyUidlWriter.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.server.communication;
  17. import java.io.IOException;
  18. import java.io.Serializable;
  19. import java.io.Writer;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.Collections;
  23. import java.util.Comparator;
  24. import java.util.List;
  25. import java.util.logging.Logger;
  26. import com.vaadin.server.ClientConnector;
  27. import com.vaadin.server.LegacyPaint;
  28. import com.vaadin.server.PaintTarget;
  29. import com.vaadin.ui.Component;
  30. import com.vaadin.ui.LegacyComponent;
  31. import com.vaadin.ui.UI;
  32. /**
  33. * Serializes legacy UIDL changes to JSON.
  34. *
  35. * @author Vaadin Ltd
  36. * @since 7.1
  37. */
  38. public class LegacyUidlWriter implements Serializable {
  39. /**
  40. * Writes a JSON array containing the changes of all dirty
  41. * {@link LegacyComponent}s in the given UI.
  42. *
  43. * @param ui
  44. * The {@link UI} whose legacy changes to write
  45. * @param writer
  46. * The {@link Writer} to write the JSON with
  47. * @param target
  48. * The {@link PaintTarget} to use
  49. * @throws IOException
  50. * If the serialization fails.
  51. */
  52. public void write(UI ui, Writer writer, PaintTarget target)
  53. throws IOException {
  54. Collection<ClientConnector> dirtyVisibleConnectors = ui
  55. .getConnectorTracker().getDirtyVisibleConnectors();
  56. List<Component> legacyComponents = new ArrayList<>(
  57. dirtyVisibleConnectors.size());
  58. for (ClientConnector connector : dirtyVisibleConnectors) {
  59. // All Components that want to use paintContent must implement
  60. // LegacyComponent
  61. if (connector instanceof LegacyComponent) {
  62. legacyComponents.add((Component) connector);
  63. }
  64. }
  65. sortByHierarchy(legacyComponents);
  66. writer.write("[");
  67. for (Component c : legacyComponents) {
  68. getLogger()
  69. .fine("Painting LegacyComponent " + c.getClass().getName()
  70. + "@" + Integer.toHexString(c.hashCode()));
  71. target.startTag("change");
  72. final String pid = c.getConnectorId();
  73. target.addAttribute("pid", pid);
  74. LegacyPaint.paint(c, target);
  75. target.endTag("change");
  76. }
  77. writer.write("]");
  78. }
  79. private void sortByHierarchy(List<Component> paintables) {
  80. // Vaadin 6 requires parents to be painted before children as component
  81. // containers rely on that their updateFromUIDL method has been called
  82. // before children start calling e.g. updateCaption
  83. Collections.sort(paintables, (Component c1, Component c2) -> {
  84. int depth1 = 0;
  85. while (c1.getParent() != null) {
  86. depth1++;
  87. c1 = c1.getParent();
  88. }
  89. int depth2 = 0;
  90. while (c2.getParent() != null) {
  91. depth2++;
  92. c2 = c2.getParent();
  93. }
  94. if (depth1 < depth2) {
  95. return -1;
  96. }
  97. if (depth1 > depth2) {
  98. return 1;
  99. }
  100. return 0;
  101. });
  102. }
  103. private static final Logger getLogger() {
  104. return Logger.getLogger(LegacyUidlWriter.class.getName());
  105. }
  106. }