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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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",
  106. getSrc(uidl, 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. // Add the parameters to the Object
  123. for (String name : parameters.keySet()) {
  124. html += "<param name=\"" + escapeAttribute(name)
  125. + "\" value=\""
  126. + escapeAttribute(parameters.get(name)) + "\"/>";
  127. }
  128. html += "<embed src=\"" + getSrc(uidl, client) + "\" width=\""
  129. + width + "\" height=\"" + height + "\" ";
  130. // Add the parameters to the Embed
  131. for (String name : parameters.keySet()) {
  132. html += escapeAttribute(name) + "=\""
  133. + escapeAttribute(parameters.get(name)) + "\" ";
  134. }
  135. html += "></embed></object>";
  136. setHTML(html);
  137. } else if (mime.equals("image/svg+xml")) {
  138. String data;
  139. Map<String, String> parameters = getParameters(uidl);
  140. if (parameters.get("data") == null) {
  141. data = getSrc(uidl, client);
  142. } else {
  143. data = "data:image/svg+xml," + parameters.get("data");
  144. }
  145. setHTML("");
  146. ObjectElement obj = Document.get().createObjectElement();
  147. obj.setType(mime);
  148. obj.setData(data);
  149. if (width != null) {
  150. obj.getStyle().setProperty("width", "100%");
  151. }
  152. if (height != null) {
  153. obj.getStyle().setProperty("height", "100%");
  154. }
  155. getElement().appendChild(obj);
  156. } else {
  157. ApplicationConnection.getConsole().log(
  158. "Unknown Embedded mimetype '" + mime + "'");
  159. }
  160. } else {
  161. ApplicationConnection.getConsole().log(
  162. "Unknown Embedded; no type or mimetype attribute");
  163. }
  164. if (clearBrowserElement) {
  165. browserElement = null;
  166. }
  167. }
  168. /**
  169. * Escapes the string so it is safe to write inside an HTML attribute.
  170. *
  171. * @param attribute
  172. * The string to escape
  173. * @return An escaped version of <literal>attribute</literal>.
  174. */
  175. private String escapeAttribute(String attribute) {
  176. attribute = attribute.replace("\"", "&quot;");
  177. attribute = attribute.replace("'", "&#39;");
  178. attribute = attribute.replace(">", "&gt;");
  179. attribute = attribute.replace("<", "&lt;");
  180. attribute = attribute.replace("&", "&amp;");
  181. return attribute;
  182. }
  183. /**
  184. * Returns a map (name -> value) of all parameters in the UIDL.
  185. *
  186. * @param uidl
  187. * @return
  188. */
  189. private static Map<String, String> getParameters(UIDL uidl) {
  190. Map<String, String> parameters = new HashMap<String, String>();
  191. Iterator<Object> childIterator = uidl.getChildIterator();
  192. while (childIterator.hasNext()) {
  193. Object child = childIterator.next();
  194. if (child instanceof UIDL) {
  195. UIDL childUIDL = (UIDL) child;
  196. if (childUIDL.getTag().equals("embeddedparam")) {
  197. String name = childUIDL.getStringAttribute("name");
  198. String value = childUIDL.getStringAttribute("value");
  199. parameters.put(name, value);
  200. }
  201. }
  202. }
  203. return parameters;
  204. }
  205. /**
  206. * Helper to return translated src-attribute from embedded's UIDL
  207. *
  208. * @param uidl
  209. * @param client
  210. * @return
  211. */
  212. private String getSrc(UIDL uidl, ApplicationConnection client) {
  213. String url = client.translateVaadinUri(uidl.getStringAttribute("src"));
  214. if (url == null) {
  215. return "";
  216. }
  217. return url;
  218. }
  219. @Override
  220. public void setWidth(String width) {
  221. this.width = width;
  222. if (isDynamicHeight()) {
  223. int oldHeight = getOffsetHeight();
  224. super.setWidth(width);
  225. int newHeight = getOffsetHeight();
  226. /*
  227. * Must notify parent if the height changes as a result of a width
  228. * change
  229. */
  230. if (oldHeight != newHeight) {
  231. Util.notifyParentOfSizeChange(this, false);
  232. }
  233. } else {
  234. super.setWidth(width);
  235. }
  236. }
  237. private boolean isDynamicHeight() {
  238. return height == null || height.equals("");
  239. }
  240. @Override
  241. public void setHeight(String height) {
  242. this.height = height;
  243. super.setHeight(height);
  244. }
  245. @Override
  246. protected void onDetach() {
  247. if (BrowserInfo.get().isIE()) {
  248. // Force browser to fire unload event when component is detached
  249. // from the view (IE doesn't do this automatically)
  250. if (browserElement != null) {
  251. DOM.setElementAttribute(browserElement, "src",
  252. "javascript:false");
  253. }
  254. }
  255. super.onDetach();
  256. }
  257. @Override
  258. public void onBrowserEvent(Event event) {
  259. super.onBrowserEvent(event);
  260. if (DOM.eventGetType(event) == Event.ONLOAD) {
  261. Util.notifyParentOfSizeChange(this, true);
  262. }
  263. client.handleTooltipEvent(event, this);
  264. }
  265. }