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
.commands
;
18 import java
.io
.InputStream
;
19 import java
.lang
.reflect
.Method
;
21 import java
.nio
.file
.Files
;
22 import java
.nio
.file
.Path
;
23 import java
.nio
.file
.Paths
;
25 import javax
.jcr
.Binary
;
26 import javax
.jcr
.Node
;
27 import javax
.jcr
.Property
;
28 import javax
.jcr
.nodetype
.NodeType
;
30 import org
.argeo
.cms
.ui
.workbench
.WorkbenchUiPlugin
;
31 import org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.model
.SingleJcrNodeElem
;
32 import org
.argeo
.cms
.ui
.workbench
.internal
.jcr
.model
.WorkspaceElem
;
33 import org
.argeo
.cms
.ui
.workbench
.jcr
.JcrBrowserView
;
34 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
35 import org
.argeo
.eclipse
.ui
.TreeParent
;
36 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
37 import org
.eclipse
.core
.commands
.AbstractHandler
;
38 import org
.eclipse
.core
.commands
.ExecutionEvent
;
39 import org
.eclipse
.core
.commands
.ExecutionException
;
40 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
41 import org
.eclipse
.jface
.viewers
.ISelection
;
42 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
43 import org
.eclipse
.swt
.SWT
;
44 import org
.eclipse
.swt
.widgets
.FileDialog
;
45 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
47 /** Upload local file(s) under the currently selected node */
48 public class UploadFiles
extends AbstractHandler
{
49 // private final static Log log = LogFactory.getLog(ImportFileSystem.class);
51 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
53 ISelection selection
= HandlerUtil
.getActiveWorkbenchWindow(event
).getActivePage().getSelection();
54 JcrBrowserView view
= (JcrBrowserView
) HandlerUtil
.getActiveWorkbenchWindow(event
).getActivePage()
55 .findView(HandlerUtil
.getActivePartId(event
));
56 if (selection
!= null && !selection
.isEmpty() && selection
instanceof IStructuredSelection
) {
57 Object obj
= ((IStructuredSelection
) selection
).getFirstElement();
60 if (obj
instanceof SingleJcrNodeElem
) {
61 folder
= ((SingleJcrNodeElem
) obj
).getNode();
62 } else if (obj
instanceof WorkspaceElem
) {
63 folder
= ((WorkspaceElem
) obj
).getRootNode();
65 ErrorFeedback
.show(WorkbenchUiPlugin
.getMessage("warningInvalidNodeToImport"));
68 FileDialog dialog
= new FileDialog(HandlerUtil
.getActiveShell(event
), SWT
.MULTI
);
69 dialog
.setText("Choose one or more files to upload");
71 if (EclipseUiUtils
.notEmpty(dialog
.open())) {
72 String
[] names
= dialog
.getFileNames();
73 // Workaround small differences between RAP and RCP
74 // 1. returned names are absolute path on RAP and
76 // 2. in RCP we must use getFilterPath that does not
78 Method filterMethod
= null;
82 filterMethod
= dialog
.getClass().getDeclaredMethod("getFilterPath");
83 String filterPath
= (String
) filterMethod
.invoke(dialog
);
84 parPath
= Paths
.get(filterPath
);
85 } catch (NoSuchMethodException nsme
) { // RAP
87 if (names
.length
== 0)
90 loop
: for (String name
: names
) {
91 Path path
= Paths
.get(name
);
93 path
= parPath
.resolve(path
);
94 if (Files
.exists(path
)) {
95 URI uri
= path
.toUri();
96 String uriStr
= uri
.toString();
97 System
.out
.println(uriStr
);
99 if (Files
.isDirectory(path
)) {
100 MessageDialog
.openError(HandlerUtil
.getActiveShell(event
),
101 "Unimplemented directory import",
102 "Upload of directories in the system is not yet implemented");
105 Node fileNode
= folder
.addNode(path
.getFileName().toString(), NodeType
.NT_FILE
);
106 Node resNode
= fileNode
.addNode(Property
.JCR_CONTENT
, NodeType
.NT_RESOURCE
);
107 Binary binary
= null;
108 try (InputStream is
= Files
.newInputStream(path
)) {
109 binary
= folder
.getSession().getValueFactory().createBinary(is
);
110 resNode
.setProperty(Property
.JCR_DATA
, binary
);
112 folder
.getSession().save();
114 String msg
= "Cannot upload file at " + path
.toString();
116 msg
+= "\nPlease remember that file upload fails when choosing files from the \"Recently Used\" bookmarks on some OS";
117 MessageDialog
.openError(HandlerUtil
.getActiveShell(event
), "Missing file", msg
);
121 view
.nodeAdded((TreeParent
) obj
);
126 } catch (Exception e
) {
127 ErrorFeedback
.show("Cannot import files to " + obj
, e
);