]> git.argeo.org Git - gpl/argeo-suite.git/blob - swt/org.argeo.app.ui/src/org/argeo/app/ui/people/GroupUiProvider.java
Load maintenance resources relative to class
[gpl/argeo-suite.git] / swt / org.argeo.app.ui / src / org / argeo / app / ui / people / GroupUiProvider.java
1 package org.argeo.app.ui.people;
2
3 import org.argeo.api.acr.Content;
4 import org.argeo.api.acr.ContentSession;
5 import org.argeo.api.acr.ldap.LdapAttr;
6 import org.argeo.api.acr.ldap.LdapObj;
7 import org.argeo.api.acr.spi.ProvidedContent;
8 import org.argeo.api.cms.directory.CmsGroup;
9 import org.argeo.api.cms.directory.CmsUser;
10 import org.argeo.api.cms.directory.CmsUserManager;
11 import org.argeo.api.cms.directory.HierarchyUnit;
12 import org.argeo.app.ui.SuiteIcon;
13 import org.argeo.app.ui.SuiteMsg;
14 import org.argeo.app.ui.SuiteUiUtils;
15 import org.argeo.cms.CurrentUser;
16 import org.argeo.cms.acr.ContentUtils;
17 import org.argeo.cms.auth.CmsRole;
18 import org.argeo.cms.swt.CmsSwtTheme;
19 import org.argeo.cms.swt.CmsSwtUtils;
20 import org.argeo.cms.swt.Selected;
21 import org.argeo.cms.swt.acr.SwtSection;
22 import org.argeo.cms.swt.acr.SwtUiProvider;
23 import org.argeo.cms.swt.widgets.SwtTableView;
24 import org.argeo.cms.ux.widgets.AbstractTabularPart;
25 import org.argeo.cms.ux.widgets.CmsDialog;
26 import org.argeo.cms.ux.widgets.TabularPart;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.ToolBar;
33 import org.eclipse.swt.widgets.ToolItem;
34 import org.osgi.service.useradmin.Role;
35
36 public class GroupUiProvider implements SwtUiProvider {
37 private CmsUserManager cmsUserManager;
38
39 @Override
40 public Control createUiPart(Composite parent, Content context) {
41 CmsSwtTheme theme = CmsSwtUtils.getCmsTheme(parent);
42
43 Content hierarchyUnitContent = context.getParent().getParent();
44 HierarchyUnit hierarchyUnit = hierarchyUnitContent.adapt(HierarchyUnit.class);
45
46 ContentSession contentSession = ((ProvidedContent) context).getSession();
47
48 TabularPart<Content, Content> membersPart = new AbstractTabularPart<Content, Content>() {
49 Role[] roles;
50
51 @Override
52 public int getItemCount() {
53 roles = context.adapt(CmsGroup.class).getMembers();
54 return roles.length;
55 }
56
57 @Override
58 public Content getData(int row) {
59 Role role = roles[row];
60 Content content = ContentUtils.roleToContent(cmsUserManager, contentSession, role);
61 return content;
62 }
63
64 };
65 membersPart.addColumn(new UserColumn());
66
67 // VIEW
68 SwtSection area = new SwtSection(parent, 0, context);
69 area.setLayoutData(CmsSwtUtils.fillAll());
70 area.setLayout(new GridLayout());
71
72 // title
73 // TODO localise at content level
74 String title;
75 if (context.hasContentClass(LdapObj.organization))
76 title = SuiteMsg.org.lead() + " " + context.attr(LdapAttr.cn) + " ("
77 + hierarchyUnit.getHierarchyUnitLabel(CurrentUser.locale()) + ")";
78 else
79 title = SuiteMsg.group.lead() + " " + context.attr(LdapAttr.cn) + " ("
80 + hierarchyUnit.getHierarchyUnitLabel(CurrentUser.locale()) + ")";
81 SuiteUiUtils.addFormLabel(area, title);
82
83 // toolbar
84 ToolBar toolBar = new ToolBar(area, SWT.NONE);
85 toolBar.setLayoutData(new GridData(SWT.END, SWT.FILL, true, false));
86
87 ToolItem deleteItem = new ToolItem(toolBar, SWT.FLAT);
88 deleteItem.setEnabled(false);
89 deleteItem.setImage(theme.getSmallIcon(SuiteIcon.delete));
90
91 ToolItem addItem = new ToolItem(toolBar, SWT.FLAT);
92 addItem.setImage(theme.getSmallIcon(SuiteIcon.add));
93 addItem.setEnabled(CurrentUser.implies(CmsRole.groupAdmin, hierarchyUnit.getBase()));
94
95 // members view
96 SwtTableView<Content, Content> membersView = new SwtTableView<>(area, SWT.BORDER, membersPart);
97 membersView.setLayoutData(CmsSwtUtils.fillAll());
98 membersView.refresh();
99
100 // CONTROLLER
101 membersPart.onSelected((model) -> {
102 deleteItem.setEnabled(CurrentUser.implies(CmsRole.groupAdmin, hierarchyUnit.getBase()));
103 deleteItem.setData(model);
104 });
105
106 addItem.addSelectionListener((Selected) (e) -> {
107 ChooseUserDialog chooseUserDialog = new ChooseUserDialog(parent.getDisplay().getActiveShell(),
108 SuiteMsg.chooseAMember.lead(), contentSession, cmsUserManager, hierarchyUnit);
109 if (chooseUserDialog.open() == CmsDialog.OK) {
110 Content chosen = chooseUserDialog.getSelected();
111 cmsUserManager.addMember(context.adapt(CmsGroup.class), chosen.adapt(CmsUser.class));
112 membersPart.refresh();
113 }
114 });
115
116 deleteItem.addSelectionListener((Selected) (e) -> {
117 if (deleteItem.getData() != null) {
118 Content chosen = (Content) deleteItem.getData();
119 cmsUserManager.removeMember(context.adapt(CmsGroup.class), chosen.adapt(CmsUser.class));
120 membersPart.refresh();
121 }
122 });
123
124 return membersView;
125
126 }
127
128 public void setCmsUserManager(CmsUserManager cmsUserManager) {
129 this.cmsUserManager = cmsUserManager;
130 }
131
132 }