1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.cms.e4.jcr.handlers;
17
18 import java.util.List;
19
20 import javax.inject.Named;
21 import javax.jcr.Node;
22 import javax.jcr.RepositoryException;
23 import javax.jcr.Session;
24
25 import org.argeo.cms.e4.jcr.JcrBrowserView;
26 import org.argeo.cms.ui.jcr.model.SingleJcrNodeElem;
27 import org.argeo.eclipse.ui.EclipseUiException;
28 import org.argeo.eclipse.ui.dialogs.SingleValue;
29 import org.argeo.jcr.JcrUtils;
30 import org.eclipse.e4.core.di.annotations.Execute;
31 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
32 import org.eclipse.e4.ui.services.IServiceConstants;
33 import org.eclipse.e4.ui.workbench.modeling.EPartService;
34 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
35
36
37
38
39
40
41
42 public class RenameNode {
43 @Execute
44 public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, EPartService partService,
45 ESelectionService selectionService) {
46 List<?> selection = (List<?>) selectionService.getSelection();
47 if (selection == null || selection.size() != 1)
48 return;
49 JcrBrowserView view = (JcrBrowserView) part.getObject();
50
51 Object element = selection.get(0);
52 if (element instanceof SingleJcrNodeElem) {
53 SingleJcrNodeElem sjn = (SingleJcrNodeElem) element;
54 Node node = sjn.getNode();
55 Session session = null;
56 String newName = null;
57 String oldPath = null;
58 try {
59 newName = SingleValue.ask("New node name", "Please provide a new name for [" + node.getName() + "]");
60
61 newName = JcrUtils.replaceInvalidChars(newName);
62 oldPath = node.getPath();
63 session = node.getSession();
64 session.move(oldPath, JcrUtils.parentPath(oldPath) + "/" + newName);
65 session.save();
66
67
68 view.refresh(sjn);
69 } catch (RepositoryException e) {
70 throw new EclipseUiException("Unable to rename " + node + " to " + newName, e);
71 }
72 }
73 }
74 }