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

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