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