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 private TableViewer viewer
;
38 private ArgeoSecurityService securityService
;
40 private String simpleNatureType
= null;
43 public void createPartControl(Composite parent
) {
44 viewer
= new TableViewer(createTable(parent
));
45 viewer
.setContentProvider(new UsersContentProvider());
46 viewer
.setLabelProvider(new UsersLabelProvider());
47 viewer
.addDoubleClickListener(new ViewDoubleClickListener());
48 viewer
.setInput(getViewSite());
51 protected Table
createTable(Composite parent
) {
52 Table table
= new Table(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
);
53 table
.setLinesVisible(true);
54 table
.setHeaderVisible(true);
55 TableColumn column
= new TableColumn(table
, SWT
.LEFT
, 0);
56 column
.setText("User");
58 column
= new TableColumn(table
, SWT
.LEFT
, 1);
59 column
.setText("First Name");
61 column
= new TableColumn(table
, SWT
.LEFT
, 2);
62 column
.setText("Last Name");
64 column
= new TableColumn(table
, SWT
.LEFT
, 3);
65 column
.setText("E-mail");
71 public void setFocus() {
72 viewer
.getTable().setFocus();
75 public void setSecurityService(ArgeoSecurityService securityService
) {
76 this.securityService
= securityService
;
79 public void setSimpleNatureType(String simpleNatureType
) {
80 this.simpleNatureType
= simpleNatureType
;
83 private class UsersContentProvider
implements IStructuredContentProvider
{
85 public Object
[] getElements(Object inputElement
) {
86 return securityService
.getSecurityDao().listUsers().toArray();
89 public void dispose() {
92 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
97 private class UsersLabelProvider
extends LabelProvider
implements
99 public String
getColumnText(Object element
, int columnIndex
) {
100 String currentUsername
= CurrentUser
.getUsername();
101 ArgeoUser user
= (ArgeoUser
) element
;
102 SimpleUserNature simpleNature
= SecurityUiPlugin
103 .findSimpleUserNature(user
, simpleNatureType
);
104 switch (columnIndex
) {
106 String userName
= user
.getUsername();
107 if (userName
.equals(currentUsername
))
108 userName
= userName
+ "*";
111 return simpleNature
.getFirstName();
113 return simpleNature
.getLastName();
115 return simpleNature
.getEmail();
117 throw new ArgeoException("Unmanaged column " + columnIndex
);
121 public Image
getColumnImage(Object element
, int columnIndex
) {
122 // TODO Auto-generated method stub
128 class ViewDoubleClickListener
implements IDoubleClickListener
{
129 public void doubleClick(DoubleClickEvent evt
) {
130 Object obj
= ((IStructuredSelection
) evt
.getSelection())
133 if (obj
instanceof ArgeoUser
) {
134 ArgeoUser argeoUser
= (ArgeoUser
) obj
;
136 IWorkbench iw
= SecurityUiPlugin
.getDefault().getWorkbench();
137 IHandlerService handlerService
= (IHandlerService
) iw
138 .getService(IHandlerService
.class);
140 String commandId
= OpenArgeoUserEditor
.COMMAND_ID
;
141 String paramName
= OpenArgeoUserEditor
.PARAM_USERNAME
;
143 // TODO: factorize this
144 // execute related command
145 IWorkbenchWindow window
= iw
.getActiveWorkbenchWindow();
146 ICommandService cmdService
= (ICommandService
) window
147 .getService(ICommandService
.class);
148 Command cmd
= cmdService
.getCommand(commandId
);
149 ArrayList
<Parameterization
> parameters
= new ArrayList
<Parameterization
>();
150 IParameter iparam
= cmd
.getParameter(paramName
);
151 Parameterization param
= new Parameterization(iparam
,
152 argeoUser
.getUsername());
153 parameters
.add(param
);
154 ParameterizedCommand pc
= new ParameterizedCommand(cmd
,
155 parameters
.toArray(new Parameterization
[parameters
157 handlerService
= (IHandlerService
) window
158 .getService(IHandlerService
.class);
159 handlerService
.executeCommand(pc
, null);
160 } catch (Exception e
) {
161 throw new ArgeoException("Cannot open editor", e
);