Adapt to changes in Connect
[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.AppWorkbenchService;
15 import org.argeo.connect.ui.ConnectEditor;
16 import org.argeo.connect.ui.ConnectUiStyles;
17 import org.argeo.connect.ui.SystemWorkbenchService;
18 import org.argeo.connect.util.ConnectJcrUtils;
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.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Link;
31 import org.eclipse.ui.IEditorInput;
32 import org.eclipse.ui.IEditorSite;
33 import org.eclipse.ui.PartInitException;
34 import org.eclipse.ui.forms.widgets.FormToolkit;
35 import org.eclipse.ui.part.EditorPart;
36
37 /** Generic dashboard for Argeo Suite applications */
38 public abstract class AbstractSuiteDashboard extends EditorPart {
39
40 // DEPENDENCY INJECTION
41 private Repository repository;
42 private ResourcesService resourcesService;
43 private SystemAppService systemAppService;
44 private SystemWorkbenchService systemWorkbenchService;
45
46 private Session session;
47
48 // UI Objects
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 updateTooltip(input);
58 }
59
60 private void updateTooltip(IEditorInput input) {
61 if (input instanceof EntityEditorInput) {
62 EntityEditorInput sei = (EntityEditorInput) input;
63 sei.setTooltipText("My Dashboard");
64 }
65 }
66
67 /**
68 * Implementing classes must call super in order to create the correct form
69 * toolkit
70 */
71 @Override
72 public void createPartControl(Composite parent) {
73 toolkit = new FormToolkit(getSite().getShell().getDisplay());
74 }
75
76 // UTILS
77 protected Composite createGadgetCmp(Composite parent, int widthHint, int heightHint) {
78 Composite gadgetCmp = toolkit.createComposite(parent, SWT.BORDER);
79 GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
80 gd.widthHint = widthHint;
81 gd.heightHint = heightHint;
82 gadgetCmp.setLayoutData(gd);
83 CmsUtils.style(gadgetCmp, ConnectUiStyles.GADGET_BOX);
84 return gadgetCmp;
85 }
86
87 protected Composite createGadgetTitleCmp(Composite parent, String title) {
88 Composite titleCmp = toolkit.createComposite(parent, SWT.BACKGROUND | SWT.INHERIT_NONE);
89 CmsUtils.style(titleCmp, ConnectUiStyles.GADGET_HEADER);
90 titleCmp.setBackground(null);
91 GridData gd = new GridData(SWT.FILL, SWT.TOP, true, false);
92 titleCmp.setLayoutData(gd);
93 titleCmp.setLayout(new GridLayout());
94
95 Label titleLbl = toolkit.createLabel(titleCmp, title + " ", SWT.BOLD);
96 CmsUtils.style(titleLbl, ConnectUiStyles.GADGET_HEADER);
97 titleLbl.setBackground(null);
98 return titleCmp;
99 }
100
101 protected Composite createGadgetBodyCmp(Composite parent) {
102 Composite bodyCmp = toolkit.createComposite(parent, SWT.BACKGROUND | SWT.INHERIT_NONE);
103 bodyCmp.setLayoutData(EclipseUiUtils.fillAll());
104 bodyCmp.setLayout(new GridLayout());
105 return bodyCmp;
106 }
107
108 protected Link createOpenEntityEditorLink(final AppWorkbenchService peopleUiService, Composite parent,
109 final String label, final Node entity) {
110 Link link = new Link(parent, SWT.NONE);
111 link.setText("<a>" + label + "</a>");
112 link.setLayoutData(EclipseUiUtils.fillWidth());
113 link.addSelectionListener(new SelectionAdapter() {
114 private static final long serialVersionUID = 1L;
115
116 @Override
117 public void widgetSelected(final SelectionEvent event) {
118 Map<String, String> params = new HashMap<String, String>();
119 params.put(ConnectEditor.PARAM_JCR_ID, ConnectJcrUtils.getIdentifier(entity));
120 CommandUtils.callCommand(peopleUiService.getOpenEntityEditorCmdId(), params);
121 }
122 });
123 return link;
124 }
125
126 // Life cycle
127 @Override
128 public void dispose() {
129 JcrUtils.logoutQuietly(session);
130 super.dispose();
131 }
132
133 @Override
134 public void doSave(IProgressMonitor monitor) {
135 }
136
137 @Override
138 public void doSaveAs() {
139 }
140
141 @Override
142 public boolean isDirty() {
143 return false;
144 }
145
146 @Override
147 public boolean isSaveAsAllowed() {
148 return false;
149 }
150
151 @Override
152 public void setFocus() {
153 // Do nothing
154 }
155
156 // Expose to implementing classes
157 protected Session getSession() {
158 return session;
159 }
160
161 public ResourcesService getResourcesService() {
162 return resourcesService;
163 }
164
165 protected SystemAppService getSystemAppService() {
166 return systemAppService;
167 }
168
169 protected SystemWorkbenchService getSystemWorkbenchService() {
170 return systemWorkbenchService;
171 }
172
173 protected FormToolkit getFormToolkit() {
174 return toolkit;
175 }
176
177 /* DEPENDENCY INJECTION */
178 public void setRepository(Repository repository) {
179 this.repository = repository;
180 }
181
182 public void setResourcesService(ResourcesService resourcesService) {
183 this.resourcesService = resourcesService;
184 }
185
186 public void setSystemAppService(SystemAppService systemAppService) {
187 this.systemAppService = systemAppService;
188 }
189
190 public void setSystemWorkbenchService(SystemWorkbenchService systemWorkbenchService) {
191 this.systemWorkbenchService = systemWorkbenchService;
192 }
193 }