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
.ui
.SecurityUiPlugin
;
10 import org
.argeo
.security
.ui
.commands
.OpenArgeoUserEditor
;
11 import org
.eclipse
.core
.commands
.Command
;
12 import org
.eclipse
.core
.commands
.IParameter
;
13 import org
.eclipse
.core
.commands
.Parameterization
;
14 import org
.eclipse
.core
.commands
.ParameterizedCommand
;
15 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
16 import org
.eclipse
.jface
.viewers
.IDoubleClickListener
;
17 import org
.eclipse
.jface
.viewers
.IStructuredContentProvider
;
18 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
19 import org
.eclipse
.jface
.viewers
.ITableLabelProvider
;
20 import org
.eclipse
.jface
.viewers
.LabelProvider
;
21 import org
.eclipse
.jface
.viewers
.TableViewer
;
22 import org
.eclipse
.jface
.viewers
.Viewer
;
23 import org
.eclipse
.swt
.SWT
;
24 import org
.eclipse
.swt
.graphics
.Image
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Table
;
27 import org
.eclipse
.swt
.widgets
.TableColumn
;
28 import org
.eclipse
.ui
.IWorkbench
;
29 import org
.eclipse
.ui
.IWorkbenchWindow
;
30 import org
.eclipse
.ui
.commands
.ICommandService
;
31 import org
.eclipse
.ui
.handlers
.IHandlerService
;
32 import org
.eclipse
.ui
.part
.ViewPart
;
34 public class UsersView
extends ViewPart
{
35 private TableViewer viewer
;
36 private ArgeoSecurityService securityService
;
39 public void createPartControl(Composite parent
) {
41 // viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
43 viewer
= new TableViewer(createTable(parent
));
44 viewer
.setContentProvider(new UsersContentProvider());
45 viewer
.setLabelProvider(new UsersLabelProvider());
46 viewer
.setInput(getViewSite());
47 viewer
.addDoubleClickListener(new ViewDoubleClickListener());
48 // viewer.setInput(SecurityContextHolder.getContext());
51 protected Table
createTable(Composite parent
) {
52 int style
= SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
;
54 Table table
= new Table(parent
, style
);
56 // GridData gridData = new GridData(GridData.FILL_BOTH);
57 // gridData.grabExcessVerticalSpace = true;
58 // gridData.grabExcessHorizontalSpace = true;
59 // gridData.horizontalSpan = 3;
60 // table.setLayoutData(gridData);
62 table
.setLinesVisible(true);
63 table
.setHeaderVisible(true);
65 TableColumn column
= new TableColumn(table
, SWT
.LEFT
, 0);
69 column
= new TableColumn(table
, SWT
.LEFT
, 1);
70 column
.setText("Password");
73 // column = new TableColumn(table, SWT.LEFT, 2);
74 // column.setText("Roles");
75 // column.setWidth(300);
81 public void setFocus() {
82 viewer
.getTable().setFocus();
85 public void setSecurityService(ArgeoSecurityService securityService
) {
86 this.securityService
= securityService
;
89 private class UsersContentProvider
implements IStructuredContentProvider
{
91 public Object
[] getElements(Object inputElement
) {
92 return getChildren(inputElement
);
95 public Object
[] getChildren(Object parentElement
) {
97 // Thread.sleep(3000);
98 // } catch (InterruptedException e) {
99 // // TODO Auto-generated catch block
100 // e.printStackTrace();
103 return securityService
.getSecurityDao().listUsers().toArray();
106 public void dispose() {
107 // TODO Auto-generated method stub
111 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
112 // TODO Auto-generated method stub
118 private class UsersLabelProvider
extends LabelProvider
implements
119 ITableLabelProvider
{
120 public String
getColumnText(Object element
, int columnIndex
) {
121 String currentUsername
= CurrentUser
.getUsername();
122 ArgeoUser argeoUser
= (ArgeoUser
) element
;
123 switch (columnIndex
) {
125 String userName
= argeoUser
.getUsername();
126 if (userName
.equals(currentUsername
))
127 userName
= userName
+ "*";
130 return argeoUser
.getPassword();
132 return argeoUser
.getRoles().toString();
134 throw new ArgeoException("Unmanaged column " + columnIndex
);
138 public Image
getColumnImage(Object element
, int columnIndex
) {
139 // TODO Auto-generated method stub
145 class ViewDoubleClickListener
implements IDoubleClickListener
{
146 public void doubleClick(DoubleClickEvent evt
) {
147 Object obj
= ((IStructuredSelection
) evt
.getSelection())
150 if (obj
instanceof ArgeoUser
) {
151 ArgeoUser argeoUser
= (ArgeoUser
) obj
;
153 IWorkbench iw
= SecurityUiPlugin
.getDefault().getWorkbench();
154 IHandlerService handlerService
= (IHandlerService
) iw
155 .getService(IHandlerService
.class);
157 String commandId
= OpenArgeoUserEditor
.COMMAND_ID
;
158 String paramName
= OpenArgeoUserEditor
.PARAM_USERNAME
;
160 // TODO: factorize this
161 // execute related command
162 IWorkbenchWindow window
= iw
.getActiveWorkbenchWindow();
163 ICommandService cmdService
= (ICommandService
) window
164 .getService(ICommandService
.class);
165 Command cmd
= cmdService
.getCommand(commandId
);
166 ArrayList
<Parameterization
> parameters
= new ArrayList
<Parameterization
>();
167 IParameter iparam
= cmd
.getParameter(paramName
);
168 Parameterization param
= new Parameterization(iparam
,
169 argeoUser
.getUsername());
170 parameters
.add(param
);
171 ParameterizedCommand pc
= new ParameterizedCommand(cmd
,
172 parameters
.toArray(new Parameterization
[parameters
174 handlerService
= (IHandlerService
) window
175 .getService(IHandlerService
.class);
176 handlerService
.executeCommand(pc
, null);
177 } catch (Exception e
) {
178 throw new ArgeoException("Cannot open editor", e
);