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.

VFlash.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright 2000-2016 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 com.google.gwt.user.client.ui.HTML;
  20. import com.vaadin.client.WidgetUtil;
  21. public class VFlash extends HTML {
  22. public static final String CLASSNAME = "v-flash";
  23. protected String source;
  24. protected String altText;
  25. protected String classId;
  26. protected String codebase;
  27. protected String codetype;
  28. protected String standby;
  29. protected String archive;
  30. protected Map<String, String> embedParams = new HashMap<>();
  31. protected boolean needsRebuild = false;
  32. protected String width;
  33. protected String height;
  34. public VFlash() {
  35. setStyleName(CLASSNAME);
  36. }
  37. public void setSource(String source) {
  38. if (this.source != source) {
  39. this.source = source;
  40. needsRebuild = true;
  41. }
  42. }
  43. public void setAlternateText(String altText) {
  44. if (this.altText != altText) {
  45. this.altText = altText;
  46. needsRebuild = true;
  47. }
  48. }
  49. public void setClassId(String classId) {
  50. if (this.classId != classId) {
  51. this.classId = classId;
  52. needsRebuild = true;
  53. }
  54. }
  55. public void setCodebase(String codebase) {
  56. if (this.codebase != codebase) {
  57. this.codebase = codebase;
  58. needsRebuild = true;
  59. }
  60. }
  61. public void setCodetype(String codetype) {
  62. if (this.codetype != codetype) {
  63. this.codetype = codetype;
  64. needsRebuild = true;
  65. }
  66. }
  67. public void setStandby(String standby) {
  68. if (this.standby != standby) {
  69. this.standby = standby;
  70. needsRebuild = true;
  71. }
  72. }
  73. public void setArchive(String archive) {
  74. if (this.archive != archive) {
  75. this.archive = archive;
  76. needsRebuild = true;
  77. }
  78. }
  79. /**
  80. * Call this after changing values of widget. It will rebuild embedding
  81. * structure if needed.
  82. */
  83. public void rebuildIfNeeded() {
  84. if (needsRebuild) {
  85. needsRebuild = false;
  86. this.setHTML(createFlashEmbed());
  87. }
  88. }
  89. @Override
  90. public void setWidth(String width) {
  91. // super.setWidth(height);
  92. if (this.width != width) {
  93. this.width = width;
  94. needsRebuild = true;
  95. }
  96. }
  97. @Override
  98. public void setHeight(String height) {
  99. // super.setHeight(height);
  100. if (this.height != height) {
  101. this.height = height;
  102. needsRebuild = true;
  103. }
  104. }
  105. public void setEmbedParams(Map<String, String> params) {
  106. if (params == null) {
  107. if (!embedParams.isEmpty()) {
  108. embedParams.clear();
  109. needsRebuild = true;
  110. }
  111. return;
  112. }
  113. if (!embedParams.equals(params)) {
  114. embedParams = new HashMap<>(params);
  115. needsRebuild = true;
  116. }
  117. }
  118. protected String createFlashEmbed() {
  119. /*
  120. * To ensure cross-browser compatibility we are using the twice-cooked
  121. * method to embed flash i.e. we add a OBJECT tag for IE ActiveX and
  122. * inside it a EMBED for all other browsers.
  123. */
  124. StringBuilder html = new StringBuilder();
  125. // Start the object tag
  126. html.append("<object ");
  127. /*
  128. * Add classid required for ActiveX to recognize the flash. This is a
  129. * predefined value which ActiveX recognizes and must be the given
  130. * value. More info can be found on
  131. * http://kb2.adobe.com/cps/415/tn_4150.html. Allow user to override
  132. * this by setting his own classid.
  133. */
  134. if (classId != null) {
  135. html.append(
  136. "classid=\"" + WidgetUtil.escapeAttribute(classId) + "\" ");
  137. } else {
  138. html.append(
  139. "classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" ");
  140. }
  141. /*
  142. * Add codebase required for ActiveX and must be exactly this according
  143. * to http://kb2.adobe.com/cps/415/tn_4150.html to work with the above
  144. * given classid. Again, see more info on
  145. * http://kb2.adobe.com/cps/415/tn_4150.html. Limiting Flash version to
  146. * 6.0.0.0 and above. Allow user to override this by setting his own
  147. * codebase
  148. */
  149. if (codebase != null) {
  150. html.append("codebase=\"" + WidgetUtil.escapeAttribute(codebase)
  151. + "\" ");
  152. } else {
  153. html.append(
  154. "codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" ");
  155. }
  156. // Add width and height
  157. html.append("width=\"" + WidgetUtil.escapeAttribute(width) + "\" ");
  158. html.append("height=\"" + WidgetUtil.escapeAttribute(height) + "\" ");
  159. html.append("type=\"application/x-shockwave-flash\" ");
  160. // Codetype
  161. if (codetype != null) {
  162. html.append("codetype=\"" + WidgetUtil.escapeAttribute(codetype)
  163. + "\" ");
  164. }
  165. // Standby
  166. if (standby != null) {
  167. html.append(
  168. "standby=\"" + WidgetUtil.escapeAttribute(standby) + "\" ");
  169. }
  170. // Archive
  171. if (archive != null) {
  172. html.append(
  173. "archive=\"" + WidgetUtil.escapeAttribute(archive) + "\" ");
  174. }
  175. // End object tag
  176. html.append(">");
  177. // Ensure we have an movie parameter
  178. if (embedParams.get("movie") == null) {
  179. embedParams.put("movie", source);
  180. }
  181. // Add parameters to OBJECT
  182. for (String name : embedParams.keySet()) {
  183. html.append("<param ");
  184. html.append("name=\"" + WidgetUtil.escapeAttribute(name) + "\" ");
  185. html.append("value=\""
  186. + WidgetUtil.escapeAttribute(embedParams.get(name))
  187. + "\" ");
  188. html.append("/>");
  189. }
  190. // Build inner EMBED tag
  191. html.append("<embed ");
  192. html.append("src=\"" + WidgetUtil.escapeAttribute(source) + "\" ");
  193. html.append("width=\"" + WidgetUtil.escapeAttribute(width) + "\" ");
  194. html.append("height=\"" + WidgetUtil.escapeAttribute(height) + "\" ");
  195. html.append("type=\"application/x-shockwave-flash\" ");
  196. // Add the parameters to the Embed
  197. for (String name : embedParams.keySet()) {
  198. html.append(WidgetUtil.escapeAttribute(name));
  199. html.append("=");
  200. html.append("\"" + WidgetUtil.escapeAttribute(embedParams.get(name))
  201. + "\"");
  202. }
  203. // End embed tag
  204. html.append("></embed>");
  205. if (altText != null) {
  206. html.append("<noembed>");
  207. html.append(altText);
  208. html.append("</noembed>");
  209. }
  210. // End object tag
  211. html.append("</object>");
  212. return html.toString();
  213. }
  214. }