-
Notifications
You must be signed in to change notification settings - Fork 0
/
Media.java
168 lines (150 loc) · 4.43 KB
/
Media.java
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
package com.storedobject.vaadin;
import com.storedobject.helper.ID;
import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.DetachEvent;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.server.*;
import java.util.ArrayList;
/**
* Base class for other media classes such as {@link Audio}, {@link Video} etc. Memory leak is prevented from all the
* {@link StreamResource} set as sources.
*
* @author Syam
*/
public abstract class Media extends Component implements HasSize {
private final ArrayList<StreamResource> resources = new ArrayList<>();
private final ArrayList<StreamRegistration> registrations = new ArrayList<>();
private final ArrayList<Element> uriSources = new ArrayList<>();
/**
* Constructor.
*/
public Media() {
ID.set(this);
showControls(true);
}
/**
* Consructor.
* @param resources Sources to be set
*/
public Media(StreamResource... resources) {
ID.set(this);
addSource(resources);
showControls(true);
}
/**
* Constructor.
* @param uri URI source
* @param type Content type of the media
*/
public Media(String uri, String type) {
ID.set(this);
addSource(uri, type);
showControls(true);
}
private void reload() {
String id = getId().orElse(null);
if(id != null) {
UI ui = getUI().orElse(null);
if(ui == null) {
ui = UI.getCurrent();
}
if (ui != null) {
ui.getPage().executeJs("document.getElementById('" + id + "').load();");
}
}
}
/**
* Set sources.
* @param resources Sources to be set
*/
public void setSource(StreamResource... resources) {
clear();
addSource(resources);
}
/**
* Add sources.
* @param resources Sources to be added
*/
public void addSource(StreamResource... resources) {
registerAll();
for(StreamResource sr: resources) {
if(sr == null) {
continue;
}
this.resources.add(sr);
register(sr);
}
reload();
}
/**
* Set a URI source.
* @param uri URI source to be set
* @param type Content type of the media
*/
public void setSource(String uri, String type) {
clear();
addSource(uri, type);
}
/**
* Add a URI source.
* @param uri URI source to be added
* @param type Content type of the media
*/
public void addSource(String uri, String type) {
registerAll();
Element s = new Element("source");
s.setAttribute("src", uri);
s.setAttribute("type", type);
getElement().appendChild(s);
uriSources.add(s);
reload();
}
/**
* Clear the current sources.
*/
public void clear() {
registrations.forEach(StreamRegistration::unregister);
registrations.clear();
resources.clear();
getElement().removeAllChildren();
getElement().setText("Media not supported on this browser!");
}
@Override
protected void onDetach(DetachEvent detachEvent) {
super.onDetach(detachEvent);
registrations.forEach(StreamRegistration::unregister);
registrations.clear();
}
@Override
protected void onAttach(AttachEvent attachEvent) {
super.onAttach(attachEvent);
registerAll();
}
private void registerAll() {
if(!registrations.isEmpty()) {
return;
}
resources.forEach(this::register);
uriSources.forEach(ur -> getElement().appendChild(ur));
}
private void register(StreamResource sr) {
if(sr == null) {
return;
}
StreamRegistration r = VaadinSession.getCurrent().getResourceRegistry().registerResource(sr);
registrations.add(r);
Element s = new Element("source");
s.setAttribute("src", StreamResourceRegistry.getURI(sr).toASCIIString());
s.setAttribute("type", sr.getContentTypeResolver().apply(sr, VaadinServlet.getCurrent().getServletContext()));
getElement().appendChild(s);
}
/**
* Show controls.
* @param on Whether to show or not
*/
public void showControls(boolean on) {
getElement().setAttribute("controls", on);
}
}