]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui.workbench/src/org/argeo/cms/ui/workbench/internal/jcr/commands/AddFolderNode.java
Merge branch 'master' of https://code.argeo.org/git/apache2/argeo-commons
[lgpl/argeo-commons.git] / org.argeo.cms.ui.workbench / src / org / argeo / cms / ui / workbench / internal / jcr / commands / AddFolderNode.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.commands;
17
18 import javax.jcr.Node;
19 import javax.jcr.RepositoryException;
20 import javax.jcr.nodetype.NodeType;
21
22 import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
23 import org.argeo.cms.ui.workbench.internal.jcr.model.SingleJcrNodeElem;
24 import org.argeo.cms.ui.workbench.internal.jcr.model.WorkspaceElem;
25 import org.argeo.cms.ui.workbench.jcr.JcrBrowserView;
26 import org.argeo.eclipse.ui.TreeParent;
27 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
28 import org.argeo.eclipse.ui.dialogs.SingleValue;
29 import org.eclipse.core.commands.AbstractHandler;
30 import org.eclipse.core.commands.ExecutionEvent;
31 import org.eclipse.core.commands.ExecutionException;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.ui.handlers.HandlerUtil;
35
36 /**
37 * Adds a node of type nt:folder, only on {@link SingleJcrNodeElem} and
38 * {@link WorkspaceElem} TreeObject types.
39 *
40 * This handler assumes that a selection provider is available and picks only
41 * first selected item. It is UI's job to enable the command only when the
42 * selection contains one and only one element. Thus no parameter is passed
43 * through the command.
44 */
45 public class AddFolderNode extends AbstractHandler {
46
47 public final static String ID = WorkbenchUiPlugin.PLUGIN_ID
48 + ".addFolderNode";
49
50 public Object execute(ExecutionEvent event) throws ExecutionException {
51
52 ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
53 .getActivePage().getSelection();
54
55 JcrBrowserView view = (JcrBrowserView) HandlerUtil
56 .getActiveWorkbenchWindow(event).getActivePage()
57 .findView(HandlerUtil.getActivePartId(event));
58
59 if (selection != null && !selection.isEmpty()
60 && selection instanceof IStructuredSelection) {
61 Object obj = ((IStructuredSelection) selection).getFirstElement();
62 TreeParent treeParentNode = null;
63 Node jcrParentNode = null;
64
65 if (obj instanceof SingleJcrNodeElem) {
66 treeParentNode = (TreeParent) obj;
67 jcrParentNode = ((SingleJcrNodeElem) treeParentNode).getNode();
68 } else if (obj instanceof WorkspaceElem) {
69 treeParentNode = (TreeParent) obj;
70 jcrParentNode = ((WorkspaceElem) treeParentNode).getRootNode();
71 } else
72 return null;
73
74 String folderName = SingleValue.ask("Folder name",
75 "Enter folder name");
76 if (folderName != null) {
77 try {
78 jcrParentNode.addNode(folderName, NodeType.NT_FOLDER);
79 jcrParentNode.getSession().save();
80 view.nodeAdded(treeParentNode);
81 } catch (RepositoryException e) {
82 ErrorFeedback.show("Cannot create folder " + folderName
83 + " under " + treeParentNode, e);
84 }
85 }
86 } else {
87 ErrorFeedback.show(WorkbenchUiPlugin
88 .getMessage("errorUnvalidNtFolderNodeType"));
89 }
90 return null;
91 }
92 }