001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.markup.html.media;
018
019import java.util.Locale;
020
021import org.apache.wicket.markup.ComponentTag;
022import org.apache.wicket.markup.html.WebMarkupContainer;
023import org.apache.wicket.model.IModel;
024import org.apache.wicket.request.cycle.RequestCycle;
025import org.apache.wicket.request.mapper.parameter.PageParameters;
026import org.apache.wicket.request.resource.ResourceReference;
027
028/**
029 * The track tag is used to provide subtitles, captions, descriptions, chapters, metadata to a video
030 * media component
031 *
032 * @author Tobias Soloschenko
033 * @since 7.0.0
034 */
035public class Track extends WebMarkupContainer
036{
037        private static final long serialVersionUID = 1L;
038
039        /**
040         * To be used for the kind attribute
041         */
042        public enum Kind {
043                /**
044                 * the track is used for subtitles
045                 */
046                SUBTITLES("subtitles"),
047                /**
048                 * the track is used for captions
049                 */
050                CAPTIONS("captions"),
051                /**
052                 * the track is used for descriptions
053                 */
054                DESCRIPTIONS("descriptions"),
055                /**
056                 * the track is used for chapters
057                 */
058                CHAPTERS("chapters"),
059                /**
060                 * the track is used to provide metadata
061                 */
062                METADATA("metadata");
063
064                private String realName;
065
066                Kind(String realName)
067                {
068                        this.realName = realName;
069                }
070
071                /**
072                 * The real name of the kind
073                 * 
074                 * @return the real name
075                 */
076                public String getRealName()
077                {
078                        return realName;
079                }
080        }
081
082        private Kind kind;
083
084        private String label;
085
086        private boolean defaultTrack;
087
088        private Locale srclang;
089
090        private final ResourceReference resourceReference;
091
092        private final String url;
093
094        private PageParameters pageParameters;
095
096        /**
097         * Creates a track
098         * 
099         * @param id
100         *            the component id
101         */
102        public Track(String id)
103        {
104                this(id, null, null, null, null);
105        }
106
107        /**
108         * Creates a track
109         * 
110         * @param id
111         *            the component id
112         * @param model
113         *            the internally used model
114         */
115        public Track(String id, IModel<?> model)
116        {
117                this(id, model, null, null, null);
118        }
119
120        /**
121         * Creates a track
122         * 
123         * @param id
124         *            the component id
125         * @param resourceReference
126         *            the resource reference to provide track information - like .vtt
127         */
128        public Track(String id, ResourceReference resourceReference)
129        {
130                this(id, null, null, null, resourceReference);
131        }
132
133        /**
134         * Creates a track
135         * 
136         * @param id
137         *            the component id
138         * @param model
139         *            the internally used model
140         * @param resourceReference
141         *            the resource reference to provide track information - like .vtt
142         */
143        public Track(String id, IModel<?> model, ResourceReference resourceReference)
144        {
145                this(id, model, null, null, resourceReference);
146        }
147
148        /**
149         * Creates a track
150         * 
151         * @param id
152         *            the component id
153         * @param resourceReference
154         *            the resource reference to provide track information - like .vtt
155         * @param pageParameters
156         *            the page parameters applied to the track URL
157         */
158        public Track(String id, ResourceReference resourceReference, PageParameters pageParameters)
159        {
160                this(id, null, null, pageParameters, resourceReference);
161        }
162
163        /**
164         * Creates a track
165         * 
166         * @param id
167         *            the component id
168         * @param model
169         *            the internally used model
170         * @param resourceReference
171         *            the resource reference to provide track information - like .vtt
172         * @param pageParameters
173         *            the page parameters applied to the track URL
174         */
175        public Track(String id, IModel<?> model, ResourceReference resourceReference,
176                PageParameters pageParameters)
177        {
178                this(id, model, null, pageParameters, resourceReference);
179        }
180
181        /**
182         * Creates a track
183         * 
184         * @param id
185         *            the component id
186         * @param url
187         *            an external URL to provide the track information
188         */
189        public Track(String id, String url)
190        {
191                this(id, null, url, null, null);
192        }
193
194        /**
195         * Creates a track
196         * 
197         * @param id
198         *            the component id
199         * @param model
200         *            the internally used model
201         * @param url
202         *            an external URL to provide the track information
203         */
204        public Track(String id, IModel<?> model, String url)
205        {
206                this(id, model, url, null, null);
207        }
208
209        private Track(String id, IModel<?> model, String url, PageParameters pageParameters,
210                ResourceReference resourceReference)
211        {
212                super(id, model);
213                this.url = url;
214                this.pageParameters = pageParameters;
215                this.resourceReference = resourceReference;
216        }
217
218        @Override
219        protected void onComponentTag(ComponentTag tag)
220        {
221                checkComponentTag(tag, "track");
222                super.onComponentTag(tag);
223
224                if (resourceReference != null)
225                {
226                        tag.put("src", RequestCycle.get().urlFor(resourceReference, pageParameters));
227                }
228                else if (url != null)
229                {
230                        tag.put("src", url);
231                }
232
233                Kind _kind = getKind();
234                if (_kind != null)
235                {
236                        tag.put("kind", _kind.getRealName());
237                }
238
239                String _label = getLabel();
240                if (_label != null)
241                {
242                        tag.put("label", _label);
243                }
244
245                if (defaultTrack)
246                {
247                        tag.put("default", "default");
248                }
249
250                // if the srclang field is set use this, else if the
251                // resource reference provides a locale use the language
252                // of the resource reference
253                Locale _srclang = getSrclang();
254                if (_srclang != null)
255                {
256                        tag.put("srclang", _srclang.getLanguage());
257                }
258                else if (resourceReference != null && resourceReference.getLocale() != null)
259                {
260                        tag.put("srclang", resourceReference.getLocale().getLanguage());
261                }
262        }
263
264        /**
265         * Gets the kind of the track belongs to the media component
266         *
267         * @see #setKind(Kind)
268         *
269         * @return the kind
270         */
271        public Kind getKind()
272        {
273                return kind;
274        }
275
276        /**
277         * Sets the kind of the track belongs to the media component<br>
278         * <br>
279         * <b>SUBTITLES</b>: Transcription or translation of the dialogue, suitable for when the sound
280         * is available but not understood (e.g. because the user does not understand the language of
281         * the media resource's soundtrack). Displayed over the video.<br>
282         * <br>
283         * <b>CAPTIONS</b>: Transcription or translation of the dialogue, sound effects, relevant
284         * musical cues, and other relevant audio information, suitable for when the soundtrack is
285         * unavailable (e.g. because it is muted or because the user is deaf). Displayed over the video;
286         * labeled as appropriate for the hard-of-hearing.<br>
287         * <br>
288         * <b>DESCRIPTIONS</b>: Textual descriptions of the video component of the media resource,
289         * intended for audio synthesis when the visual component is unavailable (e.g. because the user
290         * is interacting with the application without a screen while driving, or because the user is
291         * blind). Synthesized as separate audio track.<br>
292         * <br>
293         * <b>CHAPTERS</b>: Chapter titles, intended to be used for navigating the media resource.
294         * Displayed as an interactive list in the user agent's interface.<br>
295         * <br>
296         * <b>METADATA</b>: Tracks intended for use from script. Not displayed by the user agent.<br>
297         * <br>
298         *
299         * @param kind
300         *            the kind
301         */
302        public void setKind(Kind kind)
303        {
304                this.kind = kind;
305        }
306
307        /**
308         * The label for this track
309         *
310         * @return the label
311         */
312        public String getLabel()
313        {
314                return label;
315        }
316
317        /**
318         * Sets the label for this track
319         *
320         * @param label
321         *            the label to be set
322         */
323        public void setLabel(String label)
324        {
325                this.label = label;
326        }
327
328        /**
329         * If the track is the default track
330         *
331         * @return if the track is the default track
332         */
333        public boolean isDefaultTrack()
334        {
335                return defaultTrack;
336        }
337
338        /**
339         * Sets if this track is the default track
340         *
341         * @param defaultTrack
342         *            if the track is the default track
343         */
344        public void setDefaultTrack(Boolean defaultTrack)
345        {
346                this.defaultTrack = defaultTrack;
347        }
348
349        /**
350         * Gets the src lang
351         *
352         * @return the src lang
353         */
354        public Locale getSrclang()
355        {
356                return srclang;
357        }
358
359        /**
360         * Sets the src lang
361         *
362         * @param srclang
363         *            the src lang to set
364         */
365        public void setSrclang(Locale srclang)
366        {
367                this.srclang = srclang;
368        }
369
370        /**
371         * Gets the page parameter applied to the URL of the track
372         * 
373         * @return the page parameter applied to the URL of the track
374         */
375        public PageParameters getPageParameters()
376        {
377                return pageParameters;
378        }
379
380        /**
381         * Sets the page parameter applied to the URL of the track
382         * 
383         * @param pageParameters
384         *            the page parameter which are going to be applied to the URL of the track
385         */
386        public void setPageParameters(PageParameters pageParameters)
387        {
388                this.pageParameters = pageParameters;
389        }
390}