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