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