diff options
Diffstat (limited to 'src/com/vaadin/ui')
-rw-r--r-- | src/com/vaadin/ui/AbstractMedia.java | 217 | ||||
-rw-r--r-- | src/com/vaadin/ui/Audio.java | 48 | ||||
-rw-r--r-- | src/com/vaadin/ui/Video.java | 77 |
3 files changed, 342 insertions, 0 deletions
diff --git a/src/com/vaadin/ui/AbstractMedia.java b/src/com/vaadin/ui/AbstractMedia.java new file mode 100644 index 0000000000..e085f04367 --- /dev/null +++ b/src/com/vaadin/ui/AbstractMedia.java @@ -0,0 +1,217 @@ +package com.vaadin.ui; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import com.vaadin.terminal.PaintException; +import com.vaadin.terminal.PaintTarget; +import com.vaadin.terminal.Resource; +import com.vaadin.terminal.gwt.client.ui.VMediaBase; + +/** + * Abstract base class for the HTML5 media components. + * + * @author Vaadin Ltd + */ +public class AbstractMedia extends AbstractComponent { + + private List<Resource> sources = new ArrayList<Resource>(); + + private boolean showControls; + + private String altText; + + private boolean htmlContentAllowed; + + private boolean autoplay; + + private boolean muted; + + private boolean pause; + + private boolean play; + + /** + * Sets a single media file as the source of the media component. + * + * @param source + */ + public void setSource(Resource source) { + sources.clear(); + addSource(source); + } + + /** + * Adds an alternative media file to the sources list. Which of the sources + * is used is selected by the browser depending on which file formats it + * supports. See <a + * href="http://en.wikipedia.org/wiki/HTML5_video#Table">wikipedia</a> for a + * table of formats supported by different browsers. + * + * @param source + */ + public void addSource(Resource source) { + if (source != null) { + sources.add(source); + requestRepaint(); + } + } + + /** + * Set multiple sources at once. Which of the sources is used is selected by + * the browser depending on which file formats it supports. See <a + * href="http://en.wikipedia.org/wiki/HTML5_video#Table">wikipedia</a> for a + * table of formats supported by different browsers. + * + * @param sources + */ + public void setSources(Resource... sources) { + this.sources.addAll(Arrays.asList(sources)); + requestRepaint(); + } + + /** + * @return The sources pointed to in this media. + */ + public List<Resource> getSources() { + return sources; + } + + /** + * Sets whether or not the browser should show native media controls. + * + * @param showControls + */ + public void setShowControls(boolean showControls) { + this.showControls = showControls; + requestRepaint(); + } + + /** + * @return true if the browser is to show native media controls. + */ + public boolean isShowControls() { + return showControls; + } + + /** + * Sets the alternative text to be displayed if the browser does not support + * HTML5. This text is rendered as HTML if + * {@link #setHtmlContentAllowed(boolean)} is set to true. With HTML + * rendering, this method can also be used to implement fallback to a + * flash-based player, see the <a href= + * "https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox#Using_Flash" + * >Mozilla Developer Network</a> for details. + * + * @param text + */ + public void setAltText(String text) { + altText = text; + requestRepaint(); + } + + /** + * @return The text/html that is displayed when a browser doesn't support + * HTML5. + */ + public String getAltText() { + return altText; + } + + /** + * Set whether the alternative text ({@link #setAltText(String)}) is + * rendered as HTML or not. + * + * @param htmlContentAllowed + */ + public void setHtmlContentAllowed(boolean htmlContentAllowed) { + this.htmlContentAllowed = htmlContentAllowed; + requestRepaint(); + } + + /** + * @return true if the alternative text ({@link #setAltText(String)}) is to + * be rendered as HTML. + */ + public boolean isHtmlContentAllowed() { + return htmlContentAllowed; + } + + /** + * Sets whether the media is to automatically start playback when enough + * data has been loaded. + * + * @param autoplay + */ + public void setAutoplay(boolean autoplay) { + this.autoplay = autoplay; + requestRepaint(); + } + + /** + * @return true if the media is set to automatically start playback. + */ + public boolean isAutoplay() { + return autoplay; + } + + /** + * Set whether to mute the audio or not. + * + * @param muted + */ + public void setMuted(boolean muted) { + this.muted = muted; + requestRepaint(); + } + + /** + * @return true if the audio is muted. + */ + public boolean isMuted() { + return muted; + } + + /** + * Pauses the media. + */ + public void pause() { + pause = true; + requestRepaint(); + } + + /** + * Starts playback of the media. + */ + public void play() { + play = true; + requestRepaint(); + } + + @Override + public void paintContent(PaintTarget target) throws PaintException { + super.paintContent(target); + target.addAttribute(VMediaBase.ATTR_CONTROLS, isShowControls()); + if (getAltText() != null) { + target.addAttribute(VMediaBase.ATTR_ALT_TEXT, getAltText()); + } + target.addAttribute(VMediaBase.ATTR_HTML, isHtmlContentAllowed()); + target.addAttribute(VMediaBase.ATTR_AUTOPLAY, isAutoplay()); + for (Resource r : getSources()) { + target.startTag(VMediaBase.TAG_SOURCE); + target.addAttribute(VMediaBase.ATTR_RESOURCE, r); + target.addAttribute(VMediaBase.ATTR_RESOURCE_TYPE, r.getMIMEType()); + target.endTag(VMediaBase.TAG_SOURCE); + } + target.addAttribute(VMediaBase.ATTR_MUTED, isMuted()); + if (play) { + target.addAttribute(VMediaBase.ATTR_PLAY, true); + play = false; + } + if (pause) { + target.addAttribute(VMediaBase.ATTR_PAUSE, true); + pause = false; + } + } +} diff --git a/src/com/vaadin/ui/Audio.java b/src/com/vaadin/ui/Audio.java new file mode 100644 index 0000000000..c3d7cf713e --- /dev/null +++ b/src/com/vaadin/ui/Audio.java @@ -0,0 +1,48 @@ +package com.vaadin.ui; + +import com.vaadin.terminal.Resource; +import com.vaadin.terminal.gwt.client.ui.VAudio; + +/** + * The Audio component translates into an HTML5 <audio> element and as + * such is only supported in browsers that support HTML5 media markup. Browsers + * that do not support HTML5 display the text or HTML set by calling + * {@link #setAltText(String)}. + * + * A flash-player fallback can be implemented by setting HTML content allowed ( + * {@link #setHtmlContentAllowed(boolean)} and calling + * {@link #setAltText(String)} with the flash player markup. An example of flash + * fallback can be found at the <a href= + * "https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox#Using_Flash" + * >Mozilla Developer Network</a>. + * + * @author Vaadin Ltd + * @since 6.7.0 + */ +@ClientWidget(VAudio.class) +public class Audio extends AbstractMedia { + + public Audio() { + this("", null); + } + + /** + * @param caption + * The caption of the audio component. + */ + public Audio(String caption) { + this(caption, null); + } + + /** + * @param caption + * The caption of the audio component + * @param source + * The audio file to play. + */ + public Audio(String caption, Resource source) { + setCaption(caption); + setSource(source); + setShowControls(true); + } +} diff --git a/src/com/vaadin/ui/Video.java b/src/com/vaadin/ui/Video.java new file mode 100644 index 0000000000..efa7e97317 --- /dev/null +++ b/src/com/vaadin/ui/Video.java @@ -0,0 +1,77 @@ +package com.vaadin.ui; + +import com.vaadin.terminal.PaintException; +import com.vaadin.terminal.PaintTarget; +import com.vaadin.terminal.Resource; +import com.vaadin.terminal.gwt.client.ui.VVideo; + +/** + * The Video component translates into an HTML5 <video> element and as + * such is only supported in browsers that support HTML5 media markup. Browsers + * that do not support HTML5 display the text or HTML set by calling + * {@link #setAltText(String)}. + * + * A flash-player fallback can be implemented by setting HTML content allowed ( + * {@link #setHtmlContentAllowed(boolean)} and calling + * {@link #setAltText(String)} with the flash player markup. An example of flash + * fallback can be found at the <a href= + * "https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox#Using_Flash" + * >Mozilla Developer Network</a>. + * + * @author Vaadin Ltd + * @since 6.7.0 + */ +@ClientWidget(VVideo.class) +public class Video extends AbstractMedia { + + private Resource poster; + + public Video() { + this("", null); + } + + /** + * @param caption + * The caption for this video. + */ + public Video(String caption) { + this(caption, null); + } + + /** + * @param caption + * The caption for this video. + * @param source + * The Resource containing the video to play. + */ + public Video(String caption, Resource source) { + setCaption(caption); + setSource(source); + setShowControls(true); + } + + /** + * Sets the poster image, which is shown in place of the video before the + * user presses play. + * + * @param poster + */ + public void setPoster(Resource poster) { + this.poster = poster; + } + + /** + * @return The poster image. + */ + public Resource getPoster() { + return poster; + } + + @Override + public void paintContent(PaintTarget target) throws PaintException { + super.paintContent(target); + if (getPoster() != null) { + target.addAttribute(VVideo.ATTR_POSTER, getPoster()); + } + } +} |