First part of the refactoring: clean backends.
[gpl/argeo-suite.git] / org.argeo.suite.workbench.rap / src / org / argeo / suite / workbench / parts / AbstractSuiteDashboard.java
1 package org.argeo.suite.workbench.parts;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.jcr.Node;
7 import javax.jcr.Repository;
8 import javax.jcr.Session;
9
10 import org.argeo.cms.ui.workbench.util.CommandUtils;
11 import org.argeo.cms.util.CmsUtils;
12 import org.argeo.connect.documents.DocumentsService;
13 import org.argeo.connect.resources.ResourceService;
14 import org.argeo.connect.activities.ActivityService;
15 import org.argeo.connect.activities.ActivitiesTypes;
16 import org.argeo.connect.people.PeopleService;
17 import org.argeo.connect.people.workbench.rap.PeopleStyles;
18 import org.argeo.connect.people.workbench.rap.editors.util.EntityEditorInput;
19 import org.argeo.connect.ui.workbench.AppWorkbenchService;
20 import org.argeo.connect.util.ConnectJcrUtils;
21 import org.argeo.eclipse.ui.EclipseUiUtils;
22 import org.argeo.jcr.JcrUtils;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.graphics.Image;
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.Label;
32 import org.eclipse.swt.widgets.Link;
33 import org.eclipse.ui.IEditorInput;
34 import org.eclipse.ui.IEditorSite;
35 import org.eclipse.ui.PartInitException;
36 import org.eclipse.ui.forms.widgets.FormToolkit;
37 import org.eclipse.ui.part.EditorPart;
38
39 /** Generic dashboard for Argeo Suite applications */
40 public abstract class AbstractSuiteDashboard extends EditorPart {
41
42 // DEPENDENCY INJECTION
43 private Repository repository;
44 private ResourceService resourceService;
45 private ActivityService activityService;
46 private PeopleService peopleService;
47 private DocumentsService documentsService;
48 private AppWorkbenchService appWorkbenchService;
49
50 private Session session;
51
52 // UI Objects
53 private Image logoImg;
54 private FormToolkit toolkit;
55
56 @Override
57 public void init(IEditorSite site, IEditorInput input) throws PartInitException {
58 setSite(site);
59 setInput(input);
60
61 session = ConnectJcrUtils.login(repository);
62 // initialiseImg();
63 updateTooltip(input);
64 }
65
66 private void updateTooltip(IEditorInput input) {
67 if (input instanceof EntityEditorInput) {
68 EntityEditorInput sei = (EntityEditorInput) input;
69 sei.setTooltipText("My Dashboard");
70 }
71 }
72
73 // private void initialiseImg() {
74 // InputStream is = null;
75 // try {
76 // String imgPath = peopleService.getInstanceConfPath() + "/"
77 // + AoNames.AO_DEFAULT_LOGO;
78 // if (session.nodeExists(imgPath)) {
79 // Node imageNode = session.getNode(imgPath).getNode(
80 // Node.JCR_CONTENT);
81 // is = imageNode.getProperty(Property.JCR_DATA).getBinary()
82 // .getStream();
83 // logoImg = new Image(this.getSite().getShell().getDisplay(), is);
84 // }
85 // } catch (RepositoryException re) {
86 // throw new AoException(
87 // "Unable to initialise specific logo for demo app", re);
88 // } finally {
89 // IOUtils.closeQuietly(is);
90 // }
91 // }
92
93 /**
94 * Implementing classes must call super in order to create the correct form
95 * toolkit
96 */
97 @Override
98 public void createPartControl(Composite parent) {
99 toolkit = new FormToolkit(getSite().getShell().getDisplay());
100 }
101
102 // UTILS
103 protected Composite createGadgetCmp(Composite parent, int widthHint, int heightHint) {
104 Composite gadgetCmp = toolkit.createComposite(parent, SWT.BORDER);
105 GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
106 gd.widthHint = widthHint;
107 gd.heightHint = heightHint;
108 gadgetCmp.setLayoutData(gd);
109 CmsUtils.style(gadgetCmp, PeopleStyles.PEOPLE_CLASS_GADGET);
110 return gadgetCmp;
111 }
112
113 protected Composite createGadgetTitleCmp(Composite parent, String title) {
114 Composite titleCmp = toolkit.createComposite(parent, SWT.BACKGROUND | SWT.INHERIT_NONE);
115 CmsUtils.style(titleCmp, PeopleStyles.GADGET_HEADER);
116 titleCmp.setBackground(null);
117 GridData gd = new GridData(SWT.FILL, SWT.TOP, true, false);
118 titleCmp.setLayoutData(gd);
119 titleCmp.setLayout(new GridLayout());
120
121 Label titleLbl = toolkit.createLabel(titleCmp, title + " ", SWT.BOLD);
122 CmsUtils.style(titleLbl, PeopleStyles.GADGET_HEADER);
123 titleLbl.setBackground(null);
124 return titleCmp;
125 }
126
127 protected Composite createGadgetBodyCmp(Composite parent) {
128 Composite bodyCmp = toolkit.createComposite(parent, SWT.BACKGROUND | SWT.INHERIT_NONE);
129 bodyCmp.setLayoutData(EclipseUiUtils.fillAll());
130 bodyCmp.setLayout(new GridLayout());
131 return bodyCmp;
132 }
133
134 protected Link createOpenEntityEditorLink(final AppWorkbenchService peopleUiService, Composite parent,
135 final String label, final Node entity) {
136 Link link = new Link(parent, SWT.NONE);
137 link.setText("<a>" + label + "</a>");
138 link.setLayoutData(EclipseUiUtils.fillWidth());
139 link.addSelectionListener(new SelectionAdapter() {
140 private static final long serialVersionUID = 1L;
141
142 @Override
143 public void widgetSelected(final SelectionEvent event) {
144 Map<String, String> params = new HashMap<String, String>();
145 // FIXME
146 params.put("param.jcrId", ConnectJcrUtils.getIdentifier(entity));
147 CommandUtils.callCommand(peopleUiService.getOpenEntityEditorCmdId(), params);
148 }
149 });
150 return link;
151 }
152
153 // LIFE CYCLE
154 @Override
155 public void dispose() {
156 JcrUtils.logoutQuietly(session);
157 if (logoImg != null)
158 logoImg.dispose();
159 super.dispose();
160 }
161
162 @Override
163 public void doSave(IProgressMonitor monitor) {
164 }
165
166 @Override
167 public void doSaveAs() {
168 }
169
170 @Override
171 public boolean isDirty() {
172 return false;
173 }
174
175 @Override
176 public boolean isSaveAsAllowed() {
177 return false;
178 }
179
180 @Override
181 public void setFocus() {
182 // Do nothing
183 }
184
185 // Expose to implementing classes
186 protected Session getSession() {
187 return session;
188 }
189
190 public ResourceService getResourceService() {
191 return resourceService;
192 }
193
194 protected ActivityService getActivityService() {
195 return activityService;
196 }
197
198 protected PeopleService getPeopleService() {
199 return peopleService;
200 }
201
202 protected DocumentsService getDocumentsService() {
203 return documentsService;
204 }
205
206
207 protected AppWorkbenchService getAppWorkbenchService() {
208 return appWorkbenchService;
209 }
210
211 protected Image getLogoImg() {
212 return logoImg;
213 }
214
215 protected FormToolkit getFormToolkit() {
216 return toolkit;
217 }
218
219 /* DEPENDENCY INJECTION */
220 public void setRepository(Repository repository) {
221 this.repository = repository;
222 }
223
224 public void setResourceService(ResourceService resourceService) {
225 this.resourceService = resourceService;
226 }
227
228 public void setActivityService(ActivityService activityService) {
229 this.activityService = activityService;
230 }
231
232 public void setDocumentsService(DocumentsService documentsService) {
233 this.documentsService = documentsService;
234 }
235
236 public void setPeopleService(PeopleService peopleService) {
237 this.peopleService = peopleService;
238 }
239
240
241 public void setAppWorkbenchService(AppWorkbenchService appWorkbenchService) {
242 this.appWorkbenchService = appWorkbenchService;
243 }
244
245 }