]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - workbench/commands/RenameNode.java
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
.eclipse
.ui
.workbench
.commands
;
18 import java
.util
.Iterator
;
20 import javax
.jcr
.Node
;
21 import javax
.jcr
.RepositoryException
;
22 import javax
.jcr
.Session
;
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
.jcr
.JcrBrowserView
;
28 import org
.argeo
.eclipse
.ui
.workbench
.jcr
.internal
.model
.SingleJcrNodeElem
;
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
;
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
44 public class RenameNode
extends AbstractHandler
{
45 public final static String ID
= WorkbenchUiPlugin
.ID
+ ".renameNode";
47 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
48 IWorkbenchPage iwp
= HandlerUtil
.getActiveWorkbenchWindow(event
)
51 ISelection selection
= iwp
.getSelection();
52 if (selection
== null || !(selection
instanceof IStructuredSelection
))
55 Iterator
<?
> lst
= ((IStructuredSelection
) selection
).iterator();
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;
65 newName
= SingleValue
.ask("New node name",
66 "Please provide a new name for [" + node
.getName()
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
) + "/"
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
);