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