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.

Video.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.ui;
  17. import java.util.Collection;
  18. import org.jsoup.nodes.Attributes;
  19. import org.jsoup.nodes.Element;
  20. import org.jsoup.select.Elements;
  21. import com.vaadin.server.Resource;
  22. import com.vaadin.shared.ui.video.VideoConstants;
  23. import com.vaadin.shared.ui.video.VideoState;
  24. import com.vaadin.ui.declarative.DesignAttributeHandler;
  25. import com.vaadin.ui.declarative.DesignContext;
  26. /**
  27. * The Video component translates into an HTML5 <video> element and as
  28. * such is only supported in browsers that support HTML5 media markup. Browsers
  29. * that do not support HTML5 display the text or HTML set by calling
  30. * {@link #setAltText(String)}.
  31. *
  32. * A flash-player fallback can be implemented by setting HTML content allowed (
  33. * {@link #setHtmlContentAllowed(boolean)} and calling
  34. * {@link #setAltText(String)} with the flash player markup. An example of flash
  35. * fallback can be found at the <a href=
  36. * "https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox#Using_Flash"
  37. * >Mozilla Developer Network</a>.
  38. *
  39. * Multiple sources can be specified. Which of the sources is used is selected
  40. * by the browser depending on which file formats it supports. See
  41. * <a href="http://en.wikipedia.org/wiki/HTML5_video#Table">wikipedia</a> for a
  42. * table of formats supported by different browsers.
  43. *
  44. * @author Vaadin Ltd
  45. * @since 6.7.0
  46. */
  47. public class Video extends AbstractMedia {
  48. public Video() {
  49. this("", null);
  50. }
  51. /**
  52. * @param caption
  53. * The caption for this video.
  54. */
  55. public Video(String caption) {
  56. this(caption, null);
  57. }
  58. /**
  59. * @param caption
  60. * The caption for this video.
  61. * @param source
  62. * The Resource containing the video to play.
  63. */
  64. public Video(String caption, Resource source) {
  65. setCaption(caption);
  66. setSource(source);
  67. setShowControls(true);
  68. }
  69. /**
  70. * Sets the poster image, which is shown in place of the video before the
  71. * user presses play.
  72. *
  73. * @param poster
  74. */
  75. public void setPoster(Resource poster) {
  76. setResource(VideoConstants.POSTER_RESOURCE, poster);
  77. }
  78. /**
  79. * @return The poster image.
  80. */
  81. public Resource getPoster() {
  82. return getResource(VideoConstants.POSTER_RESOURCE);
  83. }
  84. @Override
  85. public void readDesign(Element design, DesignContext designContext) {
  86. Elements elems = design.getElementsByTag("poster");
  87. for (Element poster : elems) {
  88. if (getPoster() == null && poster.hasAttr("href")) {
  89. setPoster(DesignAttributeHandler.readAttribute("href",
  90. poster.attributes(), Resource.class));
  91. }
  92. poster.remove();
  93. }
  94. // Poster is extracted so AbstractMedia does not include it in alt text
  95. super.readDesign(design, designContext);
  96. }
  97. @Override
  98. public void writeDesign(Element design, DesignContext designContext) {
  99. super.writeDesign(design, designContext);
  100. if (getPoster() != null) {
  101. Attributes attr = design.appendElement("poster").attributes();
  102. DesignAttributeHandler.writeAttribute("href", attr, getPoster(),
  103. null, Resource.class, designContext);
  104. }
  105. }
  106. @Override
  107. protected Collection<String> getCustomAttributes() {
  108. Collection<String> result = super.getCustomAttributes();
  109. result.add("poster");
  110. return result;
  111. }
  112. @Override
  113. protected VideoState getState() {
  114. return (VideoState) super.getState();
  115. }
  116. @Override
  117. protected VideoState getState(boolean markAsDirty) {
  118. return (VideoState) super.getState(markAsDirty);
  119. }
  120. }