1 package org
.argeo
.app
.swt
.ux
;
3 import java
.util
.ArrayList
;
6 import java
.util
.Objects
;
8 import org
.argeo
.api
.acr
.Content
;
9 import org
.argeo
.app
.ux
.SuiteIcon
;
10 import org
.argeo
.app
.ux
.SuiteStyle
;
11 import org
.argeo
.cms
.Localized
;
12 import org
.argeo
.cms
.swt
.CmsSwtTheme
;
13 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
14 import org
.argeo
.cms
.swt
.acr
.SwtTabbedArea
;
15 import org
.argeo
.cms
.swt
.acr
.SwtUiProvider
;
16 import org
.argeo
.cms
.util
.LangUtils
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.custom
.SashForm
;
19 import org
.eclipse
.swt
.layout
.GridLayout
;
20 import org
.eclipse
.swt
.widgets
.Composite
;
21 import org
.eclipse
.swt
.widgets
.Control
;
22 import org
.osgi
.framework
.BundleContext
;
23 import org
.osgi
.framework
.Constants
;
24 import org
.osgi
.framework
.wiring
.BundleWiring
;
26 /** An app layer based on an entry area and an editor area. */
27 public class DefaultEditionLayer
implements SwtAppLayer
{
29 private SwtUiProvider entryArea
;
30 private SwtUiProvider defaultView
;
31 private SwtUiProvider workArea
;
32 private List
<String
> weights
= new ArrayList
<>();
33 private boolean startMaximized
= false;
34 private boolean fixedEntryArea
= false;
35 private boolean singleTab
= false;
36 private Localized title
= null;
37 private Localized singleTabTitle
= null;
40 public Control
createUiPart(Composite parent
, Content context
) {
41 // TODO Factorize more, or split into more specialised classes?
42 if (entryArea
!= null) {
44 FixedEditionArea editionArea
= new FixedEditionArea(parent
, parent
.getStyle());
45 Control entryAreaC
= entryArea
.createUiPart(editionArea
.getEntryArea(), context
);
46 CmsSwtUtils
.style(entryAreaC
, SuiteStyle
.entryArea
);
47 if (this.defaultView
!= null) {
48 editionArea
.getTabbedArea().view(defaultView
, context
);
52 SashFormEditionArea editionArea
= new SashFormEditionArea(parent
, parent
.getStyle());
53 entryArea
.createUiPart(editionArea
.getEntryArea(), context
);
54 if (this.defaultView
!= null) {
55 editionArea
.getTabbedArea().view(defaultView
, context
);
60 if (this.workArea
!= null) {
61 Composite area
= new Composite(parent
, SWT
.NONE
);
62 this.workArea
.createUiPart(area
, context
);
65 CmsSwtTheme theme
= CmsSwtUtils
.getCmsTheme(parent
);
66 SwtTabbedArea tabbedArea
= createTabbedArea(parent
, theme
);
72 public void view(SwtUiProvider uiProvider
, Composite workAreaC
, Content context
) {
73 if (workArea
!= null) {
74 CmsSwtUtils
.clear(workAreaC
);
75 workArea
.createUiPart(workAreaC
, context
);
76 workAreaC
.layout(true, true);
81 SwtTabbedArea tabbedArea
= findTabbedArea(workAreaC
);
82 if (tabbedArea
== null)
83 throw new IllegalArgumentException("Unsupported work area " + workAreaC
.getClass().getName());
84 if (uiProvider
== null) {
86 tabbedArea
.closeAllTabs();
87 if (this.defaultView
!= null) {
88 tabbedArea
.view(defaultView
, context
);
91 tabbedArea
.view(uiProvider
, context
);
96 public Content
getCurrentContext(Composite workArea
) {
97 SwtTabbedArea tabbedArea
= findTabbedArea(workArea
);
98 if (tabbedArea
== null)
100 return tabbedArea
.getCurrentContext();
103 private SwtTabbedArea
findTabbedArea(Composite workArea
) {
104 SwtTabbedArea tabbedArea
= null;
105 if (workArea
instanceof SashFormEditionArea
) {
106 tabbedArea
= ((SashFormEditionArea
) workArea
).getTabbedArea();
107 } else if (workArea
instanceof FixedEditionArea
) {
108 tabbedArea
= ((FixedEditionArea
) workArea
).getTabbedArea();
109 } else if (workArea
instanceof SwtTabbedArea
) {
110 tabbedArea
= (SwtTabbedArea
) workArea
;
116 public void open(SwtUiProvider uiProvider
, Composite workArea
, Content context
) {
117 SwtTabbedArea tabbedArea
= ((SashFormEditionArea
) workArea
).getTabbedArea();
118 tabbedArea
.open(uiProvider
, context
);
122 public Localized
getTitle() {
127 public String
getId() {
131 public void init(BundleContext bundleContext
, Map
<String
, Object
> properties
) {
132 String pid
= (String
) properties
.get(Constants
.SERVICE_PID
);
134 Objects
.requireNonNull(id
, "Layer id must be set.");
136 weights
= LangUtils
.toStringList(properties
.get(Property
.weights
.name()));
137 startMaximized
= properties
.containsKey(Property
.startMaximized
.name())
138 && "true".equals(properties
.get(Property
.startMaximized
.name()));
139 fixedEntryArea
= properties
.containsKey(Property
.fixedEntryArea
.name())
140 && "true".equals(properties
.get(Property
.fixedEntryArea
.name()));
141 if (fixedEntryArea
&& weights
.size() != 0) {
142 throw new IllegalArgumentException("Property " + Property
.weights
.name() + " should not be set if property "
143 + Property
.fixedEntryArea
.name() + " is set.");
145 singleTab
= properties
.containsKey(Property
.singleTab
.name())
146 && "true".equals(properties
.get(Property
.singleTab
.name()));
148 String titleStr
= (String
) properties
.get(SwtAppLayer
.Property
.title
.name());
149 if (titleStr
!= null) {
150 if (titleStr
.startsWith("%")) {
151 title
= new Localized() {
154 public String
name() {
159 public ClassLoader
getL10nClassLoader() {
160 return bundleContext
!= null
161 ? bundleContext
.getBundle().adapt(BundleWiring
.class).getClassLoader()
162 : getClass().getClassLoader();
166 title
= new Localized
.Untranslated(titleStr
);
170 String singleTabTitleStr
= (String
) properties
.get(SwtAppLayer
.Property
.singleTabTitle
.name());
171 if (singleTabTitleStr
!= null) {
172 if (singleTabTitleStr
.startsWith("%")) {
173 singleTabTitle
= new Localized() {
176 public String
name() {
177 return singleTabTitleStr
;
181 public ClassLoader
getL10nClassLoader() {
182 return bundleContext
!= null
183 ? bundleContext
.getBundle().adapt(BundleWiring
.class).getClassLoader()
184 : getClass().getClassLoader();
188 singleTabTitle
= new Localized
.Untranslated(singleTabTitleStr
);
194 public void destroy(BundleContext bundleContext
, Map
<String
, String
> properties
) {
198 public void setEntryArea(SwtUiProvider entryArea
) {
199 this.entryArea
= entryArea
;
202 public void setWorkArea(SwtUiProvider workArea
) {
203 this.workArea
= workArea
;
206 public void setDefaultView(SwtUiProvider defaultView
) {
207 this.defaultView
= defaultView
;
210 SwtTabbedArea
createTabbedArea(Composite parent
, CmsSwtTheme theme
) {
211 SwtTabbedArea tabbedArea
= new SwtTabbedArea(parent
, SWT
.NONE
);
212 tabbedArea
.setSingleTab(singleTab
);
213 if (singleTabTitle
!= null)
214 tabbedArea
.setSingleTabTitle(singleTabTitle
.lead());
215 tabbedArea
.setBodyStyle(SuiteStyle
.mainTabBody
.style());
216 tabbedArea
.setTabStyle(SuiteStyle
.mainTab
.style());
217 tabbedArea
.setTabSelectedStyle(SuiteStyle
.mainTabSelected
.style());
218 tabbedArea
.setCloseIcon(theme
.getSmallIcon(SuiteIcon
.close
));
219 tabbedArea
.setLayoutData(CmsSwtUtils
.fillAll());
223 // /** A work area based on an entry area and and a tabbed area. */
224 class SashFormEditionArea
extends SashForm
{
225 private static final long serialVersionUID
= 2219125778722702618L;
226 private SwtTabbedArea tabbedArea
;
227 private Composite entryC
;
229 SashFormEditionArea(Composite parent
, int style
) {
230 super(parent
, SWT
.HORIZONTAL
);
231 CmsSwtTheme theme
= CmsSwtUtils
.getCmsTheme(parent
);
234 if (SWT
.RIGHT_TO_LEFT
== (style
& SWT
.RIGHT_TO_LEFT
)) {// arabic, hebrew, etc.
235 editorC
= new Composite(this, SWT
.BORDER
);
236 entryC
= new Composite(this, SWT
.BORDER
);
238 entryC
= new Composite(this, SWT
.NONE
);
239 editorC
= new Composite(this, SWT
.NONE
);
242 // sash form specific
243 if (weights
.size() != 0) {
244 int[] actualWeight
= new int[weights
.size()];
245 for (int i
= 0; i
< weights
.size(); i
++) {
246 actualWeight
[i
] = Integer
.parseInt(weights
.get(i
));
248 setWeights(actualWeight
);
250 int[] actualWeights
= new int[] { 3000, 7000 };
251 setWeights(actualWeights
);
254 setMaximizedControl(editorC
);
256 GridLayout editorAreaLayout
= CmsSwtUtils
.noSpaceGridLayout();
257 // editorAreaLayout.verticalSpacing = 0;
258 // editorAreaLayout.marginBottom = 0;
259 // editorAreaLayout.marginHeight = 0;
260 // editorAreaLayout.marginLeft = 0;
261 // editorAreaLayout.marginRight = 0;
262 editorC
.setLayout(editorAreaLayout
);
264 tabbedArea
= createTabbedArea(editorC
, theme
);
267 SwtTabbedArea
getTabbedArea() {
271 Composite
getEntryArea() {
277 class FixedEditionArea
extends Composite
{
278 private static final long serialVersionUID
= -5525672639277322465L;
279 private SwtTabbedArea tabbedArea
;
280 private Composite entryC
;
282 public FixedEditionArea(Composite parent
, int style
) {
283 super(parent
, style
);
284 CmsSwtTheme theme
= CmsSwtUtils
.getCmsTheme(parent
);
286 setLayout(CmsSwtUtils
.noSpaceGridLayout(2));
289 if (SWT
.RIGHT_TO_LEFT
== (style
& SWT
.RIGHT_TO_LEFT
)) {// arabic, hebrew, etc.
290 editorC
= new Composite(this, SWT
.NONE
);
291 entryC
= new Composite(this, SWT
.NONE
);
293 entryC
= new Composite(this, SWT
.NONE
);
294 editorC
= new Composite(this, SWT
.NONE
);
296 entryC
.setLayoutData(CmsSwtUtils
.fillHeight());
298 GridLayout editorAreaLayout
= CmsSwtUtils
.noSpaceGridLayout();
299 // editorAreaLayout.verticalSpacing = 0;
300 // editorAreaLayout.marginBottom = 0;
301 // editorAreaLayout.marginHeight = 0;
302 // editorAreaLayout.marginLeft = 0;
303 // editorAreaLayout.marginRight = 0;
304 editorC
.setLayout(editorAreaLayout
);
305 editorC
.setLayoutData(CmsSwtUtils
.fillAll());
307 tabbedArea
= createTabbedArea(editorC
, theme
);
310 SwtTabbedArea
getTabbedArea() {
314 Composite
getEntryArea() {