]> git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/org.argeo.cms.e4/src/org/argeo/cms/e4/jcr/handlers/RenameNode.java
Simplify hierarchy units
[lgpl/argeo-commons.git] / eclipse / org.argeo.cms.e4 / src / org / argeo / cms / e4 / jcr / handlers / RenameNode.java
1 package org.argeo.cms.e4.jcr.handlers;
2
3 import java.util.List;
4
5 import javax.inject.Named;
6 import javax.jcr.Node;
7 import javax.jcr.RepositoryException;
8 import javax.jcr.Session;
9
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;
20
21 /**
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
26 */
27 public class RenameNode {
28 @Execute
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)
33 return;
34 JcrBrowserView view = (JcrBrowserView) part.getObject();
35
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;
43 try {
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);
50 session.save();
51
52 // Manually refresh the browser view. Must be enhanced
53 view.refresh(sjn);
54 } catch (RepositoryException e) {
55 throw new EclipseUiException("Unable to rename " + node + " to " + newName, e);
56 }
57 }
58 }
59 }