]> git.argeo.org Git - lgpl/argeo-commons.git/blob - AddFileFolder.java
42eff5e1cb75bddbc6ba6eab36e05c272b183f9a
[lgpl/argeo-commons.git] / AddFileFolder.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.eclipse.ui.jcr.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.dialogs.SingleValue;
24 import org.argeo.eclipse.ui.jcr.JcrUiPlugin;
25 import org.argeo.eclipse.ui.jcr.views.AbstractJcrBrowser;
26 import org.eclipse.core.commands.AbstractHandler;
27 import org.eclipse.core.commands.ExecutionEvent;
28 import org.eclipse.core.commands.ExecutionException;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.ui.handlers.HandlerUtil;
32
33 /** Adds a node of type nt:folder */
34 public class AddFileFolder extends AbstractHandler {
35
36 public Object execute(ExecutionEvent event) throws ExecutionException {
37 ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
38 .getActivePage().getSelection();
39 AbstractJcrBrowser view = (AbstractJcrBrowser) HandlerUtil
40 .getActiveWorkbenchWindow(event).getActivePage()
41 .findView(HandlerUtil.getActivePartId(event));
42 if (selection != null && !selection.isEmpty()
43 && selection instanceof IStructuredSelection) {
44 Object obj = ((IStructuredSelection) selection).getFirstElement();
45
46 if (obj instanceof Node) {
47 String folderName = SingleValue.ask("Folder name",
48 "Enter folder name");
49 if (folderName != null) {
50 Node parentNode = (Node) obj;
51 try {
52 Node newNode = parentNode.addNode(folderName,
53 NodeType.NT_FOLDER);
54 view.nodeAdded(parentNode, newNode);
55 parentNode.getSession().save();
56 } catch (RepositoryException e) {
57 ErrorFeedback.show("Cannot create folder " + folderName
58 + " under " + parentNode, e);
59 }
60 }
61 } else {
62 ErrorFeedback.show(JcrUiPlugin
63 .getMessage("errorUnvalidNtFolderNodeType"));
64 }
65 }
66 return null;
67 }
68
69 }