]> git.argeo.org Git - gpl/argeo-suite.git/blob - core/org.argeo.suite.ui/src/org/argeo/suite/ui/SuiteUi.java
Use " instead of ' in map.
[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(footer, SuiteStyle.footer);
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
154 void addLayer(String id, SuiteLayer layer) {
155 layers.put(id, layer);
156 }
157
158 void removeLayer(String id) {
159 layers.remove(id);
160 if (workAreas.containsKey(id)) {
161 Composite workArea = workAreas.remove(id);
162 if (!workArea.isDisposed())
163 workArea.dispose();
164 }
165 }
166
167 protected Composite initLayer(String id, SuiteLayer layer, Node context) {
168 Composite workArea = cmsView.doAs(() -> (Composite) layer.createUiPart(dynamicArea, context));
169 CmsUiUtils.style(workArea, SuiteStyle.workArea);
170 workArea.setLayoutData(CmsUiUtils.coverAll());
171 workAreas.put(id, workArea);
172 return workArea;
173 }
174
175 synchronized void logout() {
176 userDir = null;
177 Jcr.logout(sysSession);
178 Jcr.logout(homeSession);
179 currentLayerId = null;
180 workAreas.clear();
181 }
182
183 /*
184 * GETTERS / SETTERS
185 */
186
187 Composite getHeader() {
188 return header;
189 }
190
191 Composite getFooter() {
192 return footer;
193 }
194
195 Composite getLeadPane() {
196 return leadPane;
197 }
198
199 Composite getBelowHeader() {
200 return belowHeader;
201 }
202
203 // Session getSysSession() {
204 // return sysSession;
205 // }
206 //
207 synchronized void initSessions(Repository repository, String userDirPath) throws RepositoryException {
208 this.sysSession = repository.login();
209 this.homeSession = repository.login(NodeConstants.HOME_WORKSPACE);
210 userDir = sysSession.getNode(userDirPath);
211 addDisposeListener((e) -> {
212 Jcr.logout(sysSession);
213 Jcr.logout(homeSession);
214 });
215 }
216
217 Node getUserDir() {
218 return userDir;
219 }
220
221 Session getSysSession() {
222 return sysSession;
223 }
224
225 Session getSession(String workspaceName) {
226 if (workspaceName == null)
227 return sysSession;
228 if (NodeConstants.SYS_WORKSPACE.equals(workspaceName))
229 return sysSession;
230 else if (NodeConstants.HOME_WORKSPACE.equals(workspaceName))
231 return homeSession;
232 else
233 throw new IllegalArgumentException("Unknown workspace " + workspaceName);
234 }
235
236 public CmsView getCmsView() {
237 return cmsView;
238 }
239
240 public Localized getTitle() {
241 return title;
242 }
243
244 public void setTitle(Localized title) {
245 this.title = title;
246 }
247
248 }