cad5a9b994e093dce57c08c18dc147e630f54686
[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                         throw new IllegalArgumentException("No layer " + id + " is available.");
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                 if (currentLayerId != null) {
101                         Composite current = getCurrentWorkArea();
102                         if (currentLayerId.equals(layerId))
103                                 return current;
104                 }
105                 if (context == null) {
106                         if (!cmsView.isAnonymous())
107                                 context = userDir;
108                 }
109                 Composite toShow = getLayer(layerId, context);
110                 currentLayerId = layerId;
111                 if (!isDisposed())
112                         getDisplay().syncExec(() -> {
113                                 if (!toShow.isDisposed())
114                                         toShow.moveAbove(null);
115                                 else
116                                         log.warn("Cannot show work area because it is disposed.");
117                                 dynamicArea.layout(true, true);
118                         });
119                 return toShow;
120         }
121
122         Composite switchToLayer(SuiteLayer layer, Node context) {
123                 // TODO make it more robust
124                 for (String layerId : layers.keySet()) {
125                         SuiteLayer l = layers.get(layerId);
126                         if (layer == l) {
127                                 return switchToLayer(layerId, context);
128                         }
129                 }
130                 throw new IllegalArgumentException("Layer is not registered.");
131         }
132
133         void addLayer(String id, SuiteLayer layer) {
134                 layers.put(id, layer);
135         }
136
137         void removeLayer(String id) {
138                 layers.remove(id);
139                 if (workAreas.containsKey(id)) {
140                         Composite workArea = workAreas.remove(id);
141                         if (!workArea.isDisposed())
142                                 workArea.dispose();
143                 }
144         }
145
146         protected Composite initLayer(String id, SuiteLayer layer, Node context) {
147                 Composite workArea = cmsView.doAs(() -> (Composite) layer.createUiPart(dynamicArea, context));
148                 workArea.setLayoutData(CmsUiUtils.coverAll());
149                 workAreas.put(id, workArea);
150                 return workArea;
151         }
152
153         synchronized void logout() {
154                 userDir = null;
155                 Jcr.logout(sysSession);
156 //              Jcr.logout(homeSession);
157                 currentLayerId = null;
158                 workAreas.clear();
159         }
160
161         /*
162          * GETTERS / SETTERS
163          */
164
165         Composite getHeader() {
166                 return header;
167         }
168
169         Composite getLeadPane() {
170                 return leadPane;
171         }
172
173         Composite getBelowHeader() {
174                 return belowHeader;
175         }
176
177 //      Session getSysSession() {
178 //              return sysSession;
179 //      }
180 //
181         synchronized void initSessions(Repository repository, String userDirPath) throws RepositoryException {
182                 this.sysSession = repository.login();
183 //              this.homeSession = repository.login(NodeConstants.HOME_WORKSPACE);
184                 userDir = sysSession.getNode(userDirPath);
185                 addDisposeListener((e) -> {
186                         Jcr.logout(sysSession);
187 //                      Jcr.logout(homeSession);
188                 });
189         }
190
191         Node getUserDir() {
192                 return userDir;
193         }
194
195         Session getSysSession() {
196                 return sysSession;
197         }
198
199         Session getSession(String workspaceName) {
200                 if (workspaceName == null)
201                         return sysSession;
202                 if (NodeConstants.SYS_WORKSPACE.equals(workspaceName))
203                         return sysSession;
204 //              else if (NodeConstants.HOME_WORKSPACE.equals(workspaceName))
205 //                      return homeSession;
206                 else
207                         throw new IllegalArgumentException("Unknown workspace " + workspaceName);
208         }
209
210         public CmsView getCmsView() {
211                 return cmsView;
212         }
213
214 }