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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  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.user.client.DOM;
  9. import com.google.gwt.user.client.Element;
  10. import com.google.gwt.user.client.Event;
  11. import com.google.gwt.user.client.ui.HTML;
  12. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  13. import com.vaadin.terminal.gwt.client.BrowserInfo;
  14. import com.vaadin.terminal.gwt.client.ComponentConnector;
  15. import com.vaadin.terminal.gwt.client.ConnectorMap;
  16. import com.vaadin.terminal.gwt.client.UIDL;
  17. import com.vaadin.terminal.gwt.client.Util;
  18. import com.vaadin.terminal.gwt.client.VConsole;
  19. public class VEmbedded extends HTML {
  20. public static String CLASSNAME = "v-embedded";
  21. protected Element browserElement;
  22. protected String type;
  23. protected ApplicationConnection client;
  24. public VEmbedded() {
  25. setStyleName(CLASSNAME);
  26. }
  27. /**
  28. * Creates the Object and Embed tags for the Flash plugin so it works
  29. * cross-browser
  30. *
  31. * @param uidl
  32. * The UIDL
  33. * @return Tags concatenated into a string
  34. */
  35. protected String createFlashEmbed(UIDL uidl) {
  36. /*
  37. * To ensure cross-browser compatibility we are using the twice-cooked
  38. * method to embed flash i.e. we add a OBJECT tag for IE ActiveX and
  39. * inside it a EMBED for all other browsers.
  40. */
  41. StringBuilder html = new StringBuilder();
  42. // Start the object tag
  43. html.append("<object ");
  44. /*
  45. * Add classid required for ActiveX to recognize the flash. This is a
  46. * predefined value which ActiveX recognizes and must be the given
  47. * value. More info can be found on
  48. * http://kb2.adobe.com/cps/415/tn_4150.html. Allow user to override
  49. * this by setting his own classid.
  50. */
  51. if (uidl.hasAttribute("classid")) {
  52. html.append("classid=\""
  53. + Util.escapeAttribute(uidl.getStringAttribute("classid"))
  54. + "\" ");
  55. } else {
  56. html.append("classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" ");
  57. }
  58. /*
  59. * Add codebase required for ActiveX and must be exactly this according
  60. * to http://kb2.adobe.com/cps/415/tn_4150.html to work with the above
  61. * given classid. Again, see more info on
  62. * http://kb2.adobe.com/cps/415/tn_4150.html. Limiting Flash version to
  63. * 6.0.0.0 and above. Allow user to override this by setting his own
  64. * codebase
  65. */
  66. if (uidl.hasAttribute("codebase")) {
  67. html.append("codebase=\""
  68. + Util.escapeAttribute(uidl.getStringAttribute("codebase"))
  69. + "\" ");
  70. } else {
  71. html.append("codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" ");
  72. }
  73. ComponentConnector paintable = ConnectorMap.get(client).getConnector(
  74. this);
  75. String height = paintable.getDeclaredHeight();
  76. String width = paintable.getDeclaredWidth();
  77. // Add width and height
  78. html.append("width=\"" + Util.escapeAttribute(width) + "\" ");
  79. html.append("height=\"" + Util.escapeAttribute(height) + "\" ");
  80. html.append("type=\"application/x-shockwave-flash\" ");
  81. // Codetype
  82. if (uidl.hasAttribute("codetype")) {
  83. html.append("codetype=\""
  84. + Util.escapeAttribute(uidl.getStringAttribute("codetype"))
  85. + "\" ");
  86. }
  87. // Standby
  88. if (uidl.hasAttribute("standby")) {
  89. html.append("standby=\""
  90. + Util.escapeAttribute(uidl.getStringAttribute("standby"))
  91. + "\" ");
  92. }
  93. // Archive
  94. if (uidl.hasAttribute("archive")) {
  95. html.append("archive=\""
  96. + Util.escapeAttribute(uidl.getStringAttribute("archive"))
  97. + "\" ");
  98. }
  99. // End object tag
  100. html.append(">");
  101. // Ensure we have an movie parameter
  102. Map<String, String> parameters = getParameters(uidl);
  103. if (parameters.get("movie") == null) {
  104. parameters.put("movie", getSrc(uidl, client));
  105. }
  106. // Add parameters to OBJECT
  107. for (String name : parameters.keySet()) {
  108. html.append("<param ");
  109. html.append("name=\"" + Util.escapeAttribute(name) + "\" ");
  110. html.append("value=\"" + Util.escapeAttribute(parameters.get(name))
  111. + "\" ");
  112. html.append("/>");
  113. }
  114. // Build inner EMBED tag
  115. html.append("<embed ");
  116. html.append("src=\"" + Util.escapeAttribute(getSrc(uidl, client))
  117. + "\" ");
  118. html.append("width=\"" + Util.escapeAttribute(width) + "\" ");
  119. html.append("height=\"" + Util.escapeAttribute(height) + "\" ");
  120. html.append("type=\"application/x-shockwave-flash\" ");
  121. // Add the parameters to the Embed
  122. for (String name : parameters.keySet()) {
  123. html.append(Util.escapeAttribute(name));
  124. html.append("=");
  125. html.append("\"" + Util.escapeAttribute(parameters.get(name))
  126. + "\"");
  127. }
  128. // End embed tag
  129. html.append("></embed>");
  130. // End object tag
  131. html.append("</object>");
  132. return html.toString();
  133. }
  134. /**
  135. * Returns a map (name -> value) of all parameters in the UIDL.
  136. *
  137. * @param uidl
  138. * @return
  139. */
  140. protected static Map<String, String> getParameters(UIDL uidl) {
  141. Map<String, String> parameters = new HashMap<String, String>();
  142. Iterator<Object> childIterator = uidl.getChildIterator();
  143. while (childIterator.hasNext()) {
  144. Object child = childIterator.next();
  145. if (child instanceof UIDL) {
  146. UIDL childUIDL = (UIDL) child;
  147. if (childUIDL.getTag().equals("embeddedparam")) {
  148. String name = childUIDL.getStringAttribute("name");
  149. String value = childUIDL.getStringAttribute("value");
  150. parameters.put(name, value);
  151. }
  152. }
  153. }
  154. return parameters;
  155. }
  156. /**
  157. * Helper to return translated src-attribute from embedded's UIDL
  158. *
  159. * @param uidl
  160. * @param client
  161. * @return
  162. */
  163. protected String getSrc(UIDL uidl, ApplicationConnection client) {
  164. String url = client.translateVaadinUri(uidl.getStringAttribute("src"));
  165. if (url == null) {
  166. return "";
  167. }
  168. return url;
  169. }
  170. @Override
  171. protected void onDetach() {
  172. if (BrowserInfo.get().isIE()) {
  173. // Force browser to fire unload event when component is detached
  174. // from the view (IE doesn't do this automatically)
  175. if (browserElement != null) {
  176. DOM.setElementAttribute(browserElement, "src",
  177. "javascript:false");
  178. }
  179. }
  180. super.onDetach();
  181. }
  182. @Override
  183. public void onBrowserEvent(Event event) {
  184. super.onBrowserEvent(event);
  185. if (DOM.eventGetType(event) == Event.ONLOAD) {
  186. VConsole.log("Embeddable onload");
  187. Util.notifyParentOfSizeChange(this, true);
  188. }
  189. client.handleTooltipEvent(event, this);
  190. }
  191. }