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
.jcr
.ui
.explorer
.commands
;
18 import javax
.jcr
.Node
;
19 import javax
.jcr
.RepositoryException
;
20 import javax
.jcr
.nodetype
.NodeType
;
22 import org
.argeo
.eclipse
.ui
.ErrorFeedback
;
23 import org
.argeo
.eclipse
.ui
.TreeParent
;
24 import org
.argeo
.eclipse
.ui
.dialogs
.SingleValue
;
25 import org
.argeo
.eclipse
.ui
.jcr
.JcrUiPlugin
;
26 import org
.argeo
.jcr
.ui
.explorer
.JcrExplorerPlugin
;
27 import org
.argeo
.jcr
.ui
.explorer
.model
.SingleJcrNodeElem
;
28 import org
.argeo
.jcr
.ui
.explorer
.model
.WorkspaceElem
;
29 import org
.argeo
.jcr
.ui
.explorer
.views
.GenericJcrBrowser
;
30 import org
.eclipse
.core
.commands
.AbstractHandler
;
31 import org
.eclipse
.core
.commands
.ExecutionEvent
;
32 import org
.eclipse
.core
.commands
.ExecutionException
;
33 import org
.eclipse
.jface
.viewers
.ISelection
;
34 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
35 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
38 * Adds a node of type nt:folder, only on {@link SingleJcrNodeElem} and
39 * {@link WorkspaceElem} TreeObject types.
42 * This handler assumes that a selection provider is available and picks only
43 * first selected item. It is UI's job to enable the command only when the
44 * selection contains one and only one element. Thus no parameter is passed
45 * through the command.
47 * This handler is still 'hard linked' to a GenericJcrBrowser view to enable
48 * correct tree refresh when a node is added. This must be corrected in future
51 public class AddFolderNode
extends AbstractHandler
{
53 public final static String ID
= JcrExplorerPlugin
.ID
+ ".addFolderNode";
55 // public final static String DEFAULT_LABEL = JcrExplorerPlugin
56 // .getMessage("addFolderNodeCmdLbl");
57 // public final static String DEFAULT_ICON_REL_PATH = "icons/addRepo.gif";
59 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
61 ISelection selection
= HandlerUtil
.getActiveWorkbenchWindow(event
)
62 .getActivePage().getSelection();
64 GenericJcrBrowser view
= (GenericJcrBrowser
) HandlerUtil
65 .getActiveWorkbenchWindow(event
).getActivePage()
66 .findView(HandlerUtil
.getActivePartId(event
));
68 if (selection
!= null && !selection
.isEmpty()
69 && selection
instanceof IStructuredSelection
) {
70 Object obj
= ((IStructuredSelection
) selection
).getFirstElement();
71 TreeParent treeParentNode
= null;
72 Node jcrParentNode
= null;
74 if (obj
instanceof SingleJcrNodeElem
) {
75 treeParentNode
= (TreeParent
) obj
;
76 jcrParentNode
= ((SingleJcrNodeElem
) treeParentNode
).getNode();
77 } else if (obj
instanceof WorkspaceElem
) {
78 treeParentNode
= (TreeParent
) obj
;
79 jcrParentNode
= ((WorkspaceElem
) treeParentNode
).getRootNode();
83 String folderName
= SingleValue
.ask("Folder name",
85 if (folderName
!= null) {
87 jcrParentNode
.addNode(folderName
, NodeType
.NT_FOLDER
);
88 jcrParentNode
.getSession().save();
89 view
.nodeAdded(treeParentNode
);
90 } catch (RepositoryException e
) {
91 ErrorFeedback
.show("Cannot create folder " + folderName
92 + " under " + treeParentNode
, e
);
96 ErrorFeedback
.show(JcrUiPlugin
97 .getMessage("errorUnvalidNtFolderNodeType"));