]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/JcrDClickListener.java
Prepare next development cycle
[lgpl/argeo-commons.git] / jcr / JcrDClickListener.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.cms.ui.workbench.internal.jcr;
17
18 import static javax.jcr.Node.JCR_CONTENT;
19 import static javax.jcr.Property.JCR_DATA;
20
21 import java.io.File;
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;
29 import java.util.Map;
30
31 import javax.jcr.Binary;
32 import javax.jcr.Node;
33 import javax.jcr.RepositoryException;
34 import javax.jcr.nodetype.NodeType;
35
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;
52
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);
57
58 private TreeViewer nodeViewer;
59
60 // private JcrFileProvider jfp;
61 // private FileHandler fileHandler;
62
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);
69 }
70
71 public void doubleClick(DoubleClickEvent event) {
72 if (event.getSelection() == null || event.getSelection().isEmpty())
73 return;
74 Object obj = ((IStructuredSelection) event.getSelection()).getFirstElement();
75 if (obj instanceof RepositoryElem) {
76 RepositoryElem rpNode = (RepositoryElem) obj;
77 if (!rpNode.isConnected()) {
78 rpNode.login();
79 nodeViewer.refresh(obj);
80 }
81 } else if (obj instanceof WorkspaceElem) {
82 WorkspaceElem wn = (WorkspaceElem) obj;
83 if (wn.isConnected())
84 wn.logout();
85 else
86 wn.login();
87 nodeViewer.refresh(obj);
88 } else if (obj instanceof SingleJcrNodeElem) {
89 SingleJcrNodeElem sjn = (SingleJcrNodeElem) obj;
90 Node node = sjn.getNode();
91 try {
92 if (node.isNodeType(NodeType.NT_FILE)) {
93 // Also open it
94
95 String name = node.getName();
96 Map<String, String> params = new HashMap<String, String>();
97 params.put(OpenFile.PARAM_FILE_NAME, name);
98
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());
104
105 // we copy the node to a tmp file to be opened as a dirty
106 // workaround
107 File tmpFile = null;
108 OutputStream os = null;
109 InputStream is = null;
110 int i = name.lastIndexOf('.');
111 String prefix, suffix;
112 if (i == -1) {
113 prefix = name;
114 suffix = null;
115 } else {
116 prefix = name.substring(0, i);
117 suffix = name.substring(i);
118 }
119 try {
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);
128 } finally {
129 IOUtils.closeQuietly(is);
130 IOUtils.closeQuietly(os);
131 }
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);
136 }
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);
144 }
145 }
146 }
147 }