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