aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/terminal/gwt/client/ui/VMediaBase.java
blob: 53638917b2d38e8163cbad1604a817bb2b7807b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
@VaadinApache2LicenseForJavaFiles@
 */

package com.vaadin.terminal.gwt.client.ui;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.MediaElement;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Widget;
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;

public abstract class VMediaBase extends Widget implements Paintable {
    public static final String ATTR_PAUSE = "pause";
    public static final String ATTR_PLAY = "play";
    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 TAG_SOURCE = "src";
    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";

    private MediaElement media;
    protected ApplicationConnection client;

    /**
     * Sets the MediaElement that is to receive all commands and properties.
     * 
     * @param element
     */
    public void setMediaElement(MediaElement element) {
        setElement(element);
        media = element;
    }

    public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
        if (client.updateComponent(this, uidl, true)) {
            return;
        }

        this.client = client;

        media.setControls(shouldShowControls(uidl));
        media.setAutoplay(shouldAutoplay(uidl));
        media.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())) {
                Element src = Document.get().createElement("source").cast();
                src.setAttribute("src", getSourceUrl(child));
                src.setAttribute("type", getSourceType(child));
                media.appendChild(src);
            }
        }
        setAltText(uidl);

        evalPauseCommand(uidl);
        evalPlayCommand(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);
    }

    /**
     * @param uidl
     * @return the URL of a resource to be used as a source for the media
     */
    private String getSourceUrl(UIDL uidl) {
        String url = client.translateVaadinUri(uidl
                .getStringAttribute(ATTR_RESOURCE));
        if (url == null) {
            return "";
        }
        return url;
    }

    /**
     * @param uidl
     * @return the mime type of the media
     */
    private String getSourceType(UIDL uidl) {
        return uidl.getStringAttribute(ATTR_RESOURCE_TYPE);
    }

    private void setAltText(UIDL uidl) {
        String alt = uidl.getStringAttribute(ATTR_ALT_TEXT);

        if (alt == null || "".equals(alt)) {
            alt = getDefaultAltHtml();
        } else if (!allowHtmlContent(uidl)) {
            alt = Util.escapeHTML(alt);
        }
        media.appendChild(Document.get().createTextNode(alt));
    }

    private boolean allowHtmlContent(UIDL uidl) {
        return uidl.getBooleanAttribute(ATTR_HTML);
    }

    private void evalPlayCommand(UIDL uidl) {
        if (uidl.hasAttribute(ATTR_PLAY)) {
            media.play();
        }
    }

    private void evalPauseCommand(UIDL uidl) {
        if (uidl.hasAttribute(ATTR_PAUSE)) {
            media.pause();
        }
    }

    /**
     * @return the default HTML to show users with browsers that do not support
     *         HTML5 media markup.
     */
    protected abstract String getDefaultAltHtml();
}