]> git.argeo.org Git - gpl/argeo-suite.git/blob - ux/SwtAppUi.java
Prepare next development cycle
[gpl/argeo-suite.git] / ux / SwtAppUi.java
1 package org.argeo.app.swt.ux;
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.app.ux.AppUi;
9 import org.argeo.app.ux.SuiteStyle;
10 import org.argeo.cms.Localized;
11 import org.argeo.cms.swt.CmsSwtUi;
12 import org.argeo.cms.swt.CmsSwtUtils;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.layout.FormLayout;
15 import org.eclipse.swt.widgets.Composite;
16
17 /** The view for the default UX of Argeo Suite. */
18 public class SwtAppUi extends CmsSwtUi implements AppUi {
19 static enum Structural {
20 header, footer, leadPane, sidePane, loginScreen, adminLeadPane;
21 }
22
23 private static final long serialVersionUID = 6207018859086689108L;
24 private final static CmsLog log = CmsLog.getLog(SwtAppUi.class);
25
26 private Localized title;
27 private Composite header;
28 private Composite footer;
29 private Composite belowHeader;
30 private Composite leadPane;
31 private Composite sidePane;
32 private Composite dynamicArea;
33
34 private Content userDir;
35
36 private Map<String, SwtAppLayer> layers = new HashMap<>();
37 private Map<String, Composite> workAreas = new HashMap<>();
38 private String currentLayerId = null;
39
40 private boolean loginScreen = false;
41
42 public SwtAppUi(Composite parent, int style) {
43 super(parent, style);
44 this.setLayout(CmsSwtUtils.noSpaceGridLayout());
45
46 header = new Composite(this, SWT.NONE);
47 header.setLayout(CmsSwtUtils.noSpaceGridLayout());
48 CmsSwtUtils.style(header, SuiteStyle.header);
49 header.setLayoutData(CmsSwtUtils.fillWidth());
50
51 belowHeader = new Composite(this, SWT.NONE);
52 belowHeader.setLayoutData(CmsSwtUtils.fillAll());
53
54 footer = new Composite(this, SWT.NONE);
55 footer.setLayout(CmsSwtUtils.noSpaceGridLayout());
56 CmsSwtUtils.style(footer, SuiteStyle.footer);
57 footer.setLayoutData(CmsSwtUtils.fillWidth());
58 }
59
60 public void refreshBelowHeader(boolean initApp) {
61 CmsSwtUtils.clear(belowHeader);
62 int style = getStyle();
63 if (initApp) {
64 belowHeader.setLayout(CmsSwtUtils.noSpaceGridLayout(3));
65
66 if (SWT.RIGHT_TO_LEFT == (style & SWT.RIGHT_TO_LEFT)) {// arabic, hebrew, etc.
67 sidePane = new Composite(belowHeader, SWT.NONE);
68 sidePane.setLayout(CmsSwtUtils.noSpaceGridLayout());
69 sidePane.setLayoutData(CmsSwtUtils.fillHeight());
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 sidePane = new Composite(belowHeader, SWT.NONE);
76 sidePane.setLayout(CmsSwtUtils.noSpaceGridLayout());
77 sidePane.setLayoutData(CmsSwtUtils.fillHeight());
78 }
79 leadPane.setLayoutData(CmsSwtUtils.fillHeight());
80 leadPane.setLayout(CmsSwtUtils.noSpaceGridLayout());
81 CmsSwtUtils.style(leadPane, SuiteStyle.leadPane);
82
83 dynamicArea.setLayoutData(CmsSwtUtils.fillAll());
84 dynamicArea.setLayout(new FormLayout());
85
86 } else {
87 belowHeader.setLayout(CmsSwtUtils.noSpaceGridLayout());
88 }
89 }
90
91 /*
92 * LAYERS
93 */
94
95 public Composite getCurrentWorkArea() {
96 if (currentLayerId == null)
97 throw new IllegalStateException("No current layer");
98 return workAreas.get(currentLayerId);
99 }
100
101 public String getCurrentLayerId() {
102 return currentLayerId;
103 }
104
105 private Composite getLayer(String id, Content context) {
106 if (!layers.containsKey(id))
107 return null;
108 if (!workAreas.containsKey(id))
109 initLayer(id, layers.get(id), context);
110 return workAreas.get(id);
111 }
112
113 public Composite switchToLayer(String layerId, Content context) {
114 Composite current = null;
115 if (currentLayerId != null) {
116 current = getCurrentWorkArea();
117 if (currentLayerId.equals(layerId))
118 return current;
119 }
120 if (context == null) {
121 if (!getCmsView().isAnonymous())
122 context = getUserDir();
123 }
124 Composite toShow = getLayer(layerId, context);
125 if (toShow != null) {
126 currentLayerId = layerId;
127 if (!isDisposed()) {
128 if (!toShow.isDisposed()) {
129 toShow.moveAbove(null);
130 } else {
131 log.warn("Cannot show work area because it is disposed.");
132 toShow = initLayer(layerId, layers.get(layerId), context);
133 toShow.moveAbove(null);
134 }
135 dynamicArea.layout(true, true);
136 }
137 return toShow;
138 } else {
139 return current;
140 }
141 }
142
143 public void switchToLayer(SwtAppLayer layer, Content context) {
144 // TODO make it more robust
145 for (String layerId : layers.keySet()) {
146 SwtAppLayer l = layers.get(layerId);
147 if (layer.getId().equals(l.getId())) {
148 switchToLayer(layerId, context);
149 return;
150 }
151 }
152 throw new IllegalArgumentException("Layer is not registered.");
153 }
154
155 public void addLayer(String id, SwtAppLayer layer) {
156 if (!id.equals(layer.getId())) {
157 log.error("Layer id as key '" + id + "' is not consistent with layer id '" + layer.getId()
158 + "', ignoring...");
159 return;
160 }
161 layers.put(id, layer);
162 }
163
164 public 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, SwtAppLayer layer, Content context) {
174 Composite workArea = getCmsView().doAs(() -> (Composite) layer.createUiPart(dynamicArea, context));
175 CmsSwtUtils.style(workArea, SuiteStyle.workArea);
176 workArea.setLayoutData(CmsSwtUtils.coverAll());
177 workAreas.put(id, workArea);
178 return workArea;
179 }
180
181 public synchronized void logout() {
182 userDir = null;
183 currentLayerId = null;
184 workAreas.clear();
185 }
186
187 /*
188 * GETTERS / SETTERS
189 */
190
191 public Composite getHeader() {
192 return header;
193 }
194
195 public Composite getFooter() {
196 return footer;
197 }
198
199 public Composite getLeadPane() {
200 return leadPane;
201 }
202
203 public Composite getSidePane() {
204 return sidePane;
205 }
206
207 public Composite getBelowHeader() {
208 return belowHeader;
209 }
210
211 public Content getUserDir() {
212 return userDir;
213 }
214
215 public void setUserDir(Content userDir) {
216 this.userDir = userDir;
217 }
218
219 // @Override
220 public Localized getTitle() {
221 return title;
222 }
223
224 public void setTitle(Localized title) {
225 this.title = title;
226 }
227
228 @Override
229 public boolean isLoginScreen() {
230 return loginScreen;
231 }
232
233 public void setLoginScreen(boolean loginScreen) {
234 this.loginScreen = loginScreen;
235 }
236 }