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 13KB

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