1 package org.argeo.suite.ui;
3 import java.util.HashMap;
7 import javax.jcr.Repository;
8 import javax.jcr.RepositoryException;
9 import javax.jcr.Session;
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;
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);
27 private Localized title;
28 private Composite header;
29 private Composite footer;
30 private Composite belowHeader;
31 private Composite leadPane;
32 private Composite dynamicArea;
34 private Session sysSession;
35 private Session homeSession;
38 private Map<String, SuiteLayer> layers = new HashMap<>();
39 private Map<String, Composite> workAreas = new HashMap<>();
40 private String currentLayerId = null;
42 private CmsView cmsView;
44 public SuiteUi(Composite parent, int style) {
46 cmsView = CmsView.getCmsView(parent);
47 this.setLayout(CmsUiUtils.noSpaceGridLayout());
49 header = new Composite(this, SWT.NONE);
50 header.setLayout(CmsUiUtils.noSpaceGridLayout());
51 CmsUiUtils.style(header, SuiteStyle.header);
52 header.setLayoutData(CmsUiUtils.fillWidth());
54 belowHeader = new Composite(this, SWT.NONE);
55 belowHeader.setLayoutData(CmsUiUtils.fillAll());
57 footer = new Composite(this, SWT.NONE);
58 footer.setLayout(CmsUiUtils.noSpaceGridLayout());
59 CmsUiUtils.style(footer, SuiteStyle.footer);
60 footer.setLayoutData(CmsUiUtils.fillWidth());
63 public void refreshBelowHeader(boolean initApp) {
64 CmsUiUtils.clear(belowHeader);
65 int style = getStyle();
67 belowHeader.setLayout(CmsUiUtils.noSpaceGridLayout(2));
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);
73 leadPane = new Composite(belowHeader, SWT.NONE);
74 dynamicArea = new Composite(belowHeader, SWT.NONE);
76 leadPane.setLayoutData(CmsUiUtils.fillHeight());
77 leadPane.setLayout(CmsUiUtils.noSpaceGridLayout());
78 CmsUiUtils.style(leadPane, SuiteStyle.leadPane);
80 dynamicArea.setLayoutData(CmsUiUtils.fillAll());
81 dynamicArea.setLayout(new FormLayout());
84 belowHeader.setLayout(CmsUiUtils.noSpaceGridLayout());
92 Composite getCurrentWorkArea() {
93 if (currentLayerId == null)
94 throw new IllegalStateException("No current layer");
95 return workAreas.get(currentLayerId);
98 String getCurrentLayerId() {
99 return currentLayerId;
102 private Composite getLayer(String id, Node context) {
103 if (!layers.containsKey(id))
105 if (!workAreas.containsKey(id))
106 initLayer(id, layers.get(id), context);
107 return workAreas.get(id);
110 Composite switchToLayer(String layerId, Node context) {
111 Composite current = null;
112 if (currentLayerId != null) {
113 current = getCurrentWorkArea();
114 if (currentLayerId.equals(layerId))
117 if (context == null) {
118 if (!cmsView.isAnonymous())
121 Composite toShow = getLayer(layerId, context);
122 if (toShow != null) {
123 currentLayerId = layerId;
125 // getDisplay().syncExec(() -> {
126 if (!toShow.isDisposed()) {
127 toShow.moveAbove(null);
129 log.warn("Cannot show work area because it is disposed.");
130 toShow = initLayer(layerId, layers.get(layerId), context);
131 toShow.moveAbove(null);
133 dynamicArea.layout(true, true);
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);
147 return switchToLayer(layerId, context);
150 throw new IllegalArgumentException("Layer is not registered.");
153 void addLayer(String id, SuiteLayer layer) {
154 layers.put(id, layer);
157 void removeLayer(String id) {
159 if (workAreas.containsKey(id)) {
160 Composite workArea = workAreas.remove(id);
161 if (!workArea.isDisposed())
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);
174 synchronized void logout() {
176 Jcr.logout(sysSession);
177 // Jcr.logout(homeSession);
178 currentLayerId = null;
186 Composite getHeader() {
190 Composite getFooter() {
194 Composite getLeadPane() {
198 Composite getBelowHeader() {
202 // Session getSysSession() {
203 // return sysSession;
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);
220 Session getSysSession() {
224 Session getSession(String workspaceName) {
225 if (workspaceName == null)
227 if (NodeConstants.SYS_WORKSPACE.equals(workspaceName))
229 else if (NodeConstants.HOME_WORKSPACE.equals(workspaceName))
232 throw new IllegalArgumentException("Unknown workspace " + workspaceName);
235 public CmsView getCmsView() {
239 public Localized getTitle() {
243 public void setTitle(Localized title) {