]>
git.argeo.org Git - gpl/argeo-suite.git/blob - swt/org.argeo.app.swt/src/org/argeo/app/swt/docbook/DbkVideo.java
1 package org
.argeo
.app
.swt
.docbook
;
4 import java
.net
.URISyntaxException
;
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
;
30 public class DbkVideo
extends StyledControl
implements SwtSectionPart
, ContentPart
{
31 private static final long serialVersionUID
= -8753232181570351880L;
32 private SwtSection section
;
34 private int width
= 640;
35 private int height
= 360;
37 private boolean editable
;
39 public DbkVideo(Composite parent
, int style
, Content node
) {
40 this(SwtSection
.findSection(parent
), parent
, style
, node
);
43 DbkVideo(SwtSection section
, Composite parent
, int style
, Content node
) {
45 editable
= !(SWT
.READ_ONLY
== (style
& SWT
.READ_ONLY
));
46 this.section
= section
;
47 // set data before setting style since it creates the control
49 setStyle(DbkType
.videoobject
.name());
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());
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
);
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
);
74 text
.setText(fileref
);
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() {
82 private static final long serialVersionUID
= -8234047511858456222L;
85 public void widgetSelected(SelectionEvent e
) {
86 Content videodata
= mediaobject
.child(DbkType
.videoobject
).child(DbkType
.videodata
);
87 String txt
= text
.getText();
91 } catch (URISyntaxException e1
) {
93 text
.setMessage("Invalid URL");
97 // Transform watch URL in embed
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);
107 } else if ("youtu.be".equals(uri
.getHost())) {
108 videoId
= uri
.getPath().substring(1);
110 if (videoId
!= null) {
112 uri
= new URI("https://www.youtube.com/embed/" + videoId
);
113 text
.setText(uri
.toString());
114 } catch (URISyntaxException e1
) {
115 throw new IllegalStateException(e1
);
121 if ("vimeo.com".equals(uri
.getHost())) {
122 videoId
= uri
.getPath().substring(1);
123 if (videoId
!= null) {
125 uri
= new URI("https://player.vimeo.com/video/" + videoId
);
126 text
.setText(uri
.toString());
127 } catch (URISyntaxException e1
) {
128 throw new IllegalStateException(e1
);
133 videodata
.put(DbkAttr
.fileref
, uri
.toString());
134 // TODO better integrate it in the edition lifecycle
135 // videodata.getSession().save();
141 Button deleteB
= new Button(editor
, SWT
.FLAT
);
142 deleteB
.setText("Delete");
143 deleteB
.addSelectionListener(new Selected() {
145 private static final long serialVersionUID
= -7552456185687361642L;
148 public void widgetSelected(SelectionEvent e
) {
149 mediaobject
.remove();
150 // mediaobject.getSession().save();
152 getSection().getParent().layout(true, true);
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();
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("\" ");
178 browser
.setText(html
.toString());
184 protected void setContainerLayoutData(Composite composite
) {
185 composite
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.FILL
, true, true));
189 protected void setControlLayoutData(Control control
) {
190 control
.setLayoutData(CmsSwtUtils
.fillAll());
194 public Content
getContent() {
195 return (Content
) getData();
199 public String
getPartId() {
200 return ((ProvidedContent
) getContent()).getSessionLocalId();
204 public SwtSection
getSection() {
208 public int getWidth() {
212 public int getHeight() {