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