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.

AbstractMedia.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.ui;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.List;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import java.util.regex.Matcher;
  24. import java.util.regex.Pattern;
  25. import org.jsoup.nodes.Attributes;
  26. import org.jsoup.nodes.Element;
  27. import org.jsoup.nodes.Node;
  28. import com.vaadin.server.ConnectorResource;
  29. import com.vaadin.server.DownloadStream;
  30. import com.vaadin.server.Resource;
  31. import com.vaadin.server.ResourceReference;
  32. import com.vaadin.server.VaadinRequest;
  33. import com.vaadin.server.VaadinResponse;
  34. import com.vaadin.server.VaadinSession;
  35. import com.vaadin.shared.communication.URLReference;
  36. import com.vaadin.shared.ui.AbstractMediaState;
  37. import com.vaadin.shared.ui.MediaControl;
  38. import com.vaadin.ui.declarative.DesignAttributeHandler;
  39. import com.vaadin.ui.declarative.DesignContext;
  40. /**
  41. * Abstract base class for the HTML5 media components.
  42. *
  43. * @author Vaadin Ltd
  44. */
  45. public abstract class AbstractMedia extends AbstractComponent {
  46. @Override
  47. protected AbstractMediaState getState() {
  48. return (AbstractMediaState) super.getState();
  49. }
  50. @Override
  51. protected AbstractMediaState getState(boolean markAsDirty) {
  52. return (AbstractMediaState) super.getState(markAsDirty);
  53. }
  54. /**
  55. * Sets a single media file as the source of the media component.
  56. *
  57. * @param source
  58. */
  59. public void setSource(Resource source) {
  60. clearSources();
  61. addSource(source);
  62. }
  63. private void clearSources() {
  64. getState().sources.clear();
  65. getState().sourceTypes.clear();
  66. }
  67. /**
  68. * Adds an alternative media file to the sources list. Which of the sources
  69. * is used is selected by the browser depending on which file formats it
  70. * supports. See
  71. * <a href="http://en.wikipedia.org/wiki/HTML5_video#Table">wikipedia</a>
  72. * for a table of formats supported by different browsers.
  73. *
  74. * @param source
  75. */
  76. public void addSource(Resource source) {
  77. if (source != null) {
  78. List<URLReference> sources = getState().sources;
  79. sources.add(new ResourceReference(source, this,
  80. Integer.toString(sources.size())));
  81. getState().sourceTypes.add(source.getMIMEType());
  82. }
  83. }
  84. @Override
  85. public boolean handleConnectorRequest(VaadinRequest request,
  86. VaadinResponse response, String path) throws IOException {
  87. Matcher matcher = Pattern.compile("(\\d+)(/.*)?").matcher(path);
  88. if (!matcher.matches()) {
  89. return super.handleConnectorRequest(request, response, path);
  90. }
  91. DownloadStream stream;
  92. VaadinSession session = getSession();
  93. session.lock();
  94. try {
  95. List<URLReference> sources = getState().sources;
  96. int sourceIndex = Integer.parseInt(matcher.group(1));
  97. if (sourceIndex < 0 || sourceIndex >= sources.size()) {
  98. getLogger().log(Level.WARNING,
  99. "Requested source index {0} is out of bounds",
  100. sourceIndex);
  101. return false;
  102. }
  103. URLReference reference = sources.get(sourceIndex);
  104. ConnectorResource resource = (ConnectorResource) ResourceReference
  105. .getResource(reference);
  106. stream = resource.getStream();
  107. } finally {
  108. session.unlock();
  109. }
  110. stream.writeResponse(request, response);
  111. return true;
  112. }
  113. private Logger getLogger() {
  114. return Logger.getLogger(AbstractMedia.class.getName());
  115. }
  116. /**
  117. * Set multiple sources at once. Which of the sources is used is selected by
  118. * the browser depending on which file formats it supports. See
  119. * <a href="http://en.wikipedia.org/wiki/HTML5_video#Table">wikipedia</a>
  120. * for a table of formats supported by different browsers.
  121. *
  122. * @param sources
  123. */
  124. public void setSources(Resource... sources) {
  125. clearSources();
  126. for (Resource source : sources) {
  127. addSource(source);
  128. }
  129. }
  130. /**
  131. * @return The sources pointed to in this media.
  132. */
  133. public List<Resource> getSources() {
  134. ArrayList<Resource> sources = new ArrayList<Resource>();
  135. for (URLReference ref : getState(false).sources) {
  136. sources.add(((ResourceReference) ref).getResource());
  137. }
  138. return sources;
  139. }
  140. /**
  141. * Sets whether or not the browser should show native media controls.
  142. *
  143. * @param showControls
  144. */
  145. public void setShowControls(boolean showControls) {
  146. getState().showControls = showControls;
  147. }
  148. /**
  149. * @return true if the browser is to show native media controls.
  150. */
  151. public boolean isShowControls() {
  152. return getState(false).showControls;
  153. }
  154. /**
  155. * Sets the alternative text to be displayed if the browser does not support
  156. * HTML5. This text is rendered as HTML if
  157. * {@link #setHtmlContentAllowed(boolean)} is set to true. With HTML
  158. * rendering, this method can also be used to implement fallback to a
  159. * flash-based player, see the <a href=
  160. * "https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox#Using_Flash"
  161. * >Mozilla Developer Network</a> for details.
  162. *
  163. * @param altText
  164. */
  165. public void setAltText(String altText) {
  166. getState().altText = altText;
  167. }
  168. /**
  169. * @return The text/html that is displayed when a browser doesn't support
  170. * HTML5.
  171. */
  172. public String getAltText() {
  173. return getState(false).altText;
  174. }
  175. /**
  176. * Set whether the alternative text ({@link #setAltText(String)}) is
  177. * rendered as HTML or not.
  178. *
  179. * @param htmlContentAllowed
  180. */
  181. public void setHtmlContentAllowed(boolean htmlContentAllowed) {
  182. getState().htmlContentAllowed = htmlContentAllowed;
  183. }
  184. /**
  185. * @return true if the alternative text ({@link #setAltText(String)}) is to
  186. * be rendered as HTML.
  187. */
  188. public boolean isHtmlContentAllowed() {
  189. return getState(false).htmlContentAllowed;
  190. }
  191. /**
  192. * Sets whether the media is to automatically start playback when enough
  193. * data has been loaded.
  194. *
  195. * @param autoplay
  196. */
  197. public void setAutoplay(boolean autoplay) {
  198. getState().autoplay = autoplay;
  199. }
  200. /**
  201. * @return true if the media is set to automatically start playback.
  202. */
  203. public boolean isAutoplay() {
  204. return getState(false).autoplay;
  205. }
  206. /**
  207. * Set whether to mute the audio or not.
  208. *
  209. * @param muted
  210. */
  211. public void setMuted(boolean muted) {
  212. getState().muted = muted;
  213. }
  214. /**
  215. * @return true if the audio is muted.
  216. */
  217. public boolean isMuted() {
  218. return getState(false).muted;
  219. }
  220. /**
  221. * Pauses the media.
  222. */
  223. public void pause() {
  224. getRpcProxy(MediaControl.class).pause();
  225. }
  226. /**
  227. * Starts playback of the media.
  228. */
  229. public void play() {
  230. getRpcProxy(MediaControl.class).play();
  231. }
  232. @Override
  233. public void writeDesign(Element design, DesignContext designContext) {
  234. super.writeDesign(design, designContext);
  235. String altText = getAltText();
  236. if (altText != null && !altText.isEmpty()) {
  237. design.append(altText);
  238. }
  239. for (Resource r : getSources()) {
  240. Attributes attr = design.appendElement("source").attributes();
  241. DesignAttributeHandler.writeAttribute("href", attr, r, null,
  242. Resource.class);
  243. }
  244. }
  245. @Override
  246. public void readDesign(Element design, DesignContext designContext) {
  247. super.readDesign(design, designContext);
  248. String altText = "";
  249. for (Node child : design.childNodes()) {
  250. if (child instanceof Element
  251. && ((Element) child).tagName().equals("source")
  252. && child.hasAttr("href")) {
  253. addSource(DesignAttributeHandler.readAttribute("href",
  254. child.attributes(), Resource.class));
  255. } else {
  256. altText += child.toString();
  257. }
  258. }
  259. altText = altText.trim();
  260. if (!altText.isEmpty()) {
  261. setAltText(altText);
  262. }
  263. }
  264. @Override
  265. protected Collection<String> getCustomAttributes() {
  266. Collection<String> result = super.getCustomAttributes();
  267. result.add("alt-text");
  268. return result;
  269. }
  270. }