]>
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
.spi
.ProvidedContent
;
10 import org
.argeo
.app
.docbook
.DbkAcrUtils
;
11 import org
.argeo
.app
.docbook
.DbkAttr
;
12 import org
.argeo
.app
.docbook
.DbkType
;
13 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
14 import org
.argeo
.cms
.swt
.Selected
;
15 import org
.argeo
.cms
.swt
.acr
.SwtSection
;
16 import org
.argeo
.cms
.swt
.acr
.SwtSectionPart
;
17 import org
.argeo
.cms
.swt
.widgets
.StyledControl
;
18 import org
.argeo
.cms
.ux
.acr
.ContentPart
;
19 import org
.argeo
.util
.naming
.NamingUtils
;
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 setStyle(DbkType
.videoobject
.name());
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());
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
);
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
);
73 text
.setText(fileref
);
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() {
82 public void widgetSelected(SelectionEvent e
) {
83 Content videodata
= mediaobject
.child(DbkType
.videoobject
).child(DbkType
.videodata
);
84 String txt
= text
.getText();
88 } catch (URISyntaxException e1
) {
90 text
.setMessage("Invalid URL");
94 // Transform watch URL in embed
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);
104 } else if ("youtu.be".equals(uri
.getHost())) {
105 videoId
= uri
.getPath().substring(1);
107 if (videoId
!= null) {
109 uri
= new URI("https://www.youtube.com/embed/" + videoId
);
110 text
.setText(uri
.toString());
111 } catch (URISyntaxException e1
) {
112 throw new IllegalStateException(e1
);
118 if ("vimeo.com".equals(uri
.getHost())) {
119 videoId
= uri
.getPath().substring(1);
120 if (videoId
!= null) {
122 uri
= new URI("https://player.vimeo.com/video/" + videoId
);
123 text
.setText(uri
.toString());
124 } catch (URISyntaxException e1
) {
125 throw new IllegalStateException(e1
);
130 videodata
.put(DbkAttr
.fileref
, uri
.toString());
131 // TODO better integrate it in the edition lifecycle
132 // videodata.getSession().save();
138 Button deleteB
= new Button(editor
, SWT
.FLAT
);
139 deleteB
.setText("Delete");
140 deleteB
.addSelectionListener(new Selected() {
143 public void widgetSelected(SelectionEvent e
) {
144 mediaobject
.remove();
145 // mediaobject.getSession().save();
147 getSection().getParent().layout(true, true);
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();
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("\" ");
173 browser
.setText(html
.toString());
179 protected void setContainerLayoutData(Composite composite
) {
180 composite
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.FILL
, true, true));
184 protected void setControlLayoutData(Control control
) {
185 control
.setLayoutData(CmsSwtUtils
.fillAll());
189 public Content
getContent() {
190 return (Content
) getData();
194 public String
getPartId() {
195 return ((ProvidedContent
) getContent()).getSessionLocalId();
199 public SwtSection
getSection() {
203 public int getWidth() {
207 public int getHeight() {