View Javadoc
1   package org.argeo.people.e4.parts;
2   
3   import javax.inject.Inject;
4   import javax.jcr.Node;
5   import javax.jcr.Property;
6   
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   import org.argeo.activities.ActivitiesService;
10  import org.argeo.activities.ui.RelatedActivityList;
11  import org.argeo.cms.ui.eclipse.forms.AbstractFormPart;
12  import org.argeo.cms.util.CmsUtils;
13  import org.argeo.connect.ConnectConstants;
14  import org.argeo.connect.e4.ConnectE4Msg;
15  import org.argeo.connect.resources.ResourcesNames;
16  import org.argeo.connect.ui.ConnectUiUtils;
17  import org.argeo.connect.ui.parts.TagLikeListPart;
18  import org.argeo.connect.ui.util.LazyCTabControl;
19  import org.argeo.connect.util.ConnectJcrUtils;
20  import org.argeo.eclipse.ui.EclipseUiUtils;
21  import org.argeo.people.PeopleException;
22  import org.argeo.people.PeopleService;
23  import org.argeo.people.e4.PeopleRapConstants;
24  import org.argeo.people.ui.PeopleMsg;
25  import org.argeo.people.ui.providers.OrgOverviewLabelProvider;
26  import org.eclipse.jface.viewers.ColumnLabelProvider;
27  import org.eclipse.swt.SWT;
28  import org.eclipse.swt.custom.CTabFolder;
29  import org.eclipse.swt.layout.FormLayout;
30  import org.eclipse.swt.layout.GridData;
31  import org.eclipse.swt.layout.GridLayout;
32  import org.eclipse.swt.widgets.Composite;
33  import org.eclipse.swt.widgets.Label;
34  
35  /** Display an organisation with corresponding details */
36  public class OrgEditor extends AbstractPeopleWithImgEditor {
37  	// public final static String ID = PeopleRapPlugin.PLUGIN_ID + ".orgEditor";
38  	// private BundleContext bc =
39  	// FrameworkUtil.getBundle(OrgEditor.class).getBundleContext();
40  
41  	final static Log log = LogFactory.getLog(OrgEditor.class);
42  
43  	// Context
44  	@Inject
45  	private ActivitiesService activitiesService;
46  	@Inject
47  	private PeopleService peopleService;
48  
49  	private Node org;
50  
51  	public OrgEditor() {
52  		// peopleService = bc.getService(bc.getServiceReference(PeopleService.class));
53  		// activitiesService =
54  		// bc.getService(bc.getServiceReference(ActivitiesService.class));
55  	}
56  
57  	public void init() {
58  		super.init();
59  		org = getNode();
60  	}
61  
62  	@Override
63  	protected void updatePartName() {
64  		String shortName = ConnectJcrUtils.get(getNode(), Property.JCR_TITLE);
65  		if (EclipseUiUtils.notEmpty(shortName)) {
66  			if (shortName.length() > SHORT_NAME_LENGHT)
67  				shortName = shortName.substring(0, SHORT_NAME_LENGHT - 1) + "...";
68  			setPartName(shortName);
69  		}
70  	}
71  
72  	@Override
73  	protected void populateHeader(Composite parent) {
74  		GridLayout gl = EclipseUiUtils.noSpaceGridLayout();
75  		gl.marginBottom = 10;
76  		parent.setLayout(gl);
77  
78  		Composite titleCmp = getFormToolkit().createComposite(parent, SWT.NO_FOCUS);
79  		titleCmp.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
80  		populateTitleComposite(titleCmp);
81  
82  		// Tags Management
83  		Composite tagsCmp = new TagLikeListPart(this, parent, SWT.NO_FOCUS, getResourcesService(),
84  				getSystemWorkbenchService(), ConnectConstants.RESOURCE_TAG, org, ResourcesNames.CONNECT_TAGS,
85  				ConnectE4Msg.addTag.lead());
86  		tagsCmp.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
87  		//
88  		// // Mailing lists management
89  		// Composite mlCmp = new MailingListListPart(this, parent, SWT.NO_FOCUS,
90  		// getResourcesService(),
91  		// getSystemWorkbenchService(), PeopleTypes.PEOPLE_MAILING_LIST, org,
92  		// PeopleNames.PEOPLE_MAILING_LISTS,
93  		// "Add a mailing");
94  		// mlCmp.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
95  	}
96  
97  	protected void populateTabFolder(CTabFolder folder) {
98  		// Contact informations
99  		String tooltip = "";// "Contact information for " + JcrUtils.get(org,
100 							// PeopleNames.PEOPLE_LEGAL_NAME);
101 		LazyCTabControl cpc = new ContactListCTab(folder, SWT.NO_FOCUS, this, getNode(), getResourcesService(),
102 				getPeopleService(), getSystemWorkbenchService());
103 		cpc.setLayoutData(EclipseUiUtils.fillAll());
104 		addLazyTabToFolder(folder, cpc, PeopleMsg.orgDetails.lead(), PeopleRapConstants.CTAB_CONTACT_DETAILS, tooltip);
105 
106 		// Activities and tasks
107 		tooltip = "";// "Activities and tasks related to " + JcrUtils.get(org, Property.JCR_TITLE);
108 		LazyCTabControl activitiesCmp = new RelatedActivityList(folder, SWT.NO_FOCUS, this, getUserAdminService(),
109 				getResourcesService(), getActivitiesService(), getSystemAppService(), getSystemWorkbenchService(), org);
110 		activitiesCmp.setLayoutData(EclipseUiUtils.fillAll());
111 		addLazyTabToFolder(folder, activitiesCmp, PeopleMsg.orgActivityLog.lead(), PeopleRapConstants.CTAB_ACTIVITY_LOG,
112 				tooltip);
113 
114 		// Employees
115 		tooltip = "";// "Known employees of " + JcrUtils.get(org, PeopleNames.PEOPLE_LEGAL_NAME);
116 		LazyCTabControl employeesCmp = new JobListCTab(folder, SWT.NO_FOCUS, this, getResourcesService(),
117 				getPeopleService(), getSystemWorkbenchService(), org);
118 		employeesCmp.setLayoutData(EclipseUiUtils.fillAll());
119 		addLazyTabToFolder(folder, employeesCmp, PeopleMsg.team.lead(), PeopleRapConstants.CTAB_EMPLOYEES, tooltip);
120 
121 		// Legal informations
122 		tooltip = "";// "Legal information for " + JcrUtils.get(org, PeopleNames.PEOPLE_LEGAL_NAME);
123 		LazyCTabControl legalCmp = new OrgAdminInfoCTab(folder, SWT.NO_FOCUS, this, org);
124 		legalCmp.setLayoutData(EclipseUiUtils.fillAll());
125 		addLazyTabToFolder(folder, legalCmp, PeopleMsg.orgAdmin.lead(), PeopleRapConstants.CTAB_LEGAL_INFO, tooltip);
126 	}
127 
128 	protected void populateTitleComposite(final Composite parent) {
129 		try {
130 			parent.setLayout(new FormLayout());
131 			// READ ONLY
132 			final Composite roPanelCmp = getFormToolkit().createComposite(parent, SWT.NO_FOCUS);
133 			ConnectUiUtils.setSwitchingFormData(roPanelCmp);
134 			roPanelCmp.setLayout(new GridLayout());
135 
136 			// Add a label with info provided by the OrgOverviewLabelProvider
137 			final Label orgInfoROLbl = getFormToolkit().createLabel(roPanelCmp, "", SWT.WRAP);
138 			CmsUtils.markup(orgInfoROLbl);
139 			final ColumnLabelProvider orgLP = new OrgOverviewLabelProvider(false, getResourcesService(),
140 					getPeopleService(), getSystemWorkbenchService());
141 
142 			// EDIT
143 			final Composite editPanelCmp = getFormToolkit().createComposite(parent, SWT.NONE);
144 			ConnectUiUtils.setSwitchingFormData(editPanelCmp);
145 			editPanelCmp.setLayout(new GridLayout(2, false));
146 
147 			// Create edit text
148 			// final Text displayNameTxt =
149 			// ConnectWorkbenchUtils.createGDText(getFormToolkit(), editPanelCmp,
150 			// "Display name", "Display name used for this organisation", 300, 1);
151 			// final Button useDistinctDisplayBtn =
152 			// getFormToolkit().createButton(editPanelCmp,
153 			// "Use a specific display name", SWT.CHECK);
154 			// useDistinctDisplayBtn.setToolTipText("Use a display name that is not the
155 			// legal name");
156 			// // Initialise checkbox
157 			// if (!EclipseUiUtils.isEmpty(ConnectJcrUtils.get(org,
158 			// PeopleNames.PEOPLE_DISPLAY_NAME)))
159 			// useDistinctDisplayBtn.setSelection(true);
160 
161 			final AbstractFormPart editPart = new AbstractFormPart() {
162 				// Update values on refresh
163 				public void refresh() {
164 					// EDIT PART
165 					// boolean useDistinct = useDistinctDisplayBtn.getSelection();
166 					// if (useDistinct)
167 					// ConnectUiUtils.refreshTextWidgetValue(displayNameTxt, org,
168 					// PeopleNames.PEOPLE_DISPLAY_NAME);
169 					// else
170 					// displayNameTxt.setText(getPeopleService().getPersonService().getDefaultDisplayName(org));
171 					// displayNameTxt.setEnabled(useDistinct);
172 
173 					// READ ONLY PART
174 					String roText = orgLP.getText(org);
175 					orgInfoROLbl.setText(roText);
176 
177 					// if (isEditing())
178 					// editPanelCmp.moveAbove(roPanelCmp);
179 					// else
180 					editPanelCmp.moveBelow(roPanelCmp);
181 					orgInfoROLbl.pack();
182 					editPanelCmp.getParent().layout();
183 					super.refresh();
184 				}
185 			};
186 
187 			// useDistinctDisplayBtn.addSelectionListener(new SelectionAdapter() {
188 			// private static final long serialVersionUID = 1L;
189 			//
190 			// @Override
191 			// public void widgetSelected(SelectionEvent e) {
192 			// try {
193 			// boolean defineDistinct = useDistinctDisplayBtn.getSelection();
194 			// String dName = ConnectJcrUtils.get(org, PeopleNames.PEOPLE_LEGAL_NAME);
195 			// if (defineDistinct) {
196 			// ConnectJcrUtils.setJcrProperty(org, PeopleNames.PEOPLE_DISPLAY_NAME,
197 			// PropertyType.STRING,
198 			// dName);
199 			// } else if (org.hasProperty(PeopleNames.PEOPLE_DISPLAY_NAME)) {
200 			// displayNameTxt.setText(dName);
201 			// org.getProperty(PeopleNames.PEOPLE_DISPLAY_NAME).remove();
202 			// }
203 			// displayNameTxt.setEnabled(defineDistinct);
204 			// editPart.markDirty();
205 			// } catch (RepositoryException e1) {
206 			// throw new PeopleException("Unable to reset display name management for " +
207 			// org, e1);
208 			// }
209 			// }
210 			// });
211 			//
212 			// addDNameModifyListener(displayNameTxt, useDistinctDisplayBtn, org,
213 			// PeopleNames.PEOPLE_DISPLAY_NAME,
214 			// editPart);
215 
216 			editPart.initialize(getManagedForm());
217 			getManagedForm().addPart(editPart);
218 		} catch (Exception e) {
219 			throw new PeopleException("Cannot create main info section", e);
220 		}
221 	}
222 
223 	// private void addDNameModifyListener(final Text text, final Button
224 	// useDistinctBtn, final Node node,
225 	// final String propName, final AbstractFormPart part) {
226 	// text.addModifyListener(new ModifyListener() {
227 	// private static final long serialVersionUID = 1L;
228 	//
229 	// @Override
230 	// public void modifyText(ModifyEvent event) {
231 	// if (useDistinctBtn.getSelection()) {
232 	// if (ConnectJcrUtils.setJcrProperty(node, propName, PropertyType.STRING,
233 	// text.getText()))
234 	// part.markDirty();
235 	// }
236 	// }
237 	// });
238 	// }
239 
240 	protected ActivitiesService getActivitiesService() {
241 		return activitiesService;
242 	}
243 
244 	protected PeopleService getPeopleService() {
245 		return peopleService;
246 	}
247 
248 	/* DEPENDENCY INJECTION */
249 	public void setActivitiesService(ActivitiesService activitiesService) {
250 		this.activitiesService = activitiesService;
251 	}
252 
253 	public void setPeopleService(PeopleService peopleService) {
254 		this.peopleService = peopleService;
255 	}
256 }