]> git.argeo.org Git - gpl/argeo-suite.git/blob - docbook/DbkVideo.java
Prepare next development cycle
[gpl/argeo-suite.git] / docbook / DbkVideo.java
1 package org.argeo.app.swt.docbook;
2
3 import java.net.URI;
4 import java.net.URISyntaxException;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.argeo.api.acr.Content;
9 import org.argeo.api.acr.ldap.NamingUtils;
10 import org.argeo.api.acr.spi.ProvidedContent;
11 import org.argeo.app.docbook.DbkAcrUtils;
12 import org.argeo.app.docbook.DbkAttr;
13 import org.argeo.app.docbook.DbkType;
14 import org.argeo.cms.swt.CmsSwtUtils;
15 import org.argeo.cms.swt.Selected;
16 import org.argeo.cms.swt.acr.SwtSection;
17 import org.argeo.cms.swt.acr.SwtSectionPart;
18 import org.argeo.cms.swt.widgets.StyledControl;
19 import org.argeo.cms.ux.acr.ContentPart;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.browser.Browser;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Text;
29
30 public class DbkVideo extends StyledControl implements SwtSectionPart, ContentPart {
31 private static final long serialVersionUID = -8753232181570351880L;
32 private SwtSection section;
33
34 private int width = 640;
35 private int height = 360;
36
37 private boolean editable;
38
39 public DbkVideo(Composite parent, int style, Content node) {
40 this(SwtSection.findSection(parent), parent, style, node);
41 }
42
43 DbkVideo(SwtSection section, Composite parent, int style, Content node) {
44 super(parent, style);
45 editable = !(SWT.READ_ONLY == (style & SWT.READ_ONLY));
46 this.section = section;
47 // set data before setting style since it creates the control
48 setData(node);
49 setStyle(DbkType.videoobject.name());
50 }
51
52 @Override
53 protected Control createControl(Composite box, String style) {
54 Content mediaobject = getContent();
55 Composite wrapper = new Composite(box, SWT.NONE);
56 wrapper.setLayout(CmsSwtUtils.noSpaceGridLayout());
57
58 Composite browserC = new Composite(wrapper, SWT.NONE);
59 browserC.setLayout(CmsSwtUtils.noSpaceGridLayout());
60 GridData gd = new GridData(SWT.CENTER, SWT.FILL, true, true);
61 gd.widthHint = getWidth();
62 gd.heightHint = getHeight();
63 browserC.setLayoutData(gd);
64 // wrapper.setLayoutData(CmsUiUtils.fillAll());
65 Browser browser = new Browser(browserC, SWT.NONE);
66
67 if (editable) {
68 Composite editor = new Composite(wrapper, SWT.BORDER);
69 editor.setLayout(new GridLayout(3, false));
70 editor.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
71 String fileref = DbkAcrUtils.getMediaFileref(mediaobject);
72 Text text = new Text(editor, SWT.SINGLE);
73 if (fileref != null)
74 text.setText(fileref);
75 else
76 text.setMessage("Embed URL of the video");
77 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
78 Button updateB = new Button(editor, SWT.FLAT);
79 updateB.setText("Update");
80 updateB.addSelectionListener(new Selected() {
81
82 private static final long serialVersionUID = -8234047511858456222L;
83
84 @Override
85 public void widgetSelected(SelectionEvent e) {
86 Content videodata = mediaobject.child(DbkType.videoobject).child(DbkType.videodata);
87 String txt = text.getText();
88 URI uri;
89 try {
90 uri = new URI(txt);
91 } catch (URISyntaxException e1) {
92 text.setText("");
93 text.setMessage("Invalid URL");
94 return;
95 }
96
97 // Transform watch URL in embed
98 // YouTube
99 String videoId = null;
100 if ("www.youtube.com".equals(uri.getHost()) || "youtube.com".equals(uri.getHost())
101 || "youtu.be".equals(uri.getHost())) {
102 if ("www.youtube.com".equals(uri.getHost()) || "youtube.com".equals(uri.getHost())) {
103 if ("/watch".equals(uri.getPath())) {
104 Map<String, List<String>> map = NamingUtils.queryToMap(uri);
105 videoId = map.get("v").get(0);
106 }
107 } else if ("youtu.be".equals(uri.getHost())) {
108 videoId = uri.getPath().substring(1);
109 }
110 if (videoId != null) {
111 try {
112 uri = new URI("https://www.youtube.com/embed/" + videoId);
113 text.setText(uri.toString());
114 } catch (URISyntaxException e1) {
115 throw new IllegalStateException(e1);
116 }
117 }
118 }
119
120 // Vimeo
121 if ("vimeo.com".equals(uri.getHost())) {
122 videoId = uri.getPath().substring(1);
123 if (videoId != null) {
124 try {
125 uri = new URI("https://player.vimeo.com/video/" + videoId);
126 text.setText(uri.toString());
127 } catch (URISyntaxException e1) {
128 throw new IllegalStateException(e1);
129 }
130 }
131 }
132
133 videodata.put(DbkAttr.fileref, uri.toString());
134 // TODO better integrate it in the edition lifecycle
135 // videodata.getSession().save();
136 load(browser);
137
138 }
139 });
140
141 Button deleteB = new Button(editor, SWT.FLAT);
142 deleteB.setText("Delete");
143 deleteB.addSelectionListener(new Selected() {
144
145 private static final long serialVersionUID = -7552456185687361642L;
146
147 @Override
148 public void widgetSelected(SelectionEvent e) {
149 mediaobject.remove();
150 // mediaobject.getSession().save();
151 dispose();
152 getSection().getParent().layout(true, true);
153
154 }
155 });
156 }
157
158 // TODO caption
159 return browser;
160 }
161
162 public void load(Control control) {
163 if (control instanceof Browser) {
164 Browser browser = (Browser) control;
165 // getNode().getSession();
166 String fileref = DbkAcrUtils.getMediaFileref(getContent());
167 if (fileref != null) {
168 // TODO manage self-hosted videos
169 // TODO for YouTube videos, check whether the URL starts with
170 // https://www.youtube.com/embed/ and not https://www.youtube.com/watch?v=
171 StringBuilder html = new StringBuilder();
172 html.append(
173 "<iframe frameborder=\"0\" allow=\"autoplay; fullscreen; picture-in-picture\" allowfullscreen=\"true\"");
174 // TODO make size configurable
175 html.append("width=\"").append(width).append("\" height=\"").append(height).append("\" ");
176 html.append("src=\"").append(fileref).append("\" ");
177 html.append("/>");
178 browser.setText(html.toString());
179 }
180 }
181 }
182
183 @Override
184 protected void setContainerLayoutData(Composite composite) {
185 composite.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true));
186 }
187
188 @Override
189 protected void setControlLayoutData(Control control) {
190 control.setLayoutData(CmsSwtUtils.fillAll());
191 }
192
193 @Override
194 public Content getContent() {
195 return (Content) getData();
196 }
197
198 @Override
199 public String getPartId() {
200 return ((ProvidedContent) getContent()).getSessionLocalId();
201 }
202
203 @Override
204 public SwtSection getSection() {
205 return section;
206 }
207
208 public int getWidth() {
209 return width;
210 }
211
212 public int getHeight() {
213 return height;
214 }
215
216 }