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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * Copyright 2000-2021 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. /**
  22. * Widget class for the Flash component.
  23. *
  24. * @author Vaadin Ltd
  25. *
  26. * @deprecated No modern browsers support Flash content anymore.
  27. */
  28. @Deprecated
  29. public class VFlash extends HTML {
  30. /** Default classname for this widget. */
  31. public static final String CLASSNAME = "v-flash";
  32. /** @see #setSource(String) */
  33. protected String source;
  34. /** @see #setAlternateText(String) */
  35. protected String altText;
  36. /** @see #setClassId(String) */
  37. protected String classId;
  38. /** @see #setCodebase(String) */
  39. protected String codebase;
  40. /** @see #setCodetype(String) */
  41. protected String codetype;
  42. /** @see #setStandby(String) */
  43. protected String standby;
  44. /** @see #setArchive(String) */
  45. protected String archive;
  46. /** @see #setEmbedParams(Map) */
  47. protected Map<String, String> embedParams = new HashMap<>();
  48. /** Determines whether {@link #rebuildIfNeeded()} does anything. */
  49. protected boolean needsRebuild = false;
  50. /** @see #setWidth(String) */
  51. protected String width;
  52. /** @see #setHeight(String) */
  53. protected String height;
  54. private int slotOffsetHeight = -1;
  55. private int slotOffsetWidth = -1;
  56. /**
  57. * Default constructor.
  58. */
  59. public VFlash() {
  60. setStyleName(CLASSNAME);
  61. }
  62. /**
  63. * Set the resource representing the Flash content that should be displayed.
  64. *
  65. * @param source
  66. * the resource URL
  67. */
  68. public void setSource(String source) {
  69. if (this.source != source) {
  70. this.source = source;
  71. needsRebuild = true;
  72. }
  73. }
  74. /**
  75. * Sets this component's alternate text that can be presented instead of the
  76. * component's normal content for accessibility purposes.
  77. *
  78. * @param altText
  79. * a short, human-readable description of this component's
  80. * content
  81. */
  82. public void setAlternateText(String altText) {
  83. if (this.altText != altText) {
  84. this.altText = altText;
  85. needsRebuild = true;
  86. }
  87. }
  88. /**
  89. * Set the class id that is required for ActiveX to recognize the flash.
  90. * This is a predefined value which ActiveX recognizes and must be the given
  91. * value.
  92. *
  93. * @param classId
  94. * the classId
  95. */
  96. public void setClassId(String classId) {
  97. if (this.classId != classId) {
  98. this.classId = classId;
  99. needsRebuild = true;
  100. }
  101. }
  102. /**
  103. * This attribute specifies the base path used to resolve relative URIs
  104. * specified by the classid, data, and archive attributes. The default value
  105. * is the base URI of the current document.
  106. *
  107. * @param codebase
  108. * The base path
  109. *
  110. * @see #setClassId(String)
  111. * @see #setArchive(String)
  112. */
  113. public void setCodebase(String codebase) {
  114. if (this.codebase != codebase) {
  115. this.codebase = codebase;
  116. needsRebuild = true;
  117. }
  118. }
  119. /**
  120. * This attribute specifies the content type of data expected when
  121. * downloading the object specified by classid. This attribute is optional
  122. * but recommended when classid is specified since it allows the user agent
  123. * to avoid loading information for unsupported content types. The default
  124. * value is the value of the type attribute.
  125. *
  126. * @param codetype
  127. * the codetype to set.
  128. */
  129. public void setCodetype(String codetype) {
  130. if (this.codetype != codetype) {
  131. this.codetype = codetype;
  132. needsRebuild = true;
  133. }
  134. }
  135. /**
  136. * Sets standby.
  137. *
  138. * @param standby
  139. * the standby text
  140. */
  141. public void setStandby(String standby) {
  142. if (this.standby != standby) {
  143. this.standby = standby;
  144. needsRebuild = true;
  145. }
  146. }
  147. /**
  148. * This attribute may be used to specify a space-separated list of URIs for
  149. * archives containing resources relevant to the object, which may include
  150. * the resources specified by the classid and data attributes. Preloading
  151. * archives will generally result in reduced load times for objects.
  152. * Archives specified as relative URIs should be interpreted relative to the
  153. * codebase attribute.
  154. *
  155. * @param archive
  156. * Space-separated list of URIs with resources relevant to the
  157. * object
  158. */
  159. public void setArchive(String archive) {
  160. if (this.archive != archive) {
  161. this.archive = archive;
  162. needsRebuild = true;
  163. }
  164. }
  165. /**
  166. * Call this after changing values of widget. It will rebuild embedding
  167. * structure if needed.
  168. */
  169. public void rebuildIfNeeded() {
  170. if (needsRebuild) {
  171. needsRebuild = false;
  172. this.setHTML(createFlashEmbed());
  173. }
  174. }
  175. @Override
  176. public void setWidth(String width) {
  177. // explicitly not calling super here
  178. if (this.width != width) {
  179. this.width = width;
  180. needsRebuild = true;
  181. }
  182. }
  183. @Override
  184. public void setHeight(String height) {
  185. // explicitly not calling super here
  186. if (this.height != height) {
  187. this.height = height;
  188. needsRebuild = true;
  189. }
  190. }
  191. /**
  192. * Sets the map of object parameters. Parameters are optional information,
  193. * and they are passed to the instantiated object. Parameters are are stored
  194. * as name value pairs. Calling this method for a second time overrides the
  195. * previously given map.
  196. *
  197. * @param params
  198. * the parameter map
  199. */
  200. public void setEmbedParams(Map<String, String> params) {
  201. if (params == null) {
  202. if (!embedParams.isEmpty()) {
  203. embedParams.clear();
  204. needsRebuild = true;
  205. }
  206. return;
  207. }
  208. if (!embedParams.equals(params)) {
  209. embedParams = new HashMap<>(params);
  210. needsRebuild = true;
  211. }
  212. }
  213. /**
  214. * Set dimensions of the containing layout slot so that the size of the
  215. * embed object can be calculated from percentages if needed.
  216. *
  217. * Triggers embed resizing if percentage sizes are in use.
  218. *
  219. * @since 7.7.8
  220. * @param slotOffsetHeight
  221. * offset height of the layout slot
  222. * @param slotOffsetWidth
  223. * offset width of the layout slot
  224. */
  225. public void setSlotHeightAndWidth(int slotOffsetHeight,
  226. int slotOffsetWidth) {
  227. this.slotOffsetHeight = slotOffsetHeight;
  228. this.slotOffsetWidth = slotOffsetWidth;
  229. if (hasPercentageHeight() || hasPercentageWidth()) {
  230. resizeEmbedElement();
  231. }
  232. }
  233. /**
  234. * Creates the embed String.
  235. *
  236. * @return the embed String
  237. */
  238. protected String createFlashEmbed() {
  239. /*
  240. * To ensure cross-browser compatibility we are using the twice-cooked
  241. * method to embed flash i.e. we add a OBJECT tag for IE ActiveX and
  242. * inside it a EMBED for all other browsers.
  243. */
  244. StringBuilder html = new StringBuilder();
  245. // Start the object tag
  246. html.append("<object ");
  247. /*
  248. * Add classid required for ActiveX to recognize the flash. This is a
  249. * predefined value which ActiveX recognizes and must be the given
  250. * value. More info can be found on
  251. * http://kb2.adobe.com/cps/415/tn_4150.html. Allow user to override
  252. * this by setting his own classid.
  253. */
  254. if (classId != null) {
  255. html.append(
  256. "classid=\"" + WidgetUtil.escapeAttribute(classId) + "\" ");
  257. } else {
  258. html.append(
  259. "classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" ");
  260. }
  261. /*
  262. * Add codebase required for ActiveX and must be exactly this according
  263. * to http://kb2.adobe.com/cps/415/tn_4150.html to work with the above
  264. * given classid. Again, see more info on
  265. * http://kb2.adobe.com/cps/415/tn_4150.html. Limiting Flash version to
  266. * 6.0.0.0 and above. Allow user to override this by setting his own
  267. * codebase
  268. */
  269. if (codebase != null) {
  270. html.append("codebase=\"" + WidgetUtil.escapeAttribute(codebase)
  271. + "\" ");
  272. } else {
  273. html.append(
  274. "codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" ");
  275. }
  276. // Add width and height
  277. html.append("width=\"" + WidgetUtil.escapeAttribute(width) + "\" ");
  278. html.append("height=\"" + WidgetUtil.escapeAttribute(height) + "\" ");
  279. html.append("type=\"application/x-shockwave-flash\" ");
  280. // Codetype
  281. if (codetype != null) {
  282. html.append("codetype=\"" + WidgetUtil.escapeAttribute(codetype)
  283. + "\" ");
  284. }
  285. // Standby
  286. if (standby != null) {
  287. html.append(
  288. "standby=\"" + WidgetUtil.escapeAttribute(standby) + "\" ");
  289. }
  290. // Archive
  291. if (archive != null) {
  292. html.append(
  293. "archive=\"" + WidgetUtil.escapeAttribute(archive) + "\" ");
  294. }
  295. // End object tag
  296. html.append('>');
  297. // Ensure we have an movie parameter
  298. if (embedParams.get("movie") == null) {
  299. embedParams.put("movie", source);
  300. }
  301. // Add parameters to OBJECT
  302. for (String name : embedParams.keySet()) {
  303. html.append("<param ");
  304. html.append("name=\"" + WidgetUtil.escapeAttribute(name) + "\" ");
  305. html.append("value=\""
  306. + WidgetUtil.escapeAttribute(embedParams.get(name))
  307. + "\" ");
  308. html.append("/>");
  309. }
  310. // Build inner EMBED tag
  311. html.append("<embed ");
  312. html.append("src=\"" + WidgetUtil.escapeAttribute(source) + "\" ");
  313. if (hasPercentageWidth() && slotOffsetWidth >= 0) {
  314. html.append("width=\"" + getRelativePixelWidth() + "\" ");
  315. } else {
  316. html.append("width=\"" + WidgetUtil.escapeAttribute(width) + "\" ");
  317. }
  318. if (hasPercentageHeight() && slotOffsetHeight >= 0) {
  319. html.append("height=\"" + getRelativePixelHeight() + "px\" ");
  320. } else {
  321. html.append(
  322. "height=\"" + WidgetUtil.escapeAttribute(height) + "\" ");
  323. }
  324. html.append("type=\"application/x-shockwave-flash\" ");
  325. // Add the parameters to the Embed
  326. for (String name : embedParams.keySet()) {
  327. html.append(WidgetUtil.escapeAttribute(name));
  328. html.append('=');
  329. html.append("\"" + WidgetUtil.escapeAttribute(embedParams.get(name))
  330. + "\"");
  331. }
  332. // End embed tag
  333. html.append("></embed>");
  334. if (altText != null) {
  335. html.append("<noembed>");
  336. html.append(altText);
  337. html.append("</noembed>");
  338. }
  339. // End object tag
  340. html.append("</object>");
  341. return html.toString();
  342. }
  343. private void resizeEmbedElement() {
  344. // find <embed> element
  345. com.google.gwt.dom.client.Element objectElem = getElement()
  346. .getFirstChildElement();
  347. com.google.gwt.dom.client.Element objectChild = objectElem
  348. .getFirstChildElement();
  349. while (!"EMBED".equalsIgnoreCase(objectChild.getTagName())) {
  350. objectChild = objectChild.getNextSiblingElement();
  351. if (objectChild == null) {
  352. return;
  353. }
  354. }
  355. // update height & width from slot offset, if percentage size is given
  356. if (hasPercentageHeight() && slotOffsetHeight >= 0) {
  357. objectChild.setAttribute("height", getRelativePixelHeight());
  358. }
  359. if (hasPercentageWidth() && slotOffsetWidth >= 0) {
  360. objectChild.setAttribute("width", getRelativePixelWidth());
  361. }
  362. }
  363. private String getRelativePixelWidth() {
  364. float relative = WidgetUtil.parseRelativeSize(width);
  365. int widthInPixels = (int) (relative / 100) * slotOffsetWidth;
  366. return widthInPixels + "px";
  367. }
  368. private String getRelativePixelHeight() {
  369. float relative = WidgetUtil.parseRelativeSize(height);
  370. int heightInPixels = (int) (relative / 100) * slotOffsetHeight;
  371. return heightInPixels + "px";
  372. }
  373. private boolean hasPercentageHeight() {
  374. return ((height != null) && (height.indexOf('%') > 0));
  375. }
  376. private boolean hasPercentageWidth() {
  377. return ((width != null) && (width.indexOf('%') > 0));
  378. }
  379. }