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