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