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.

HierarchyPanel.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.debug.internal;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import com.google.gwt.event.dom.client.ClickEvent;
  20. import com.google.gwt.event.dom.client.HasDoubleClickHandlers;
  21. import com.google.gwt.user.client.ui.FlowPanel;
  22. import com.google.gwt.user.client.ui.HasWidgets;
  23. import com.google.gwt.user.client.ui.Label;
  24. import com.google.gwt.user.client.ui.SimplePanel;
  25. import com.google.gwt.user.client.ui.Widget;
  26. import com.vaadin.client.ApplicationConfiguration;
  27. import com.vaadin.client.ApplicationConnection;
  28. import com.vaadin.client.FastStringSet;
  29. import com.vaadin.client.ServerConnector;
  30. import com.vaadin.client.SimpleTree;
  31. import com.vaadin.client.Util;
  32. /**
  33. * Hierarchy view panel of the debug window. This class can be used in various
  34. * debug window sections to show the current connector hierarchy.
  35. *
  36. * @since 7.1.4
  37. */
  38. public class HierarchyPanel extends FlowPanel {
  39. // TODO separate click listeners for simple selection and doubleclick
  40. private List<SelectConnectorListener> listeners = new ArrayList<>();
  41. public void update() {
  42. // Try to keep track of currently open nodes and reopen them
  43. FastStringSet openNodes = FastStringSet.create();
  44. for (Widget widget : this) {
  45. collectOpenNodes(widget, openNodes);
  46. }
  47. clear();
  48. SimplePanel trees = new SimplePanel();
  49. for (ApplicationConnection application : ApplicationConfiguration
  50. .getRunningApplications()) {
  51. ServerConnector uiConnector = application.getUIConnector();
  52. Widget connectorTree = buildConnectorTree(uiConnector, openNodes);
  53. trees.add(connectorTree);
  54. }
  55. add(trees);
  56. }
  57. /**
  58. * Adds the captions of all open (non-leaf) nodes in the hierarchy tree
  59. * recursively.
  60. *
  61. * @param widget
  62. * the widget in which to search for open nodes (if SimpleTree)
  63. * @param openNodes
  64. * the set in which open nodes should be added
  65. */
  66. private void collectOpenNodes(Widget widget, FastStringSet openNodes) {
  67. if (widget instanceof SimpleTree) {
  68. SimpleTree tree = (SimpleTree) widget;
  69. if (tree.isOpen()) {
  70. openNodes.add(tree.getCaption());
  71. } else {
  72. // no need to look inside closed nodes
  73. return;
  74. }
  75. }
  76. if (widget instanceof HasWidgets) {
  77. for (Widget child : (HasWidgets) widget) {
  78. collectOpenNodes(child, openNodes);
  79. }
  80. }
  81. }
  82. private Widget buildConnectorTree(final ServerConnector connector,
  83. FastStringSet openNodes) {
  84. String connectorString = Util.getConnectorString(connector);
  85. List<ServerConnector> children = connector.getChildren();
  86. Widget widget;
  87. if (children == null || children.isEmpty()) {
  88. // Leaf node, just add a label
  89. Label label = new Label(connectorString);
  90. label.addClickHandler(event -> {
  91. Highlight.showOnly(connector);
  92. showServerDebugInfo(connector);
  93. });
  94. widget = label;
  95. } else {
  96. SimpleTree tree = new SimpleTree(connectorString) {
  97. @Override
  98. protected void select(ClickEvent event) {
  99. super.select(event);
  100. Highlight.showOnly(connector);
  101. showServerDebugInfo(connector);
  102. }
  103. };
  104. for (ServerConnector child : children) {
  105. tree.add(buildConnectorTree(child, openNodes));
  106. }
  107. if (openNodes.contains(connectorString)) {
  108. tree.open(false);
  109. }
  110. widget = tree;
  111. }
  112. if (widget instanceof HasDoubleClickHandlers) {
  113. HasDoubleClickHandlers has = (HasDoubleClickHandlers) widget;
  114. has.addDoubleClickHandler(event -> fireSelectEvent(connector));
  115. }
  116. return widget;
  117. }
  118. public void addListener(SelectConnectorListener listener) {
  119. listeners.add(listener);
  120. }
  121. public void removeListener(SelectConnectorListener listener) {
  122. listeners.remove(listener);
  123. }
  124. private void fireSelectEvent(ServerConnector connector) {
  125. for (SelectConnectorListener listener : listeners) {
  126. listener.select(connector, null);
  127. }
  128. }
  129. /**
  130. * Outputs debug information on the server - usually in the console of an
  131. * IDE, with a clickable reference to the relevant code location.
  132. *
  133. * @since 7.1
  134. * @param connector
  135. * show debug info for this connector
  136. */
  137. static void showServerDebugInfo(ServerConnector connector) {
  138. if (connector != null) {
  139. connector.getConnection().getUIConnector()
  140. .showServerDebugInfo(connector);
  141. }
  142. }
  143. }