]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/commands/RenameNode.java
Add the ability to remove a JCR privilege from a given node
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui.workbench / src / org / argeo / eclipse / ui / workbench / commands / RenameNode.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.eclipse.ui.workbench.commands;
17
18 import java.util.Iterator;
19
20 import javax.jcr.Node;
21 import javax.jcr.RepositoryException;
22 import javax.jcr.Session;
23
24 import org.argeo.ArgeoException;
25 import org.argeo.eclipse.ui.dialogs.SingleValue;
26 import org.argeo.eclipse.ui.workbench.WorkbenchUiPlugin;
27 import org.argeo.eclipse.ui.workbench.internal.jcr.model.SingleJcrNodeElem;
28 import org.argeo.eclipse.ui.workbench.jcr.JcrBrowserView;
29 import org.argeo.jcr.JcrUtils;
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.IWorkbenchPage;
36 import org.eclipse.ui.handlers.HandlerUtil;
37
38 /**
39 * Canonically call JCR {@link Session#move(String, String)} on the first
40 * element returned by {@link HandlerUtil#getActiveWorkbenchWindow()}
41 * (...getActivePage().getSelection()), if it is a {@link SingleJcrNodeElem}.
42 * The user must then fill a new name in and confirm
43 */
44 public class RenameNode extends AbstractHandler {
45 public final static String ID = WorkbenchUiPlugin.ID + ".renameNode";
46
47 public Object execute(ExecutionEvent event) throws ExecutionException {
48 IWorkbenchPage iwp = HandlerUtil.getActiveWorkbenchWindow(event)
49 .getActivePage();
50
51 ISelection selection = iwp.getSelection();
52 if (selection == null || !(selection instanceof IStructuredSelection))
53 return null;
54
55 Iterator<?> lst = ((IStructuredSelection) selection).iterator();
56 if (lst.hasNext()) {
57 Object element = lst.next();
58 if (element instanceof SingleJcrNodeElem) {
59 SingleJcrNodeElem sjn = (SingleJcrNodeElem) element;
60 Node node = sjn.getNode();
61 Session session = null;
62 String newName = null;
63 String oldPath = null;
64 try {
65 newName = SingleValue.ask("New node name",
66 "Please provide a new name for [" + node.getName()
67 + "]");
68 // TODO sanity check and user feedback
69 newName = JcrUtils.replaceInvalidChars(newName);
70 oldPath = node.getPath();
71 session = node.getSession();
72 session.move(oldPath, JcrUtils.parentPath(oldPath) + "/"
73 + newName);
74 session.save();
75
76 // Manually refresh the browser view. Must be enhanced
77 if (iwp.getActivePart() instanceof JcrBrowserView)
78 ((JcrBrowserView) iwp.getActivePart()).refresh(sjn);
79 } catch (RepositoryException e) {
80 throw new ArgeoException("Unable to rename " + node
81 + " to " + newName, e);
82 }
83 }
84 }
85 return null;
86 }
87 }