選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

VEmbedded.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright 2000-2018 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.Map;
  19. import java.util.logging.Logger;
  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.WidgetUtil;
  31. import com.vaadin.shared.ui.embedded.EmbeddedState;
  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 state
  52. * The EmbeddedState
  53. * @param src
  54. * The src attribute
  55. * @return Tags concatenated into a string
  56. * @since 8.2
  57. */
  58. public String createFlashEmbed(EmbeddedState state, String src) {
  59. /*
  60. * To ensure cross-browser compatibility we are using the twice-cooked
  61. * method to embed flash i.e. we add a OBJECT tag for IE ActiveX and
  62. * inside it a EMBED for all other browsers.
  63. */
  64. StringBuilder html = new StringBuilder();
  65. // Start the object tag
  66. html.append("<object ");
  67. /*
  68. * Add classid required for ActiveX to recognize the flash. This is a
  69. * predefined value which ActiveX recognizes and must be the given
  70. * value. More info can be found on
  71. * http://kb2.adobe.com/cps/415/tn_4150.html. Allow user to override
  72. * this by setting his own classid.
  73. */
  74. if (state.classId != null) {
  75. html.append("classid=\"" + WidgetUtil.escapeAttribute(state.classId)
  76. + "\" ");
  77. } else {
  78. html.append(
  79. "classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" ");
  80. }
  81. /*
  82. * Add codebase required for ActiveX and must be exactly this according
  83. * to http://kb2.adobe.com/cps/415/tn_4150.html to work with the above
  84. * given classid. Again, see more info on
  85. * http://kb2.adobe.com/cps/415/tn_4150.html. Limiting Flash version to
  86. * 6.0.0.0 and above. Allow user to override this by setting his own
  87. * codebase
  88. */
  89. if (state.codebase != null) {
  90. html.append("codebase=\""
  91. + WidgetUtil.escapeAttribute(state.codebase) + "\" ");
  92. } else {
  93. html.append(
  94. "codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" ");
  95. }
  96. ComponentConnector paintable = ConnectorMap.get(client)
  97. .getConnector(this);
  98. String height = paintable.getState().height;
  99. String width = paintable.getState().width;
  100. // Add width and height
  101. html.append("width=\"" + WidgetUtil.escapeAttribute(width) + "\" ");
  102. html.append("height=\"" + WidgetUtil.escapeAttribute(height) + "\" ");
  103. html.append("type=\"application/x-shockwave-flash\" ");
  104. // Codetype
  105. if (state.codetype != null) {
  106. html.append("codetype=\""
  107. + WidgetUtil.escapeAttribute(state.codetype) + "\" ");
  108. }
  109. // Standby
  110. if (state.standby != null) {
  111. html.append("standby=\"" + WidgetUtil.escapeAttribute(state.standby)
  112. + "\" ");
  113. }
  114. // Archive
  115. if (state.archive != null) {
  116. html.append("archive=\"" + WidgetUtil.escapeAttribute(state.archive)
  117. + "\" ");
  118. }
  119. // End object tag
  120. html.append('>');
  121. // Ensure we have an movie parameter
  122. Map<String, String> parameters = state.parameters;
  123. if (parameters.get("movie") == null) {
  124. parameters.put("movie", getSrc(src, client));
  125. }
  126. // Add parameters to OBJECT
  127. for (String name : parameters.keySet()) {
  128. html.append("<param ");
  129. html.append("name=\"" + WidgetUtil.escapeAttribute(name) + "\" ");
  130. html.append("value=\""
  131. + WidgetUtil.escapeAttribute(parameters.get(name)) + "\" ");
  132. html.append("/>");
  133. }
  134. // Build inner EMBED tag
  135. html.append("<embed ");
  136. html.append("src=\"" + WidgetUtil.escapeAttribute(getSrc(src, client))
  137. + "\" ");
  138. html.append("width=\"" + WidgetUtil.escapeAttribute(width) + "\" ");
  139. html.append("height=\"" + WidgetUtil.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(WidgetUtil.escapeAttribute(name));
  144. html.append('=');
  145. html.append("\"" + WidgetUtil.escapeAttribute(parameters.get(name))
  146. + "\"");
  147. }
  148. // End embed tag
  149. html.append("></embed>");
  150. if (state.altText != null) {
  151. html.append(state.altText);
  152. }
  153. // End object tag
  154. html.append("</object>");
  155. return html.toString();
  156. }
  157. /**
  158. * Returns a map (name -> value) of all parameters in the UIDL.
  159. * <p>
  160. * For internal use only. May be removed or replaced in the future.
  161. *
  162. * @param uidl
  163. * @return
  164. */
  165. public static Map<String, String> getParameters(UIDL uidl) {
  166. Map<String, String> parameters = new HashMap<>();
  167. for (Object child : uidl) {
  168. if (child instanceof UIDL) {
  169. UIDL childUIDL = (UIDL) child;
  170. if (childUIDL.getTag().equals("embeddedparam")) {
  171. String name = childUIDL.getStringAttribute("name");
  172. String value = childUIDL.getStringAttribute("value");
  173. parameters.put(name, value);
  174. }
  175. }
  176. }
  177. return parameters;
  178. }
  179. /**
  180. * Helper to return translated src-attribute from embedded's UIDL
  181. * <p>
  182. * For internal use only. May be removed or replaced in the future.
  183. *
  184. * @param src
  185. * the src attribute
  186. * @param client
  187. * @return
  188. */
  189. public String getSrc(String src, ApplicationConnection client) {
  190. String url = client.translateVaadinUri(src);
  191. if (url == null) {
  192. return "";
  193. }
  194. return url;
  195. }
  196. @Override
  197. protected void onDetach() {
  198. if (BrowserInfo.get().isIE()) {
  199. // Force browser to fire unload event when component is detached
  200. // from the view (IE doesn't do this automatically)
  201. if (browserElement != null) {
  202. /*
  203. * src was previously set to javascript:false, but this was not
  204. * enough to overcome a bug when detaching an iframe with a pdf
  205. * loaded in IE9. about:blank seems to cause the adobe reader
  206. * plugin to unload properly before the iframe is removed. See
  207. * #7855
  208. */
  209. DOM.setElementAttribute(browserElement, "src", "about:blank");
  210. }
  211. }
  212. super.onDetach();
  213. }
  214. @Override
  215. public void onBrowserEvent(Event event) {
  216. super.onBrowserEvent(event);
  217. if (DOM.eventGetType(event) == Event.ONLOAD) {
  218. getLogger().info("Embeddable onload");
  219. Util.notifyParentOfSizeChange(this, true);
  220. }
  221. }
  222. private static Logger getLogger() {
  223. return Logger.getLogger(VEmbedded.class.getName());
  224. }
  225. }