2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.cms
.e4
.users
.handlers
;
18 import java
.util
.List
;
20 import javax
.inject
.Inject
;
21 import javax
.inject
.Named
;
23 import org
.argeo
.cms
.e4
.users
.UserAdminWrapper
;
24 import org
.argeo
.cms
.e4
.users
.UsersView
;
25 import org
.argeo
.cms
.util
.UserAdminUtils
;
26 import org
.eclipse
.e4
.core
.di
.annotations
.CanExecute
;
27 import org
.eclipse
.e4
.core
.di
.annotations
.Execute
;
28 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
29 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
30 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
31 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
32 import org
.eclipse
.swt
.widgets
.Display
;
33 import org
.osgi
.service
.useradmin
.User
;
34 import org
.osgi
.service
.useradmin
.UserAdmin
;
35 import org
.osgi
.service
.useradmin
.UserAdminEvent
;
37 /** Delete the selected users */
38 public class DeleteUsers
{
39 // public final static String ID = WorkbenchUiPlugin.PLUGIN_ID + ".deleteUsers";
41 /* DEPENDENCY INJECTION */
43 private UserAdminWrapper userAdminWrapper
;
45 @SuppressWarnings("unchecked")
47 public void execute(@Named(IServiceConstants
.ACTIVE_PART
) MPart part
, ESelectionService selectionService
) {
48 // ISelection selection = null;// HandlerUtil.getCurrentSelection(event);
49 // if (selection.isEmpty())
51 List
<User
> selection
= (List
<User
>) selectionService
.getSelection();
52 if (selection
== null)
55 // Iterator<User> it = ((IStructuredSelection) selection).iterator();
56 // List<User> users = new ArrayList<User>();
57 StringBuilder builder
= new StringBuilder();
59 for(User user
:selection
) {
61 // User currUser = it.next();
62 String userName
= UserAdminUtils
.getUserLocalId(currUser
.getName());
63 if (UserAdminUtils
.isCurrentUser(currUser
)) {
64 MessageDialog
.openError(Display
.getCurrent().getActiveShell(), "Deletion forbidden",
65 "You cannot delete your own user this way.");
68 builder
.append(userName
).append("; ");
69 // users.add(currUser);
72 if (!MessageDialog
.openQuestion(Display
.getCurrent().getActiveShell(), "Delete Users",
73 "Are you sure that you want to delete these users?\n" + builder
.substring(0, builder
.length() - 2)))
76 userAdminWrapper
.beginTransactionIfNeeded();
77 UserAdmin userAdmin
= userAdminWrapper
.getUserAdmin();
78 // IWorkbenchPage iwp =
79 // HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
81 for (User user
: selection
) {
82 String userName
= user
.getName();
83 // TODO find a way to close the editor cleanly if opened. Cannot be
84 // done through the UserAdminListeners, it causes a
85 // java.util.ConcurrentModificationException because disposing the
86 // editor unregisters and disposes the listener
87 // IEditorPart part = iwp.findEditor(new UserEditorInput(userName));
89 // iwp.closeEditor(part, false);
90 userAdmin
.removeRole(userName
);
92 userAdminWrapper
.commitOrNotifyTransactionStateChange();
94 for (User user
: selection
) {
95 userAdminWrapper
.notifyListeners(new UserAdminEvent(null, UserAdminEvent
.ROLE_REMOVED
, user
));
100 public boolean canExecute(@Named(IServiceConstants
.ACTIVE_PART
) MPart part
, ESelectionService selectionService
) {
101 return part
.getObject() instanceof UsersView
&& selectionService
.getSelection() != null;