Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

VFlash.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 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. private int slotOffsetHeight = -1;
  35. private int slotOffsetWidth = -1;
  36. public VFlash() {
  37. setStyleName(CLASSNAME);
  38. }
  39. public void setSource(String source) {
  40. if (this.source != source) {
  41. this.source = source;
  42. needsRebuild = true;
  43. }
  44. }
  45. public void setAlternateText(String altText) {
  46. if (this.altText != altText) {
  47. this.altText = altText;
  48. needsRebuild = true;
  49. }
  50. }
  51. public void setClassId(String classId) {
  52. if (this.classId != classId) {
  53. this.classId = classId;
  54. needsRebuild = true;
  55. }
  56. }
  57. public void setCodebase(String codebase) {
  58. if (this.codebase != codebase) {
  59. this.codebase = codebase;
  60. needsRebuild = true;
  61. }
  62. }
  63. public void setCodetype(String codetype) {
  64. if (this.codetype != codetype) {
  65. this.codetype = codetype;
  66. needsRebuild = true;
  67. }
  68. }
  69. public void setStandby(String standby) {
  70. if (this.standby != standby) {
  71. this.standby = standby;
  72. needsRebuild = true;
  73. }
  74. }
  75. public void setArchive(String archive) {
  76. if (this.archive != archive) {
  77. this.archive = archive;
  78. needsRebuild = true;
  79. }
  80. }
  81. /**
  82. * Call this after changing values of widget. It will rebuild embedding
  83. * structure if needed.
  84. */
  85. public void rebuildIfNeeded() {
  86. if (needsRebuild) {
  87. needsRebuild = false;
  88. this.setHTML(createFlashEmbed());
  89. }
  90. }
  91. @Override
  92. public void setWidth(String width) {
  93. // explicitly not calling super here
  94. if (this.width != width) {
  95. this.width = width;
  96. needsRebuild = true;
  97. }
  98. }
  99. @Override
  100. public void setHeight(String height) {
  101. // explicitly not calling super here
  102. if (this.height != height) {
  103. this.height = height;
  104. needsRebuild = true;
  105. }
  106. }
  107. public void setEmbedParams(Map<String, String> params) {
  108. if (params == null) {
  109. if (!embedParams.isEmpty()) {
  110. embedParams.clear();
  111. needsRebuild = true;
  112. }
  113. return;
  114. }
  115. if (!embedParams.equals(params)) {
  116. embedParams = new HashMap<>(params);
  117. needsRebuild = true;
  118. }
  119. }
  120. /**
  121. * Set dimensions of the containing layout slot so that the size of the
  122. * embed object can be calculated from percentages if needed.
  123. *
  124. * Triggers embed resizing if percentage sizes are in use.
  125. *
  126. * @since 7.7.8
  127. * @param slotOffsetHeight
  128. * offset height of the layout slot
  129. * @param slotOffsetWidth
  130. * offset width of the layout slot
  131. */
  132. public void setSlotHeightAndWidth(int slotOffsetHeight,
  133. int slotOffsetWidth) {
  134. this.slotOffsetHeight = slotOffsetHeight;
  135. this.slotOffsetWidth = slotOffsetWidth;
  136. if (hasPercentageHeight() || hasPercentageWidth()) {
  137. resizeEmbedElement();
  138. }
  139. }
  140. protected String createFlashEmbed() {
  141. /*
  142. * To ensure cross-browser compatibility we are using the twice-cooked
  143. * method to embed flash i.e. we add a OBJECT tag for IE ActiveX and
  144. * inside it a EMBED for all other browsers.
  145. */
  146. StringBuilder html = new StringBuilder();
  147. // Start the object tag
  148. html.append("<object ");
  149. /*
  150. * Add classid required for ActiveX to recognize the flash. This is a
  151. * predefined value which ActiveX recognizes and must be the given
  152. * value. More info can be found on
  153. * http://kb2.adobe.com/cps/415/tn_4150.html. Allow user to override
  154. * this by setting his own classid.
  155. */
  156. if (classId != null) {
  157. html.append(
  158. "classid=\"" + WidgetUtil.escapeAttribute(classId) + "\" ");
  159. } else {
  160. html.append(
  161. "classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" ");
  162. }
  163. /*
  164. * Add codebase required for ActiveX and must be exactly this according
  165. * to http://kb2.adobe.com/cps/415/tn_4150.html to work with the above
  166. * given classid. Again, see more info on
  167. * http://kb2.adobe.com/cps/415/tn_4150.html. Limiting Flash version to
  168. * 6.0.0.0 and above. Allow user to override this by setting his own
  169. * codebase
  170. */
  171. if (codebase != null) {
  172. html.append("codebase=\"" + WidgetUtil.escapeAttribute(codebase)
  173. + "\" ");
  174. } else {
  175. html.append(
  176. "codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" ");
  177. }
  178. // Add width and height
  179. html.append("width=\"" + WidgetUtil.escapeAttribute(width) + "\" ");
  180. html.append("height=\"" + WidgetUtil.escapeAttribute(height) + "\" ");
  181. html.append("type=\"application/x-shockwave-flash\" ");
  182. // Codetype
  183. if (codetype != null) {
  184. html.append("codetype=\"" + WidgetUtil.escapeAttribute(codetype)
  185. + "\" ");
  186. }
  187. // Standby
  188. if (standby != null) {
  189. html.append(
  190. "standby=\"" + WidgetUtil.escapeAttribute(standby) + "\" ");
  191. }
  192. // Archive
  193. if (archive != null) {
  194. html.append(
  195. "archive=\"" + WidgetUtil.escapeAttribute(archive) + "\" ");
  196. }
  197. // End object tag
  198. html.append('>');
  199. // Ensure we have an movie parameter
  200. if (embedParams.get("movie") == null) {
  201. embedParams.put("movie", source);
  202. }
  203. // Add parameters to OBJECT
  204. for (String name : embedParams.keySet()) {
  205. html.append("<param ");
  206. html.append("name=\"" + WidgetUtil.escapeAttribute(name) + "\" ");
  207. html.append("value=\""
  208. + WidgetUtil.escapeAttribute(embedParams.get(name))
  209. + "\" ");
  210. html.append("/>");
  211. }
  212. // Build inner EMBED tag
  213. html.append("<embed ");
  214. html.append("src=\"" + WidgetUtil.escapeAttribute(source) + "\" ");
  215. if (hasPercentageWidth() && slotOffsetWidth >= 0) {
  216. html.append("width=\"" + getRelativePixelWidth() + "\" ");
  217. } else {
  218. html.append("width=\"" + WidgetUtil.escapeAttribute(width) + "\" ");
  219. }
  220. if (hasPercentageHeight() && slotOffsetHeight >= 0) {
  221. html.append("height=\"" + getRelativePixelHeight() + "px\" ");
  222. } else {
  223. html.append(
  224. "height=\"" + WidgetUtil.escapeAttribute(height) + "\" ");
  225. }
  226. html.append("type=\"application/x-shockwave-flash\" ");
  227. // Add the parameters to the Embed
  228. for (String name : embedParams.keySet()) {
  229. html.append(WidgetUtil.escapeAttribute(name));
  230. html.append('=');
  231. html.append("\"" + WidgetUtil.escapeAttribute(embedParams.get(name))
  232. + "\"");
  233. }
  234. // End embed tag
  235. html.append("></embed>");
  236. if (altText != null) {
  237. html.append("<noembed>");
  238. html.append(altText);
  239. html.append("</noembed>");
  240. }
  241. // End object tag
  242. html.append("</object>");
  243. return html.toString();
  244. }
  245. private void resizeEmbedElement() {
  246. // find <embed> element
  247. com.google.gwt.dom.client.Element objectElem = getElement()
  248. .getFirstChildElement();
  249. com.google.gwt.dom.client.Element objectChild = objectElem
  250. .getFirstChildElement();
  251. while (!"EMBED".equalsIgnoreCase(objectChild.getTagName())) {
  252. objectChild = objectChild.getNextSiblingElement();
  253. if (objectChild == null) {
  254. return;
  255. }
  256. }
  257. // update height & width from slot offset, if percentage size is given
  258. if (hasPercentageHeight() && slotOffsetHeight >= 0) {
  259. objectChild.setAttribute("height", getRelativePixelHeight());
  260. }
  261. if (hasPercentageWidth() && slotOffsetWidth >= 0) {
  262. objectChild.setAttribute("width", getRelativePixelWidth());
  263. }
  264. }
  265. private String getRelativePixelWidth() {
  266. float relative = WidgetUtil.parseRelativeSize(width);
  267. int widthInPixels = (int) (relative / 100) * slotOffsetWidth;
  268. return widthInPixels + "px";
  269. }
  270. private String getRelativePixelHeight() {
  271. float relative = WidgetUtil.parseRelativeSize(height);
  272. int heightInPixels = (int) (relative / 100) * slotOffsetHeight;
  273. return heightInPixels + "px";
  274. }
  275. private boolean hasPercentageHeight() {
  276. return ((height != null) && (height.indexOf('%') > 0));
  277. }
  278. private boolean hasPercentageWidth() {
  279. return ((width != null) && (width.indexOf('%') > 0));
  280. }
  281. }