]> git.argeo.org Git - gpl/argeo-suite.git/blob - library/org.argeo.documents.ui/src/org/argeo/documents/ui/DocumentsFileComposite.java
Support for docbook image object.
[gpl/argeo-suite.git] / library / org.argeo.documents.ui / src / org / argeo / documents / ui / DocumentsFileComposite.java
1 package org.argeo.documents.ui;
2
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6 import java.nio.file.spi.FileSystemProvider;
7
8 import javax.jcr.Node;
9 import javax.jcr.RepositoryException;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.argeo.cms.fs.CmsFsUtils;
14 import org.argeo.cms.ui.util.CmsUiUtils;
15 import org.argeo.eclipse.ui.EclipseUiUtils;
16 import org.argeo.eclipse.ui.fs.FsUiUtils;
17 import org.argeo.eclipse.ui.specific.UiContext;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.browser.Browser;
20 import org.eclipse.swt.custom.SashForm;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Label;
25
26 /**
27 * Default Documents file composite: a sashForm with a browser in the middle and
28 * meta data at right hand side.
29 */
30 public class DocumentsFileComposite extends Composite {
31 private static final long serialVersionUID = -7567632342889241793L;
32
33 private final static Log log = LogFactory.getLog(DocumentsFileComposite.class);
34
35 private final Node currentBaseContext;
36
37 // UI Parts for the browser
38 private Composite rightPannelCmp;
39
40 public DocumentsFileComposite(Composite parent, int style, Node context,
41 FileSystemProvider fsp) {
42 super(parent, style);
43 this.currentBaseContext = context;
44 this.setLayout(EclipseUiUtils.noSpaceGridLayout());
45 SashForm form = new SashForm(this, SWT.HORIZONTAL);
46
47 Composite centerCmp = new Composite(form, SWT.BORDER | SWT.NO_FOCUS);
48 createDisplay(centerCmp);
49
50 rightPannelCmp = new Composite(form, SWT.NO_FOCUS);
51
52 Path path = CmsFsUtils.getPath(fsp, context);
53 setOverviewInput(path);
54 form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
55 form.setWeights(new int[] { 55, 20 });
56 }
57
58 private void createDisplay(final Composite parent) {
59 parent.setLayout(EclipseUiUtils.noSpaceGridLayout());
60 Browser browser = new Browser(parent, SWT.NONE);
61 // browser.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
62 // true));
63 browser.setLayoutData(EclipseUiUtils.fillAll());
64 try {
65 // FIXME make it more robust
66 String url = CmsUiUtils.getDataUrl(currentBaseContext, UiContext.getHttpRequest());
67 // FIXME issue with the redirection to https
68 if (url.startsWith("http://") && !url.startsWith("http://localhost"))
69 url = "https://" + url.substring("http://".length(), url.length());
70 if (log.isTraceEnabled())
71 log.debug("Trying to display " + url);
72 browser.setUrl(url);
73 browser.layout(true, true);
74 } catch (RepositoryException re) {
75 throw new IllegalStateException("Cannot open file at " + currentBaseContext, re);
76 }
77 }
78
79 /**
80 * Recreates the content of the box that displays information about the current
81 * selected Path.
82 */
83 private void setOverviewInput(Path path) {
84 try {
85 EclipseUiUtils.clear(rightPannelCmp);
86 rightPannelCmp.setLayout(new GridLayout());
87 if (path != null) {
88 // if (isImg(context)) {
89 // EditableImage image = new Img(parent, RIGHT, context,
90 // imageWidth);
91 // image.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER,
92 // true, false,
93 // 2, 1));
94 // }
95
96 Label contextL = new Label(rightPannelCmp, SWT.NONE);
97 contextL.setText(path.getFileName().toString());
98 contextL.setFont(EclipseUiUtils.getBoldFont(rightPannelCmp));
99 addProperty(rightPannelCmp, "Last modified", Files.getLastModifiedTime(path).toString());
100 // addProperty(rightPannelCmp, "Owner",
101 // Files.getOwner(path).getName());
102 if (Files.isDirectory(path)) {
103 addProperty(rightPannelCmp, "Type", "Folder");
104 } else {
105 String mimeType = Files.probeContentType(path);
106 if (EclipseUiUtils.isEmpty(mimeType))
107 mimeType = "<i>Unknown</i>";
108 addProperty(rightPannelCmp, "Type", mimeType);
109 addProperty(rightPannelCmp, "Size", FsUiUtils.humanReadableByteCount(Files.size(path), false));
110 }
111 }
112 rightPannelCmp.layout(true, true);
113 } catch (IOException e) {
114 throw new IllegalStateException("Cannot display details for " + path.toString(), e);
115 }
116 }
117
118 // Simplify UI implementation
119 private void addProperty(Composite parent, String propName, String value) {
120 Label propLbl = new Label(parent, SWT.NONE);
121 //propLbl.setText(ConnectUtils.replaceAmpersand(propName + ": " + value));
122 propLbl.setText(value);
123 //CmsUiUtils.markup(propLbl);
124 }
125 }