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