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