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.

VEmbedded.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.Map;
  8. import com.google.gwt.dom.client.Document;
  9. import com.google.gwt.dom.client.Node;
  10. import com.google.gwt.dom.client.NodeList;
  11. import com.google.gwt.dom.client.ObjectElement;
  12. import com.google.gwt.dom.client.Style;
  13. import com.google.gwt.event.dom.client.DomEvent.Type;
  14. import com.google.gwt.event.shared.EventHandler;
  15. import com.google.gwt.event.shared.HandlerRegistration;
  16. import com.google.gwt.user.client.DOM;
  17. import com.google.gwt.user.client.Element;
  18. import com.google.gwt.user.client.Event;
  19. import com.google.gwt.user.client.ui.HTML;
  20. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  21. import com.vaadin.terminal.gwt.client.BrowserInfo;
  22. import com.vaadin.terminal.gwt.client.Paintable;
  23. import com.vaadin.terminal.gwt.client.UIDL;
  24. import com.vaadin.terminal.gwt.client.Util;
  25. import com.vaadin.terminal.gwt.client.VTooltip;
  26. public class VEmbedded extends HTML implements Paintable {
  27. public static final String CLICK_EVENT_IDENTIFIER = "click";
  28. private static String CLASSNAME = "v-embedded";
  29. private String height;
  30. private String width;
  31. private Element browserElement;
  32. private ApplicationConnection client;
  33. private ClickEventHandler clickEventHandler = new ClickEventHandler(this,
  34. CLICK_EVENT_IDENTIFIER) {
  35. @Override
  36. protected <H extends EventHandler> HandlerRegistration registerHandler(
  37. H handler, Type<H> type) {
  38. return addDomHandler(handler, type);
  39. }
  40. };
  41. public VEmbedded() {
  42. setStyleName(CLASSNAME);
  43. }
  44. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  45. if (client.updateComponent(this, uidl, true)) {
  46. return;
  47. }
  48. this.client = client;
  49. boolean clearBrowserElement = true;
  50. clickEventHandler.handleEventHandlerRegistration(client);
  51. if (uidl.hasAttribute("type")) {
  52. final String type = uidl.getStringAttribute("type");
  53. if (type.equals("image")) {
  54. Element el = null;
  55. boolean created = false;
  56. NodeList<Node> nodes = getElement().getChildNodes();
  57. if (nodes != null && nodes.getLength() == 1) {
  58. Node n = nodes.getItem(0);
  59. if (n.getNodeType() == Node.ELEMENT_NODE) {
  60. Element e = (Element) n;
  61. if (e.getTagName().equals("IMG")) {
  62. el = e;
  63. }
  64. }
  65. }
  66. if (el == null) {
  67. setHTML("");
  68. el = DOM.createImg();
  69. created = true;
  70. client.addPngFix(el);
  71. DOM.sinkEvents(el, Event.ONLOAD);
  72. }
  73. // Set attributes
  74. Style style = el.getStyle();
  75. String w = uidl.getStringAttribute("width");
  76. if (w != null) {
  77. style.setProperty("width", w);
  78. } else {
  79. style.setProperty("width", "");
  80. }
  81. String h = uidl.getStringAttribute("height");
  82. if (h != null) {
  83. style.setProperty("height", h);
  84. } else {
  85. style.setProperty("height", "");
  86. }
  87. DOM.setElementProperty(el, "src", getSrc(uidl, client));
  88. if (created) {
  89. // insert in dom late
  90. getElement().appendChild(el);
  91. }
  92. /*
  93. * Sink tooltip events so tooltip is displayed when hovering the
  94. * image.
  95. */
  96. sinkEvents(VTooltip.TOOLTIP_EVENTS);
  97. } else if (type.equals("browser")) {
  98. if (browserElement == null) {
  99. setHTML("<iframe width=\"100%\" height=\"100%\" frameborder=\"0\" allowTransparency=\"true\" src=\""
  100. + getSrc(uidl, client)
  101. + "\" name=\""
  102. + uidl.getId() + "\"></iframe>");
  103. browserElement = DOM.getFirstChild(getElement());
  104. } else {
  105. DOM.setElementAttribute(browserElement, "src", getSrc(uidl,
  106. client));
  107. }
  108. clearBrowserElement = false;
  109. } else {
  110. ApplicationConnection.getConsole().log(
  111. "Unknown Embedded type '" + type + "'");
  112. }
  113. } else if (uidl.hasAttribute("mimetype")) {
  114. final String mime = uidl.getStringAttribute("mimetype");
  115. if (mime.equals("application/x-shockwave-flash")) {
  116. String html = "<object width=\"" + width + "\" height=\""
  117. + height + "\">";
  118. Map<String, String> parameters = getParameters(uidl);
  119. if (parameters.get("movie") == null) {
  120. parameters.put("movie", getSrc(uidl, client));
  121. }
  122. for (String name : parameters.keySet()) {
  123. html += "<param name=\"" + escapeAttribute(name)
  124. + "\" value=\""
  125. + escapeAttribute(parameters.get(name)) + "\"/>";
  126. }
  127. html += "<embed src=\"" + getSrc(uidl, client) + "\" width=\""
  128. + width + "\" height=\"" + height + "\"></embed>";
  129. html += "</object>";
  130. setHTML(html);
  131. } else if (mime.equals("image/svg+xml")) {
  132. String data;
  133. Map<String, String> parameters = getParameters(uidl);
  134. if (parameters.get("data") == null) {
  135. data = getSrc(uidl, client);
  136. } else {
  137. data = "data:image/svg+xml," + parameters.get("data");
  138. }
  139. setHTML("");
  140. ObjectElement obj = Document.get().createObjectElement();
  141. obj.setType(mime);
  142. obj.setData(data);
  143. if (width != null) {
  144. obj.getStyle().setProperty("width", "100%");
  145. }
  146. if (height != null) {
  147. obj.getStyle().setProperty("height", "100%");
  148. }
  149. getElement().appendChild(obj);
  150. } else {
  151. ApplicationConnection.getConsole().log(
  152. "Unknown Embedded mimetype '" + mime + "'");
  153. }
  154. } else {
  155. ApplicationConnection.getConsole().log(
  156. "Unknown Embedded; no type or mimetype attribute");
  157. }
  158. if (clearBrowserElement) {
  159. browserElement = null;
  160. }
  161. }
  162. /**
  163. * Escapes the string so it is safe to write inside an HTML attribute.
  164. *
  165. * @param attribute
  166. * The string to escape
  167. * @return An escaped version of <literal>attribute</literal>.
  168. */
  169. private String escapeAttribute(String attribute) {
  170. attribute = attribute.replace("\"", "&quot;");
  171. attribute = attribute.replace("'", "&#39;");
  172. attribute = attribute.replace(">", "&gt;");
  173. attribute = attribute.replace("<", "&lt;");
  174. attribute = attribute.replace("&", "&amp;");
  175. return attribute;
  176. }
  177. /**
  178. * Returns a map (name -> value) of all parameters in the UIDL.
  179. *
  180. * @param uidl
  181. * @return
  182. */
  183. private static Map<String, String> getParameters(UIDL uidl) {
  184. Map<String, String> parameters = new HashMap<String, String>();
  185. Iterator<Object> childIterator = uidl.getChildIterator();
  186. while (childIterator.hasNext()) {
  187. Object child = childIterator.next();
  188. if (child instanceof UIDL) {
  189. UIDL childUIDL = (UIDL) child;
  190. if (childUIDL.getTag().equals("embeddedparam")) {
  191. String name = childUIDL.getStringAttribute("name");
  192. String value = childUIDL.getStringAttribute("value");
  193. parameters.put(name, value);
  194. }
  195. }
  196. }
  197. return parameters;
  198. }
  199. /**
  200. * Helper to return translated src-attribute from embedded's UIDL
  201. *
  202. * @param uidl
  203. * @param client
  204. * @return
  205. */
  206. private String getSrc(UIDL uidl, ApplicationConnection client) {
  207. String url = client.translateVaadinUri(uidl.getStringAttribute("src"));
  208. if (url == null) {
  209. return "";
  210. }
  211. return url;
  212. }
  213. @Override
  214. public void setWidth(String width) {
  215. this.width = width;
  216. if (isDynamicHeight()) {
  217. int oldHeight = getOffsetHeight();
  218. super.setWidth(width);
  219. int newHeight = getOffsetHeight();
  220. /*
  221. * Must notify parent if the height changes as a result of a width
  222. * change
  223. */
  224. if (oldHeight != newHeight) {
  225. Util.notifyParentOfSizeChange(this, false);
  226. }
  227. } else {
  228. super.setWidth(width);
  229. }
  230. }
  231. private boolean isDynamicHeight() {
  232. return height == null || height.equals("");
  233. }
  234. @Override
  235. public void setHeight(String height) {
  236. this.height = height;
  237. super.setHeight(height);
  238. }
  239. @Override
  240. protected void onDetach() {
  241. if (BrowserInfo.get().isIE()) {
  242. // Force browser to fire unload event when component is detached
  243. // from the view (IE doesn't do this automatically)
  244. if (browserElement != null) {
  245. DOM.setElementAttribute(browserElement, "src",
  246. "javascript:false");
  247. }
  248. }
  249. super.onDetach();
  250. }
  251. @Override
  252. public void onBrowserEvent(Event event) {
  253. super.onBrowserEvent(event);
  254. if (DOM.eventGetType(event) == Event.ONLOAD) {
  255. Util.notifyParentOfSizeChange(this, true);
  256. }
  257. client.handleTooltipEvent(event, this);
  258. }
  259. }