]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/AddFolderNode.java
fix bug 85 : JCR explorer refresh was not implemented for TreeParent objects of type...
[lgpl/argeo-commons.git] / server / plugins / org.argeo.jcr.ui.explorer / src / main / java / org / argeo / jcr / ui / explorer / commands / AddFolderNode.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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.jcr.ui.explorer.commands;
17
18 import javax.jcr.Node;
19 import javax.jcr.RepositoryException;
20 import javax.jcr.nodetype.NodeType;
21
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.SingleJcrNode;
28 import org.argeo.jcr.ui.explorer.model.WorkspaceNode;
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;
36
37 /**
38 * Adds a node of type nt:folder, only on {@link SingleJcrNode} and
39 * {@link WorkspaceNode} TreeObject types.
40 *
41 *
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.
46 *
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
49 * versions.
50 */
51 public class AddFolderNode extends AbstractHandler {
52
53 public final static String ID = JcrExplorerPlugin.ID + ".addFolderNode";
54
55 // public final static String DEFAULT_LABEL = JcrExplorerPlugin
56 // .getMessage("addFolderNodeCmdLbl");
57 // public final static String DEFAULT_ICON_REL_PATH = "icons/addRepo.gif";
58
59 public Object execute(ExecutionEvent event) throws ExecutionException {
60
61 ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
62 .getActivePage().getSelection();
63
64 GenericJcrBrowser view = (GenericJcrBrowser) HandlerUtil
65 .getActiveWorkbenchWindow(event).getActivePage()
66 .findView(HandlerUtil.getActivePartId(event));
67
68 if (selection != null && !selection.isEmpty()
69 && selection instanceof IStructuredSelection) {
70 Object obj = ((IStructuredSelection) selection).getFirstElement();
71 TreeParent treeParentNode = null;
72 Node jcrParentNode = null;
73
74 if (obj instanceof SingleJcrNode) {
75 treeParentNode = (TreeParent) obj;
76 jcrParentNode = ((SingleJcrNode) treeParentNode).getNode();
77 } else if (obj instanceof WorkspaceNode) {
78 treeParentNode = (TreeParent) obj;
79 jcrParentNode = ((WorkspaceNode) treeParentNode).getRootNode();
80 } else
81 return null;
82
83 String folderName = SingleValue.ask("Folder name",
84 "Enter folder name");
85 if (folderName != null) {
86 try {
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);
93 }
94 }
95 } else {
96 ErrorFeedback.show(JcrUiPlugin
97 .getMessage("errorUnvalidNtFolderNodeType"));
98 }
99 return null;
100 }
101 }