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.slc.client.ui.commands;
17  
18  import javax.jcr.Node;
19  import javax.jcr.Property;
20  import javax.jcr.RepositoryException;
21  import javax.jcr.Session;
22  
23  import org.argeo.eclipse.ui.dialogs.SingleValue;
24  import org.argeo.jcr.JcrUtils;
25  import org.argeo.slc.SlcException;
26  import org.argeo.slc.client.ui.ClientUiPlugin;
27  import org.argeo.slc.client.ui.model.SingleResultNode;
28  import org.eclipse.core.commands.AbstractHandler;
29  import org.eclipse.core.commands.ExecutionEvent;
30  import org.eclipse.core.commands.ExecutionException;
31  import org.eclipse.jface.dialogs.MessageDialog;
32  import org.eclipse.jface.resource.ImageDescriptor;
33  import org.eclipse.jface.viewers.IStructuredSelection;
34  import org.eclipse.swt.widgets.Display;
35  import org.eclipse.ui.handlers.HandlerUtil;
36  
37  /**
38   * Rename a node of type SlcType.SLC_RESULT_FOLDER by moving it.
39   */
40  
41  public class RenameResultNode extends AbstractHandler {
42  	public final static String ID = ClientUiPlugin.ID + ".renameResultNode";
43  	public final static ImageDescriptor DEFAULT_IMG_DESCRIPTOR = ClientUiPlugin
44  			.getImageDescriptor("icons/rename.png");
45  	public final static String DEFAULT_LABEL = "Rename result";
46  
47  	public Object execute(ExecutionEvent event) throws ExecutionException {
48  		IStructuredSelection selection = (IStructuredSelection) HandlerUtil
49  				.getActiveWorkbenchWindow(event).getActivePage().getSelection();
50  
51  		// Sanity check, already done when populating the corresponding popup
52  		// menu.
53  		if (selection != null && selection.size() == 1) {
54  			Object obj = selection.getFirstElement();
55  			try {
56  				if (obj instanceof SingleResultNode) {
57  					SingleResultNode rf = (SingleResultNode) obj;
58  					Node sourceNode = rf.getNode();
59  					String folderName = SingleValue.ask("Rename result",
60  							"Enter a new result name");
61  					if (folderName != null) {
62  
63  						if (sourceNode.getParent().hasNode(folderName)) {
64  							MessageDialog
65  									.openError(Display.getDefault()
66  											.getActiveShell(), "Error",
67  											"Another object with the same name already exists.");
68  							return null;
69  						}
70  
71  						String sourcePath = sourceNode.getPath();
72  						String targetPath = JcrUtils.parentPath(sourcePath)
73  								+ "/" + folderName;
74  						Session session = sourceNode.getSession();
75  						session.move(sourcePath, targetPath);
76  						session.getNode(targetPath).setProperty(
77  								Property.JCR_TITLE, folderName);
78  						session.save();
79  					}
80  				}
81  			} catch (RepositoryException e) {
82  				throw new SlcException(
83  						"Unexpected exception while refactoring result folder",
84  						e);
85  			}
86  		}
87  		return null;
88  	}
89  }