]> git.argeo.org Git - gpl/argeo-suite.git/blob - org.argeo.suite.ui/src/org/argeo/suite/ui/SuiteUi.java
Fix plain Argeo Suite.
[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 toShow = initLayer(layerId, layers.get(layerId), context);
120 toShow.moveAbove(null);
121 }
122 dynamicArea.layout(true, true);
123 // });
124 }
125 return toShow;
126 } else {
127 return current;
128 }
129 }
130
131 Composite switchToLayer(SuiteLayer layer, Node context) {
132 // TODO make it more robust
133 for (String layerId : layers.keySet()) {
134 SuiteLayer l = layers.get(layerId);
135 if (layer == l) {
136 return switchToLayer(layerId, context);
137 }
138 }
139 throw new IllegalArgumentException("Layer is not registered.");
140 }
141
142 void addLayer(String id, SuiteLayer layer) {
143 layers.put(id, layer);
144 }
145
146 void removeLayer(String id) {
147 layers.remove(id);
148 if (workAreas.containsKey(id)) {
149 Composite workArea = workAreas.remove(id);
150 if (!workArea.isDisposed())
151 workArea.dispose();
152 }
153 }
154
155 protected Composite initLayer(String id, SuiteLayer layer, Node context) {
156 Composite workArea = cmsView.doAs(() -> (Composite) layer.createUiPart(dynamicArea, context));
157 workArea.setLayoutData(CmsUiUtils.coverAll());
158 workAreas.put(id, workArea);
159 return workArea;
160 }
161
162 synchronized void logout() {
163 userDir = null;
164 Jcr.logout(sysSession);
165 // Jcr.logout(homeSession);
166 currentLayerId = null;
167 workAreas.clear();
168 }
169
170 /*
171 * GETTERS / SETTERS
172 */
173
174 Composite getHeader() {
175 return header;
176 }
177
178 Composite getLeadPane() {
179 return leadPane;
180 }
181
182 Composite getBelowHeader() {
183 return belowHeader;
184 }
185
186 // Session getSysSession() {
187 // return sysSession;
188 // }
189 //
190 synchronized void initSessions(Repository repository, String userDirPath) throws RepositoryException {
191 this.sysSession = repository.login();
192 // this.homeSession = repository.login(NodeConstants.HOME_WORKSPACE);
193 userDir = sysSession.getNode(userDirPath);
194 addDisposeListener((e) -> {
195 Jcr.logout(sysSession);
196 // Jcr.logout(homeSession);
197 });
198 }
199
200 Node getUserDir() {
201 return userDir;
202 }
203
204 Session getSysSession() {
205 return sysSession;
206 }
207
208 Session getSession(String workspaceName) {
209 if (workspaceName == null)
210 return sysSession;
211 if (NodeConstants.SYS_WORKSPACE.equals(workspaceName))
212 return sysSession;
213 // else if (NodeConstants.HOME_WORKSPACE.equals(workspaceName))
214 // return homeSession;
215 else
216 throw new IllegalArgumentException("Unknown workspace " + workspaceName);
217 }
218
219 public CmsView getCmsView() {
220 return cmsView;
221 }
222
223 }