View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
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
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
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.
15   */
16  package org.argeo.cms.ui.workbench.internal.jcr.commands;
17  
18  import java.util.Iterator;
19  
20  import javax.jcr.Node;
21  import javax.jcr.RepositoryException;
22  import javax.jcr.Session;
23  
24  import org.argeo.cms.ui.jcr.model.SingleJcrNodeElem;
25  import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
26  import org.argeo.cms.ui.workbench.jcr.JcrBrowserView;
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.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;
37  
38  /**
39   * Canonically call JCR Session#move(String, String) on the first element
40   * returned by HandlerUtil#getActiveWorkbenchWindow()
41   * (...getActivePage().getSelection()), if it is a {@link SingleJcrNodeElem}.
42   * The user must then fill a new name in and confirm
43   */
44  public class RenameNode extends AbstractHandler {
45  	public final static String ID = WorkbenchUiPlugin.PLUGIN_ID + ".renameNode";
46  
47  	public Object execute(ExecutionEvent event) throws ExecutionException {
48  		IWorkbenchPage iwp = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
49  
50  		ISelection selection = iwp.getSelection();
51  		if (selection == null || !(selection instanceof IStructuredSelection))
52  			return null;
53  
54  		Iterator<?> lst = ((IStructuredSelection) selection).iterator();
55  		if (lst.hasNext()) {
56  			Object element = lst.next();
57  			if (element instanceof SingleJcrNodeElem) {
58  				SingleJcrNodeElem sjn = (SingleJcrNodeElem) element;
59  				Node node = sjn.getNode();
60  				Session session = null;
61  				String newName = null;
62  				String oldPath = null;
63  				try {
64  					newName = SingleValue.ask("New node name",
65  							"Please provide a new name for [" + node.getName() + "]");
66  					// TODO sanity check and user feedback
67  					newName = JcrUtils.replaceInvalidChars(newName);
68  					oldPath = node.getPath();
69  					session = node.getSession();
70  					session.move(oldPath, JcrUtils.parentPath(oldPath) + "/" + newName);
71  					session.save();
72  
73  					// Manually refresh the browser view. Must be enhanced
74  					if (iwp.getActivePart() instanceof JcrBrowserView)
75  						((JcrBrowserView) iwp.getActivePart()).refresh(sjn);
76  				} catch (RepositoryException e) {
77  					throw new EclipseUiException("Unable to rename " + node + " to " + newName, e);
78  				}
79  			}
80  		}
81  		return null;
82  	}
83  }