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