Introduce footer.
[gpl/argeo-suite.git] / core / org.argeo.suite.ui / src / org / argeo / suite / ui / SuiteUi.java
1 package org.argeo.suite.ui;
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.RepositoryException;
9 import javax.jcr.Session;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.argeo.api.NodeConstants;
14 import org.argeo.cms.Localized;
15 import org.argeo.cms.ui.CmsView;
16 import org.argeo.cms.ui.util.CmsUiUtils;
17 import org.argeo.jcr.Jcr;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.layout.FormLayout;
20 import org.eclipse.swt.widgets.Composite;
21
22 /** The view for the default ergonomics of Argeo Suite. */
23 class SuiteUi extends Composite {
24         private static final long serialVersionUID = 6207018859086689108L;
25         private final static Log log = LogFactory.getLog(SuiteUi.class);
26
27         private Localized title;
28         private Composite header;
29         private Composite footer;
30         private Composite belowHeader;
31         private Composite leadPane;
32         private Composite dynamicArea;
33
34         private Session sysSession;
35 //      private Session homeSession;
36         private Node userDir;
37
38         private Map<String, SuiteLayer> layers = new HashMap<>();
39         private Map<String, Composite> workAreas = new HashMap<>();
40         private String currentLayerId = null;
41
42         private CmsView cmsView;
43
44         public SuiteUi(Composite parent, int style) {
45                 super(parent, style);
46                 cmsView = CmsView.getCmsView(parent);
47                 this.setLayout(CmsUiUtils.noSpaceGridLayout());
48
49                 header = new Composite(this, SWT.NONE);
50                 header.setLayout(CmsUiUtils.noSpaceGridLayout());
51                 CmsUiUtils.style(header, SuiteStyle.header);
52                 header.setLayoutData(CmsUiUtils.fillWidth());
53
54                 belowHeader = new Composite(this, SWT.NONE);
55                 belowHeader.setLayoutData(CmsUiUtils.fillAll());
56
57                 footer = new Composite(this, SWT.NONE);
58                 footer.setLayout(CmsUiUtils.noSpaceGridLayout());
59                 CmsUiUtils.style(header, SuiteStyle.header);
60                 footer.setLayoutData(CmsUiUtils.fillWidth());
61         }
62
63         public void refreshBelowHeader(boolean initApp) {
64                 CmsUiUtils.clear(belowHeader);
65                 int style = getStyle();
66                 if (initApp) {
67                         belowHeader.setLayout(CmsUiUtils.noSpaceGridLayout(2));
68
69                         if (SWT.RIGHT_TO_LEFT == (style & SWT.RIGHT_TO_LEFT)) {// arabic, hebrew, etc.
70                                 dynamicArea = new Composite(belowHeader, SWT.NONE);
71                                 leadPane = new Composite(belowHeader, SWT.NONE);
72                         } else {
73                                 leadPane = new Composite(belowHeader, SWT.NONE);
74                                 dynamicArea = new Composite(belowHeader, SWT.NONE);
75                         }
76                         leadPane.setLayoutData(CmsUiUtils.fillHeight());
77                         leadPane.setLayout(CmsUiUtils.noSpaceGridLayout());
78                         CmsUiUtils.style(leadPane, SuiteStyle.leadPane);
79
80                         dynamicArea.setLayoutData(CmsUiUtils.fillAll());
81                         dynamicArea.setLayout(new FormLayout());
82
83                 } else {
84                         belowHeader.setLayout(CmsUiUtils.noSpaceGridLayout());
85                 }
86         }
87
88         /*
89          * LAYERS
90          */
91
92         Composite getCurrentWorkArea() {
93                 if (currentLayerId == null)
94                         throw new IllegalStateException("No current layer");
95                 return workAreas.get(currentLayerId);
96         }
97
98         String getCurrentLayerId() {
99                 return currentLayerId;
100         }
101
102         private Composite getLayer(String id, Node context) {
103                 if (!layers.containsKey(id))
104                         return null;
105                 if (!workAreas.containsKey(id))
106                         initLayer(id, layers.get(id), context);
107                 return workAreas.get(id);
108         }
109
110         Composite switchToLayer(String layerId, Node context) {
111                 Composite current = null;
112                 if (currentLayerId != null) {
113                         current = getCurrentWorkArea();
114                         if (currentLayerId.equals(layerId))
115                                 return current;
116                 }
117                 if (context == null) {
118                         if (!cmsView.isAnonymous())
119                                 context = userDir;
120                 }
121                 Composite toShow = getLayer(layerId, context);
122                 if (toShow != null) {
123                         currentLayerId = layerId;
124                         if (!isDisposed()) {
125 //                              getDisplay().syncExec(() -> {
126                                 if (!toShow.isDisposed()) {
127                                         toShow.moveAbove(null);
128                                 } else {
129                                         log.warn("Cannot show work area because it is disposed.");
130                                         toShow = initLayer(layerId, layers.get(layerId), context);
131                                         toShow.moveAbove(null);
132                                 }
133                                 dynamicArea.layout(true, true);
134 //                              });
135                         }
136                         return toShow;
137                 } else {
138                         return current;
139                 }
140         }
141
142         Composite switchToLayer(SuiteLayer layer, Node context) {
143                 // TODO make it more robust
144                 for (String layerId : layers.keySet()) {
145                         SuiteLayer l = layers.get(layerId);
146                         if (layer == l) {
147                                 return switchToLayer(layerId, context);
148                         }
149                 }
150                 throw new IllegalArgumentException("Layer is not registered.");
151         }
152
153         void addLayer(String id, SuiteLayer layer) {
154                 layers.put(id, layer);
155         }
156
157         void removeLayer(String id) {
158                 layers.remove(id);
159                 if (workAreas.containsKey(id)) {
160                         Composite workArea = workAreas.remove(id);
161                         if (!workArea.isDisposed())
162                                 workArea.dispose();
163                 }
164         }
165
166         protected Composite initLayer(String id, SuiteLayer layer, Node context) {
167                 Composite workArea = cmsView.doAs(() -> (Composite) layer.createUiPart(dynamicArea, context));
168                 CmsUiUtils.style(workArea, SuiteStyle.workArea);
169                 workArea.setLayoutData(CmsUiUtils.coverAll());
170                 workAreas.put(id, workArea);
171                 return workArea;
172         }
173
174         synchronized void logout() {
175                 userDir = null;
176                 Jcr.logout(sysSession);
177 //              Jcr.logout(homeSession);
178                 currentLayerId = null;
179                 workAreas.clear();
180         }
181
182         /*
183          * GETTERS / SETTERS
184          */
185
186         Composite getHeader() {
187                 return header;
188         }
189
190         Composite getFooter() {
191                 return footer;
192         }
193
194         Composite getLeadPane() {
195                 return leadPane;
196         }
197
198         Composite getBelowHeader() {
199                 return belowHeader;
200         }
201
202 //      Session getSysSession() {
203 //              return sysSession;
204 //      }
205 //
206         synchronized void initSessions(Repository repository, String userDirPath) throws RepositoryException {
207                 this.sysSession = repository.login();
208 //              this.homeSession = repository.login(NodeConstants.HOME_WORKSPACE);
209                 userDir = sysSession.getNode(userDirPath);
210                 addDisposeListener((e) -> {
211                         Jcr.logout(sysSession);
212 //                      Jcr.logout(homeSession);
213                 });
214         }
215
216         Node getUserDir() {
217                 return userDir;
218         }
219
220         Session getSysSession() {
221                 return sysSession;
222         }
223
224         Session getSession(String workspaceName) {
225                 if (workspaceName == null)
226                         return sysSession;
227                 if (NodeConstants.SYS_WORKSPACE.equals(workspaceName))
228                         return sysSession;
229 //              else if (NodeConstants.HOME_WORKSPACE.equals(workspaceName))
230 //                      return homeSession;
231                 else
232                         throw new IllegalArgumentException("Unknown workspace " + workspaceName);
233         }
234
235         public CmsView getCmsView() {
236                 return cmsView;
237         }
238
239         public Localized getTitle() {
240                 return title;
241         }
242
243         public void setTitle(Localized title) {
244                 this.title = title;
245         }
246
247 }