Introduce singleTab property.
[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.suite.ui.widgets.TabbedArea;
15 import org.argeo.util.LangUtils;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.custom.SashForm;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.wiring.BundleWiring;
23
24 /** An app layer based on an entry area and an editor area. */
25 public class DefaultEditionLayer implements SuiteLayer {
26         private CmsUiProvider entryArea;
27         private CmsUiProvider workArea;
28         private List<String> weights = new ArrayList<>();
29         private boolean startMaximized = false;
30         private boolean singleTab = false;
31         private Localized title = null;
32
33         @Override
34         public Control createUi(Composite parent, Node context) throws RepositoryException {
35                 if (entryArea != null) {
36                         SashFormEditionArea sashFormEditionArea = new SashFormEditionArea(parent, parent.getStyle());
37                         entryArea.createUi(sashFormEditionArea.getEntryArea(), context);
38                         if (this.workArea != null) {
39                                 this.workArea.createUi(sashFormEditionArea.getEditorArea(), context);
40                         }
41                         return sashFormEditionArea;
42                 } else {
43                         if (this.workArea != null) {
44                                 Composite area = new Composite(parent, SWT.NONE);
45                                 this.workArea.createUi(area, context);
46                                 return area;
47                         }
48                         CmsTheme theme = CmsTheme.getCmsTheme(parent);
49                         TabbedArea tabbedArea = createTabbedArea(parent, theme);
50                         return tabbedArea;
51                 }
52         }
53
54         @Override
55         public void view(CmsUiProvider uiProvider, Composite workArea, Node context) {
56                 TabbedArea tabbedArea;
57                 if (workArea instanceof SashFormEditionArea) {
58                         tabbedArea = ((SashFormEditionArea) workArea).getTabbedArea();
59                 } else if (workArea instanceof TabbedArea) {
60                         tabbedArea = (TabbedArea) workArea;
61                 } else
62                         throw new IllegalArgumentException("Unsupported work area " + workArea.getClass().getName());
63                 tabbedArea.view(uiProvider, context);
64         }
65
66         @Override
67         public void open(CmsUiProvider uiProvider, Composite workArea, Node context) {
68                 TabbedArea tabbedArea = ((SashFormEditionArea) workArea).getTabbedArea();
69                 tabbedArea.open(uiProvider, context);
70         }
71
72         @Override
73         public Localized getTitle() {
74                 return title;
75         }
76
77         public void init(BundleContext bundleContext, Map<String, Object> properties) {
78                 weights = LangUtils.toStringList(properties.get(Property.weights.name()));
79                 startMaximized = properties.containsKey(Property.startMaximized.name())
80                                 && "true".equals(properties.get(Property.startMaximized.name()));
81                 singleTab = properties.containsKey(Property.singleTab.name())
82                                 && "true".equals(properties.get(Property.singleTab.name()));
83
84                 String titleStr = (String) properties.get(SuiteLayer.Property.title.name());
85                 if (titleStr != null) {
86                         if (titleStr.startsWith("%")) {
87                                 title = new Localized() {
88
89                                         @Override
90                                         public String name() {
91                                                 return titleStr;
92                                         }
93
94                                         @Override
95                                         public ClassLoader getL10nClassLoader() {
96                                                 return bundleContext != null
97                                                                 ? bundleContext.getBundle().adapt(BundleWiring.class).getClassLoader()
98                                                                 : getClass().getClassLoader();
99                                         }
100                                 };
101                         } else {
102                                 title = new Localized.Untranslated(titleStr);
103                         }
104                 }
105         }
106
107         public void destroy(BundleContext bundleContext, Map<String, String> properties) {
108                 
109         }
110         
111         public void setEntryArea(CmsUiProvider entryArea) {
112                 this.entryArea = entryArea;
113         }
114
115         public void setWorkArea(CmsUiProvider workArea) {
116                 this.workArea = workArea;
117         }
118
119         TabbedArea createTabbedArea(Composite parent, CmsTheme theme) {
120                 TabbedArea tabbedArea = new TabbedArea(parent, SWT.NONE);
121                 tabbedArea.setSingleTab(singleTab);
122                 tabbedArea.setBodyStyle(SuiteStyle.mainTabBody.style());
123                 tabbedArea.setTabStyle(SuiteStyle.mainTab.style());
124                 tabbedArea.setTabSelectedStyle(SuiteStyle.mainTabSelected.style());
125                 tabbedArea.setCloseIcon(SuiteIcon.close.getSmallIcon(theme));
126                 tabbedArea.setLayoutData(CmsUiUtils.fillAll());
127                 return tabbedArea;
128         }
129
130         /** A work area based on an entry area and and a tabbed area. */
131         class SashFormEditionArea extends SashForm {
132                 private static final long serialVersionUID = 2219125778722702618L;
133                 private CmsTheme theme;
134                 private Composite entryArea;
135                 private Composite editorArea;
136                 private TabbedArea tabbedArea;
137
138                 SashFormEditionArea(Composite parent, int style) {
139                         super(parent, SWT.HORIZONTAL);
140                         theme = CmsTheme.getCmsTheme(parent);
141
142                         if (SWT.RIGHT_TO_LEFT == (style & SWT.RIGHT_TO_LEFT)) {// arabic, hebrew, etc.
143                                 editorArea = new Composite(this, SWT.BORDER);
144                                 entryArea = new Composite(this, SWT.BORDER);
145                         } else {
146                                 entryArea = new Composite(this, SWT.NONE);
147                                 editorArea = new Composite(this, SWT.NONE);
148                         }
149
150                         if (weights.size() != 0) {
151                                 int[] actualWeight = new int[weights.size()];
152                                 for (int i = 0; i < weights.size(); i++) {
153                                         actualWeight[i] = Integer.parseInt(weights.get(i));
154                                 }
155                                 setWeights(actualWeight);
156                         } else {
157                                 int[] actualWeights = new int[] { 3000, 7000 };
158                                 setWeights(actualWeights);
159                         }
160                         if (startMaximized)
161                                 setMaximizedControl(editorArea);
162                         editorArea.setLayout(new GridLayout());
163
164                         if (DefaultEditionLayer.this.workArea == null) {
165                                 tabbedArea = createTabbedArea(editorArea, theme);
166                         }
167
168                 }
169
170                 Composite getEntryArea() {
171                         return entryArea;
172                 }
173
174                 TabbedArea getTabbedArea() {
175                         return tabbedArea;
176                 }
177
178                 Composite getEditorArea() {
179                         return editorArea;
180                 }
181
182         }
183 }