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.

AbstractMedia.java 7.4KB

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