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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright 2011 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.ArrayList;
  18. import java.util.List;
  19. import com.vaadin.server.Resource;
  20. import com.vaadin.server.ResourceReference;
  21. import com.vaadin.shared.communication.URLReference;
  22. import com.vaadin.shared.ui.AbstractMediaState;
  23. import com.vaadin.shared.ui.MediaControl;
  24. /**
  25. * Abstract base class for the HTML5 media components.
  26. *
  27. * @author Vaadin Ltd
  28. */
  29. public abstract class AbstractMedia extends AbstractComponent {
  30. @Override
  31. protected AbstractMediaState getState() {
  32. return (AbstractMediaState) super.getState();
  33. }
  34. /**
  35. * Sets a single media file as the source of the media component.
  36. *
  37. * @param source
  38. */
  39. public void setSource(Resource source) {
  40. clearSources();
  41. addSource(source);
  42. }
  43. private void clearSources() {
  44. getState().getSources().clear();
  45. getState().getSourceTypes().clear();
  46. }
  47. /**
  48. * Adds an alternative media file to the sources list. Which of the sources
  49. * is used is selected by the browser depending on which file formats it
  50. * supports. See <a
  51. * href="http://en.wikipedia.org/wiki/HTML5_video#Table">wikipedia</a> for a
  52. * table of formats supported by different browsers.
  53. *
  54. * @param source
  55. */
  56. public void addSource(Resource source) {
  57. if (source != null) {
  58. getState().getSources().add(new ResourceReference(source));
  59. getState().getSourceTypes().add(source.getMIMEType());
  60. }
  61. }
  62. /**
  63. * Set multiple sources at once. Which of the sources is used is selected by
  64. * the browser depending on which file formats it 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 sources
  69. */
  70. public void setSources(Resource... sources) {
  71. clearSources();
  72. for (Resource source : sources) {
  73. addSource(source);
  74. }
  75. }
  76. /**
  77. * @return The sources pointed to in this media.
  78. */
  79. public List<Resource> getSources() {
  80. ArrayList<Resource> sources = new ArrayList<Resource>();
  81. for (URLReference ref : getState().getSources()) {
  82. sources.add(((ResourceReference) ref).getResource());
  83. }
  84. return sources;
  85. }
  86. /**
  87. * Sets whether or not the browser should show native media controls.
  88. *
  89. * @param showControls
  90. */
  91. public void setShowControls(boolean showControls) {
  92. getState().setShowControls(showControls);
  93. }
  94. /**
  95. * @return true if the browser is to show native media controls.
  96. */
  97. public boolean isShowControls() {
  98. return getState().isShowControls();
  99. }
  100. /**
  101. * Sets the alternative text to be displayed if the browser does not support
  102. * HTML5. This text is rendered as HTML if
  103. * {@link #setHtmlContentAllowed(boolean)} is set to true. With HTML
  104. * rendering, this method can also be used to implement fallback to a
  105. * flash-based player, see the <a href=
  106. * "https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox#Using_Flash"
  107. * >Mozilla Developer Network</a> for details.
  108. *
  109. * @param altText
  110. */
  111. public void setAltText(String altText) {
  112. getState().setAltText(altText);
  113. }
  114. /**
  115. * @return The text/html that is displayed when a browser doesn't support
  116. * HTML5.
  117. */
  118. public String getAltText() {
  119. return getState().getAltText();
  120. }
  121. /**
  122. * Set whether the alternative text ({@link #setAltText(String)}) is
  123. * rendered as HTML or not.
  124. *
  125. * @param htmlContentAllowed
  126. */
  127. public void setHtmlContentAllowed(boolean htmlContentAllowed) {
  128. getState().setHtmlContentAllowed(htmlContentAllowed);
  129. }
  130. /**
  131. * @return true if the alternative text ({@link #setAltText(String)}) is to
  132. * be rendered as HTML.
  133. */
  134. public boolean isHtmlContentAllowed() {
  135. return getState().isHtmlContentAllowed();
  136. }
  137. /**
  138. * Sets whether the media is to automatically start playback when enough
  139. * data has been loaded.
  140. *
  141. * @param autoplay
  142. */
  143. public void setAutoplay(boolean autoplay) {
  144. getState().setAutoplay(autoplay);
  145. }
  146. /**
  147. * @return true if the media is set to automatically start playback.
  148. */
  149. public boolean isAutoplay() {
  150. return getState().isAutoplay();
  151. }
  152. /**
  153. * Set whether to mute the audio or not.
  154. *
  155. * @param muted
  156. */
  157. public void setMuted(boolean muted) {
  158. getState().setMuted(muted);
  159. }
  160. /**
  161. * @return true if the audio is muted.
  162. */
  163. public boolean isMuted() {
  164. return getState().isMuted();
  165. }
  166. /**
  167. * Pauses the media.
  168. */
  169. public void pause() {
  170. getRpcProxy(MediaControl.class).pause();
  171. }
  172. /**
  173. * Starts playback of the media.
  174. */
  175. public void play() {
  176. getRpcProxy(MediaControl.class).play();
  177. }
  178. }