1 package org
.argeo
.cms
.e4
.jcr
.handlers
;
5 import javax
.inject
.Named
;
7 import javax
.jcr
.RepositoryException
;
8 import javax
.jcr
.Session
;
10 import org
.argeo
.cms
.e4
.jcr
.JcrBrowserView
;
11 import org
.argeo
.cms
.ui
.jcr
.model
.SingleJcrNodeElem
;
12 import org
.argeo
.eclipse
.ui
.EclipseUiException
;
13 import org
.argeo
.eclipse
.ui
.dialogs
.SingleValue
;
14 import org
.argeo
.jcr
.JcrUtils
;
15 import org
.eclipse
.e4
.core
.di
.annotations
.Execute
;
16 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
17 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
18 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
;
19 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
22 * Canonically call JCR Session#move(String, String) on the first element
23 * returned by HandlerUtil#getActiveWorkbenchWindow()
24 * (...getActivePage().getSelection()), if it is a {@link SingleJcrNodeElem}.
25 * The user must then fill a new name in and confirm
27 public class RenameNode
{
29 public void execute(@Named(IServiceConstants
.ACTIVE_PART
) MPart part
, EPartService partService
,
30 ESelectionService selectionService
) {
31 List
<?
> selection
= (List
<?
>) selectionService
.getSelection();
32 if (selection
== null || selection
.size() != 1)
34 JcrBrowserView view
= (JcrBrowserView
) part
.getObject();
36 Object element
= selection
.get(0);
37 if (element
instanceof SingleJcrNodeElem
) {
38 SingleJcrNodeElem sjn
= (SingleJcrNodeElem
) element
;
39 Node node
= sjn
.getNode();
40 Session session
= null;
41 String newName
= null;
42 String oldPath
= null;
44 newName
= SingleValue
.ask("New node name", "Please provide a new name for [" + node
.getName() + "]");
45 // TODO sanity check and user feedback
46 newName
= JcrUtils
.replaceInvalidChars(newName
);
47 oldPath
= node
.getPath();
48 session
= node
.getSession();
49 session
.move(oldPath
, JcrUtils
.parentPath(oldPath
) + "/" + newName
);
52 // Manually refresh the browser view. Must be enhanced
54 } catch (RepositoryException e
) {
55 throw new EclipseUiException("Unable to rename " + node
+ " to " + newName
, e
);