Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ICustomLayout.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal.gwt.client.ui;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.Iterator;
  8. import java.util.Set;
  9. import com.google.gwt.user.client.DOM;
  10. import com.google.gwt.user.client.Element;
  11. import com.google.gwt.user.client.ui.ComplexPanel;
  12. import com.google.gwt.user.client.ui.Widget;
  13. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  14. import com.itmill.toolkit.terminal.gwt.client.ICaption;
  15. import com.itmill.toolkit.terminal.gwt.client.ICaptionWrapper;
  16. import com.itmill.toolkit.terminal.gwt.client.Container;
  17. import com.itmill.toolkit.terminal.gwt.client.ContainerResizedListener;
  18. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  19. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  20. import com.itmill.toolkit.terminal.gwt.client.Util;
  21. /**
  22. * Custom Layout implements complex layout defined with HTML template.
  23. *
  24. * @author IT Mill
  25. *
  26. */
  27. public class ICustomLayout extends ComplexPanel implements Paintable,
  28. Container, ContainerResizedListener {
  29. public static final String CLASSNAME = "i-customlayout";
  30. /** Location-name to containing element in DOM map */
  31. private final HashMap locationToElement = new HashMap();
  32. /** Location-name to contained widget map */
  33. private final HashMap locationToWidget = new HashMap();
  34. /** Widget to captionwrapper map */
  35. private final HashMap widgetToCaptionWrapper = new HashMap();
  36. /** Name of the currently rendered style */
  37. String currentTemplateName;
  38. /** Unexecuted scripts loaded from the template */
  39. private String scripts = "";
  40. /** Paintable ID of this paintable */
  41. private String pid;
  42. private ApplicationConnection client;
  43. /** Has the template been loaded from contents passed in UIDL **/
  44. private boolean hasTemplateContents = false;
  45. public ICustomLayout() {
  46. setElement(DOM.createDiv());
  47. // Clear any unwanted styling
  48. DOM.setStyleAttribute(getElement(), "border", "none");
  49. DOM.setStyleAttribute(getElement(), "margin", "0");
  50. DOM.setStyleAttribute(getElement(), "padding", "0");
  51. setStyleName(CLASSNAME);
  52. }
  53. /**
  54. * Sets widget to given location.
  55. *
  56. * If location already contains a widget it will be removed.
  57. *
  58. * @param widget
  59. * Widget to be set into location.
  60. * @param location
  61. * location name where widget will be added
  62. *
  63. * @throws IllegalArgumentException
  64. * if no such location is found in the layout.
  65. */
  66. public void setWidget(Widget widget, String location) {
  67. if (widget == null) {
  68. return;
  69. }
  70. // If no given location is found in the layout, and exception is throws
  71. Element elem = (Element) locationToElement.get(location);
  72. if (elem == null && hasTemplate()) {
  73. throw new IllegalArgumentException("No location " + location
  74. + " found");
  75. }
  76. // Get previous widget
  77. final Widget previous = (Widget) locationToWidget.get(location);
  78. // NOP if given widget already exists in this location
  79. if (previous == widget) {
  80. return;
  81. }
  82. remove(previous);
  83. // if template is missing add element in order
  84. if (!hasTemplate()) {
  85. elem = getElement();
  86. }
  87. // Add widget to location
  88. super.add(widget, elem);
  89. locationToWidget.put(location, widget);
  90. }
  91. /** Update the layout from UIDL */
  92. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  93. this.client = client;
  94. // ApplicationConnection manages generic component features
  95. if (client.updateComponent(this, uidl, true)) {
  96. return;
  97. }
  98. pid = uidl.getId();
  99. if (!hasTemplate()) {
  100. // Update HTML template only once
  101. initializeHTML(uidl, client);
  102. }
  103. // Set size
  104. if (uidl.hasAttribute("width")) {
  105. setWidth(uidl.getStringAttribute("width"));
  106. } else {
  107. setWidth("100%");
  108. }
  109. if (uidl.hasAttribute("height")) {
  110. setHeight(uidl.getStringAttribute("height"));
  111. } else {
  112. setHeight("100%");
  113. }
  114. // Evaluate scripts
  115. eval(scripts);
  116. scripts = null;
  117. iLayout();
  118. Set oldWidgets = new HashSet();
  119. oldWidgets.addAll(locationToWidget.values());
  120. // For all contained widgets
  121. for (final Iterator i = uidl.getChildIterator(); i.hasNext();) {
  122. final UIDL uidlForChild = (UIDL) i.next();
  123. if (uidlForChild.getTag().equals("location")) {
  124. final String location = uidlForChild.getStringAttribute("name");
  125. final Paintable child = client.getPaintable(uidlForChild
  126. .getChildUIDL(0));
  127. try {
  128. setWidget((Widget) child, location);
  129. child.updateFromUIDL(uidlForChild.getChildUIDL(0), client);
  130. } catch (final IllegalArgumentException e) {
  131. // If no location is found, this component is not visible
  132. }
  133. oldWidgets.remove(child);
  134. }
  135. }
  136. for (Iterator iterator = oldWidgets.iterator(); iterator.hasNext();) {
  137. Widget oldWidget = (Widget) iterator.next();
  138. if (oldWidget.isAttached()) {
  139. // slot of this widget is emptied, remove it
  140. remove(oldWidget);
  141. }
  142. }
  143. iLayout();
  144. }
  145. /** Initialize HTML-layout. */
  146. private void initializeHTML(UIDL uidl, ApplicationConnection client) {
  147. final String newTemplateContents = uidl
  148. .getStringAttribute("templateContents");
  149. final String newTemplate = uidl.getStringAttribute("template");
  150. currentTemplateName = null;
  151. hasTemplateContents = false;
  152. String template = "";
  153. if (newTemplate != null) {
  154. // Get the HTML-template from client
  155. template = client.getResource("layouts/" + newTemplate + ".html");
  156. if (template == null) {
  157. template = "<em>Layout file layouts/"
  158. + newTemplate
  159. + ".html is missing. Components will be drawn for debug purposes.</em>";
  160. } else {
  161. currentTemplateName = newTemplate;
  162. }
  163. } else {
  164. hasTemplateContents = true;
  165. template = newTemplateContents;
  166. }
  167. // Connect body of the template to DOM
  168. template = extractBodyAndScriptsFromTemplate(template);
  169. DOM.setInnerHTML(getElement(), template);
  170. // Remap locations to elements
  171. locationToElement.clear();
  172. scanForLocations(getElement());
  173. String themeUri = client.getThemeUri();
  174. prefixImgSrcs(getElement(), themeUri + "/layouts/");
  175. publishResizedFunction(DOM.getFirstChild(getElement()));
  176. }
  177. private native boolean uriEndsWithSlash()
  178. /*-{
  179. var path = $wnd.location.pathname;
  180. if(path.charAt(path.length - 1) == "/")
  181. return true;
  182. return false;
  183. }-*/;
  184. private boolean hasTemplate() {
  185. if (currentTemplateName == null && !hasTemplateContents) {
  186. return false;
  187. } else {
  188. return true;
  189. }
  190. }
  191. /** Collect locations from template */
  192. private void scanForLocations(Element elem) {
  193. final String location = getLocation(elem);
  194. if (location != null) {
  195. locationToElement.put(location, elem);
  196. DOM.setInnerHTML(elem, "");
  197. } else {
  198. final int len = DOM.getChildCount(elem);
  199. for (int i = 0; i < len; i++) {
  200. scanForLocations(DOM.getChild(elem, i));
  201. }
  202. }
  203. }
  204. /** Get the location attribute for given element */
  205. private static native String getLocation(Element elem)
  206. /*-{
  207. return elem.getAttribute("location");
  208. }-*/;
  209. /** Evaluate given script in browser document */
  210. private static native void eval(String script)
  211. /*-{
  212. try {
  213. if (script != null)
  214. eval("{ var document = $doc; var window = $wnd; "+ script + "}");
  215. } catch (e) {
  216. }
  217. }-*/;
  218. /** Prefix all img tag srcs with given prefix. */
  219. private static native void prefixImgSrcs(Element e, String srcPrefix)
  220. /*-{
  221. try {
  222. var divs = e.getElementsByTagName("img");
  223. var base = "" + $doc.location;
  224. var l = base.length-1;
  225. while (l >= 0 && base.charAt(l) != "/") l--;
  226. base = base.substring(0,l+1);
  227. for (var i = 0; i < divs.length; i++) {
  228. var div = divs[i];
  229. var src = div.getAttribute("src");
  230. if (src.indexOf("/")==0 || src.match(/\w+:\/\//)) {
  231. continue;
  232. }
  233. div.setAttribute("src",srcPrefix + src);
  234. }
  235. } catch (e) { alert(e + " " + srcPrefix);}
  236. }-*/;
  237. /**
  238. * Extract body part and script tags from raw html-template.
  239. *
  240. * Saves contents of all script-tags to private property: scripts. Returns
  241. * contents of the body part for the html without script-tags. Also replaces
  242. * all _UID_ tags with an unique id-string.
  243. *
  244. * @param html
  245. * Original HTML-template received from server
  246. * @return html that is used to create the HTMLPanel.
  247. */
  248. private String extractBodyAndScriptsFromTemplate(String html) {
  249. // Replace UID:s
  250. html = html.replaceAll("_UID_", pid + "__");
  251. // Exctract script-tags
  252. scripts = "";
  253. int endOfPrevScript = 0;
  254. int nextPosToCheck = 0;
  255. String lc = html.toLowerCase();
  256. String res = "";
  257. int scriptStart = lc.indexOf("<script", nextPosToCheck);
  258. while (scriptStart > 0) {
  259. res += html.substring(endOfPrevScript, scriptStart);
  260. scriptStart = lc.indexOf(">", scriptStart);
  261. final int j = lc.indexOf("</script>", scriptStart);
  262. scripts += html.substring(scriptStart + 1, j) + ";";
  263. nextPosToCheck = endOfPrevScript = j + "</script>".length();
  264. scriptStart = lc.indexOf("<script", nextPosToCheck);
  265. }
  266. res += html.substring(endOfPrevScript);
  267. // Extract body
  268. html = res;
  269. lc = html.toLowerCase();
  270. int startOfBody = lc.indexOf("<body");
  271. if (startOfBody < 0) {
  272. res = html;
  273. } else {
  274. res = "";
  275. startOfBody = lc.indexOf(">", startOfBody) + 1;
  276. final int endOfBody = lc.indexOf("</body>", startOfBody);
  277. if (endOfBody > startOfBody) {
  278. res = html.substring(startOfBody, endOfBody);
  279. } else {
  280. res = html.substring(startOfBody);
  281. }
  282. }
  283. return res;
  284. }
  285. /** Replace child components */
  286. public void replaceChildComponent(Widget from, Widget to) {
  287. final String location = getLocation(from);
  288. if (location == null) {
  289. throw new IllegalArgumentException();
  290. }
  291. setWidget(to, location);
  292. }
  293. /** Does this layout contain given child */
  294. public boolean hasChildComponent(Widget component) {
  295. return locationToWidget.containsValue(component);
  296. }
  297. /** Update caption for given widget */
  298. public void updateCaption(Paintable component, UIDL uidl) {
  299. ICaptionWrapper wrapper = (ICaptionWrapper) widgetToCaptionWrapper
  300. .get(component);
  301. if (ICaption.isNeeded(uidl)) {
  302. if (wrapper == null) {
  303. final String loc = getLocation((Widget) component);
  304. super.remove((Widget) component);
  305. wrapper = new ICaptionWrapper(component, client);
  306. super.add(wrapper, (Element) locationToElement.get(loc));
  307. widgetToCaptionWrapper.put(component, wrapper);
  308. }
  309. wrapper.updateCaption(uidl);
  310. } else {
  311. if (wrapper != null) {
  312. final String loc = getLocation((Widget) component);
  313. super.remove(wrapper);
  314. super.add((Widget) wrapper.getPaintable(),
  315. (Element) locationToElement.get(loc));
  316. widgetToCaptionWrapper.remove(component);
  317. }
  318. }
  319. }
  320. /** Get the location of an widget */
  321. public String getLocation(Widget w) {
  322. for (final Iterator i = locationToWidget.keySet().iterator(); i
  323. .hasNext();) {
  324. final String location = (String) i.next();
  325. if (locationToWidget.get(location) == w) {
  326. return location;
  327. }
  328. }
  329. return null;
  330. }
  331. /** Removes given widget from the layout */
  332. public boolean remove(Widget w) {
  333. client.unregisterPaintable((Paintable) w);
  334. final String location = getLocation(w);
  335. if (location != null) {
  336. locationToWidget.remove(location);
  337. }
  338. final ICaptionWrapper cw = (ICaptionWrapper) widgetToCaptionWrapper
  339. .get(w);
  340. if (cw != null) {
  341. widgetToCaptionWrapper.remove(w);
  342. return super.remove(cw);
  343. } else if (w != null) {
  344. return super.remove(w);
  345. }
  346. return false;
  347. }
  348. /** Adding widget without specifying location is not supported */
  349. public void add(Widget w) {
  350. throw new UnsupportedOperationException();
  351. }
  352. /** Clear all widgets from the layout */
  353. public void clear() {
  354. super.clear();
  355. locationToWidget.clear();
  356. widgetToCaptionWrapper.clear();
  357. }
  358. public void iLayout() {
  359. if (!iLayoutJS(DOM.getFirstChild(getElement()))) {
  360. Util.runDescendentsLayout(this);
  361. }
  362. }
  363. /**
  364. * This method is published to JS side with the same name into first DOM
  365. * node of custom layout. This way if one implements some resizeable
  366. * containers in custom layout he/she can notify children after resize.
  367. */
  368. public void notifyChildrenOfSizeChange() {
  369. Util.runDescendentsLayout(this);
  370. }
  371. public void onDetach() {
  372. detachResizedFunction(DOM.getFirstChild(getElement()));
  373. }
  374. private native void detachResizedFunction(Element element)
  375. /*-{
  376. element.notifyChildrenOfSizeChange = null;
  377. }-*/;
  378. private native void publishResizedFunction(Element element)
  379. /*-{
  380. var self = this;
  381. element.notifyChildrenOfSizeChange = function() {
  382. self.@com.itmill.toolkit.terminal.gwt.client.ui.ICustomLayout::notifyChildrenOfSizeChange()();
  383. };
  384. }-*/;
  385. /**
  386. * In custom layout one may want to run layout functions made with
  387. * JavaScript. This function tests if one exists (with name "iLayoutJS" in
  388. * layouts first DOM node) and runs et. Return value is used to determine if
  389. * children needs to be notified of size changes.
  390. *
  391. * Note! When implementing a JS layout function you most likely want to call
  392. * notifyChildrenOfSizeChange() function on your custom layouts main
  393. * element. That method is used to control whether child components layout
  394. * functions are to be run.
  395. *
  396. * @param el
  397. * @return true if layout function exists and was run successfully, else
  398. * false.
  399. */
  400. private native boolean iLayoutJS(Element el)
  401. /*-{
  402. if(el && el.iLayoutJS) {
  403. try {
  404. el.iLayoutJS();
  405. return true;
  406. } catch (e) {
  407. return false;
  408. }
  409. } else {
  410. return false;
  411. }
  412. }-*/;
  413. }