]> git.argeo.org Git - gpl/argeo-suite.git/blob - ui/people/PersonUiProvider.java
Prepare next development cycle
[gpl/argeo-suite.git] / ui / people / PersonUiProvider.java
1 package org.argeo.app.ui.people;
2
3 import java.util.Arrays;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.argeo.api.acr.Content;
8 import org.argeo.app.ui.SuiteMsg;
9 import org.argeo.app.ui.SuiteStyle;
10 import org.argeo.app.ui.SuiteUiUtils;
11 import org.argeo.cms.CmsUserManager;
12 import org.argeo.cms.Localized;
13 import org.argeo.cms.swt.CmsSwtUtils;
14 import org.argeo.cms.swt.acr.SwtSection;
15 import org.argeo.cms.swt.widgets.EditableText;
16 import org.argeo.cms.ui.CmsUiProvider;
17 import org.argeo.util.naming.LdapAttrs;
18 import org.argeo.util.naming.LdapObjs;
19 import org.argeo.util.naming.QNamed;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.MouseAdapter;
22 import org.eclipse.swt.events.MouseEvent;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.events.SelectionListener;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Text;
31 import org.osgi.service.useradmin.User;
32
33 /** Edit a suite user. */
34 public class PersonUiProvider implements CmsUiProvider {
35 private String[] availableRoles;
36 private CmsUserManager cmsUserManager;
37
38 @Override
39 public Control createUiPart(Composite parent, Content context) {
40 SwtSection main = new SwtSection(parent, SWT.NONE, context);
41 main.setLayoutData(CmsSwtUtils.fillAll());
42
43 main.setLayout(new GridLayout(2, false));
44
45 User user = context.adapt(User.class);
46
47 if (context.hasContentClass(LdapObjs.person.qName())) {
48 addFormLine(main, SuiteMsg.firstName, context, LdapAttrs.givenName);
49 addFormLine(main, SuiteMsg.lastName, context, LdapAttrs.sn);
50 addFormLine(main, SuiteMsg.email, context, LdapAttrs.mail);
51
52 Composite rolesSection = new Composite(main, SWT.NONE);
53 // rolesSection.setText("Roles");
54 rolesSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
55 rolesSection.setLayout(new GridLayout());
56 // new Label(rolesSection, SWT.NONE).setText("Roles:");
57 List<String> roles = Arrays.asList(cmsUserManager.getUserRoles(user.getName()));
58 for (String role : roles) {
59 // new Label(rolesSection, SWT.NONE).setText(role);
60 Button radio = new Button(rolesSection, SWT.CHECK);
61 radio.setText(role);
62 if (roles.contains(role))
63 radio.setSelection(true);
64 }
65
66 // Composite facetsSection = new Composite(main, SWT.NONE);
67 // facetsSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
68 // facetsSection.setLayout(new GridLayout());
69 // if (context.hasContentClass(LdapObjs.groupOfNames.qName())) {
70 // String[] members = context.attr(LdapAttrs.member.qName()).split("\n");
71 // for (String member : members) {
72 // new Label(facetsSection, SWT.NONE).setText(member);
73 // }
74 // }
75 }
76
77 // if (user instanceof Group) {
78 // String cn = context.getName().getLocalPart();
79 // Text cnT = SuiteUiUtils.addFormLine(main, "uid", getUserProperty(user, LdapAttrs.uid.name()));
80 // cnT.setText(cn);
81 //
82 // } else {
83 // String uid = context.getName().getLocalPart();
84 //
85 //// Text givenName = new Text(main, SWT.SINGLE);
86 //// givenName.setText(getUserProperty(user, LdapAttrs.givenName.name()));
87 // Text givenName = SuiteUiUtils.addFormInput(main, SuiteMsg.firstName.lead(),
88 // getUserProperty(user, LdapAttrs.givenName.name()));
89 //
90 // Text sn = SuiteUiUtils.addFormInput(main, SuiteMsg.lastName.lead(),
91 // getUserProperty(user, LdapAttrs.sn.name()));
92 // // sn.setText(getUserProperty(user, LdapAttrs.sn.name()));
93 //
94 // Text email = SuiteUiUtils.addFormInput(main, SuiteMsg.email.lead(),
95 // getUserProperty(user, LdapAttrs.mail.name()));
96 // // email.setText(getUserProperty(user, LdapAttrs.mail.name()));
97 //
98 // Text uidT = SuiteUiUtils.addFormLine(main, "uid", getUserProperty(user, LdapAttrs.uid.name()));
99 // uidT.setText(uid);
100 //
101 //// Label dnL = new Label(main, SWT.NONE);
102 //// dnL.setText(user.getName());
103 //
104 // // roles
105 // // Section rolesSection = new Section(main, SWT.NONE, context);
106 // Composite rolesSection = new Composite(main, SWT.NONE);
107 // // rolesSection.setText("Roles");
108 // rolesSection.setLayoutData(CmsSwtUtils.fillWidth());
109 // rolesSection.setLayout(new GridLayout());
110 // // new Label(rolesSection, SWT.NONE).setText("Roles:");
111 // List<String> roles = Arrays.asList(cmsUserManager.getUserRoles(user.getName()));
112 // for (String role : availableRoles) {
113 // // new Label(rolesSection, SWT.NONE).setText(role);
114 // Button radio = new Button(rolesSection, SWT.CHECK);
115 // radio.setText(role);
116 // if (roles.contains(role))
117 // radio.setSelection(true);
118 // }
119 // }
120
121 return main;
122 }
123
124 private void addFormLine(SwtSection parent, Localized msg, Content context, QNamed attr) {
125 SuiteUiUtils.addFormLabel(parent, msg.lead());
126 EditableText text = new EditableText(parent, SWT.SINGLE | SWT.FLAT);
127 text.setLayoutData(CmsSwtUtils.fillWidth());
128 text.setStyle(SuiteStyle.simpleInput);
129 String txt = context.attr(attr.qName());
130 if (txt == null) // FIXME understand why email is not found in IPA
131 txt = "";
132 text.setText(txt);
133 text.setMouseListener(new MouseAdapter() {
134
135 @Override
136 public void mouseDoubleClick(MouseEvent e) {
137 String currentTxt = text.getText();
138 text.startEditing();
139 text.setText(currentTxt);
140 ((Text) text.getControl()).addSelectionListener(new SelectionListener() {
141
142 @Override
143 public void widgetSelected(SelectionEvent e) {
144 }
145
146 @Override
147 public void widgetDefaultSelected(SelectionEvent e) {
148 String editedTxt = text.getText();
149 text.stopEditing();
150 text.setText(editedTxt);
151 text.getParent().layout(new Control[] { text.getControl() });
152 }
153 });
154 }
155
156 });
157 }
158
159 public void setCmsUserManager(CmsUserManager cmsUserManager) {
160 this.cmsUserManager = cmsUserManager;
161 }
162
163 private String getUserProperty(Object element, String key) {
164 Object value = ((User) element).getProperties().get(key);
165 return value != null ? value.toString() : null;
166 }
167
168 public void init(Map<String, Object> properties) {
169 availableRoles = (String[]) properties.get("availableRoles");
170 // cmsUserManager.getRoles(null);
171 }
172 }