Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

HierarchyPanel.java 6.0KB

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