Work on styling.
[gpl/argeo-suite.git] / 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.ui.CmsTheme;
11 import org.argeo.cms.ui.CmsUiProvider;
12 import org.argeo.cms.ui.util.CmsUiUtils;
13 import org.argeo.cms.ui.widgets.TabbedArea;
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
21 /** An app layer based on an entry area and an editor area. */
22 public class DefaultEditionLayer implements SuiteLayer {
23         private CmsUiProvider entryArea;
24         private CmsUiProvider workArea;
25         private List<String> weights = new ArrayList<>();
26         private boolean startMaximized = false;
27
28         @Override
29         public Control createUi(Composite parent, Node context) throws RepositoryException {
30                 if (entryArea != null) {
31                         SashFormEditionArea sashFormEditionArea = new SashFormEditionArea(parent, parent.getStyle());
32                         entryArea.createUi(sashFormEditionArea.getEntryArea(), context);
33                         if (this.workArea != null) {
34                                 this.workArea.createUi(sashFormEditionArea.getEditorArea(), context);
35                         }
36                         return sashFormEditionArea;
37                 } else {
38                         if (this.workArea != null) {
39                                 Composite area = new Composite(parent, SWT.NONE);
40                                 this.workArea.createUi(area, context);
41                                 return area;
42                         }
43                         CmsTheme theme = CmsTheme.getCmsTheme(parent);
44                         TabbedArea tabbedArea = createTabbedArea(parent, theme);
45                         return tabbedArea;
46                 }
47         }
48
49         @Override
50         public void view(CmsUiProvider uiProvider, Composite workArea, Node context) {
51                 TabbedArea tabbedArea;
52                 if (workArea instanceof SashFormEditionArea) {
53                         tabbedArea = ((SashFormEditionArea) workArea).getTabbedArea();
54                 } else if (workArea instanceof TabbedArea) {
55                         tabbedArea = (TabbedArea) workArea;
56                 } else
57                         throw new IllegalArgumentException("Unsupported work area " + workArea.getClass().getName());
58                 tabbedArea.view(uiProvider, context);
59         }
60
61         @Override
62         public void open(CmsUiProvider uiProvider, Composite workArea, Node context) {
63                 TabbedArea tabbedArea = ((SashFormEditionArea) workArea).getTabbedArea();
64                 tabbedArea.open(uiProvider, context);
65         }
66
67         public void init(Map<String, Object> properties) {
68                 weights = LangUtils.toStringList(properties.get(Property.weights.name()));
69                 startMaximized = properties.containsKey(Property.startMaximized.name())
70                                 && "true".equals(properties.get(Property.startMaximized.name()));
71         }
72
73         public void setEntryArea(CmsUiProvider entryArea) {
74                 this.entryArea = entryArea;
75         }
76
77         public void setWorkArea(CmsUiProvider workArea) {
78                 this.workArea = workArea;
79         }
80
81         TabbedArea createTabbedArea(Composite parent, CmsTheme theme) {
82                 TabbedArea tabbedArea = new TabbedArea(parent, SWT.NONE);
83                 tabbedArea.setBodyStyle(SuiteStyle.mainTabBody.style());
84                 tabbedArea.setTabStyle(SuiteStyle.mainTab.style());
85                 tabbedArea.setTabSelectedStyle(SuiteStyle.mainTabSelected.style());
86                 tabbedArea.setCloseIcon(SuiteIcon.close.getSmallIcon(theme));
87                 tabbedArea.setLayoutData(CmsUiUtils.fillAll());
88                 return tabbedArea;
89         }
90
91         /** A work area based on an entry area and and a tabbed area. */
92         class SashFormEditionArea extends SashForm {
93                 private static final long serialVersionUID = 2219125778722702618L;
94                 private CmsTheme theme;
95                 private Composite entryArea;
96                 private Composite editorArea;
97                 private TabbedArea tabbedArea;
98
99                 SashFormEditionArea(Composite parent, int style) {
100                         super(parent, SWT.HORIZONTAL);
101                         theme = CmsTheme.getCmsTheme(parent);
102
103                         if (SWT.RIGHT_TO_LEFT == (style & SWT.RIGHT_TO_LEFT)) {// arabic, hebrew, etc.
104                                 editorArea = new Composite(this, SWT.BORDER);
105                                 entryArea = new Composite(this, SWT.BORDER);
106                         } else {
107                                 entryArea = new Composite(this, SWT.NONE);
108                                 editorArea = new Composite(this, SWT.NONE);
109                         }
110
111                         if (weights.size() != 0) {
112                                 int[] actualWeight = new int[weights.size()];
113                                 for (int i = 0; i < weights.size(); i++) {
114                                         actualWeight[i] = Integer.parseInt(weights.get(i));
115                                 }
116                                 setWeights(actualWeight);
117                         } else {
118                                 int[] actualWeights = new int[] { 3000, 7000 };
119                                 setWeights(actualWeights);
120                         }
121                         if (startMaximized)
122                                 setMaximizedControl(editorArea);
123                         editorArea.setLayout(new GridLayout());
124
125                         if (DefaultEditionLayer.this.workArea == null) {
126                                 tabbedArea = createTabbedArea(editorArea, theme);
127                         }
128
129                 }
130
131                 Composite getEntryArea() {
132                         return entryArea;
133                 }
134
135                 TabbedArea getTabbedArea() {
136                         return tabbedArea;
137                 }
138
139                 Composite getEditorArea() {
140                         return editorArea;
141                 }
142
143         }
144 }