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