]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/jcr/WorkbenchJcrDClickListener.java
Work on hardening.
[lgpl/argeo-commons.git] / org.argeo.cms.ui.workbench / src / org / argeo / cms / ui / workbench / jcr / WorkbenchJcrDClickListener.java
1 package org.argeo.cms.ui.workbench.jcr;
2
3 import static javax.jcr.Node.JCR_CONTENT;
4 import static javax.jcr.Property.JCR_DATA;
5
6 import java.io.File;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.OutputStream;
11 import java.nio.file.Path;
12 import java.nio.file.Paths;
13 import java.util.HashMap;
14 import java.util.Map;
15
16 import javax.jcr.Binary;
17 import javax.jcr.Node;
18 import javax.jcr.RepositoryException;
19 import javax.jcr.nodetype.NodeType;
20
21 import org.apache.commons.io.IOUtils;
22 import org.argeo.cms.ui.jcr.JcrDClickListener;
23 import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
24 import org.argeo.cms.ui.workbench.internal.jcr.parts.GenericNodeEditorInput;
25 import org.argeo.cms.ui.workbench.util.CommandUtils;
26 import org.argeo.eclipse.ui.EclipseUiException;
27 import org.argeo.eclipse.ui.specific.OpenFile;
28 import org.argeo.eclipse.ui.specific.SingleSourcingException;
29 import org.argeo.jcr.JcrUtils;
30 import org.eclipse.jface.viewers.TreeViewer;
31
32 public class WorkbenchJcrDClickListener extends JcrDClickListener {
33
34 public WorkbenchJcrDClickListener(TreeViewer nodeViewer) {
35 super(nodeViewer);
36 }
37
38 @Override
39 protected void openNode(Node node) {
40 try {
41 if (node.isNodeType(NodeType.NT_FILE)) {
42 // Also open it
43
44 String name = node.getName();
45 Map<String, String> params = new HashMap<String, String>();
46 params.put(OpenFile.PARAM_FILE_NAME, name);
47
48 // TODO rather directly transmit the path to the node, once
49 // we have defined convention to provide an Absolute URI to
50 // a node in a multi repo / workspace / user context
51 // params.put(OpenFile.PARAM_FILE_URI,
52 // OpenFileService.JCR_SCHEME + node.getPath());
53
54 // we copy the node to a tmp file to be opened as a dirty
55 // workaround
56 File tmpFile = null;
57 // OutputStream os = null;
58 // InputStream is = null;
59 int i = name.lastIndexOf('.');
60 String prefix, suffix;
61 if (i == -1) {
62 prefix = name;
63 suffix = null;
64 } else {
65 prefix = name.substring(0, i);
66 suffix = name.substring(i);
67 }
68 Binary binary = null;
69 try {
70 tmpFile = File.createTempFile(prefix, suffix);
71 tmpFile.deleteOnExit();
72 } catch (IOException e1) {
73 throw new EclipseUiException("Cannot create temp file", e1);
74 }
75 try (OutputStream os = new FileOutputStream(tmpFile)) {
76 binary = node.getNode(JCR_CONTENT).getProperty(JCR_DATA).getBinary();
77 try (InputStream is = binary.getStream();) {
78 IOUtils.copy(is, os);
79 }
80 } catch (IOException e) {
81 throw new SingleSourcingException("Cannot open file " + prefix + "." + suffix, e);
82 } finally {
83 // IOUtils.closeQuietly(is);
84 // IOUtils.closeQuietly(os);
85 JcrUtils.closeQuietly(binary);
86 }
87 Path path = Paths.get(tmpFile.getAbsolutePath());
88 String uri = path.toUri().toString();
89 params.put(OpenFile.PARAM_FILE_URI, uri);
90 CommandUtils.callCommand(OpenFile.ID, params);
91 }
92 GenericNodeEditorInput gnei = new GenericNodeEditorInput(node);
93 WorkbenchUiPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(gnei,
94 DefaultNodeEditor.ID);
95 } catch (RepositoryException re) {
96 throw new EclipseUiException("Repository error while getting node info", re);
97 } catch (Exception pie) {
98 throw new EclipseUiException("Unexpected exception while opening node editor", pie);
99 }
100 }
101 }