Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

VUIDLBrowser.java 12KB

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