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