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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright 2000-2014 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.json.client.JSONArray;
  35. import com.google.gwt.json.client.JSONObject;
  36. import com.google.gwt.json.client.JSONValue;
  37. import com.google.gwt.user.client.ui.RootPanel;
  38. import com.google.gwt.user.client.ui.Widget;
  39. import com.vaadin.client.ui.UnknownComponentConnector;
  40. import com.vaadin.client.ui.VWindow;
  41. /**
  42. * @author Vaadin Ltd
  43. *
  44. * @deprecated as of 7.1. This class was mainly used by the old debug console
  45. * but is retained for now for backwards compatibility.
  46. */
  47. @Deprecated
  48. public class VUIDLBrowser extends SimpleTree {
  49. private static final String HELP = "Alt-click handle to open recursively. ";
  50. private ApplicationConnection client;
  51. private String highlightedPid;
  52. public VUIDLBrowser(final UIDL uidl, ApplicationConnection client) {
  53. this.client = client;
  54. final UIDLItem root = new UIDLItem(uidl);
  55. add(root);
  56. }
  57. public VUIDLBrowser(ValueMap u, ApplicationConnection client) {
  58. this.client = client;
  59. ValueMap valueMap = u.getValueMap("meta");
  60. if (valueMap.containsKey("hl")) {
  61. highlightedPid = valueMap.getString("hl");
  62. }
  63. Set<String> keySet = u.getKeySet();
  64. for (String key : keySet) {
  65. if (key.equals("state")) {
  66. ValueMap stateJson = u.getValueMap(key);
  67. SimpleTree stateChanges = new SimpleTree("shared state");
  68. for (String connectorId : stateJson.getKeySet()) {
  69. stateChanges.add(new SharedStateItem(connectorId, stateJson
  70. .getValueMap(connectorId)));
  71. }
  72. add(stateChanges);
  73. } else if (key.equals("changes")) {
  74. JsArray<UIDL> jsValueMapArray = u.getJSValueMapArray(key)
  75. .cast();
  76. for (int i = 0; i < jsValueMapArray.length(); i++) {
  77. UIDL uidl = jsValueMapArray.get(i);
  78. UIDLItem change = new UIDLItem(uidl);
  79. change.setTitle("change " + i);
  80. add(change);
  81. }
  82. } else if (key.equals("meta")) {
  83. } else {
  84. // TODO consider pretty printing other request data such as
  85. // hierarchy changes
  86. // addItem(key + " : " + u.getAsString(key));
  87. }
  88. }
  89. open(highlightedPid != null);
  90. setTitle(HELP);
  91. }
  92. /**
  93. * A debug view of a server-originated component state change.
  94. */
  95. abstract class StateChangeItem extends SimpleTree {
  96. protected StateChangeItem() {
  97. setTitle(HELP);
  98. addDomHandler(new MouseOutHandler() {
  99. @Override
  100. public void onMouseOut(MouseOutEvent event) {
  101. deHiglight();
  102. }
  103. }, MouseOutEvent.getType());
  104. }
  105. @Override
  106. protected void select(ClickEvent event) {
  107. ServerConnector connector = getConnector();
  108. if (connector != null && event != null) {
  109. connector.getConnection().highlightConnector(connector);
  110. }
  111. // For connectors that do not have a widget, highlight the widget of
  112. // their ancestor component connector if any
  113. while (connector != null
  114. && !(connector instanceof ComponentConnector)) {
  115. connector = connector.getParent();
  116. }
  117. if (connector != null) {
  118. ComponentConnector cc = (ComponentConnector) connector;
  119. highlight(cc);
  120. }
  121. super.select(event);
  122. }
  123. /**
  124. * Returns the Connector associated with this state change.
  125. */
  126. protected ServerConnector getConnector() {
  127. return client.getConnectorMap().getConnector(getConnectorId());
  128. }
  129. protected abstract String getConnectorId();
  130. }
  131. /**
  132. * A debug view of a Vaadin 7 style shared state change.
  133. */
  134. class SharedStateItem extends StateChangeItem {
  135. private String connectorId;
  136. SharedStateItem(String connectorId, ValueMap stateChanges) {
  137. this.connectorId = connectorId;
  138. ServerConnector connector = getConnector();
  139. if (connector != null) {
  140. setText(Util.getConnectorString(connector));
  141. } else {
  142. setText("Unknown connector (" + connectorId + ")");
  143. }
  144. dir(new JSONObject(stateChanges), this);
  145. }
  146. @Override
  147. protected String getConnectorId() {
  148. return connectorId;
  149. }
  150. private void dir(String key, JSONValue value, SimpleTree tree) {
  151. if (value.isObject() != null) {
  152. SimpleTree subtree = new SimpleTree(key + "=object");
  153. tree.add(subtree);
  154. dir(value.isObject(), subtree);
  155. } else if (value.isArray() != null) {
  156. SimpleTree subtree = new SimpleTree(key + "=array");
  157. dir(value.isArray(), subtree);
  158. tree.add(subtree);
  159. } else {
  160. tree.addItem(key + "=" + value);
  161. }
  162. }
  163. private void dir(JSONObject state, SimpleTree tree) {
  164. for (String key : state.keySet()) {
  165. dir(key, state.get(key), tree);
  166. }
  167. }
  168. private void dir(JSONArray array, SimpleTree tree) {
  169. for (int i = 0; i < array.size(); ++i) {
  170. dir("" + i, array.get(i), tree);
  171. }
  172. }
  173. }
  174. /**
  175. * A debug view of a Vaadin 6 style hierarchical component state change.
  176. */
  177. class UIDLItem extends StateChangeItem {
  178. private UIDL uidl;
  179. UIDLItem(UIDL uidl) {
  180. this.uidl = uidl;
  181. try {
  182. String name = uidl.getTag();
  183. try {
  184. name = getNodeName(uidl, client.getConfiguration(),
  185. Integer.parseInt(name));
  186. } catch (Exception e) {
  187. // NOP
  188. }
  189. setText(name);
  190. addItem("LOADING");
  191. } catch (Exception e) {
  192. setText(uidl.toString());
  193. }
  194. }
  195. @Override
  196. protected String getConnectorId() {
  197. return uidl.getId();
  198. }
  199. private String getNodeName(UIDL uidl, ApplicationConfiguration conf,
  200. int tag) {
  201. Class<? extends ServerConnector> widgetClassByDecodedTag = conf
  202. .getConnectorClassByEncodedTag(tag);
  203. if (widgetClassByDecodedTag == UnknownComponentConnector.class) {
  204. return conf.getUnknownServerClassNameByTag(tag)
  205. + "(NO CLIENT IMPLEMENTATION FOUND)";
  206. } else {
  207. return widgetClassByDecodedTag.getName();
  208. }
  209. }
  210. @Override
  211. public void open(boolean recursive) {
  212. if (getWidgetCount() == 1
  213. && getWidget(0).getElement().getInnerText()
  214. .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. }