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