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

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