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.

VMediaBase.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import com.google.gwt.dom.client.Document;
  6. import com.google.gwt.dom.client.MediaElement;
  7. import com.google.gwt.user.client.Element;
  8. import com.google.gwt.user.client.ui.Widget;
  9. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  10. import com.vaadin.terminal.gwt.client.VPaintableWidget;
  11. import com.vaadin.terminal.gwt.client.UIDL;
  12. import com.vaadin.terminal.gwt.client.Util;
  13. public abstract class VMediaBase extends Widget implements VPaintableWidget {
  14. public static final String ATTR_PAUSE = "pause";
  15. public static final String ATTR_PLAY = "play";
  16. public static final String ATTR_MUTED = "muted";
  17. public static final String ATTR_CONTROLS = "ctrl";
  18. public static final String ATTR_AUTOPLAY = "auto";
  19. public static final String TAG_SOURCE = "src";
  20. public static final String ATTR_RESOURCE = "res";
  21. public static final String ATTR_RESOURCE_TYPE = "type";
  22. public static final String ATTR_HTML = "html";
  23. public static final String ATTR_ALT_TEXT = "alt";
  24. private MediaElement media;
  25. protected ApplicationConnection client;
  26. /**
  27. * Sets the MediaElement that is to receive all commands and properties.
  28. *
  29. * @param element
  30. */
  31. public void setMediaElement(MediaElement element) {
  32. setElement(element);
  33. media = element;
  34. }
  35. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  36. if (client.updateComponent(this, uidl, true)) {
  37. return;
  38. }
  39. this.client = client;
  40. media.setControls(shouldShowControls(uidl));
  41. media.setAutoplay(shouldAutoplay(uidl));
  42. media.setMuted(isMediaMuted(uidl));
  43. // Add all sources
  44. for (int ix = 0; ix < uidl.getChildCount(); ix++) {
  45. UIDL child = uidl.getChildUIDL(ix);
  46. if (TAG_SOURCE.equals(child.getTag())) {
  47. Element src = Document.get().createElement("source").cast();
  48. src.setAttribute("src", getSourceUrl(child));
  49. src.setAttribute("type", getSourceType(child));
  50. media.appendChild(src);
  51. }
  52. }
  53. setAltText(uidl);
  54. evalPauseCommand(uidl);
  55. evalPlayCommand(uidl);
  56. }
  57. protected boolean shouldShowControls(UIDL uidl) {
  58. return uidl.getBooleanAttribute(ATTR_CONTROLS);
  59. }
  60. private boolean shouldAutoplay(UIDL uidl) {
  61. return uidl.getBooleanAttribute(ATTR_AUTOPLAY);
  62. }
  63. private boolean isMediaMuted(UIDL uidl) {
  64. return uidl.getBooleanAttribute(ATTR_MUTED);
  65. }
  66. /**
  67. * @param uidl
  68. * @return the URL of a resource to be used as a source for the media
  69. */
  70. private String getSourceUrl(UIDL uidl) {
  71. String url = client.translateVaadinUri(uidl
  72. .getStringAttribute(ATTR_RESOURCE));
  73. if (url == null) {
  74. return "";
  75. }
  76. return url;
  77. }
  78. /**
  79. * @param uidl
  80. * @return the mime type of the media
  81. */
  82. private String getSourceType(UIDL uidl) {
  83. return uidl.getStringAttribute(ATTR_RESOURCE_TYPE);
  84. }
  85. private void setAltText(UIDL uidl) {
  86. String alt = uidl.getStringAttribute(ATTR_ALT_TEXT);
  87. if (alt == null || "".equals(alt)) {
  88. alt = getDefaultAltHtml();
  89. } else if (!allowHtmlContent(uidl)) {
  90. alt = Util.escapeHTML(alt);
  91. }
  92. media.appendChild(Document.get().createTextNode(alt));
  93. }
  94. private boolean allowHtmlContent(UIDL uidl) {
  95. return uidl.getBooleanAttribute(ATTR_HTML);
  96. }
  97. private void evalPlayCommand(UIDL uidl) {
  98. if (uidl.hasAttribute(ATTR_PLAY)) {
  99. media.play();
  100. }
  101. }
  102. private void evalPauseCommand(UIDL uidl) {
  103. if (uidl.hasAttribute(ATTR_PAUSE)) {
  104. media.pause();
  105. }
  106. }
  107. /**
  108. * @return the default HTML to show users with browsers that do not support
  109. * HTML5 media markup.
  110. */
  111. protected abstract String getDefaultAltHtml();
  112. }