2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.cms
.ui
.workbench
.internal
.jcr
;
18 import static javax
.jcr
.Node
.JCR_CONTENT
;
19 import static javax
.jcr
.Property
.JCR_DATA
;
22 import java
.io
.FileOutputStream
;
23 import java
.io
.IOException
;
24 import java
.io
.InputStream
;
25 import java
.io
.OutputStream
;
26 import java
.nio
.file
.Path
;
27 import java
.nio
.file
.Paths
;
28 import java
.util
.HashMap
;
31 import javax
.jcr
.Binary
;
32 import javax
.jcr
.Node
;
33 import javax
.jcr
.RepositoryException
;
34 import javax
.jcr
.nodetype
.NodeType
;
36 import org
.apache
.commons
.io
.IOUtils
;
37 import org
.argeo
.cms
.ui
.workbench
.WorkbenchUiPlugin
;
38 import org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.model
.RepositoryElem
;
39 import org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.model
.SingleJcrNodeElem
;
40 import org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.model
.WorkspaceElem
;
41 import org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.parts
.GenericNodeEditorInput
;
42 import org
.argeo
.cms
.ui
.workbench
.jcr
.DefaultNodeEditor
;
43 import org
.argeo
.cms
.ui
.workbench
.util
.CommandUtils
;
44 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
45 import org
.argeo
.eclipse
.ui
.specific
.OpenFile
;
46 import org
.argeo
.eclipse
.ui
.specific
.SingleSourcingException
;
47 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
48 import org
.eclipse
.jface
.viewers
.IDoubleClickListener
;
49 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
50 import org
.eclipse
.jface
.viewers
.TreeViewer
;
51 import org
.eclipse
.ui
.PartInitException
;
53 /** Centralizes the management of double click on a NodeTreeViewer */
54 public class JcrDClickListener
implements IDoubleClickListener
{
55 // private final static Log log = LogFactory
56 // .getLog(GenericNodeDoubleClickListener.class);
58 private TreeViewer nodeViewer
;
60 // private JcrFileProvider jfp;
61 // private FileHandler fileHandler;
63 public JcrDClickListener(TreeViewer nodeViewer
) {
64 this.nodeViewer
= nodeViewer
;
65 // jfp = new JcrFileProvider();
66 // Commented out. see https://www.argeo.org/bugzilla/show_bug.cgi?id=188
67 // fileHandler = null;
68 // fileHandler = new FileHandler(jfp);
71 public void doubleClick(DoubleClickEvent event
) {
72 if (event
.getSelection() == null || event
.getSelection().isEmpty())
74 Object obj
= ((IStructuredSelection
) event
.getSelection()).getFirstElement();
75 if (obj
instanceof RepositoryElem
) {
76 RepositoryElem rpNode
= (RepositoryElem
) obj
;
77 if (!rpNode
.isConnected()) {
79 nodeViewer
.refresh(obj
);
81 } else if (obj
instanceof WorkspaceElem
) {
82 WorkspaceElem wn
= (WorkspaceElem
) obj
;
87 nodeViewer
.refresh(obj
);
88 } else if (obj
instanceof SingleJcrNodeElem
) {
89 SingleJcrNodeElem sjn
= (SingleJcrNodeElem
) obj
;
90 Node node
= sjn
.getNode();
92 if (node
.isNodeType(NodeType
.NT_FILE
)) {
95 String name
= node
.getName();
96 Map
<String
, String
> params
= new HashMap
<String
, String
>();
97 params
.put(OpenFile
.PARAM_FILE_NAME
, name
);
99 // TODO rather directly transmit the path to the node, once
100 // we have defined convention to provide an Absolute URI to
101 // a node in a multi repo / workspace / user context
102 // params.put(OpenFile.PARAM_FILE_URI,
103 // OpenFileService.JCR_SCHEME + node.getPath());
105 // we copy the node to a tmp file to be opened as a dirty
108 OutputStream os
= null;
109 InputStream is
= null;
110 int i
= name
.lastIndexOf('.');
111 String prefix
, suffix
;
116 prefix
= name
.substring(0, i
);
117 suffix
= name
.substring(i
);
120 tmpFile
= File
.createTempFile(prefix
, suffix
);
121 tmpFile
.deleteOnExit();
122 os
= new FileOutputStream(tmpFile
);
123 Binary binary
= node
.getNode(JCR_CONTENT
).getProperty(JCR_DATA
).getBinary();
124 is
= binary
.getStream();
125 IOUtils
.copy(is
, os
);
126 } catch (IOException e
) {
127 throw new SingleSourcingException("Cannot open file " + prefix
+ "." + suffix
, e
);
129 IOUtils
.closeQuietly(is
);
130 IOUtils
.closeQuietly(os
);
132 Path path
= Paths
.get(tmpFile
.getAbsolutePath());
133 String uri
= path
.toUri().toString();
134 params
.put(OpenFile
.PARAM_FILE_URI
, uri
);
135 CommandUtils
.callCommand(OpenFile
.ID
, params
);
137 GenericNodeEditorInput gnei
= new GenericNodeEditorInput(node
);
138 WorkbenchUiPlugin
.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage()
139 .openEditor(gnei
, DefaultNodeEditor
.ID
);
140 } catch (RepositoryException re
) {
141 throw new EclipseUiException("Repository error while getting node info", re
);
142 } catch (PartInitException pie
) {
143 throw new EclipseUiException("Unexepected exception while opening node editor", pie
);