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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui;
  17. import java.util.HashMap;
  18. import java.util.Iterator;
  19. import java.util.Map;
  20. import com.google.gwt.dom.client.Element;
  21. import com.google.gwt.user.client.DOM;
  22. import com.google.gwt.user.client.Event;
  23. import com.google.gwt.user.client.ui.HTML;
  24. import com.vaadin.client.ApplicationConnection;
  25. import com.vaadin.client.BrowserInfo;
  26. import com.vaadin.client.ComponentConnector;
  27. import com.vaadin.client.ConnectorMap;
  28. import com.vaadin.client.UIDL;
  29. import com.vaadin.client.Util;
  30. import com.vaadin.client.VConsole;
  31. import com.vaadin.shared.ui.embedded.EmbeddedConstants;
  32. public class VEmbedded extends HTML {
  33. public static String CLASSNAME = "v-embedded";
  34. /** For internal use only. May be removed or replaced in the future. */
  35. public Element browserElement;
  36. /** For internal use only. May be removed or replaced in the future. */
  37. public String type;
  38. /** For internal use only. May be removed or replaced in the future. */
  39. public String mimetype;
  40. /** For internal use only. May be removed or replaced in the future. */
  41. public ApplicationConnection client;
  42. public VEmbedded() {
  43. setStyleName(CLASSNAME);
  44. }
  45. /**
  46. * Creates the Object and Embed tags for the Flash plugin so it works
  47. * cross-browser.
  48. * <p>
  49. * For internal use only. May be removed or replaced in the future.
  50. *
  51. * @param uidl
  52. * The UIDL
  53. * @return Tags concatenated into a string
  54. */
  55. public String createFlashEmbed(UIDL uidl) {
  56. /*
  57. * To ensure cross-browser compatibility we are using the twice-cooked
  58. * method to embed flash i.e. we add a OBJECT tag for IE ActiveX and
  59. * inside it a EMBED for all other browsers.
  60. */
  61. StringBuilder html = new StringBuilder();
  62. // Start the object tag
  63. html.append("<object ");
  64. /*
  65. * Add classid required for ActiveX to recognize the flash. This is a
  66. * predefined value which ActiveX recognizes and must be the given
  67. * value. More info can be found on
  68. * http://kb2.adobe.com/cps/415/tn_4150.html. Allow user to override
  69. * this by setting his own classid.
  70. */
  71. if (uidl.hasAttribute("classid")) {
  72. html.append("classid=\""
  73. + Util.escapeAttribute(uidl.getStringAttribute("classid"))
  74. + "\" ");
  75. } else {
  76. html.append("classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" ");
  77. }
  78. /*
  79. * Add codebase required for ActiveX and must be exactly this according
  80. * to http://kb2.adobe.com/cps/415/tn_4150.html to work with the above
  81. * given classid. Again, see more info on
  82. * http://kb2.adobe.com/cps/415/tn_4150.html. Limiting Flash version to
  83. * 6.0.0.0 and above. Allow user to override this by setting his own
  84. * codebase
  85. */
  86. if (uidl.hasAttribute("codebase")) {
  87. html.append("codebase=\""
  88. + Util.escapeAttribute(uidl.getStringAttribute("codebase"))
  89. + "\" ");
  90. } else {
  91. html.append("codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" ");
  92. }
  93. ComponentConnector paintable = ConnectorMap.get(client).getConnector(
  94. this);
  95. String height = paintable.getState().height;
  96. String width = paintable.getState().width;
  97. // Add width and height
  98. html.append("width=\"" + Util.escapeAttribute(width) + "\" ");
  99. html.append("height=\"" + Util.escapeAttribute(height) + "\" ");
  100. html.append("type=\"application/x-shockwave-flash\" ");
  101. // Codetype
  102. if (uidl.hasAttribute("codetype")) {
  103. html.append("codetype=\""
  104. + Util.escapeAttribute(uidl.getStringAttribute("codetype"))
  105. + "\" ");
  106. }
  107. // Standby
  108. if (uidl.hasAttribute("standby")) {
  109. html.append("standby=\""
  110. + Util.escapeAttribute(uidl.getStringAttribute("standby"))
  111. + "\" ");
  112. }
  113. // Archive
  114. if (uidl.hasAttribute("archive")) {
  115. html.append("archive=\""
  116. + Util.escapeAttribute(uidl.getStringAttribute("archive"))
  117. + "\" ");
  118. }
  119. // End object tag
  120. html.append(">");
  121. // Ensure we have an movie parameter
  122. Map<String, String> parameters = getParameters(uidl);
  123. if (parameters.get("movie") == null) {
  124. parameters.put("movie", getSrc(uidl, client));
  125. }
  126. // Add parameters to OBJECT
  127. for (String name : parameters.keySet()) {
  128. html.append("<param ");
  129. html.append("name=\"" + Util.escapeAttribute(name) + "\" ");
  130. html.append("value=\"" + Util.escapeAttribute(parameters.get(name))
  131. + "\" ");
  132. html.append("/>");
  133. }
  134. // Build inner EMBED tag
  135. html.append("<embed ");
  136. html.append("src=\"" + Util.escapeAttribute(getSrc(uidl, client))
  137. + "\" ");
  138. html.append("width=\"" + Util.escapeAttribute(width) + "\" ");
  139. html.append("height=\"" + Util.escapeAttribute(height) + "\" ");
  140. html.append("type=\"application/x-shockwave-flash\" ");
  141. // Add the parameters to the Embed
  142. for (String name : parameters.keySet()) {
  143. html.append(Util.escapeAttribute(name));
  144. html.append("=");
  145. html.append("\"" + Util.escapeAttribute(parameters.get(name))
  146. + "\"");
  147. }
  148. // End embed tag
  149. html.append("></embed>");
  150. if (uidl.hasAttribute(EmbeddedConstants.ALTERNATE_TEXT)) {
  151. html.append(uidl
  152. .getStringAttribute(EmbeddedConstants.ALTERNATE_TEXT));
  153. }
  154. // End object tag
  155. html.append("</object>");
  156. return html.toString();
  157. }
  158. /**
  159. * Returns a map (name -> value) of all parameters in the UIDL.
  160. * <p>
  161. * For internal use only. May be removed or replaced in the future.
  162. *
  163. * @param uidl
  164. * @return
  165. */
  166. public static Map<String, String> getParameters(UIDL uidl) {
  167. Map<String, String> parameters = new HashMap<String, String>();
  168. Iterator<Object> childIterator = uidl.getChildIterator();
  169. while (childIterator.hasNext()) {
  170. Object child = childIterator.next();
  171. if (child instanceof UIDL) {
  172. UIDL childUIDL = (UIDL) child;
  173. if (childUIDL.getTag().equals("embeddedparam")) {
  174. String name = childUIDL.getStringAttribute("name");
  175. String value = childUIDL.getStringAttribute("value");
  176. parameters.put(name, value);
  177. }
  178. }
  179. }
  180. return parameters;
  181. }
  182. /**
  183. * Helper to return translated src-attribute from embedded's UIDL
  184. * <p>
  185. * For internal use only. May be removed or replaced in the future.
  186. *
  187. * @param uidl
  188. * @param client
  189. * @return
  190. */
  191. public String getSrc(UIDL uidl, ApplicationConnection client) {
  192. String url = client.translateVaadinUri(uidl.getStringAttribute("src"));
  193. if (url == null) {
  194. return "";
  195. }
  196. return url;
  197. }
  198. @Override
  199. protected void onDetach() {
  200. if (BrowserInfo.get().isIE()) {
  201. // Force browser to fire unload event when component is detached
  202. // from the view (IE doesn't do this automatically)
  203. if (browserElement != null) {
  204. /*
  205. * src was previously set to javascript:false, but this was not
  206. * enough to overcome a bug when detaching an iframe with a pdf
  207. * loaded in IE9. about:blank seems to cause the adobe reader
  208. * plugin to unload properly before the iframe is removed. See
  209. * #7855
  210. */
  211. DOM.setElementAttribute(browserElement, "src", "about:blank");
  212. }
  213. }
  214. super.onDetach();
  215. }
  216. @Override
  217. public void onBrowserEvent(Event event) {
  218. super.onBrowserEvent(event);
  219. if (DOM.eventGetType(event) == Event.ONLOAD) {
  220. VConsole.log("Embeddable onload");
  221. Util.notifyParentOfSizeChange(this, true);
  222. }
  223. }
  224. }