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
.cms
.e4
.jcr
.handlers
;
18 import java
.util
.List
;
20 import javax
.inject
.Named
;
21 import javax
.jcr
.Node
;
22 import javax
.jcr
.RepositoryException
;
23 import javax
.jcr
.Session
;
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
;
37 * Canonically call JCR Session#move(String, String) on the first element
38 * returned by HandlerUtil#getActiveWorkbenchWindow()
39 * (...getActivePage().getSelection()), if it is a {@link SingleJcrNodeElem}.
40 * The user must then fill a new name in and confirm
42 public class RenameNode
{
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)
49 JcrBrowserView view
= (JcrBrowserView
) part
.getObject();
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;
59 newName
= SingleValue
.ask("New node name", "Please provide a new name for [" + node
.getName() + "]");
60 // TODO sanity check and user feedback
61 newName
= JcrUtils
.replaceInvalidChars(newName
);
62 oldPath
= node
.getPath();
63 session
= node
.getSession();
64 session
.move(oldPath
, JcrUtils
.parentPath(oldPath
) + "/" + newName
);
67 // Manually refresh the browser view. Must be enhanced
69 } catch (RepositoryException e
) {
70 throw new EclipseUiException("Unable to rename " + node
+ " to " + newName
, e
);