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