]>
git.argeo.org Git - gpl/argeo-suite.git/blob - org.argeo.app.ui/src/org/argeo/app/ui/docbook/DbkVideo.java
1 package org
.argeo
.app
.ui
.docbook
;
4 import java
.net
.URISyntaxException
;
10 import javax
.jcr
.RepositoryException
;
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
;
34 public class DbkVideo
extends StyledControl
implements SectionPart
, NodePart
{
35 private static final long serialVersionUID
= -8753232181570351880L;
36 private Section section
;
38 private int width
= 640;
39 private int height
= 360;
41 private boolean editable
;
43 public DbkVideo(Composite parent
, int style
, Node node
) {
44 this(Section
.findSection(parent
), parent
, style
, node
);
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());
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());
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
);
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
);
76 text
.setText(fileref
);
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() {
85 public void widgetSelected(SelectionEvent e
) {
87 Node videodata
= mediaobject
.getNode(DbkType
.videoobject
.get())
88 .getNode(DbkType
.videodata
.get());
89 String txt
= text
.getText();
93 } catch (URISyntaxException e1
) {
95 text
.setMessage("Invalid URL");
99 // Transform watch URL in embed
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);
109 } else if ("youtu.be".equals(uri
.getHost())) {
110 videoId
= uri
.getPath().substring(1);
112 if (videoId
!= null) {
114 uri
= new URI("https://www.youtube.com/embed/" + videoId
);
115 text
.setText(uri
.toString());
116 } catch (URISyntaxException e1
) {
117 throw new IllegalStateException(e1
);
123 if ("vimeo.com".equals(uri
.getHost())) {
124 videoId
= uri
.getPath().substring(1);
125 if (videoId
!= null) {
127 uri
= new URI("https://player.vimeo.com/video/" + videoId
);
128 text
.setText(uri
.toString());
129 } catch (URISyntaxException e1
) {
130 throw new IllegalStateException(e1
);
135 videodata
.setProperty(DbkAttr
.fileref
.name(), uri
.toString());
136 // TODO better integrate it in the edition lifecycle
137 videodata
.getSession().save();
139 } catch (RepositoryException e1
) {
140 throw new JcrException("Cannot update " + mediaobject
, e1
);
146 Button deleteB
= new Button(editor
, SWT
.FLAT
);
147 deleteB
.setText("Delete");
148 deleteB
.addSelectionListener(new Selected() {
151 public void widgetSelected(SelectionEvent e
) {
153 mediaobject
.remove();
154 mediaobject
.getSession().save();
156 getSection().getParent().layout(true, true);
157 } catch (RepositoryException e1
) {
158 throw new JcrException("Cannot update " + mediaobject
, e1
);
169 public void load(Control control
) {
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();
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("\" ");
186 browser
.setText(html
.toString());
189 } catch (RepositoryException e
) {
190 throw new JcrException("Cannot retrieve src for video " + getNode(), e
);
195 protected void setContainerLayoutData(Composite composite
) {
196 composite
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.FILL
, true, true));
200 protected void setControlLayoutData(Control control
) {
201 control
.setLayoutData(CmsSwtUtils
.fillAll());
205 public Item
getItem() throws RepositoryException
{
210 public String
getPartId() {
215 public Section
getSection() {
219 public int getWidth() {
223 public int getHeight() {