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.ui.workbench.internal.jcr.commands;
17  
18  import javax.jcr.Node;
19  import javax.jcr.RepositoryException;
20  
21  import org.argeo.cms.ui.jcr.model.SingleJcrNodeElem;
22  import org.argeo.cms.ui.jcr.model.WorkspaceElem;
23  import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
24  import org.argeo.cms.ui.workbench.internal.jcr.parts.AddPrivilegeWizard;
25  import org.argeo.eclipse.ui.EclipseUiException;
26  import org.argeo.eclipse.ui.TreeParent;
27  import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
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.ISelection;
32  import org.eclipse.jface.viewers.IStructuredSelection;
33  import org.eclipse.jface.wizard.WizardDialog;
34  import org.eclipse.ui.handlers.HandlerUtil;
35  import org.osgi.service.useradmin.UserAdmin;
36  
37  /** Open a dialog to add privileges on the selected node to a chosen group */
38  public class AddPrivileges extends AbstractHandler {
39  	public final static String ID = WorkbenchUiPlugin.PLUGIN_ID
40  			+ ".addPrivileges";
41  
42  	/* DEPENDENCY INJECTION */
43  	private UserAdmin userAdmin;
44  
45  	public Object execute(ExecutionEvent event) throws ExecutionException {
46  
47  		ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
48  				.getActivePage().getSelection();
49  		if (selection != null && !selection.isEmpty()
50  				&& selection instanceof IStructuredSelection) {
51  			Object obj = ((IStructuredSelection) selection).getFirstElement();
52  			TreeParent treeParentNode = null;
53  			Node jcrParentNode = null;
54  
55  			if (obj instanceof SingleJcrNodeElem) {
56  				treeParentNode = (TreeParent) obj;
57  				jcrParentNode = ((SingleJcrNodeElem) treeParentNode).getNode();
58  			} else if (obj instanceof WorkspaceElem) {
59  				treeParentNode = (TreeParent) obj;
60  				jcrParentNode = ((WorkspaceElem) treeParentNode).getRootNode();
61  			} else
62  				return null;
63  
64  			try {
65  				String targetPath = jcrParentNode.getPath();
66  				AddPrivilegeWizard wizard = new AddPrivilegeWizard(
67  						jcrParentNode.getSession(), targetPath, userAdmin);
68  				WizardDialog dialog = new WizardDialog(
69  						HandlerUtil.getActiveShell(event), wizard);
70  				dialog.open();
71  				return null;
72  			} catch (RepositoryException re) {
73  				throw new EclipseUiException("Unable to retrieve "
74  						+ "path or JCR session to add privilege on "
75  						+ jcrParentNode, re);
76  			}
77  		} else {
78  			ErrorFeedback.show("Cannot add privileges");
79  		}
80  		return null;
81  	}
82  
83  	/* DEPENDENCY INJECTION */
84  	public void setUserAdmin(UserAdmin userAdmin) {
85  		this.userAdmin = userAdmin;
86  	}
87  }