]> git.argeo.org Git - gpl/argeo-suite.git/blob - swt/org.argeo.app.swt/src/org/argeo/app/swt/docbook/DbkVideo.java
Move vcard to another package
[gpl/argeo-suite.git] / swt / org.argeo.app.swt / src / org / argeo / app / swt / 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 setStyle(DbkType.videoobject.name());
48 setData(node);
49 }
50
51 @Override
52 protected Control createControl(Composite box, String style) {
53 Content mediaobject = getNode();
54 Composite wrapper = new Composite(box, SWT.NONE);
55 wrapper.setLayout(CmsSwtUtils.noSpaceGridLayout());
56
57 Composite browserC = new Composite(wrapper, SWT.NONE);
58 browserC.setLayout(CmsSwtUtils.noSpaceGridLayout());
59 GridData gd = new GridData(SWT.CENTER, SWT.FILL, true, true);
60 gd.widthHint = getWidth();
61 gd.heightHint = getHeight();
62 browserC.setLayoutData(gd);
63 // wrapper.setLayoutData(CmsUiUtils.fillAll());
64 Browser browser = new Browser(browserC, SWT.NONE);
65
66 if (editable) {
67 Composite editor = new Composite(wrapper, SWT.BORDER);
68 editor.setLayout(new GridLayout(3, false));
69 editor.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
70 String fileref = DbkAcrUtils.getMediaFileref(mediaobject);
71 Text text = new Text(editor, SWT.SINGLE);
72 if (fileref != null)
73 text.setText(fileref);
74 else
75 text.setMessage("Embed URL of the video");
76 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
77 Button updateB = new Button(editor, SWT.FLAT);
78 updateB.setText("Update");
79 updateB.addSelectionListener(new Selected() {
80
81 @Override
82 public void widgetSelected(SelectionEvent e) {
83 Content videodata = mediaobject.child(DbkType.videoobject).child(DbkType.videodata);
84 String txt = text.getText();
85 URI uri;
86 try {
87 uri = new URI(txt);
88 } catch (URISyntaxException e1) {
89 text.setText("");
90 text.setMessage("Invalid URL");
91 return;
92 }
93
94 // Transform watch URL in embed
95 // YouTube
96 String videoId = null;
97 if ("www.youtube.com".equals(uri.getHost()) || "youtube.com".equals(uri.getHost())
98 || "youtu.be".equals(uri.getHost())) {
99 if ("www.youtube.com".equals(uri.getHost()) || "youtube.com".equals(uri.getHost())) {
100 if ("/watch".equals(uri.getPath())) {
101 Map<String, List<String>> map = NamingUtils.queryToMap(uri);
102 videoId = map.get("v").get(0);
103 }
104 } else if ("youtu.be".equals(uri.getHost())) {
105 videoId = uri.getPath().substring(1);
106 }
107 if (videoId != null) {
108 try {
109 uri = new URI("https://www.youtube.com/embed/" + videoId);
110 text.setText(uri.toString());
111 } catch (URISyntaxException e1) {
112 throw new IllegalStateException(e1);
113 }
114 }
115 }
116
117 // Vimeo
118 if ("vimeo.com".equals(uri.getHost())) {
119 videoId = uri.getPath().substring(1);
120 if (videoId != null) {
121 try {
122 uri = new URI("https://player.vimeo.com/video/" + videoId);
123 text.setText(uri.toString());
124 } catch (URISyntaxException e1) {
125 throw new IllegalStateException(e1);
126 }
127 }
128 }
129
130 videodata.put(DbkAttr.fileref, uri.toString());
131 // TODO better integrate it in the edition lifecycle
132 // videodata.getSession().save();
133 load(browser);
134
135 }
136 });
137
138 Button deleteB = new Button(editor, SWT.FLAT);
139 deleteB.setText("Delete");
140 deleteB.addSelectionListener(new Selected() {
141
142 @Override
143 public void widgetSelected(SelectionEvent e) {
144 mediaobject.remove();
145 // mediaobject.getSession().save();
146 dispose();
147 getSection().getParent().layout(true, true);
148
149 }
150 });
151 }
152
153 // TODO caption
154 return browser;
155 }
156
157 public void load(Control control) {
158 if (control instanceof Browser) {
159 Browser browser = (Browser) control;
160 // getNode().getSession();
161 String fileref = DbkAcrUtils.getMediaFileref(getContent());
162 if (fileref != null) {
163 // TODO manage self-hosted videos
164 // TODO for YouTube videos, check whether the URL starts with
165 // https://www.youtube.com/embed/ and not https://www.youtube.com/watch?v=
166 StringBuilder html = new StringBuilder();
167 html.append(
168 "<iframe frameborder=\"0\" allow=\"autoplay; fullscreen; picture-in-picture\" allowfullscreen=\"true\"");
169 // TODO make size configurable
170 html.append("width=\"").append(width).append("\" height=\"").append(height).append("\" ");
171 html.append("src=\"").append(fileref).append("\" ");
172 html.append("/>");
173 browser.setText(html.toString());
174 }
175 }
176 }
177
178 @Override
179 protected void setContainerLayoutData(Composite composite) {
180 composite.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true));
181 }
182
183 @Override
184 protected void setControlLayoutData(Control control) {
185 control.setLayoutData(CmsSwtUtils.fillAll());
186 }
187
188 @Override
189 public Content getContent() {
190 return (Content) getData();
191 }
192
193 @Override
194 public String getPartId() {
195 return ((ProvidedContent) getContent()).getSessionLocalId();
196 }
197
198 @Override
199 public SwtSection getSection() {
200 return section;
201 }
202
203 public int getWidth() {
204 return width;
205 }
206
207 public int getHeight() {
208 return height;
209 }
210
211 }