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.

VUIDLBrowser.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright 2000-2018 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;
  17. import java.util.Set;
  18. import com.google.gwt.core.client.JsArray;
  19. import com.google.gwt.core.client.JsArrayString;
  20. import com.google.gwt.core.client.Scheduler;
  21. import com.google.gwt.dom.client.Document;
  22. import com.google.gwt.dom.client.Element;
  23. import com.google.gwt.dom.client.Style;
  24. import com.google.gwt.dom.client.Style.Position;
  25. import com.google.gwt.dom.client.Style.Unit;
  26. import com.google.gwt.event.dom.client.ClickEvent;
  27. import com.google.gwt.event.dom.client.MouseOutEvent;
  28. import com.google.gwt.user.client.ui.RootPanel;
  29. import com.google.gwt.user.client.ui.Widget;
  30. import com.vaadin.client.ui.UnknownComponentConnector;
  31. import com.vaadin.client.ui.UnknownExtensionConnector;
  32. import com.vaadin.client.ui.VWindow;
  33. import elemental.json.JsonArray;
  34. import elemental.json.JsonObject;
  35. import elemental.json.JsonType;
  36. import elemental.json.JsonValue;
  37. /**
  38. * @author Vaadin Ltd
  39. *
  40. * @deprecated as of 7.1. This class was mainly used by the old debug console
  41. * but is retained for now for backwards compatibility.
  42. */
  43. @Deprecated
  44. public class VUIDLBrowser extends SimpleTree {
  45. private static final String HELP = "Alt-click handle to open recursively. ";
  46. private ApplicationConnection client;
  47. private String highlightedPid;
  48. public VUIDLBrowser(final UIDL uidl, ApplicationConnection client) {
  49. this.client = client;
  50. final UIDLItem root = new UIDLItem(uidl);
  51. add(root);
  52. }
  53. public VUIDLBrowser(ValueMap u, ApplicationConnection client) {
  54. this.client = client;
  55. ValueMap valueMap = u.getValueMap("meta");
  56. if (valueMap.containsKey("hl")) {
  57. highlightedPid = valueMap.getString("hl");
  58. }
  59. Set<String> keySet = u.getKeySet();
  60. for (String key : keySet) {
  61. if (key.equals("state")) {
  62. ValueMap stateJson = u.getValueMap(key);
  63. SimpleTree stateChanges = new SimpleTree("shared state");
  64. for (String connectorId : stateJson.getKeySet()) {
  65. stateChanges.add(new SharedStateItem(connectorId,
  66. stateJson.getValueMap(connectorId)));
  67. }
  68. add(stateChanges);
  69. } else if (key.equals("changes")) {
  70. JsArray<UIDL> jsValueMapArray = u.getJSValueMapArray(key)
  71. .cast();
  72. for (int i = 0; i < jsValueMapArray.length(); i++) {
  73. UIDL uidl = jsValueMapArray.get(i);
  74. UIDLItem change = new UIDLItem(uidl);
  75. change.setTitle("change " + i);
  76. add(change);
  77. }
  78. } else if (key.equals("meta")) {
  79. } else {
  80. // TODO consider pretty printing other request data such as
  81. // hierarchy changes
  82. // addItem(key + " : " + u.getAsString(key));
  83. }
  84. }
  85. open(highlightedPid != null);
  86. setTitle(HELP);
  87. }
  88. /**
  89. * A debug view of a server-originated component state change.
  90. */
  91. abstract class StateChangeItem extends SimpleTree {
  92. protected StateChangeItem() {
  93. setTitle(HELP);
  94. addDomHandler(event -> deHiglight(), MouseOutEvent.getType());
  95. }
  96. @Override
  97. protected void select(ClickEvent event) {
  98. ServerConnector connector = getConnector();
  99. if (connector != null && event != null) {
  100. connector.getConnection().highlightConnector(connector);
  101. }
  102. // For connectors that do not have a widget, highlight the widget of
  103. // their ancestor component connector if any
  104. while (connector != null
  105. && !(connector instanceof ComponentConnector)) {
  106. connector = connector.getParent();
  107. }
  108. if (connector != null) {
  109. ComponentConnector cc = (ComponentConnector) connector;
  110. highlight(cc);
  111. }
  112. super.select(event);
  113. }
  114. /**
  115. * Returns the Connector associated with this state change.
  116. */
  117. protected ServerConnector getConnector() {
  118. return client.getConnectorMap().getConnector(getConnectorId());
  119. }
  120. protected abstract String getConnectorId();
  121. }
  122. /**
  123. * A debug view of a Vaadin 7 style shared state change.
  124. */
  125. class SharedStateItem extends StateChangeItem {
  126. private String connectorId;
  127. SharedStateItem(String connectorId, ValueMap stateChanges) {
  128. this.connectorId = connectorId;
  129. ServerConnector connector = getConnector();
  130. if (connector != null) {
  131. setText(Util.getConnectorString(connector));
  132. } else {
  133. setText("Unknown connector (" + connectorId + ")");
  134. }
  135. dir((JsonObject) Util.jso2json(stateChanges), this);
  136. }
  137. @Override
  138. protected String getConnectorId() {
  139. return connectorId;
  140. }
  141. private void dir(String key, JsonValue value, SimpleTree tree) {
  142. if (value.getType() == JsonType.OBJECT) {
  143. SimpleTree subtree = new SimpleTree(key + "=object");
  144. tree.add(subtree);
  145. dir((JsonObject) value, subtree);
  146. } else if (value.getType() == JsonType.ARRAY) {
  147. SimpleTree subtree = new SimpleTree(key + "=array");
  148. dir((JsonArray) value, subtree);
  149. tree.add(subtree);
  150. } else {
  151. tree.addItem(key + "=" + value);
  152. }
  153. }
  154. private void dir(JsonObject state, SimpleTree tree) {
  155. for (String key : state.keys()) {
  156. dir(key, state.get(key), tree);
  157. }
  158. }
  159. private void dir(JsonArray array, SimpleTree tree) {
  160. for (int i = 0; i < array.length(); ++i) {
  161. dir("" + i, array.get(i), tree);
  162. }
  163. }
  164. }
  165. /**
  166. * A debug view of a Vaadin 6 style hierarchical component state change.
  167. */
  168. class UIDLItem extends StateChangeItem {
  169. private UIDL uidl;
  170. UIDLItem(UIDL uidl) {
  171. this.uidl = uidl;
  172. try {
  173. String name = uidl.getTag();
  174. try {
  175. name = getNodeName(uidl, client.getConfiguration(),
  176. Integer.parseInt(name));
  177. } catch (Exception e) {
  178. // NOP
  179. }
  180. setText(name);
  181. addItem("LOADING");
  182. } catch (Exception e) {
  183. setText(uidl.toString());
  184. }
  185. }
  186. @Override
  187. protected String getConnectorId() {
  188. return uidl.getId();
  189. }
  190. private String getNodeName(UIDL uidl, ApplicationConfiguration conf,
  191. int tag) {
  192. Class<? extends ServerConnector> widgetClassByDecodedTag = conf
  193. .getConnectorClassByEncodedTag(tag);
  194. if (widgetClassByDecodedTag == UnknownComponentConnector.class
  195. || widgetClassByDecodedTag == UnknownExtensionConnector.class) {
  196. return conf.getUnknownServerClassNameByTag(tag)
  197. + "(NO CLIENT IMPLEMENTATION FOUND)";
  198. } else {
  199. return widgetClassByDecodedTag.getName();
  200. }
  201. }
  202. @Override
  203. public void open(boolean recursive) {
  204. if (getWidgetCount() == 1 && getWidget(0).getElement()
  205. .getInnerText().equals("LOADING")) {
  206. dir();
  207. }
  208. super.open(recursive);
  209. }
  210. public void dir() {
  211. remove(0);
  212. String nodeName = uidl.getTag();
  213. try {
  214. nodeName = getNodeName(uidl, client.getConfiguration(),
  215. Integer.parseInt(nodeName));
  216. } catch (Exception e) {
  217. // NOP
  218. }
  219. Set<String> attributeNames = uidl.getAttributeNames();
  220. for (String name : attributeNames) {
  221. if (uidl.isMapAttribute(name)) {
  222. try {
  223. ValueMap map = uidl.getMapAttribute(name);
  224. JsArrayString keyArray = map.getKeyArray();
  225. nodeName += " " + name + "=" + "{";
  226. for (int i = 0; i < keyArray.length(); i++) {
  227. nodeName += keyArray.get(i) + ":"
  228. + map.getAsString(keyArray.get(i)) + ",";
  229. }
  230. nodeName += "}";
  231. } catch (Exception e) {
  232. }
  233. } else {
  234. final String value = uidl.getAttribute(name);
  235. nodeName += " " + name + "=" + value;
  236. }
  237. }
  238. setText(nodeName);
  239. try {
  240. SimpleTree tmp = null;
  241. Set<String> variableNames = uidl.getVariableNames();
  242. for (String name : variableNames) {
  243. String value = "";
  244. try {
  245. value = uidl.getVariable(name);
  246. } catch (final Exception e) {
  247. try {
  248. String[] stringArrayAttribute = uidl
  249. .getStringArrayAttribute(name);
  250. value = stringArrayAttribute.toString();
  251. } catch (final Exception e2) {
  252. try {
  253. final int intVal = uidl.getIntVariable(name);
  254. value = String.valueOf(intVal);
  255. } catch (final Exception e3) {
  256. value = "unknown";
  257. }
  258. }
  259. }
  260. if (tmp == null) {
  261. tmp = new SimpleTree("variables");
  262. }
  263. tmp.addItem(name + "=" + value);
  264. }
  265. if (tmp != null) {
  266. add(tmp);
  267. }
  268. } catch (final Exception e) {
  269. // Ignored, no variables
  270. }
  271. for (final Object child : uidl) {
  272. try {
  273. add(new UIDLItem((UIDL) child));
  274. } catch (final Exception e) {
  275. addItem(child.toString());
  276. }
  277. }
  278. if (highlightedPid != null && highlightedPid.equals(uidl.getId())) {
  279. getElement().getStyle().setBackgroundColor("#fdd");
  280. Scheduler.get()
  281. .scheduleDeferred(() -> getElement().scrollIntoView());
  282. }
  283. }
  284. }
  285. static Element highlight = Document.get().createDivElement();
  286. static {
  287. Style style = highlight.getStyle();
  288. style.setPosition(Position.ABSOLUTE);
  289. style.setZIndex(VWindow.Z_INDEX + 1000);
  290. style.setBackgroundColor("red");
  291. style.setOpacity(0.2);
  292. if (BrowserInfo.get().isIE()) {
  293. style.setProperty("filter", "alpha(opacity=20)");
  294. }
  295. }
  296. static void highlight(ComponentConnector paintable) {
  297. if (paintable != null) {
  298. Widget w = paintable.getWidget();
  299. Style style = highlight.getStyle();
  300. style.setTop(w.getAbsoluteTop(), Unit.PX);
  301. style.setLeft(w.getAbsoluteLeft(), Unit.PX);
  302. style.setWidth(w.getOffsetWidth(), Unit.PX);
  303. style.setHeight(w.getOffsetHeight(), Unit.PX);
  304. RootPanel.getBodyElement().appendChild(highlight);
  305. }
  306. }
  307. static void deHiglight() {
  308. if (highlight.getParentElement() != null) {
  309. highlight.getParentElement().removeChild(highlight);
  310. }
  311. }
  312. }