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.cms.e4.jcr.handlers;
17  
18  import java.util.List;
19  
20  import javax.inject.Named;
21  import javax.jcr.Node;
22  import javax.jcr.RepositoryException;
23  import javax.jcr.nodetype.NodeType;
24  
25  import org.argeo.cms.e4.jcr.JcrBrowserView;
26  import org.argeo.cms.ui.jcr.model.SingleJcrNodeElem;
27  import org.argeo.cms.ui.jcr.model.WorkspaceElem;
28  import org.argeo.eclipse.ui.TreeParent;
29  import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
30  import org.argeo.eclipse.ui.dialogs.SingleValue;
31  import org.eclipse.e4.core.di.annotations.Execute;
32  import org.eclipse.e4.ui.model.application.ui.basic.MPart;
33  import org.eclipse.e4.ui.services.IServiceConstants;
34  import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
35  
36  /**
37   * Adds a node of type nt:folder, only on {@link SingleJcrNodeElem} and
38   * {@link WorkspaceElem} TreeObject types.
39   * 
40   * This handler assumes that a selection provider is available and picks only
41   * first selected item. It is UI's job to enable the command only when the
42   * selection contains one and only one element. Thus no parameter is passed
43   * through the command.
44   */
45  public class AddFolderNode {
46  	@Execute
47  	public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, ESelectionService selectionService) {
48  		List<?> selection = (List<?>) selectionService.getSelection();
49  		JcrBrowserView view = (JcrBrowserView) part.getObject();
50  
51  		if (selection != null && selection.size() == 1) {
52  			TreeParent treeParentNode = null;
53  			Node jcrParentNode = null;
54  			Object obj = selection.get(0);
55  
56  			if (obj instanceof SingleJcrNodeElem) {
57  				treeParentNode = (TreeParent) obj;
58  				jcrParentNode = ((SingleJcrNodeElem) treeParentNode).getNode();
59  			} else if (obj instanceof WorkspaceElem) {
60  				treeParentNode = (TreeParent) obj;
61  				jcrParentNode = ((WorkspaceElem) treeParentNode).getRootNode();
62  			} else
63  				return;
64  
65  			String folderName = SingleValue.ask("Folder name", "Enter folder name");
66  			if (folderName != null) {
67  				try {
68  					jcrParentNode.addNode(folderName, NodeType.NT_FOLDER);
69  					jcrParentNode.getSession().save();
70  					view.nodeAdded(treeParentNode);
71  				} catch (RepositoryException e) {
72  					ErrorFeedback.show("Cannot create folder " + folderName + " under " + treeParentNode, e);
73  				}
74  			}
75  		} else {
76  			// ErrorFeedback.show(WorkbenchUiPlugin
77  			// .getMessage("errorUnvalidNtFolderNodeType"));
78  			ErrorFeedback.show("Invalid NT folder node type");
79  		}
80  	}
81  
82  }