7004ad01b1ef04e6a9926bf4e7d1aa521bf5fc52
[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                                 entryArea.createUi(editionArea.getEntryArea(), context);
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.createUi(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.createUi(area, context);
59                                 return area;
60                         }
61                         CmsTheme theme = CmsTheme.getCmsTheme(parent);
62                         TabbedArea tabbedArea = createTabbedArea(parent, theme);
63                         return tabbedArea;
64                 }
65         }
66
67         @Override
68         public void view(CmsUiProvider uiProvider, Composite workAreaC, Node context) {
69                 if (workArea != null) {
70                         try {
71                                 CmsUiUtils.clear(workAreaC);
72                                 workArea.createUi(workAreaC, context);
73                                 workAreaC.requestLayout();
74                                 return;
75                         } catch (RepositoryException e) {
76                                 throw new JcrException("Cannot rebuild work area", e);
77                         }
78                 }
79                 
80                 // tabbed area
81                 TabbedArea tabbedArea = findTabbedArea(workAreaC);
82                 if (tabbedArea == null)
83                         throw new IllegalArgumentException("Unsupported work area " + workAreaC.getClass().getName());
84                 if (uiProvider == null) {
85                         // reset
86                         tabbedArea.closeAllTabs();
87                         if (this.defaultView != null) {
88                                 tabbedArea.view(defaultView, context);
89                         }
90                 } else {
91                         tabbedArea.view(uiProvider, context);
92                 }
93         }
94
95         @Override
96         public Node getCurrentContext(Composite workArea) {
97                 TabbedArea tabbedArea = findTabbedArea(workArea);
98                 if (tabbedArea == null)
99                         return null;
100                 return tabbedArea.getCurrentContext();
101         }
102
103         private TabbedArea findTabbedArea(Composite workArea) {
104                 TabbedArea tabbedArea = null;
105                 if (workArea instanceof SashFormEditionArea) {
106                         tabbedArea = ((SashFormEditionArea) workArea).getTabbedArea();
107                 } else if (workArea instanceof FixedEditionArea) {
108                         tabbedArea = ((FixedEditionArea) workArea).getTabbedArea();
109                 } else if (workArea instanceof TabbedArea) {
110                         tabbedArea = (TabbedArea) workArea;
111                 }
112                 return tabbedArea;
113         }
114
115         @Override
116         public void open(CmsUiProvider uiProvider, Composite workArea, Node context) {
117                 TabbedArea tabbedArea = ((SashFormEditionArea) workArea).getTabbedArea();
118                 tabbedArea.open(uiProvider, context);
119         }
120
121         @Override
122         public Localized getTitle() {
123                 return title;
124         }
125
126         public void init(BundleContext bundleContext, Map<String, Object> properties) {
127                 weights = LangUtils.toStringList(properties.get(Property.weights.name()));
128                 startMaximized = properties.containsKey(Property.startMaximized.name())
129                                 && "true".equals(properties.get(Property.startMaximized.name()));
130                 fixedEntryArea = properties.containsKey(Property.fixedEntryArea.name())
131                                 && "true".equals(properties.get(Property.fixedEntryArea.name()));
132                 if (fixedEntryArea && weights.size() != 0) {
133                         throw new IllegalArgumentException("Property " + Property.weights.name() + " should not be set if property "
134                                         + Property.fixedEntryArea.name() + " is set.");
135                 }
136                 singleTab = properties.containsKey(Property.singleTab.name())
137                                 && "true".equals(properties.get(Property.singleTab.name()));
138
139                 String titleStr = (String) properties.get(SuiteLayer.Property.title.name());
140                 if (titleStr != null) {
141                         if (titleStr.startsWith("%")) {
142                                 title = new Localized() {
143
144                                         @Override
145                                         public String name() {
146                                                 return titleStr;
147                                         }
148
149                                         @Override
150                                         public ClassLoader getL10nClassLoader() {
151                                                 return bundleContext != null
152                                                                 ? bundleContext.getBundle().adapt(BundleWiring.class).getClassLoader()
153                                                                 : getClass().getClassLoader();
154                                         }
155                                 };
156                         } else {
157                                 title = new Localized.Untranslated(titleStr);
158                         }
159                 }
160         }
161
162         public void destroy(BundleContext bundleContext, Map<String, String> properties) {
163
164         }
165
166         public void setEntryArea(CmsUiProvider entryArea) {
167                 this.entryArea = entryArea;
168         }
169
170         public void setWorkArea(CmsUiProvider workArea) {
171                 this.workArea = workArea;
172         }
173
174         public void setDefaultView(CmsUiProvider defaultView) {
175                 this.defaultView = defaultView;
176         }
177
178         TabbedArea createTabbedArea(Composite parent, CmsTheme theme) {
179                 TabbedArea tabbedArea = new TabbedArea(parent, SWT.NONE);
180                 tabbedArea.setSingleTab(singleTab);
181                 tabbedArea.setBodyStyle(SuiteStyle.mainTabBody.style());
182                 tabbedArea.setTabStyle(SuiteStyle.mainTab.style());
183                 tabbedArea.setTabSelectedStyle(SuiteStyle.mainTabSelected.style());
184                 tabbedArea.setCloseIcon(SuiteIcon.close.getSmallIcon(theme));
185                 tabbedArea.setLayoutData(CmsUiUtils.fillAll());
186                 return tabbedArea;
187         }
188
189 //      /** A work area based on an entry area and and a tabbed area. */
190         class SashFormEditionArea extends SashForm {
191                 private static final long serialVersionUID = 2219125778722702618L;
192                 private TabbedArea tabbedArea;
193                 private Composite entryC;
194
195                 SashFormEditionArea(Composite parent, int style) {
196                         super(parent, SWT.HORIZONTAL);
197                         CmsTheme theme = CmsTheme.getCmsTheme(parent);
198
199                         Composite editorC;
200                         if (SWT.RIGHT_TO_LEFT == (style & SWT.RIGHT_TO_LEFT)) {// arabic, hebrew, etc.
201                                 editorC = new Composite(this, SWT.BORDER);
202                                 entryC = new Composite(this, SWT.BORDER);
203                         } else {
204                                 entryC = new Composite(this, SWT.NONE);
205                                 editorC = new Composite(this, SWT.NONE);
206                         }
207
208                         // sash form specific
209                         if (weights.size() != 0) {
210                                 int[] actualWeight = new int[weights.size()];
211                                 for (int i = 0; i < weights.size(); i++) {
212                                         actualWeight[i] = Integer.parseInt(weights.get(i));
213                                 }
214                                 setWeights(actualWeight);
215                         } else {
216                                 int[] actualWeights = new int[] { 3000, 7000 };
217                                 setWeights(actualWeights);
218                         }
219                         if (startMaximized)
220                                 setMaximizedControl(editorC);
221
222                         GridLayout editorAreaLayout = CmsUiUtils.noSpaceGridLayout();
223 //                      editorAreaLayout.verticalSpacing = 0;
224 //                      editorAreaLayout.marginBottom = 0;
225 //                      editorAreaLayout.marginHeight = 0;
226 //                      editorAreaLayout.marginLeft = 0;
227 //                      editorAreaLayout.marginRight = 0;
228                         editorC.setLayout(editorAreaLayout);
229
230                         tabbedArea = createTabbedArea(editorC, theme);
231                 }
232
233                 TabbedArea getTabbedArea() {
234                         return tabbedArea;
235                 }
236
237                 Composite getEntryArea() {
238                         return entryC;
239                 }
240
241         }
242
243         class FixedEditionArea extends Composite {
244                 private static final long serialVersionUID = -5525672639277322465L;
245                 private TabbedArea tabbedArea;
246                 private Composite entryC;
247
248                 public FixedEditionArea(Composite parent, int style) {
249                         super(parent, style);
250                         CmsTheme theme = CmsTheme.getCmsTheme(parent);
251
252                         setLayout(CmsUiUtils.noSpaceGridLayout(2));
253
254                         Composite editorC;
255                         if (SWT.RIGHT_TO_LEFT == (style & SWT.RIGHT_TO_LEFT)) {// arabic, hebrew, etc.
256                                 editorC = new Composite(this, SWT.NONE);
257                                 entryC = new Composite(this, SWT.NONE);
258                         } else {
259                                 entryC = new Composite(this, SWT.NONE);
260                                 editorC = new Composite(this, SWT.NONE);
261                         }
262                         entryC.setLayoutData(CmsUiUtils.fillHeight());
263
264                         GridLayout editorAreaLayout = CmsUiUtils.noSpaceGridLayout();
265 //                      editorAreaLayout.verticalSpacing = 0;
266 //                      editorAreaLayout.marginBottom = 0;
267 //                      editorAreaLayout.marginHeight = 0;
268 //                      editorAreaLayout.marginLeft = 0;
269 //                      editorAreaLayout.marginRight = 0;
270                         editorC.setLayout(editorAreaLayout);
271                         editorC.setLayoutData(CmsUiUtils.fillAll());
272
273                         tabbedArea = createTabbedArea(editorC, theme);
274                 }
275
276                 TabbedArea getTabbedArea() {
277                         return tabbedArea;
278                 }
279
280                 Composite getEntryArea() {
281                         return entryC;
282                 }
283         }
284
285 }