1 package org.argeo.people.e4.parts;
2
3 import javax.inject.Inject;
4 import javax.jcr.Node;
5 import javax.jcr.Property;
6 import javax.jcr.PropertyType;
7 import javax.jcr.RepositoryException;
8 import javax.jcr.Session;
9 import javax.jcr.Value;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.argeo.activities.ActivitiesService;
14 import org.argeo.activities.ui.RelatedActivityList;
15 import org.argeo.cms.ui.eclipse.forms.AbstractFormPart;
16 import org.argeo.cms.util.CmsUtils;
17 import org.argeo.connect.ConnectConstants;
18 import org.argeo.connect.e4.ConnectE4Msg;
19 import org.argeo.connect.resources.ResourcesNames;
20 import org.argeo.connect.resources.ResourcesService;
21 import org.argeo.connect.ui.ConnectUiConstants;
22 import org.argeo.connect.ui.ConnectUiUtils;
23 import org.argeo.connect.ui.ConnectWorkbenchUtils;
24 import org.argeo.connect.ui.parts.TagLikeListPart;
25 import org.argeo.connect.ui.util.LazyCTabControl;
26 import org.argeo.connect.util.ConnectJcrUtils;
27 import org.argeo.eclipse.ui.EclipseUiUtils;
28 import org.argeo.people.PeopleException;
29 import org.argeo.people.PeopleNames;
30 import org.argeo.people.PeopleService;
31 import org.argeo.people.PeopleTypes;
32 import org.argeo.people.e4.PeopleRapConstants;
33 import org.argeo.people.ui.PeopleMsg;
34 import org.argeo.people.ui.providers.PersonOverviewLabelProvider;
35 import org.eclipse.jface.viewers.ColumnLabelProvider;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.custom.CTabFolder;
38 import org.eclipse.swt.events.ModifyEvent;
39 import org.eclipse.swt.events.ModifyListener;
40 import org.eclipse.swt.layout.FormLayout;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.layout.GridLayout;
43 import org.eclipse.swt.layout.RowLayout;
44 import org.eclipse.swt.widgets.Button;
45 import org.eclipse.swt.widgets.Composite;
46 import org.eclipse.swt.widgets.Event;
47 import org.eclipse.swt.widgets.Label;
48 import org.eclipse.swt.widgets.Listener;
49 import org.eclipse.swt.widgets.Text;
50
51
52 public class PersonEditor extends AbstractPeopleWithImgEditor implements PeopleNames {
53 final static Log log = LogFactory.getLog(PersonEditor.class);
54
55
56
57
58
59 @Inject
60 private ActivitiesService activitiesService;
61 @Inject
62 private PeopleService peopleService;
63
64 private Node person;
65
66 public PersonEditor() {
67
68
69
70 }
71
72 public void init() {
73 super.init();
74 person = getNode();
75 }
76
77 @Override
78 protected void updatePartName() {
79 String shortName = ConnectJcrUtils.get(getNode(), Property.JCR_TITLE);
80 if (EclipseUiUtils.notEmpty(shortName)) {
81 if (shortName.length() > SHORT_NAME_LENGHT)
82 shortName = shortName.substring(0, SHORT_NAME_LENGHT - 1) + "...";
83 setPartName(shortName);
84 }
85 }
86
87 @Override
88 protected void populateHeader(Composite parent) {
89 GridLayout gl = EclipseUiUtils.noSpaceGridLayout();
90 gl.marginBottom = 10;
91 parent.setLayout(gl);
92
93
94 Composite titleCmp = getFormToolkit().createComposite(parent, SWT.NO_FOCUS);
95 titleCmp.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
96 populateTitleComposite(titleCmp);
97
98
99 Composite tagsCmp = new TagLikeListPart(this, parent, SWT.NO_FOCUS, getResourcesService(),
100 getSystemWorkbenchService(), ConnectConstants.RESOURCE_TAG, person, ResourcesNames.CONNECT_TAGS,
101 ConnectE4Msg.addTag.lead());
102 tagsCmp.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
103
104
105
106
107
108
109
110
111 }
112
113 @Override
114 protected void populateTabFolder(CTabFolder folder) {
115
116 String tooltip = "";
117 LazyCTabControl cpc = new ContactListCTab(folder, SWT.NO_FOCUS, this, getNode(), getResourcesService(),
118 getPeopleService(), getSystemWorkbenchService());
119 cpc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
120 addLazyTabToFolder(folder, cpc, PeopleMsg.personDetails.lead(), PeopleRapConstants.CTAB_CONTACT_DETAILS,
121 tooltip);
122
123
124 tooltip = "";
125
126 LazyCTabControl activitiesCmp = new RelatedActivityList(folder, SWT.NO_FOCUS, this, getUserAdminService(),
127 getResourcesService(), getActivitiesService(), getSystemAppService(), getSystemWorkbenchService(),
128 person);
129 activitiesCmp.setLayoutData(EclipseUiUtils.fillAll());
130 addLazyTabToFolder(folder, activitiesCmp, PeopleMsg.personActivityLog.lead(),
131 PeopleRapConstants.CTAB_ACTIVITY_LOG, tooltip);
132
133
134 tooltip = "";
135 LazyCTabControl crewCmp = new JobListCTab(folder, SWT.NO_FOCUS, this, getResourcesService(), getPeopleService(),
136 getSystemWorkbenchService(), person);
137 crewCmp.setLayoutData(EclipseUiUtils.fillAll());
138 addLazyTabToFolder(folder, crewCmp, PeopleMsg.personOrgs.lead(), PeopleRapConstants.CTAB_JOBS, tooltip);
139
140
141 try {
142 if (person.isNodeType(PeopleTypes.PEOPLE_USER)) {
143 String dn = person.getProperty(PeopleNames.PEOPLE_USERNAME).getString();
144 tooltip = "";
145 LazyCTabControl securityCmp = new PersonSecurityCTab(folder, SWT.NO_FOCUS, this, getUserAdminService(),
146 dn);
147 addLazyTabToFolder(folder, securityCmp, PeopleMsg.personSecurity.lead(),
148 PeopleRapConstants.CTAB_SECURITY, tooltip);
149 }
150 } catch (RepositoryException e) {
151
152 e.printStackTrace();
153 }
154 }
155
156 protected void populateTitleComposite(Composite parent) {
157 parent.setLayout(new FormLayout());
158
159
160 final Composite readOnlyPanel = getFormToolkit().createComposite(parent, SWT.NO_FOCUS);
161 ConnectUiUtils.setSwitchingFormData(readOnlyPanel);
162 readOnlyPanel.setLayout(new GridLayout());
163
164
165 final Label readOnlyInfoLbl = getFormToolkit().createLabel(readOnlyPanel, "", SWT.WRAP);
166 CmsUtils.markup(readOnlyInfoLbl);
167 final ColumnLabelProvider personLP = new PersonOverviewLabelProvider(
168 ConnectUiConstants.LIST_TYPE_OVERVIEW_TITLE, getResourcesService(), getPeopleService(),
169 getSystemWorkbenchService());
170
171
172 final Composite editPanelCmp = getFormToolkit().createComposite(parent, SWT.NO_FOCUS);
173 ConnectUiUtils.setSwitchingFormData(editPanelCmp);
174 editPanelCmp.setLayout(EclipseUiUtils.noSpaceGridLayout());
175
176
177 Composite firstCmp = getFormToolkit().createComposite(editPanelCmp, SWT.NO_FOCUS);
178 firstCmp.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
179 RowLayout rl = new RowLayout(SWT.HORIZONTAL);
180 rl.wrap = false;
181 firstCmp.setLayout(rl);
182
183
184 Composite secondCmp = getFormToolkit().createComposite(editPanelCmp, SWT.NO_FOCUS);
185 secondCmp.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
186 rl = new RowLayout(SWT.HORIZONTAL);
187 rl.wrap = false;
188 secondCmp.setLayout(rl);
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216 final Text salutationTxt = ConnectUiUtils.createRDText(getFormToolkit(), secondCmp, PeopleMsg.salutation.lead(),
217 "", 60);
218 final Text firstNameTxt = ConnectUiUtils.createRDText(getFormToolkit(), secondCmp, PeopleMsg.firstName.lead(),
219 "", 100);
220
221
222
223
224 final Text lastNameTxt = ConnectUiUtils.createRDText(getFormToolkit(), secondCmp, PeopleMsg.lastName.lead(), "",
225 100);
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275 final AbstractFormPart editPart = new AbstractFormPart() {
276 public void refresh() {
277
278
279
280
281
282
283
284
285
286
287 ConnectUiUtils.refreshTextWidgetValue(salutationTxt, person, PEOPLE_SALUTATION);
288 ConnectUiUtils.refreshTextWidgetValue(firstNameTxt, person, PEOPLE_FIRST_NAME);
289
290
291 ConnectUiUtils.refreshTextWidgetValue(lastNameTxt, person, PEOPLE_LAST_NAME);
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307 String roText = personLP.getText(person);
308 readOnlyInfoLbl.setText(roText);
309
310
311 if (isEditing())
312 editPanelCmp.moveAbove(readOnlyPanel);
313 else
314 editPanelCmp.moveBelow(readOnlyPanel);
315
316 readOnlyInfoLbl.pack();
317 editPanelCmp.getParent().layout();
318 super.refresh();
319 }
320 };
321
322
323
324 ConnectWorkbenchUtils.addModifyListener(firstNameTxt, person, PeopleNames.PEOPLE_FIRST_NAME, editPart);
325 ConnectWorkbenchUtils.addModifyListener(lastNameTxt, person, PeopleNames.PEOPLE_LAST_NAME, editPart);
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355 ConnectWorkbenchUtils.addTxtModifyListener(editPart, salutationTxt, person, PEOPLE_SALUTATION,
356 PropertyType.STRING);
357
358
359
360 ConnectWorkbenchUtils.addTxtModifyListener(editPart, lastNameTxt, person, PEOPLE_LAST_NAME,
361 PropertyType.STRING);
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395 Listener langRadioListener = new Listener() {
396 private static final long serialVersionUID = 1L;
397
398 public void handleEvent(Event event) {
399 Button btn = (Button) event.widget;
400 try {
401 if (!btn.getSelection())
402 return;
403
404 Session session = ConnectJcrUtils.getSession(person);
405 String newValueIso = getResourcesService().getEncodedTagCodeFromValue(session,
406 ConnectConstants.RESOURCE_LANG, btn.getText());
407 String oldValueIso = null;
408 if (person.hasProperty(PEOPLE_SPOKEN_LANGUAGES)) {
409 Value[] values = person.getProperty(PEOPLE_SPOKEN_LANGUAGES).getValues();
410 if (values[0] != null)
411 oldValueIso = values[0].getString();
412 }
413 if (!newValueIso.equals(oldValueIso)) {
414 String[] newVals = { newValueIso };
415 person.setProperty(PEOPLE_SPOKEN_LANGUAGES, newVals);
416 editPart.markDirty();
417 }
418 } catch (RepositoryException e) {
419 throw new PeopleException("Unable to update " + "spooken language on " + person, e);
420 }
421 }
422
423 };
424
425
426
427 editPart.initialize(getManagedForm());
428 getManagedForm().addPart(editPart);
429 }
430
431 private void addDNameModifyListener(final Text text, final Button useDistinctBtn, final Node node,
432 final String propName, final AbstractFormPart part) {
433 text.addModifyListener(new ModifyListener() {
434 private static final long serialVersionUID = 1L;
435
436 @Override
437 public void modifyText(ModifyEvent event) {
438 if (useDistinctBtn.getSelection()) {
439 if (ConnectJcrUtils.setJcrProperty(node, propName, PropertyType.STRING, text.getText()))
440 part.markDirty();
441 }
442 }
443 });
444 }
445
446 private void refreshFormalRadio(Button button, Node entity) {
447 Boolean tmp = false;
448 try {
449 if (entity.hasProperty(PEOPLE_USE_POLITE_FORM)) {
450 boolean value = entity.getProperty(PEOPLE_USE_POLITE_FORM).getBoolean();
451 if ("Formal".equals(button.getText()) && value || "Informal".equals(button.getText()) && !value)
452 tmp = true;
453 }
454 button.setSelection(tmp);
455 button.setEnabled(isEditing());
456 } catch (RepositoryException re) {
457 throw new PeopleException("Error getting polite form property on " + entity, re);
458 }
459 }
460
461 private void refreshLangRadio(Button button, Node entity) {
462 Boolean tmp = false;
463 try {
464 if (entity.hasProperty(PEOPLE_SPOKEN_LANGUAGES)) {
465 Value[] values = entity.getProperty(PEOPLE_SPOKEN_LANGUAGES).getValues();
466 String isoVal = null;
467 if (values[0] != null)
468 isoVal = values[0].getString();
469 ResourcesService rs = getResourcesService();
470 if (isoVal != null && rs.getEncodedTagValue(getSession(), ConnectConstants.RESOURCE_LANG, isoVal)
471 .equals(button.getText()))
472 tmp = true;
473 }
474 button.setSelection(tmp);
475 button.setEnabled(isEditing());
476 } catch (RepositoryException re) {
477 throw new PeopleException("Error getting polite form property on " + entity, re);
478 }
479 }
480
481 protected ActivitiesService getActivitiesService() {
482 return activitiesService;
483 }
484
485 protected PeopleService getPeopleService() {
486 return peopleService;
487 }
488
489
490 public void setActivitiesService(ActivitiesService activitiesService) {
491 this.activitiesService = activitiesService;
492 }
493
494 public void setPeopleService(PeopleService peopleService) {
495 this.peopleService = peopleService;
496 }
497 }