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 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.Iterator;
  6. import com.google.gwt.dom.client.Document;
  7. import com.google.gwt.dom.client.Node;
  8. import com.google.gwt.dom.client.NodeList;
  9. import com.google.gwt.dom.client.ObjectElement;
  10. import com.google.gwt.dom.client.Style;
  11. import com.google.gwt.user.client.DOM;
  12. import com.google.gwt.user.client.Element;
  13. import com.google.gwt.user.client.Event;
  14. import com.google.gwt.user.client.ui.HTML;
  15. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  16. import com.vaadin.terminal.gwt.client.Paintable;
  17. import com.vaadin.terminal.gwt.client.UIDL;
  18. import com.vaadin.terminal.gwt.client.Util;
  19. public class VEmbedded extends HTML implements Paintable {
  20. private static String CLASSNAME = "i-embedded";
  21. private String height;
  22. private String width;
  23. private Element browserElement;
  24. private ApplicationConnection client;
  25. public VEmbedded() {
  26. setStyleName(CLASSNAME);
  27. }
  28. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  29. if (client.updateComponent(this, uidl, true)) {
  30. return;
  31. }
  32. this.client = client;
  33. boolean clearBrowserElement = true;
  34. if (uidl.hasAttribute("type")) {
  35. final String type = uidl.getStringAttribute("type");
  36. if (type.equals("image")) {
  37. Element el = null;
  38. boolean created = false;
  39. NodeList nodes = getElement().getChildNodes();
  40. if (nodes != null && nodes.getLength() == 1) {
  41. Node n = nodes.getItem(0);
  42. if (n.getNodeType() == Node.ELEMENT_NODE) {
  43. Element e = (Element) n;
  44. if (e.getTagName().equals("IMG")) {
  45. el = e;
  46. }
  47. }
  48. }
  49. if (el == null) {
  50. setHTML("");
  51. el = DOM.createImg();
  52. created = true;
  53. client.addPngFix(el);
  54. DOM.sinkEvents(el, Event.ONLOAD);
  55. }
  56. // Set attributes
  57. Style style = el.getStyle();
  58. String w = uidl.getStringAttribute("width");
  59. if (w != null) {
  60. style.setProperty("width", w);
  61. } else {
  62. style.setProperty("width", "");
  63. }
  64. String h = uidl.getStringAttribute("height");
  65. if (h != null) {
  66. style.setProperty("height", h);
  67. } else {
  68. style.setProperty("height", "");
  69. }
  70. DOM.setElementProperty(el, "src", getSrc(uidl, client));
  71. if (created) {
  72. // insert in dom late
  73. getElement().appendChild(el);
  74. }
  75. } else if (type.equals("browser")) {
  76. if (browserElement == null) {
  77. setHTML("<iframe width=\"100%\" height=\"100%\" frameborder=\"0\" src=\""
  78. + getSrc(uidl, client)
  79. + "\" name=\""
  80. + uidl.getId() + "\"></iframe>");
  81. browserElement = DOM.getFirstChild(getElement());
  82. } else {
  83. DOM.setElementAttribute(browserElement, "src", getSrc(uidl,
  84. client));
  85. }
  86. clearBrowserElement = false;
  87. } else {
  88. ApplicationConnection.getConsole().log(
  89. "Unknown Embedded type '" + type + "'");
  90. }
  91. } else if (uidl.hasAttribute("mimetype")) {
  92. final String mime = uidl.getStringAttribute("mimetype");
  93. if (mime.equals("application/x-shockwave-flash")) {
  94. setHTML("<object width=\"" + width + "\" height=\"" + height
  95. + "\"><param name=\"movie\" value=\""
  96. + getSrc(uidl, client) + "\"><embed src=\""
  97. + getSrc(uidl, client) + "\" width=\"" + width
  98. + "\" height=\"" + height + "\"></embed></object>");
  99. } else if (mime.equals("image/svg+xml")) {
  100. String data;
  101. if (getParameter("data", uidl) == null) {
  102. data = getSrc(uidl, client);
  103. } else {
  104. data = "data:image/svg+xml," + getParameter("data", uidl);
  105. }
  106. setHTML("");
  107. ObjectElement obj = Document.get().createObjectElement();
  108. obj.setType(mime);
  109. obj.setData(data);
  110. if (width != null) {
  111. obj.getStyle().setProperty("width", "100%");
  112. }
  113. if (height != null) {
  114. obj.getStyle().setProperty("height", "100%");
  115. }
  116. getElement().appendChild(obj);
  117. } else {
  118. ApplicationConnection.getConsole().log(
  119. "Unknown Embedded mimetype '" + mime + "'");
  120. }
  121. } else {
  122. ApplicationConnection.getConsole().log(
  123. "Unknown Embedded; no type or mimetype attribute");
  124. }
  125. if (clearBrowserElement) {
  126. browserElement = null;
  127. }
  128. }
  129. private static String getParameter(String paramName, UIDL uidl) {
  130. Iterator childIterator = uidl.getChildIterator();
  131. while (childIterator.hasNext()) {
  132. Object child = childIterator.next();
  133. if (child instanceof UIDL) {
  134. UIDL childUIDL = (UIDL) child;
  135. if (childUIDL.getTag().equals("embeddedparam")
  136. && childUIDL.getStringAttribute("name").equals(
  137. paramName)) {
  138. return childUIDL.getStringAttribute("value");
  139. }
  140. }
  141. }
  142. return null;
  143. }
  144. /**
  145. * Helper to return translated src-attribute from embedded's UIDL
  146. *
  147. * @param uidl
  148. * @param client
  149. * @return
  150. */
  151. private String getSrc(UIDL uidl, ApplicationConnection client) {
  152. String url = client.translateToolkitUri(uidl.getStringAttribute("src"));
  153. if (url == null) {
  154. return "";
  155. }
  156. return url;
  157. }
  158. @Override
  159. public void setWidth(String width) {
  160. this.width = width;
  161. if (isDynamicHeight()) {
  162. int oldHeight = getOffsetHeight();
  163. super.setWidth(width);
  164. int newHeight = getOffsetHeight();
  165. /*
  166. * Must notify parent if the height changes as a result of a width
  167. * change
  168. */
  169. if (oldHeight != newHeight) {
  170. Util.notifyParentOfSizeChange(this, false);
  171. }
  172. } else {
  173. super.setWidth(width);
  174. }
  175. }
  176. private boolean isDynamicHeight() {
  177. return height == null || height.equals("");
  178. }
  179. @Override
  180. public void setHeight(String height) {
  181. this.height = height;
  182. super.setHeight(height);
  183. }
  184. @Override
  185. protected void onDetach() {
  186. // Force browser to fire unload event when component is detached from
  187. // the view (IE doesn't do this automatically)
  188. if (browserElement != null) {
  189. DOM.setElementAttribute(browserElement, "src", "javascript:false");
  190. }
  191. super.onDetach();
  192. }
  193. @Override
  194. public void onBrowserEvent(Event event) {
  195. super.onBrowserEvent(event);
  196. if (DOM.eventGetType(event) == Event.ONLOAD) {
  197. Util.notifyParentOfSizeChange(this, true);
  198. }
  199. }
  200. }