aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/ui/AbstractMedia.java
blob: 44f4fefcc7aefb356ff504f8e0c50390d3d75344 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
@ITMillApache2LicenseForJavaFiles@
 */

package com.vaadin.ui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
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 Collections.unmodifiableList(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() {
        // cancel any possible play command
        play = false;

        pause = true;
        requestRepaint();
    }

    /**
     * Starts playback of the media.
     */
    public void play() {
        // cancel any possible pause command.
        pause = false;

        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;
        }
    }
}