1 package org
.argeo
.security
.ui
.views
;
3 import java
.util
.ArrayList
;
5 import org
.argeo
.ArgeoException
;
6 import org
.argeo
.security
.ArgeoSecurityService
;
7 import org
.argeo
.security
.ArgeoUser
;
8 import org
.argeo
.security
.equinox
.CurrentUser
;
9 import org
.argeo
.security
.nature
.SimpleUserNature
;
10 import org
.argeo
.security
.ui
.SecurityUiPlugin
;
11 import org
.argeo
.security
.ui
.commands
.OpenArgeoUserEditor
;
12 import org
.eclipse
.core
.commands
.Command
;
13 import org
.eclipse
.core
.commands
.IParameter
;
14 import org
.eclipse
.core
.commands
.Parameterization
;
15 import org
.eclipse
.core
.commands
.ParameterizedCommand
;
16 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
17 import org
.eclipse
.jface
.viewers
.IDoubleClickListener
;
18 import org
.eclipse
.jface
.viewers
.IStructuredContentProvider
;
19 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
20 import org
.eclipse
.jface
.viewers
.ITableLabelProvider
;
21 import org
.eclipse
.jface
.viewers
.LabelProvider
;
22 import org
.eclipse
.jface
.viewers
.TableViewer
;
23 import org
.eclipse
.jface
.viewers
.Viewer
;
24 import org
.eclipse
.swt
.SWT
;
25 import org
.eclipse
.swt
.graphics
.Image
;
26 import org
.eclipse
.swt
.widgets
.Composite
;
27 import org
.eclipse
.swt
.widgets
.Table
;
28 import org
.eclipse
.swt
.widgets
.TableColumn
;
29 import org
.eclipse
.ui
.IWorkbench
;
30 import org
.eclipse
.ui
.IWorkbenchWindow
;
31 import org
.eclipse
.ui
.commands
.ICommandService
;
32 import org
.eclipse
.ui
.handlers
.IHandlerService
;
33 import org
.eclipse
.ui
.part
.ViewPart
;
35 /** List all users. */
36 public class UsersView
extends ViewPart
{
37 public final static String ID
= "org.argeo.security.ui.usersView";
39 private TableViewer viewer
;
40 private ArgeoSecurityService securityService
;
42 private String simpleNatureType
= null;
45 public void createPartControl(Composite parent
) {
46 viewer
= new TableViewer(createTable(parent
));
47 viewer
.setContentProvider(new UsersContentProvider());
48 viewer
.setLabelProvider(new UsersLabelProvider());
49 viewer
.addDoubleClickListener(new ViewDoubleClickListener());
50 viewer
.setInput(getViewSite());
53 protected Table
createTable(Composite parent
) {
54 Table table
= new Table(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
);
55 table
.setLinesVisible(true);
56 table
.setHeaderVisible(true);
57 TableColumn column
= new TableColumn(table
, SWT
.LEFT
, 0);
58 column
.setText("User");
60 column
= new TableColumn(table
, SWT
.LEFT
, 1);
61 column
.setText("First Name");
63 column
= new TableColumn(table
, SWT
.LEFT
, 2);
64 column
.setText("Last Name");
66 column
= new TableColumn(table
, SWT
.LEFT
, 3);
67 column
.setText("E-mail");
73 public void setFocus() {
74 viewer
.getTable().setFocus();
77 public void setSecurityService(ArgeoSecurityService securityService
) {
78 this.securityService
= securityService
;
81 public void setSimpleNatureType(String simpleNatureType
) {
82 this.simpleNatureType
= simpleNatureType
;
85 public void refresh() {
89 private class UsersContentProvider
implements IStructuredContentProvider
{
91 public Object
[] getElements(Object inputElement
) {
92 return securityService
.getSecurityDao().listUsers().toArray();
95 public void dispose() {
98 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
103 private class UsersLabelProvider
extends LabelProvider
implements
104 ITableLabelProvider
{
105 public String
getColumnText(Object element
, int columnIndex
) {
106 String currentUsername
= CurrentUser
.getUsername();
107 ArgeoUser user
= (ArgeoUser
) element
;
108 SimpleUserNature simpleNature
= SimpleUserNature
109 .findSimpleUserNature(user
, simpleNatureType
);
110 switch (columnIndex
) {
112 String userName
= user
.getUsername();
113 if (userName
.equals(currentUsername
))
114 userName
= userName
+ "*";
117 return simpleNature
.getFirstName();
119 return simpleNature
.getLastName();
121 return simpleNature
.getEmail();
123 throw new ArgeoException("Unmanaged column " + columnIndex
);
127 public Image
getColumnImage(Object element
, int columnIndex
) {
128 // TODO Auto-generated method stub
134 class ViewDoubleClickListener
implements IDoubleClickListener
{
135 public void doubleClick(DoubleClickEvent evt
) {
136 Object obj
= ((IStructuredSelection
) evt
.getSelection())
139 if (obj
instanceof ArgeoUser
) {
140 ArgeoUser argeoUser
= (ArgeoUser
) obj
;
142 IWorkbench iw
= SecurityUiPlugin
.getDefault().getWorkbench();
143 IHandlerService handlerService
= (IHandlerService
) iw
144 .getService(IHandlerService
.class);
146 String commandId
= OpenArgeoUserEditor
.COMMAND_ID
;
147 String paramName
= OpenArgeoUserEditor
.PARAM_USERNAME
;
149 // TODO: factorize this
150 // execute related command
151 IWorkbenchWindow window
= iw
.getActiveWorkbenchWindow();
152 ICommandService cmdService
= (ICommandService
) window
153 .getService(ICommandService
.class);
154 Command cmd
= cmdService
.getCommand(commandId
);
155 ArrayList
<Parameterization
> parameters
= new ArrayList
<Parameterization
>();
156 IParameter iparam
= cmd
.getParameter(paramName
);
157 Parameterization param
= new Parameterization(iparam
,
158 argeoUser
.getUsername());
159 parameters
.add(param
);
160 ParameterizedCommand pc
= new ParameterizedCommand(cmd
,
161 parameters
.toArray(new Parameterization
[parameters
163 handlerService
= (IHandlerService
) window
164 .getService(IHandlerService
.class);
165 handlerService
.executeCommand(pc
, null);
166 } catch (Exception e
) {
167 throw new ArgeoException("Cannot open editor", e
);