1 package org
.argeo
.cms
.ui
.workbench
.jcr
;
3 import static javax
.jcr
.Node
.JCR_CONTENT
;
4 import static javax
.jcr
.Property
.JCR_DATA
;
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
;
16 import javax
.jcr
.Binary
;
17 import javax
.jcr
.Node
;
18 import javax
.jcr
.RepositoryException
;
19 import javax
.jcr
.nodetype
.NodeType
;
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
;
32 public class WorkbenchJcrDClickListener
extends JcrDClickListener
{
34 public WorkbenchJcrDClickListener(TreeViewer nodeViewer
) {
39 protected void openNode(Node node
) {
41 if (node
.isNodeType(NodeType
.NT_FILE
)) {
44 String name
= node
.getName();
45 Map
<String
, String
> params
= new HashMap
<String
, String
>();
46 params
.put(OpenFile
.PARAM_FILE_NAME
, name
);
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());
54 // we copy the node to a tmp file to be opened as a dirty
57 // OutputStream os = null;
58 // InputStream is = null;
59 int i
= name
.lastIndexOf('.');
60 String prefix
, suffix
;
65 prefix
= name
.substring(0, i
);
66 suffix
= name
.substring(i
);
70 tmpFile
= File
.createTempFile(prefix
, suffix
);
71 tmpFile
.deleteOnExit();
72 } catch (IOException e1
) {
73 throw new EclipseUiException("Cannot create temp file", e1
);
75 try (OutputStream os
= new FileOutputStream(tmpFile
)) {
76 binary
= node
.getNode(JCR_CONTENT
).getProperty(JCR_DATA
).getBinary();
77 try (InputStream is
= binary
.getStream();) {
80 } catch (IOException e
) {
81 throw new SingleSourcingException("Cannot open file " + prefix
+ "." + suffix
, e
);
83 // IOUtils.closeQuietly(is);
84 // IOUtils.closeQuietly(os);
85 JcrUtils
.closeQuietly(binary
);
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
);
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
);