]> git.argeo.org Git - gpl/argeo-suite.git/blob - app/ui/DefaultEditionLayer.java
Prepare next development cycle
[gpl/argeo-suite.git] / app / ui / DefaultEditionLayer.java
1 package org.argeo.app.ui;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.argeo.api.acr.Content;
8 import org.argeo.cms.Localized;
9 import org.argeo.cms.swt.CmsSwtTheme;
10 import org.argeo.cms.swt.CmsSwtUtils;
11 import org.argeo.cms.swt.acr.SwtTabbedArea;
12 import org.argeo.cms.swt.acr.SwtUiProvider;
13 import org.argeo.cms.util.LangUtils;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.custom.SashForm;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.Constants;
21 import org.osgi.framework.wiring.BundleWiring;
22
23 /** An app layer based on an entry area and an editor area. */
24 public class DefaultEditionLayer implements SuiteLayer {
25 private String id;
26 private SwtUiProvider entryArea;
27 private SwtUiProvider defaultView;
28 private SwtUiProvider workArea;
29 private List<String> weights = new ArrayList<>();
30 private boolean startMaximized = false;
31 private boolean fixedEntryArea = false;
32 private boolean singleTab = false;
33 private Localized title = null;
34
35 @Override
36 public Control createUiPart(Composite parent, Content context) {
37 // TODO Factorize more, or split into more specialised classes?
38 if (entryArea != null) {
39 if (fixedEntryArea) {
40 FixedEditionArea editionArea = new FixedEditionArea(parent, parent.getStyle());
41 Control entryAreaC = entryArea.createUiPart(editionArea.getEntryArea(), context);
42 CmsSwtUtils.style(entryAreaC, SuiteStyle.entryArea);
43 if (this.defaultView != null) {
44 editionArea.getTabbedArea().view(defaultView, context);
45 }
46 return editionArea;
47 } else {
48 SashFormEditionArea editionArea = new SashFormEditionArea(parent, parent.getStyle());
49 entryArea.createUiPart(editionArea.getEntryArea(), context);
50 if (this.defaultView != null) {
51 editionArea.getTabbedArea().view(defaultView, context);
52 }
53 return editionArea;
54 }
55 } else {
56 if (this.workArea != null) {
57 Composite area = new Composite(parent, SWT.NONE);
58 this.workArea.createUiPart(area, context);
59 return area;
60 }
61 CmsSwtTheme theme = CmsSwtUtils.getCmsTheme(parent);
62 SwtTabbedArea tabbedArea = createTabbedArea(parent, theme);
63 return tabbedArea;
64 }
65 }
66
67 @Override
68 public void view(SwtUiProvider uiProvider, Composite workAreaC, Content context) {
69 if (workArea != null) {
70 CmsSwtUtils.clear(workAreaC);
71 workArea.createUiPart(workAreaC, context);
72 workAreaC.layout(true, true);
73 return;
74 }
75
76 // tabbed area
77 SwtTabbedArea tabbedArea = findTabbedArea(workAreaC);
78 if (tabbedArea == null)
79 throw new IllegalArgumentException("Unsupported work area " + workAreaC.getClass().getName());
80 if (uiProvider == null) {
81 // reset
82 tabbedArea.closeAllTabs();
83 if (this.defaultView != null) {
84 tabbedArea.view(defaultView, context);
85 }
86 } else {
87 tabbedArea.view(uiProvider, context);
88 }
89 }
90
91 @Override
92 public Content getCurrentContext(Composite workArea) {
93 SwtTabbedArea tabbedArea = findTabbedArea(workArea);
94 if (tabbedArea == null)
95 return null;
96 return tabbedArea.getCurrentContext();
97 }
98
99 private SwtTabbedArea findTabbedArea(Composite workArea) {
100 SwtTabbedArea tabbedArea = null;
101 if (workArea instanceof SashFormEditionArea) {
102 tabbedArea = ((SashFormEditionArea) workArea).getTabbedArea();
103 } else if (workArea instanceof FixedEditionArea) {
104 tabbedArea = ((FixedEditionArea) workArea).getTabbedArea();
105 } else if (workArea instanceof SwtTabbedArea) {
106 tabbedArea = (SwtTabbedArea) workArea;
107 }
108 return tabbedArea;
109 }
110
111 @Override
112 public void open(SwtUiProvider uiProvider, Composite workArea, Content context) {
113 SwtTabbedArea tabbedArea = ((SashFormEditionArea) workArea).getTabbedArea();
114 tabbedArea.open(uiProvider, context);
115 }
116
117 @Override
118 public Localized getTitle() {
119 return title;
120 }
121
122 @Override
123 public String getId() {
124 return id;
125 }
126
127 public void init(BundleContext bundleContext, Map<String, Object> properties) {
128 String pid = (String) properties.get(Constants.SERVICE_PID);
129 id = pid;
130
131 weights = LangUtils.toStringList(properties.get(Property.weights.name()));
132 startMaximized = properties.containsKey(Property.startMaximized.name())
133 && "true".equals(properties.get(Property.startMaximized.name()));
134 fixedEntryArea = properties.containsKey(Property.fixedEntryArea.name())
135 && "true".equals(properties.get(Property.fixedEntryArea.name()));
136 if (fixedEntryArea && weights.size() != 0) {
137 throw new IllegalArgumentException("Property " + Property.weights.name() + " should not be set if property "
138 + Property.fixedEntryArea.name() + " is set.");
139 }
140 singleTab = properties.containsKey(Property.singleTab.name())
141 && "true".equals(properties.get(Property.singleTab.name()));
142
143 String titleStr = (String) properties.get(SuiteLayer.Property.title.name());
144 if (titleStr != null) {
145 if (titleStr.startsWith("%")) {
146 title = new Localized() {
147
148 @Override
149 public String name() {
150 return titleStr;
151 }
152
153 @Override
154 public ClassLoader getL10nClassLoader() {
155 return bundleContext != null
156 ? bundleContext.getBundle().adapt(BundleWiring.class).getClassLoader()
157 : getClass().getClassLoader();
158 }
159 };
160 } else {
161 title = new Localized.Untranslated(titleStr);
162 }
163 }
164 }
165
166 public void destroy(BundleContext bundleContext, Map<String, String> properties) {
167
168 }
169
170 public void setEntryArea(SwtUiProvider entryArea) {
171 this.entryArea = entryArea;
172 }
173
174 public void setWorkArea(SwtUiProvider workArea) {
175 this.workArea = workArea;
176 }
177
178 public void setDefaultView(SwtUiProvider defaultView) {
179 this.defaultView = defaultView;
180 }
181
182 SwtTabbedArea createTabbedArea(Composite parent, CmsSwtTheme theme) {
183 SwtTabbedArea tabbedArea = new SwtTabbedArea(parent, SWT.NONE);
184 tabbedArea.setSingleTab(singleTab);
185 tabbedArea.setBodyStyle(SuiteStyle.mainTabBody.style());
186 tabbedArea.setTabStyle(SuiteStyle.mainTab.style());
187 tabbedArea.setTabSelectedStyle(SuiteStyle.mainTabSelected.style());
188 tabbedArea.setCloseIcon(theme.getSmallIcon(SuiteIcon.close));
189 tabbedArea.setLayoutData(CmsSwtUtils.fillAll());
190 return tabbedArea;
191 }
192
193 // /** A work area based on an entry area and and a tabbed area. */
194 class SashFormEditionArea extends SashForm {
195 private static final long serialVersionUID = 2219125778722702618L;
196 private SwtTabbedArea tabbedArea;
197 private Composite entryC;
198
199 SashFormEditionArea(Composite parent, int style) {
200 super(parent, SWT.HORIZONTAL);
201 CmsSwtTheme theme = CmsSwtUtils.getCmsTheme(parent);
202
203 Composite editorC;
204 if (SWT.RIGHT_TO_LEFT == (style & SWT.RIGHT_TO_LEFT)) {// arabic, hebrew, etc.
205 editorC = new Composite(this, SWT.BORDER);
206 entryC = new Composite(this, SWT.BORDER);
207 } else {
208 entryC = new Composite(this, SWT.NONE);
209 editorC = new Composite(this, SWT.NONE);
210 }
211
212 // sash form specific
213 if (weights.size() != 0) {
214 int[] actualWeight = new int[weights.size()];
215 for (int i = 0; i < weights.size(); i++) {
216 actualWeight[i] = Integer.parseInt(weights.get(i));
217 }
218 setWeights(actualWeight);
219 } else {
220 int[] actualWeights = new int[] { 3000, 7000 };
221 setWeights(actualWeights);
222 }
223 if (startMaximized)
224 setMaximizedControl(editorC);
225
226 GridLayout editorAreaLayout = CmsSwtUtils.noSpaceGridLayout();
227 // editorAreaLayout.verticalSpacing = 0;
228 // editorAreaLayout.marginBottom = 0;
229 // editorAreaLayout.marginHeight = 0;
230 // editorAreaLayout.marginLeft = 0;
231 // editorAreaLayout.marginRight = 0;
232 editorC.setLayout(editorAreaLayout);
233
234 tabbedArea = createTabbedArea(editorC, theme);
235 }
236
237 SwtTabbedArea getTabbedArea() {
238 return tabbedArea;
239 }
240
241 Composite getEntryArea() {
242 return entryC;
243 }
244
245 }
246
247 class FixedEditionArea extends Composite {
248 private static final long serialVersionUID = -5525672639277322465L;
249 private SwtTabbedArea tabbedArea;
250 private Composite entryC;
251
252 public FixedEditionArea(Composite parent, int style) {
253 super(parent, style);
254 CmsSwtTheme theme = CmsSwtUtils.getCmsTheme(parent);
255
256 setLayout(CmsSwtUtils.noSpaceGridLayout(2));
257
258 Composite editorC;
259 if (SWT.RIGHT_TO_LEFT == (style & SWT.RIGHT_TO_LEFT)) {// arabic, hebrew, etc.
260 editorC = new Composite(this, SWT.NONE);
261 entryC = new Composite(this, SWT.NONE);
262 } else {
263 entryC = new Composite(this, SWT.NONE);
264 editorC = new Composite(this, SWT.NONE);
265 }
266 entryC.setLayoutData(CmsSwtUtils.fillHeight());
267
268 GridLayout editorAreaLayout = CmsSwtUtils.noSpaceGridLayout();
269 // editorAreaLayout.verticalSpacing = 0;
270 // editorAreaLayout.marginBottom = 0;
271 // editorAreaLayout.marginHeight = 0;
272 // editorAreaLayout.marginLeft = 0;
273 // editorAreaLayout.marginRight = 0;
274 editorC.setLayout(editorAreaLayout);
275 editorC.setLayoutData(CmsSwtUtils.fillAll());
276
277 tabbedArea = createTabbedArea(editorC, theme);
278 }
279
280 SwtTabbedArea getTabbedArea() {
281 return tabbedArea;
282 }
283
284 Composite getEntryArea() {
285 return entryC;
286 }
287 }
288
289 }