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.

LegacyUidlWriter.java 3.7KB

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