]> git.argeo.org Git - gpl/argeo-suite.git/blob - org.argeo.app.ui/src/org/argeo/app/ui/DefaultEditionLayer.java
Remove unused imports
[gpl/argeo-suite.git] / org.argeo.app.ui / src / org / argeo / app / ui / DefaultEditionLayer.java
1 package org.argeo.app.ui;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.argeo.api.acr.Content;
8 import org.argeo.cms.Localized;
9 import org.argeo.cms.swt.CmsSwtTheme;
10 import org.argeo.cms.swt.CmsSwtUtils;
11 import org.argeo.cms.swt.acr.SwtTabbedArea;
12 import org.argeo.cms.swt.acr.SwtUiProvider;
13 import org.argeo.util.LangUtils;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.custom.SashForm;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.wiring.BundleWiring;
21
22 /** An app layer based on an entry area and an editor area. */
23 public class DefaultEditionLayer implements SuiteLayer {
24 private SwtUiProvider entryArea;
25 private SwtUiProvider defaultView;
26 private SwtUiProvider workArea;
27 private List<String> weights = new ArrayList<>();
28 private boolean startMaximized = false;
29 private boolean fixedEntryArea = false;
30 private boolean singleTab = false;
31 private Localized title = null;
32
33 @Override
34 public Control createUiPart(Composite parent, Content context) {
35 // TODO Factorize more, or split into more specialised classes?
36 if (entryArea != null) {
37 if (fixedEntryArea) {
38 FixedEditionArea editionArea = new FixedEditionArea(parent, parent.getStyle());
39 Control entryAreaC = entryArea.createUiPart(editionArea.getEntryArea(), context);
40 CmsSwtUtils.style(entryAreaC, SuiteStyle.entryArea);
41 if (this.defaultView != null) {
42 editionArea.getTabbedArea().view(defaultView, context);
43 }
44 return editionArea;
45 } else {
46 SashFormEditionArea editionArea = new SashFormEditionArea(parent, parent.getStyle());
47 entryArea.createUiPart(editionArea.getEntryArea(), context);
48 if (this.defaultView != null) {
49 editionArea.getTabbedArea().view(defaultView, context);
50 }
51 return editionArea;
52 }
53 } else {
54 if (this.workArea != null) {
55 Composite area = new Composite(parent, SWT.NONE);
56 this.workArea.createUiPart(area, context);
57 return area;
58 }
59 CmsSwtTheme theme = CmsSwtUtils.getCmsTheme(parent);
60 SwtTabbedArea tabbedArea = createTabbedArea(parent, theme);
61 return tabbedArea;
62 }
63 }
64
65 @Override
66 public void view(SwtUiProvider uiProvider, Composite workAreaC, Content context) {
67 if (workArea != null) {
68 CmsSwtUtils.clear(workAreaC);
69 workArea.createUiPart(workAreaC, context);
70 workAreaC.layout(true, true);
71 return;
72 }
73
74 // tabbed area
75 SwtTabbedArea tabbedArea = findTabbedArea(workAreaC);
76 if (tabbedArea == null)
77 throw new IllegalArgumentException("Unsupported work area " + workAreaC.getClass().getName());
78 if (uiProvider == null) {
79 // reset
80 tabbedArea.closeAllTabs();
81 if (this.defaultView != null) {
82 tabbedArea.view(defaultView, context);
83 }
84 } else {
85 tabbedArea.view(uiProvider, context);
86 }
87 }
88
89 @Override
90 public Content getCurrentContext(Composite workArea) {
91 SwtTabbedArea tabbedArea = findTabbedArea(workArea);
92 if (tabbedArea == null)
93 return null;
94 return tabbedArea.getCurrentContext();
95 }
96
97 private SwtTabbedArea findTabbedArea(Composite workArea) {
98 SwtTabbedArea tabbedArea = null;
99 if (workArea instanceof SashFormEditionArea) {
100 tabbedArea = ((SashFormEditionArea) workArea).getTabbedArea();
101 } else if (workArea instanceof FixedEditionArea) {
102 tabbedArea = ((FixedEditionArea) workArea).getTabbedArea();
103 } else if (workArea instanceof SwtTabbedArea) {
104 tabbedArea = (SwtTabbedArea) workArea;
105 }
106 return tabbedArea;
107 }
108
109 @Override
110 public void open(SwtUiProvider uiProvider, Composite workArea, Content context) {
111 SwtTabbedArea tabbedArea = ((SashFormEditionArea) workArea).getTabbedArea();
112 tabbedArea.open(uiProvider, context);
113 }
114
115 @Override
116 public Localized getTitle() {
117 return title;
118 }
119
120 public void init(BundleContext bundleContext, Map<String, Object> properties) {
121 weights = LangUtils.toStringList(properties.get(Property.weights.name()));
122 startMaximized = properties.containsKey(Property.startMaximized.name())
123 && "true".equals(properties.get(Property.startMaximized.name()));
124 fixedEntryArea = properties.containsKey(Property.fixedEntryArea.name())
125 && "true".equals(properties.get(Property.fixedEntryArea.name()));
126 if (fixedEntryArea && weights.size() != 0) {
127 throw new IllegalArgumentException("Property " + Property.weights.name() + " should not be set if property "
128 + Property.fixedEntryArea.name() + " is set.");
129 }
130 singleTab = properties.containsKey(Property.singleTab.name())
131 && "true".equals(properties.get(Property.singleTab.name()));
132
133 String titleStr = (String) properties.get(SuiteLayer.Property.title.name());
134 if (titleStr != null) {
135 if (titleStr.startsWith("%")) {
136 title = new Localized() {
137
138 @Override
139 public String name() {
140 return titleStr;
141 }
142
143 @Override
144 public ClassLoader getL10nClassLoader() {
145 return bundleContext != null
146 ? bundleContext.getBundle().adapt(BundleWiring.class).getClassLoader()
147 : getClass().getClassLoader();
148 }
149 };
150 } else {
151 title = new Localized.Untranslated(titleStr);
152 }
153 }
154 }
155
156 public void destroy(BundleContext bundleContext, Map<String, String> properties) {
157
158 }
159
160 public void setEntryArea(SwtUiProvider entryArea) {
161 this.entryArea = entryArea;
162 }
163
164 public void setWorkArea(SwtUiProvider workArea) {
165 this.workArea = workArea;
166 }
167
168 public void setDefaultView(SwtUiProvider defaultView) {
169 this.defaultView = defaultView;
170 }
171
172 SwtTabbedArea createTabbedArea(Composite parent, CmsSwtTheme theme) {
173 SwtTabbedArea tabbedArea = new SwtTabbedArea(parent, SWT.NONE);
174 tabbedArea.setSingleTab(singleTab);
175 tabbedArea.setBodyStyle(SuiteStyle.mainTabBody.style());
176 tabbedArea.setTabStyle(SuiteStyle.mainTab.style());
177 tabbedArea.setTabSelectedStyle(SuiteStyle.mainTabSelected.style());
178 tabbedArea.setCloseIcon(theme.getSmallIcon(SuiteIcon.close));
179 tabbedArea.setLayoutData(CmsSwtUtils.fillAll());
180 return tabbedArea;
181 }
182
183 // /** A work area based on an entry area and and a tabbed area. */
184 class SashFormEditionArea extends SashForm {
185 private static final long serialVersionUID = 2219125778722702618L;
186 private SwtTabbedArea tabbedArea;
187 private Composite entryC;
188
189 SashFormEditionArea(Composite parent, int style) {
190 super(parent, SWT.HORIZONTAL);
191 CmsSwtTheme theme = CmsSwtUtils.getCmsTheme(parent);
192
193 Composite editorC;
194 if (SWT.RIGHT_TO_LEFT == (style & SWT.RIGHT_TO_LEFT)) {// arabic, hebrew, etc.
195 editorC = new Composite(this, SWT.BORDER);
196 entryC = new Composite(this, SWT.BORDER);
197 } else {
198 entryC = new Composite(this, SWT.NONE);
199 editorC = new Composite(this, SWT.NONE);
200 }
201
202 // sash form specific
203 if (weights.size() != 0) {
204 int[] actualWeight = new int[weights.size()];
205 for (int i = 0; i < weights.size(); i++) {
206 actualWeight[i] = Integer.parseInt(weights.get(i));
207 }
208 setWeights(actualWeight);
209 } else {
210 int[] actualWeights = new int[] { 3000, 7000 };
211 setWeights(actualWeights);
212 }
213 if (startMaximized)
214 setMaximizedControl(editorC);
215
216 GridLayout editorAreaLayout = CmsSwtUtils.noSpaceGridLayout();
217 // editorAreaLayout.verticalSpacing = 0;
218 // editorAreaLayout.marginBottom = 0;
219 // editorAreaLayout.marginHeight = 0;
220 // editorAreaLayout.marginLeft = 0;
221 // editorAreaLayout.marginRight = 0;
222 editorC.setLayout(editorAreaLayout);
223
224 tabbedArea = createTabbedArea(editorC, theme);
225 }
226
227 SwtTabbedArea getTabbedArea() {
228 return tabbedArea;
229 }
230
231 Composite getEntryArea() {
232 return entryC;
233 }
234
235 }
236
237 class FixedEditionArea extends Composite {
238 private static final long serialVersionUID = -5525672639277322465L;
239 private SwtTabbedArea tabbedArea;
240 private Composite entryC;
241
242 public FixedEditionArea(Composite parent, int style) {
243 super(parent, style);
244 CmsSwtTheme theme = CmsSwtUtils.getCmsTheme(parent);
245
246 setLayout(CmsSwtUtils.noSpaceGridLayout(2));
247
248 Composite editorC;
249 if (SWT.RIGHT_TO_LEFT == (style & SWT.RIGHT_TO_LEFT)) {// arabic, hebrew, etc.
250 editorC = new Composite(this, SWT.NONE);
251 entryC = new Composite(this, SWT.NONE);
252 } else {
253 entryC = new Composite(this, SWT.NONE);
254 editorC = new Composite(this, SWT.NONE);
255 }
256 entryC.setLayoutData(CmsSwtUtils.fillHeight());
257
258 GridLayout editorAreaLayout = CmsSwtUtils.noSpaceGridLayout();
259 // editorAreaLayout.verticalSpacing = 0;
260 // editorAreaLayout.marginBottom = 0;
261 // editorAreaLayout.marginHeight = 0;
262 // editorAreaLayout.marginLeft = 0;
263 // editorAreaLayout.marginRight = 0;
264 editorC.setLayout(editorAreaLayout);
265 editorC.setLayoutData(CmsSwtUtils.fillAll());
266
267 tabbedArea = createTabbedArea(editorC, theme);
268 }
269
270 SwtTabbedArea getTabbedArea() {
271 return tabbedArea;
272 }
273
274 Composite getEntryArea() {
275 return entryC;
276 }
277 }
278
279 }