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