diff options
author | Artur Signell <artur@vaadin.com> | 2012-05-03 22:15:40 +0300 |
---|---|---|
committer | Artur Signell <artur@vaadin.com> | 2012-05-11 22:18:25 +0300 |
commit | 5c43a3fc241514e784d010b47266afaf4a0ab1f5 (patch) | |
tree | f2e5dab13668e8e5997adb7bb7af6b2bed091afb /src/com/vaadin | |
parent | 2e6c788c14013ebca8cdea7da9d0ff7e7547df09 (diff) | |
download | vaadin-framework-5c43a3fc241514e784d010b47266afaf4a0ab1f5.tar.gz vaadin-framework-5c43a3fc241514e784d010b47266afaf4a0ab1f5.zip |
Updated Audio and Video to use state
Diffstat (limited to 'src/com/vaadin')
10 files changed, 198 insertions, 183 deletions
diff --git a/src/com/vaadin/terminal/gwt/client/ui/AbstractMediaState.java b/src/com/vaadin/terminal/gwt/client/ui/AbstractMediaState.java new file mode 100644 index 0000000000..0ecb7159d5 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/client/ui/AbstractMediaState.java @@ -0,0 +1,79 @@ +package com.vaadin.terminal.gwt.client.ui; + +import java.util.ArrayList; +import java.util.List; + +import com.vaadin.terminal.gwt.client.ComponentState; +import com.vaadin.terminal.gwt.client.communication.URLReference; + +public class AbstractMediaState extends ComponentState { + private boolean showControls; + + private String altText; + + private boolean htmlContentAllowed; + + private boolean autoplay; + + private boolean muted; + + private List<URLReference> sources = new ArrayList<URLReference>(); + private List<String> sourceTypes = new ArrayList<String>(); + + public boolean isShowControls() { + return showControls; + } + + public void setShowControls(boolean showControls) { + this.showControls = showControls; + } + + public String getAltText() { + return altText; + } + + public void setAltText(String altText) { + this.altText = altText; + } + + public boolean isHtmlContentAllowed() { + return htmlContentAllowed; + } + + public void setHtmlContentAllowed(boolean htmlContentAllowed) { + this.htmlContentAllowed = htmlContentAllowed; + } + + public boolean isAutoplay() { + return autoplay; + } + + public void setAutoplay(boolean autoplay) { + this.autoplay = autoplay; + } + + public boolean isMuted() { + return muted; + } + + public void setMuted(boolean muted) { + this.muted = muted; + } + + public List<URLReference> getSources() { + return sources; + } + + public void setSources(List<URLReference> sources) { + this.sources = sources; + } + + public List<String> getSourceTypes() { + return sourceTypes; + } + + public void setSourceTypes(List<String> sourceTypes) { + this.sourceTypes = sourceTypes; + } + +} diff --git a/src/com/vaadin/terminal/gwt/client/ui/MediaBaseConnector.java b/src/com/vaadin/terminal/gwt/client/ui/MediaBaseConnector.java index 1e067bf6fb..42027f911b 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/MediaBaseConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/MediaBaseConnector.java @@ -3,24 +3,12 @@ */ package com.vaadin.terminal.gwt.client.ui; -import com.vaadin.terminal.gwt.client.ApplicationConnection; -import com.vaadin.terminal.gwt.client.Paintable; -import com.vaadin.terminal.gwt.client.UIDL; import com.vaadin.terminal.gwt.client.Util; import com.vaadin.terminal.gwt.client.communication.ClientRpc; +import com.vaadin.terminal.gwt.client.communication.StateChangeEvent; +import com.vaadin.terminal.gwt.client.communication.URLReference; -public abstract class MediaBaseConnector extends AbstractComponentConnector - implements Paintable { - - public static final String TAG_SOURCE = "src"; - - public static final String ATTR_MUTED = "muted"; - public static final String ATTR_CONTROLS = "ctrl"; - public static final String ATTR_AUTOPLAY = "auto"; - public static final String ATTR_RESOURCE = "res"; - public static final String ATTR_RESOURCE_TYPE = "type"; - public static final String ATTR_HTML = "html"; - public static final String ATTR_ALT_TEXT = "alt"; +public abstract class MediaBaseConnector extends AbstractComponentConnector { /** * Server to client RPC interface for controlling playback of the media. @@ -54,40 +42,24 @@ public abstract class MediaBaseConnector extends AbstractComponentConnector }); } - public void updateFromUIDL(UIDL uidl, ApplicationConnection client) { - if (!isRealUpdate(uidl)) { - return; - } - - getWidget().setControls(shouldShowControls(uidl)); - getWidget().setAutoplay(shouldAutoplay(uidl)); - getWidget().setMuted(isMediaMuted(uidl)); - - // Add all sources - for (int ix = 0; ix < uidl.getChildCount(); ix++) { - UIDL child = uidl.getChildUIDL(ix); - if (TAG_SOURCE.equals(child.getTag())) { - getWidget() - .addSource(getSourceUrl(child), getSourceType(child)); - } - } - setAltText(uidl); - } - - protected boolean shouldShowControls(UIDL uidl) { - return uidl.getBooleanAttribute(ATTR_CONTROLS); - } - - private boolean shouldAutoplay(UIDL uidl) { - return uidl.getBooleanAttribute(ATTR_AUTOPLAY); - } - - private boolean isMediaMuted(UIDL uidl) { - return uidl.getBooleanAttribute(ATTR_MUTED); + @Override + public AbstractMediaState getState() { + return (AbstractMediaState) super.getState(); } - private boolean allowHtmlContent(UIDL uidl) { - return uidl.getBooleanAttribute(ATTR_HTML); + @Override + public void onStateChanged(StateChangeEvent stateChangeEvent) { + super.onStateChanged(stateChangeEvent); + + getWidget().setControls(getState().isShowControls()); + getWidget().setAutoplay(getState().isAutoplay()); + getWidget().setMuted(getState().isMuted()); + for (int i = 0; i < getState().getSources().size(); i++) { + URLReference source = getState().getSources().get(i); + String sourceType = getState().getSourceTypes().get(i); + getWidget().addSource(source.getURL(), sourceType); + } + setAltText(getState().getAltText()); } @Override @@ -95,36 +67,20 @@ public abstract class MediaBaseConnector extends AbstractComponentConnector return (VMediaBase) super.getWidget(); } - /** - * @param uidl - * @return the URL of a resource to be used as a source for the media - */ - private String getSourceUrl(UIDL uidl) { - String url = getConnection().translateVaadinUri( - uidl.getStringAttribute(MediaBaseConnector.ATTR_RESOURCE)); - if (url == null) { - return ""; + private void setAltText(String altText) { + + if (altText == null || "".equals(altText)) { + altText = getDefaultAltHtml(); + } else if (!getState().isHtmlContentAllowed()) { + altText = Util.escapeHTML(altText); } - return url; + getWidget().setAltText(altText); } /** - * @param uidl - * @return the mime type of the media + * @return the default HTML to show users with browsers that do not support + * HTML5 media markup. */ - private String getSourceType(UIDL uidl) { - return uidl.getStringAttribute(MediaBaseConnector.ATTR_RESOURCE_TYPE); - } - - private void setAltText(UIDL uidl) { - String alt = uidl.getStringAttribute(MediaBaseConnector.ATTR_ALT_TEXT); - - if (alt == null || "".equals(alt)) { - alt = getWidget().getDefaultAltHtml(); - } else if (!allowHtmlContent(uidl)) { - alt = Util.escapeHTML(alt); - } - getWidget().setAltText(alt); - } + protected abstract String getDefaultAltHtml(); } diff --git a/src/com/vaadin/terminal/gwt/client/ui/VMediaBase.java b/src/com/vaadin/terminal/gwt/client/ui/VMediaBase.java index 6c5fbc2ef0..40696ccec5 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VMediaBase.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VMediaBase.java @@ -23,12 +23,6 @@ public abstract class VMediaBase extends Widget { media = element; } - /** - * @return the default HTML to show users with browsers that do not support - * HTML5 media markup. - */ - protected abstract String getDefaultAltHtml(); - public void play() { media.play(); } diff --git a/src/com/vaadin/terminal/gwt/client/ui/audio/AudioConnector.java b/src/com/vaadin/terminal/gwt/client/ui/audio/AudioConnector.java index d55e66dbd5..1c7feb132a 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/audio/AudioConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/audio/AudioConnector.java @@ -7,9 +7,8 @@ import com.google.gwt.core.client.GWT; import com.google.gwt.dom.client.Style; import com.google.gwt.dom.client.Style.Unit; import com.google.gwt.user.client.ui.Widget; -import com.vaadin.terminal.gwt.client.ApplicationConnection; import com.vaadin.terminal.gwt.client.BrowserInfo; -import com.vaadin.terminal.gwt.client.UIDL; +import com.vaadin.terminal.gwt.client.communication.StateChangeEvent; import com.vaadin.terminal.gwt.client.ui.Connect; import com.vaadin.terminal.gwt.client.ui.MediaBaseConnector; import com.vaadin.ui.Audio; @@ -18,15 +17,13 @@ import com.vaadin.ui.Audio; public class AudioConnector extends MediaBaseConnector { @Override - public void updateFromUIDL(UIDL uidl, ApplicationConnection client) { - super.updateFromUIDL(uidl, client); - if (!isRealUpdate(uidl)) { - return; - } + public void onStateChanged(StateChangeEvent stateChangeEvent) { + super.onStateChanged(stateChangeEvent); + Style style = getWidget().getElement().getStyle(); // Make sure that the controls are not clipped if visible. - if (shouldShowControls(uidl) + if (getState().isShowControls() && (style.getHeight() == null || "".equals(style.getHeight()))) { if (BrowserInfo.get().isChrome()) { style.setHeight(32, Unit.PX); @@ -41,4 +38,8 @@ public class AudioConnector extends MediaBaseConnector { return GWT.create(VAudio.class); } + @Override + protected String getDefaultAltHtml() { + return "Your browser does not support the <code>audio</code> element."; + } } diff --git a/src/com/vaadin/terminal/gwt/client/ui/audio/VAudio.java b/src/com/vaadin/terminal/gwt/client/ui/audio/VAudio.java index 7d5d1fe034..08bc95ba16 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/audio/VAudio.java +++ b/src/com/vaadin/terminal/gwt/client/ui/audio/VAudio.java @@ -19,9 +19,4 @@ public class VAudio extends VMediaBase { setStyleName(CLASSNAME); } - @Override - protected String getDefaultAltHtml() { - return "Your browser does not support the <code>audio</code> element."; - } - } diff --git a/src/com/vaadin/terminal/gwt/client/ui/video/VVideo.java b/src/com/vaadin/terminal/gwt/client/ui/video/VVideo.java index 484000b8d1..a2a4cd0ce3 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/video/VVideo.java +++ b/src/com/vaadin/terminal/gwt/client/ui/video/VVideo.java @@ -52,11 +52,6 @@ public class VVideo extends VMediaBase { Util.notifyParentOfSizeChange(this, true); } - @Override - protected String getDefaultAltHtml() { - return "Your browser does not support the <code>video</code> element."; - } - public void setPoster(String poster) { video.setPoster(poster); } diff --git a/src/com/vaadin/terminal/gwt/client/ui/video/VideoConnector.java b/src/com/vaadin/terminal/gwt/client/ui/video/VideoConnector.java index ec763fff07..da1631c44d 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/video/VideoConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/video/VideoConnector.java @@ -5,31 +5,28 @@ package com.vaadin.terminal.gwt.client.ui.video; import com.google.gwt.core.client.GWT; import com.google.gwt.user.client.ui.Widget; -import com.vaadin.terminal.gwt.client.ApplicationConnection; -import com.vaadin.terminal.gwt.client.UIDL; +import com.vaadin.terminal.gwt.client.communication.StateChangeEvent; +import com.vaadin.terminal.gwt.client.communication.URLReference; import com.vaadin.terminal.gwt.client.ui.Connect; import com.vaadin.terminal.gwt.client.ui.MediaBaseConnector; import com.vaadin.ui.Video; @Connect(Video.class) public class VideoConnector extends MediaBaseConnector { - public static final String ATTR_POSTER = "poster"; @Override - public void updateFromUIDL(UIDL uidl, ApplicationConnection client) { - super.updateFromUIDL(uidl, client); - if (!isRealUpdate(uidl)) { - return; - } - super.updateFromUIDL(uidl, client); - setPosterFromUIDL(uidl); + public VideoState getState() { + return (VideoState) super.getState(); } - private void setPosterFromUIDL(UIDL uidl) { - if (uidl.hasAttribute(ATTR_POSTER)) { - getWidget().setPoster( - getConnection().translateVaadinUri( - uidl.getStringAttribute(ATTR_POSTER))); + @Override + public void onStateChanged(StateChangeEvent stateChangeEvent) { + super.onStateChanged(stateChangeEvent); + URLReference poster = getState().getPoster(); + if (poster != null) { + getWidget().setPoster(poster.getURL()); + } else { + getWidget().setPoster(null); } } @@ -43,4 +40,9 @@ public class VideoConnector extends MediaBaseConnector { return GWT.create(VVideo.class); } + @Override + protected String getDefaultAltHtml() { + return "Your browser does not support the <code>video</code> element."; + } + } diff --git a/src/com/vaadin/terminal/gwt/client/ui/video/VideoState.java b/src/com/vaadin/terminal/gwt/client/ui/video/VideoState.java new file mode 100644 index 0000000000..ab37c4d352 --- /dev/null +++ b/src/com/vaadin/terminal/gwt/client/ui/video/VideoState.java @@ -0,0 +1,17 @@ +package com.vaadin.terminal.gwt.client.ui.video; + +import com.vaadin.terminal.gwt.client.communication.URLReference; +import com.vaadin.terminal.gwt.client.ui.AbstractMediaState; + +public class VideoState extends AbstractMediaState { + private URLReference poster; + + public URLReference getPoster() { + return poster; + } + + public void setPoster(URLReference poster) { + this.poster = poster; + } + +} diff --git a/src/com/vaadin/ui/AbstractMedia.java b/src/com/vaadin/ui/AbstractMedia.java index 09cfd5ff12..760d9878ca 100644 --- a/src/com/vaadin/ui/AbstractMedia.java +++ b/src/com/vaadin/ui/AbstractMedia.java @@ -5,37 +5,25 @@ package com.vaadin.ui; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; import java.util.List; -import java.util.Map; -import com.vaadin.terminal.PaintException; -import com.vaadin.terminal.PaintTarget; import com.vaadin.terminal.Resource; -import com.vaadin.terminal.Vaadin6Component; -import com.vaadin.terminal.gwt.client.ui.MediaBaseConnector; +import com.vaadin.terminal.gwt.client.communication.URLReference; +import com.vaadin.terminal.gwt.client.ui.AbstractMediaState; import com.vaadin.terminal.gwt.client.ui.MediaBaseConnector.MediaControl; +import com.vaadin.terminal.gwt.server.ResourceReference; /** * Abstract base class for the HTML5 media components. * * @author Vaadin Ltd */ -public class AbstractMedia extends AbstractComponent implements - Vaadin6Component { +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; + @Override + public AbstractMediaState getState() { + return (AbstractMediaState) super.getState(); + } /** * Sets a single media file as the source of the media component. @@ -43,10 +31,16 @@ public class AbstractMedia extends AbstractComponent implements * @param source */ public void setSource(Resource source) { - sources.clear(); + clearSources(); + addSource(source); } + private void clearSources() { + getState().getSources().clear(); + getState().getSourceTypes().clear(); + } + /** * 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 @@ -58,7 +52,8 @@ public class AbstractMedia extends AbstractComponent implements */ public void addSource(Resource source) { if (source != null) { - sources.add(source); + getState().getSources().add(new ResourceReference(source)); + getState().getSourceTypes().add(source.getMIMEType()); requestRepaint(); } } @@ -72,15 +67,21 @@ public class AbstractMedia extends AbstractComponent implements * @param sources */ public void setSources(Resource... sources) { - this.sources.addAll(Arrays.asList(sources)); - requestRepaint(); + clearSources(); + for (Resource source : sources) { + addSource(source); + } } /** * @return The sources pointed to in this media. */ public List<Resource> getSources() { - return Collections.unmodifiableList(sources); + ArrayList<Resource> sources = new ArrayList<Resource>(); + for (URLReference ref : getState().getSources()) { + sources.add(((ResourceReference) ref).getResource()); + } + return sources; } /** @@ -89,7 +90,7 @@ public class AbstractMedia extends AbstractComponent implements * @param showControls */ public void setShowControls(boolean showControls) { - this.showControls = showControls; + getState().setShowControls(showControls); requestRepaint(); } @@ -97,7 +98,7 @@ public class AbstractMedia extends AbstractComponent implements * @return true if the browser is to show native media controls. */ public boolean isShowControls() { - return showControls; + return getState().isShowControls(); } /** @@ -109,10 +110,10 @@ public class AbstractMedia extends AbstractComponent implements * "https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox#Using_Flash" * >Mozilla Developer Network</a> for details. * - * @param text + * @param altText */ - public void setAltText(String text) { - altText = text; + public void setAltText(String altText) { + getState().setAltText(altText); requestRepaint(); } @@ -121,7 +122,7 @@ public class AbstractMedia extends AbstractComponent implements * HTML5. */ public String getAltText() { - return altText; + return getState().getAltText(); } /** @@ -131,7 +132,7 @@ public class AbstractMedia extends AbstractComponent implements * @param htmlContentAllowed */ public void setHtmlContentAllowed(boolean htmlContentAllowed) { - this.htmlContentAllowed = htmlContentAllowed; + getState().setHtmlContentAllowed(htmlContentAllowed); requestRepaint(); } @@ -140,7 +141,7 @@ public class AbstractMedia extends AbstractComponent implements * be rendered as HTML. */ public boolean isHtmlContentAllowed() { - return htmlContentAllowed; + return getState().isHtmlContentAllowed(); } /** @@ -150,7 +151,7 @@ public class AbstractMedia extends AbstractComponent implements * @param autoplay */ public void setAutoplay(boolean autoplay) { - this.autoplay = autoplay; + getState().setAutoplay(autoplay); requestRepaint(); } @@ -158,7 +159,7 @@ public class AbstractMedia extends AbstractComponent implements * @return true if the media is set to automatically start playback. */ public boolean isAutoplay() { - return autoplay; + return getState().isAutoplay(); } /** @@ -167,7 +168,7 @@ public class AbstractMedia extends AbstractComponent implements * @param muted */ public void setMuted(boolean muted) { - this.muted = muted; + getState().setMuted(muted); requestRepaint(); } @@ -175,7 +176,7 @@ public class AbstractMedia extends AbstractComponent implements * @return true if the audio is muted. */ public boolean isMuted() { - return muted; + return getState().isMuted(); } /** @@ -192,25 +193,4 @@ public class AbstractMedia extends AbstractComponent implements getRpcProxy(MediaControl.class).play(); } - public void paintContent(PaintTarget target) throws PaintException { - target.addAttribute(MediaBaseConnector.ATTR_CONTROLS, isShowControls()); - if (getAltText() != null) { - target.addAttribute(MediaBaseConnector.ATTR_ALT_TEXT, getAltText()); - } - target.addAttribute(MediaBaseConnector.ATTR_HTML, - isHtmlContentAllowed()); - target.addAttribute(MediaBaseConnector.ATTR_AUTOPLAY, isAutoplay()); - for (Resource r : getSources()) { - target.startTag(MediaBaseConnector.TAG_SOURCE); - target.addAttribute(MediaBaseConnector.ATTR_RESOURCE, r); - target.addAttribute(MediaBaseConnector.ATTR_RESOURCE_TYPE, - r.getMIMEType()); - target.endTag(MediaBaseConnector.TAG_SOURCE); - } - target.addAttribute(MediaBaseConnector.ATTR_MUTED, isMuted()); - } - - public void changeVariables(Object source, Map<String, Object> variables) { - // TODO Remove once Vaadin6Component is no longer implemented - } } diff --git a/src/com/vaadin/ui/Video.java b/src/com/vaadin/ui/Video.java index 28fbfb0547..0a2eccca0f 100644 --- a/src/com/vaadin/ui/Video.java +++ b/src/com/vaadin/ui/Video.java @@ -4,10 +4,9 @@ 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.video.VideoConnector; +import com.vaadin.terminal.gwt.client.ui.video.VideoState; +import com.vaadin.terminal.gwt.server.ResourceReference; /** * The Video component translates into an HTML5 <video> element and as @@ -32,7 +31,10 @@ import com.vaadin.terminal.gwt.client.ui.video.VideoConnector; */ public class Video extends AbstractMedia { - private Resource poster; + @Override + public VideoState getState() { + return (VideoState) super.getState(); + } public Video() { this("", null); @@ -65,21 +67,15 @@ public class Video extends AbstractMedia { * @param poster */ public void setPoster(Resource poster) { - this.poster = poster; + getState().setPoster(new ResourceReference(poster)); + requestRepaint(); } /** * @return The poster image. */ public Resource getPoster() { - return poster; + return ((ResourceReference) getState().getPoster()).getResource(); } - @Override - public void paintContent(PaintTarget target) throws PaintException { - super.paintContent(target); - if (getPoster() != null) { - target.addAttribute(VideoConnector.ATTR_POSTER, getPoster()); - } - } } |