]> git.argeo.org Git - gpl/argeo-suite.git/blob - core/org.argeo.suite.ui/src/org/argeo/suite/ui/SuiteUi.java
Use new build approach.
[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 sidePane;
33 private Composite dynamicArea;
34
35 private Session sysSession;
36 private Session homeSession;
37 private Node userDir;
38
39 private Map<String, SuiteLayer> layers = new HashMap<>();
40 private Map<String, Composite> workAreas = new HashMap<>();
41 private String currentLayerId = null;
42
43 private CmsView cmsView;
44
45 public SuiteUi(Composite parent, int style) {
46 super(parent, style);
47 cmsView = CmsView.getCmsView(parent);
48 this.setLayout(CmsUiUtils.noSpaceGridLayout());
49
50 header = new Composite(this, SWT.NONE);
51 header.setLayout(CmsUiUtils.noSpaceGridLayout());
52 CmsUiUtils.style(header, SuiteStyle.header);
53 header.setLayoutData(CmsUiUtils.fillWidth());
54
55 belowHeader = new Composite(this, SWT.NONE);
56 belowHeader.setLayoutData(CmsUiUtils.fillAll());
57
58 footer = new Composite(this, SWT.NONE);
59 footer.setLayout(CmsUiUtils.noSpaceGridLayout());
60 CmsUiUtils.style(footer, SuiteStyle.footer);
61 footer.setLayoutData(CmsUiUtils.fillWidth());
62 }
63
64 public void refreshBelowHeader(boolean initApp) {
65 CmsUiUtils.clear(belowHeader);
66 int style = getStyle();
67 if (initApp) {
68 belowHeader.setLayout(CmsUiUtils.noSpaceGridLayout(3));
69
70 if (SWT.RIGHT_TO_LEFT == (style & SWT.RIGHT_TO_LEFT)) {// arabic, hebrew, etc.
71 sidePane = new Composite(belowHeader, SWT.NONE);
72 sidePane.setLayout(CmsUiUtils.noSpaceGridLayout());
73 sidePane.setLayoutData(CmsUiUtils.fillHeight());
74 dynamicArea = new Composite(belowHeader, SWT.NONE);
75 leadPane = new Composite(belowHeader, SWT.NONE);
76 } else {
77 leadPane = new Composite(belowHeader, SWT.NONE);
78 dynamicArea = new Composite(belowHeader, SWT.NONE);
79 sidePane = new Composite(belowHeader, SWT.NONE);
80 sidePane.setLayout(CmsUiUtils.noSpaceGridLayout());
81 sidePane.setLayoutData(CmsUiUtils.fillHeight());
82 }
83 leadPane.setLayoutData(CmsUiUtils.fillHeight());
84 leadPane.setLayout(CmsUiUtils.noSpaceGridLayout());
85 CmsUiUtils.style(leadPane, SuiteStyle.leadPane);
86
87 dynamicArea.setLayoutData(CmsUiUtils.fillAll());
88 dynamicArea.setLayout(new FormLayout());
89
90 } else {
91 belowHeader.setLayout(CmsUiUtils.noSpaceGridLayout());
92 }
93 }
94
95 /*
96 * LAYERS
97 */
98
99 Composite getCurrentWorkArea() {
100 if (currentLayerId == null)
101 throw new IllegalStateException("No current layer");
102 return workAreas.get(currentLayerId);
103 }
104
105 String getCurrentLayerId() {
106 return currentLayerId;
107 }
108
109 private Composite getLayer(String id, Node context) {
110 if (!layers.containsKey(id))
111 return null;
112 if (!workAreas.containsKey(id))
113 initLayer(id, layers.get(id), context);
114 return workAreas.get(id);
115 }
116
117 Composite switchToLayer(String layerId, Node context) {
118 Composite current = null;
119 if (currentLayerId != null) {
120 current = getCurrentWorkArea();
121 if (currentLayerId.equals(layerId))
122 return current;
123 }
124 if (context == null) {
125 if (!cmsView.isAnonymous())
126 context = userDir;
127 }
128 Composite toShow = getLayer(layerId, context);
129 if (toShow != null) {
130 currentLayerId = layerId;
131 if (!isDisposed()) {
132 // getDisplay().syncExec(() -> {
133 if (!toShow.isDisposed()) {
134 toShow.moveAbove(null);
135 } else {
136 log.warn("Cannot show work area because it is disposed.");
137 toShow = initLayer(layerId, layers.get(layerId), context);
138 toShow.moveAbove(null);
139 }
140 dynamicArea.layout(true, true);
141 // });
142 }
143 return toShow;
144 } else {
145 return current;
146 }
147 }
148
149 Composite switchToLayer(SuiteLayer layer, Node context) {
150 // TODO make it more robust
151 for (String layerId : layers.keySet()) {
152 SuiteLayer l = layers.get(layerId);
153 if (layer == l) {
154 return switchToLayer(layerId, context);
155 }
156 }
157 throw new IllegalArgumentException("Layer is not registered.");
158 }
159
160 void addLayer(String id, SuiteLayer layer) {
161 layers.put(id, layer);
162 }
163
164 void removeLayer(String id) {
165 layers.remove(id);
166 if (workAreas.containsKey(id)) {
167 Composite workArea = workAreas.remove(id);
168 if (!workArea.isDisposed())
169 workArea.dispose();
170 }
171 }
172
173 protected Composite initLayer(String id, SuiteLayer layer, Node context) {
174 Composite workArea = cmsView.doAs(() -> (Composite) layer.createUiPart(dynamicArea, context));
175 CmsUiUtils.style(workArea, SuiteStyle.workArea);
176 workArea.setLayoutData(CmsUiUtils.coverAll());
177 workAreas.put(id, workArea);
178 return workArea;
179 }
180
181 synchronized void logout() {
182 userDir = null;
183 Jcr.logout(sysSession);
184 Jcr.logout(homeSession);
185 currentLayerId = null;
186 workAreas.clear();
187 }
188
189 /*
190 * GETTERS / SETTERS
191 */
192
193 Composite getHeader() {
194 return header;
195 }
196
197 Composite getFooter() {
198 return footer;
199 }
200
201 Composite getLeadPane() {
202 return leadPane;
203 }
204
205 Composite getSidePane() {
206 return sidePane;
207 }
208
209 Composite getBelowHeader() {
210 return belowHeader;
211 }
212
213 // Session getSysSession() {
214 // return sysSession;
215 // }
216 //
217 synchronized void initSessions(Repository repository, String userDirPath) throws RepositoryException {
218 this.sysSession = repository.login();
219 this.homeSession = repository.login(NodeConstants.HOME_WORKSPACE);
220 userDir = sysSession.getNode(userDirPath);
221 addDisposeListener((e) -> {
222 Jcr.logout(sysSession);
223 Jcr.logout(homeSession);
224 });
225 }
226
227 Node getUserDir() {
228 return userDir;
229 }
230
231 Session getSysSession() {
232 return sysSession;
233 }
234
235 Session getSession(String workspaceName) {
236 if (workspaceName == null)
237 return sysSession;
238 if (NodeConstants.SYS_WORKSPACE.equals(workspaceName))
239 return sysSession;
240 else if (NodeConstants.HOME_WORKSPACE.equals(workspaceName))
241 return homeSession;
242 else
243 throw new IllegalArgumentException("Unknown workspace " + workspaceName);
244 }
245
246 public CmsView getCmsView() {
247 return cmsView;
248 }
249
250 public Localized getTitle() {
251 return title;
252 }
253
254 public void setTitle(Localized title) {
255 this.title = title;
256 }
257
258 }