]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/commands/AddResultFolder.java
Make execution editor more robust
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui / src / main / java / org / argeo / slc / client / ui / commands / AddResultFolder.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.RepositoryException;
21
22 import org.argeo.eclipse.ui.ErrorFeedback;
23 import org.argeo.eclipse.ui.dialogs.SingleValue;
24 import org.argeo.slc.SlcException;
25 import org.argeo.slc.client.ui.ClientUiPlugin;
26 import org.argeo.slc.client.ui.model.ParentNodeFolder;
27 import org.argeo.slc.client.ui.model.ResultFolder;
28 import org.argeo.slc.jcr.SlcJcrResultUtils;
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.viewers.IStructuredSelection;
33 import org.eclipse.ui.handlers.HandlerUtil;
34
35 /**
36 * Add a new SlcType.SLC_RESULT_FOLDER node to the current user "my result"
37 * tree. This handler is only intended to bu used with JcrResultTreeView and its
38 * descendants.
39 */
40
41 public class AddResultFolder extends AbstractHandler {
42 public final static String ID = ClientUiPlugin.ID + ".addResultFolder";
43 public final static String DEFAULT_ICON_REL_PATH = "icons/addFolder.gif";
44 public final static String DEFAULT_LABEL = "New result folder";
45
46 public Object execute(ExecutionEvent event) throws ExecutionException {
47 IStructuredSelection selection = (IStructuredSelection) HandlerUtil
48 .getActiveWorkbenchWindow(event).getActivePage().getSelection();
49
50 // Sanity check, already done when populating the corresponding popup
51 // menu.
52 if (selection != null && selection.size() == 1) {
53 Object obj = selection.getFirstElement();
54
55 try {
56 Node parentNode = null;
57
58 if (obj instanceof ResultFolder) {
59 ResultFolder rf = (ResultFolder) obj;
60 parentNode = rf.getNode();
61 } else if (obj instanceof ParentNodeFolder) {
62 Node node = ((ParentNodeFolder) obj).getNode();
63 if (node.getPath().startsWith(
64 SlcJcrResultUtils.getMyResultsBasePath(node
65 .getSession())))
66 parentNode = node;
67 }
68
69 if (parentNode != null) {
70 String folderName = SingleValue.ask("Folder name",
71 "Enter folder name");
72 if (folderName != null) {
73 String absPath = parentNode.getPath() + "/"
74 + folderName;
75 SlcJcrResultUtils.createResultFolderNode(
76 parentNode.getSession(), absPath);
77 }
78 }
79 } catch (RepositoryException e) {
80 throw new SlcException(
81 "Unexpected exception while creating result folder", e);
82 }
83 } else {
84 ErrorFeedback.show("Can only add file folder to a node");
85 }
86 return null;
87 }
88 }