]> git.argeo.org Git - gpl/argeo-suite.git/blob - org.argeo.app.ui/src/org/argeo/app/ui/SuiteUi.java
Adapt to changes in Argeo Commons.
[gpl/argeo-suite.git] / org.argeo.app.ui / src / org / argeo / app / ui / SuiteUi.java
1 package org.argeo.app.ui;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.argeo.api.acr.Content;
7 import org.argeo.api.cms.CmsLog;
8 import org.argeo.api.cms.ux.CmsUi;
9 import org.argeo.api.cms.ux.CmsView;
10 import org.argeo.cms.Localized;
11 import org.argeo.cms.swt.CmsSwtUtils;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.layout.FormLayout;
14 import org.eclipse.swt.widgets.Composite;
15
16 /** The view for the default ergonomics of Argeo Suite. */
17 class SuiteUi extends Composite implements CmsUi {
18 private static final long serialVersionUID = 6207018859086689108L;
19 private final static CmsLog log = CmsLog.getLog(SuiteUi.class);
20
21 private Localized title;
22 private Composite header;
23 private Composite footer;
24 private Composite belowHeader;
25 private Composite leadPane;
26 private Composite sidePane;
27 private Composite dynamicArea;
28
29 // private Session sysSession;
30 // private Session homeSession;
31 private Content userDir;
32
33 private Map<String, SuiteLayer> layers = new HashMap<>();
34 private Map<String, Composite> workAreas = new HashMap<>();
35 private String currentLayerId = null;
36
37 private CmsView cmsView;
38
39 public SuiteUi(Composite parent, int style) {
40 super(parent, style);
41 cmsView = CmsSwtUtils.getCmsView(parent);
42 this.setLayout(CmsSwtUtils.noSpaceGridLayout());
43
44 header = new Composite(this, SWT.NONE);
45 header.setLayout(CmsSwtUtils.noSpaceGridLayout());
46 CmsSwtUtils.style(header, SuiteStyle.header);
47 header.setLayoutData(CmsSwtUtils.fillWidth());
48
49 belowHeader = new Composite(this, SWT.NONE);
50 belowHeader.setLayoutData(CmsSwtUtils.fillAll());
51
52 footer = new Composite(this, SWT.NONE);
53 footer.setLayout(CmsSwtUtils.noSpaceGridLayout());
54 CmsSwtUtils.style(footer, SuiteStyle.footer);
55 footer.setLayoutData(CmsSwtUtils.fillWidth());
56 }
57
58 public void refreshBelowHeader(boolean initApp) {
59 CmsSwtUtils.clear(belowHeader);
60 int style = getStyle();
61 if (initApp) {
62 belowHeader.setLayout(CmsSwtUtils.noSpaceGridLayout(3));
63
64 if (SWT.RIGHT_TO_LEFT == (style & SWT.RIGHT_TO_LEFT)) {// arabic, hebrew, etc.
65 sidePane = new Composite(belowHeader, SWT.NONE);
66 sidePane.setLayout(CmsSwtUtils.noSpaceGridLayout());
67 sidePane.setLayoutData(CmsSwtUtils.fillHeight());
68 dynamicArea = new Composite(belowHeader, SWT.NONE);
69 leadPane = new Composite(belowHeader, SWT.NONE);
70 } else {
71 leadPane = new Composite(belowHeader, SWT.NONE);
72 dynamicArea = new Composite(belowHeader, SWT.NONE);
73 sidePane = new Composite(belowHeader, SWT.NONE);
74 sidePane.setLayout(CmsSwtUtils.noSpaceGridLayout());
75 sidePane.setLayoutData(CmsSwtUtils.fillHeight());
76 }
77 leadPane.setLayoutData(CmsSwtUtils.fillHeight());
78 leadPane.setLayout(CmsSwtUtils.noSpaceGridLayout());
79 CmsSwtUtils.style(leadPane, SuiteStyle.leadPane);
80
81 dynamicArea.setLayoutData(CmsSwtUtils.fillAll());
82 dynamicArea.setLayout(new FormLayout());
83
84 } else {
85 belowHeader.setLayout(CmsSwtUtils.noSpaceGridLayout());
86 }
87 }
88
89 /*
90 * LAYERS
91 */
92
93 Composite getCurrentWorkArea() {
94 if (currentLayerId == null)
95 throw new IllegalStateException("No current layer");
96 return workAreas.get(currentLayerId);
97 }
98
99 String getCurrentLayerId() {
100 return currentLayerId;
101 }
102
103 private Composite getLayer(String id, Content context) {
104 if (!layers.containsKey(id))
105 return null;
106 if (!workAreas.containsKey(id))
107 initLayer(id, layers.get(id), context);
108 return workAreas.get(id);
109 }
110
111 Composite switchToLayer(String layerId, Content context) {
112 Composite current = null;
113 if (currentLayerId != null) {
114 current = getCurrentWorkArea();
115 if (currentLayerId.equals(layerId))
116 return current;
117 }
118 if (context == null) {
119 if (!cmsView.isAnonymous())
120 context = getUserDirNode();
121 }
122 Composite toShow = getLayer(layerId, context);
123 if (toShow != null) {
124 currentLayerId = layerId;
125 if (!isDisposed()) {
126 // getDisplay().syncExec(() -> {
127 if (!toShow.isDisposed()) {
128 toShow.moveAbove(null);
129 } else {
130 log.warn("Cannot show work area because it is disposed.");
131 toShow = initLayer(layerId, layers.get(layerId), context);
132 toShow.moveAbove(null);
133 }
134 dynamicArea.layout(true, true);
135 // });
136 }
137 return toShow;
138 } else {
139 return current;
140 }
141 }
142
143 Composite switchToLayer(SuiteLayer layer, Content context) {
144 // TODO make it more robust
145 for (String layerId : layers.keySet()) {
146 SuiteLayer l = layers.get(layerId);
147 if (layer == l) {
148 return switchToLayer(layerId, context);
149 }
150 }
151 throw new IllegalArgumentException("Layer is not registered.");
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, Content context) {
168 Composite workArea = cmsView.doAs(() -> (Composite) layer.createUiPart(dynamicArea, context));
169 CmsSwtUtils.style(workArea, SuiteStyle.workArea);
170 workArea.setLayoutData(CmsSwtUtils.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 getSidePane() {
200 return sidePane;
201 }
202
203 Composite getBelowHeader() {
204 return belowHeader;
205 }
206
207 // Session getSysSession() {
208 // return sysSession;
209 // }
210 //
211 // synchronized void initSessions(Repository repository, String userDirPath) throws RepositoryException {
212 // this.sysSession = repository.login();
213 // this.homeSession = repository.login(CmsConstants.HOME_WORKSPACE);
214 // userDir = sysSession.getNode(userDirPath);
215 // addDisposeListener((e) -> {
216 // Jcr.logout(sysSession);
217 // Jcr.logout(homeSession);
218 // });
219 // }
220
221 @Deprecated
222 Content getUserDirNode() {
223 if (userDir == null)
224 return null;
225 return userDir;
226 }
227
228 Content getUserDir() {
229 return userDir;
230 }
231
232 void setUserDir(Content userDir) {
233 this.userDir = userDir;
234 }
235
236 // Session getSysSession() {
237 // return sysSession;
238 // }
239
240 // Session getSession(String workspaceName) {
241 // if (workspaceName == null)
242 // return sysSession;
243 // if (CmsConstants.SYS_WORKSPACE.equals(workspaceName))
244 // return sysSession;
245 // else if (CmsConstants.HOME_WORKSPACE.equals(workspaceName))
246 // return homeSession;
247 // else
248 // throw new IllegalArgumentException("Unknown workspace " + workspaceName);
249 // }
250
251 public CmsView getCmsView() {
252 return cmsView;
253 }
254
255 public Localized getTitle() {
256 return title;
257 }
258
259 public void setTitle(Localized title) {
260 this.title = title;
261 }
262
263 }