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