1 package org
.argeo
.suite
.ui
.widgets
;
3 import java
.util
.ArrayList
;
8 import org
.argeo
.cms
.ui
.CmsUiProvider
;
9 import org
.argeo
.cms
.ui
.util
.CmsUiUtils
;
10 import org
.argeo
.cms
.ui
.viewers
.Section
;
11 import org
.argeo
.eclipse
.ui
.Selected
;
12 import org
.argeo
.jcr
.Jcr
;
13 import org
.eclipse
.swt
.SWT
;
14 import org
.eclipse
.swt
.custom
.StackLayout
;
15 import org
.eclipse
.swt
.graphics
.Image
;
16 import org
.eclipse
.swt
.layout
.GridData
;
17 import org
.eclipse
.swt
.layout
.GridLayout
;
18 import org
.eclipse
.swt
.widgets
.Button
;
19 import org
.eclipse
.swt
.widgets
.Composite
;
20 import org
.eclipse
.swt
.widgets
.Control
;
21 import org
.eclipse
.swt
.widgets
.Label
;
22 import org
.eclipse
.swt
.widgets
.ToolBar
;
23 import org
.eclipse
.swt
.widgets
.ToolItem
;
25 /** Manages {@link Section} in a tab-like structure. */
26 public class TabbedArea
extends Composite
{
27 private static final long serialVersionUID
= 8659669229482033444L;
29 private Composite headers
;
30 private Composite body
;
32 private List
<Section
> sections
= new ArrayList
<>();
34 private Node previousNode
;
35 private CmsUiProvider previousUiProvider
;
36 private CmsUiProvider currentUiProvider
;
38 private String tabStyle
;
39 private String tabSelectedStyle
;
40 private String bodyStyle
;
41 private Image closeIcon
;
43 private StackLayout stackLayout
;
45 private boolean singleTab
= false;
47 public TabbedArea(Composite parent
, int style
) {
49 CmsUiUtils
.style(parent
, bodyStyle
);
51 setLayout(CmsUiUtils
.noSpaceGridLayout());
53 // TODO manage tabs at bottom or sides
54 headers
= new Composite(this, SWT
.NONE
);
55 headers
.setLayoutData(CmsUiUtils
.fillWidth());
56 body
= new Composite(this, SWT
.NONE
);
57 body
.setLayoutData(CmsUiUtils
.fillAll());
58 // body.setLayout(new FormLayout());
59 stackLayout
= new StackLayout();
60 body
.setLayout(stackLayout
);
64 protected void refreshTabHeaders() {
65 int tabCount
= sections
.size() > 0 ? sections
.size() : 1;
66 for (Control tab
: headers
.getChildren())
69 headers
.setLayout(CmsUiUtils
.noSpaceGridLayout(new GridLayout(tabCount
, true)));
71 if (sections
.size() == 0) {
72 Composite emptyHeader
= new Composite(headers
, SWT
.NONE
);
73 emptyHeader
.setLayoutData(CmsUiUtils
.fillAll());
74 emptyHeader
.setLayout(new GridLayout());
75 Label lbl
= new Label(emptyHeader
, SWT
.NONE
);
77 lbl
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, true, false));
81 Section currentSection
= getCurrentSection();
82 for (Section section
: sections
) {
83 boolean selected
= section
== currentSection
;
84 Composite sectionHeader
= section
.createHeader(headers
);
85 CmsUiUtils
.style(sectionHeader
, selected ? tabSelectedStyle
: tabStyle
);
86 int headerColumns
= singleTab ?
1 : 2;
87 sectionHeader
.setLayout(new GridLayout(headerColumns
, false));
88 sectionHeader
.setLayout(CmsUiUtils
.noSpaceGridLayout(headerColumns
));
89 Button title
= new Button(sectionHeader
, SWT
.FLAT
);
90 CmsUiUtils
.style(title
, selected ? tabSelectedStyle
: tabStyle
);
91 title
.setLayoutData(CmsUiUtils
.fillWidth());
92 title
.addSelectionListener((Selected
) (e
) -> showTab(tabIndex(section
.getNode())));
93 Node node
= section
.getNode();
94 String titleStr
= Jcr
.getTitle(node
);
95 // TODO internationalize
96 title
.setText(titleStr
);
98 ToolBar toolBar
= new ToolBar(sectionHeader
, SWT
.NONE
);
99 ToolItem closeItem
= new ToolItem(toolBar
, SWT
.FLAT
);
100 if (closeIcon
!= null)
101 closeItem
.setImage(closeIcon
);
103 closeItem
.setText("X");
104 CmsUiUtils
.style(closeItem
, selected ? tabSelectedStyle
: tabStyle
);
105 closeItem
.addSelectionListener((Selected
) (e
) -> closeTab(section
));
111 public void view(CmsUiProvider uiProvider
, Node context
) {
112 if (body
.isDisposed())
114 int index
= tabIndex(context
);
117 previousNode
= context
;
118 previousUiProvider
= uiProvider
;
121 Section section
= (Section
) body
.getChildren()[0];
122 previousNode
= section
.getNode();
123 if (previousNode
== null) {// empty state
124 previousNode
= context
;
125 previousUiProvider
= uiProvider
;
127 previousUiProvider
= currentUiProvider
;
129 currentUiProvider
= uiProvider
;
130 section
.setNode(context
);
131 // section.setLayoutData(CmsUiUtils.coverAll());
132 build(section
, uiProvider
, context
);
133 if (sections
.size() == 0)
134 sections
.add(section
);
136 index
= tabIndex(context
);
141 public void open(CmsUiProvider uiProvider
, Node context
) {
143 throw new UnsupportedOperationException("Open is not supported in single tab mode.");
145 if (previousNode
!= null && Jcr
.getIdentifier(previousNode
).equals(Jcr
.getIdentifier(context
))) {
149 if (sections
.size() == 0)
150 CmsUiUtils
.clear(body
);
151 Section currentSection
= getCurrentSection();
152 int currentIndex
= sections
.indexOf(currentSection
);
153 Section previousSection
= new Section(body
, SWT
.NONE
, context
);
154 build(previousSection
, previousUiProvider
, previousNode
);
155 // previousSection.setLayoutData(CmsUiUtils.coverAll());
156 int index
= currentIndex
+ 1;
157 sections
.add(index
, previousSection
);
163 public void showTab(int index
) {
164 Section sectionToShow
= sections
.get(index
);
165 // sectionToShow.moveAbove(null);
166 stackLayout
.topControl
= sectionToShow
;
171 protected void build(Section section
, CmsUiProvider uiProvider
, Node context
) {
172 for (Control child
: section
.getChildren())
174 CmsUiUtils
.style(section
, bodyStyle
);
175 section
.setNode(context
);
176 uiProvider
.createUiPart(section
, context
);
180 private int tabIndex(Node node
) {
181 for (int i
= 0; i
< sections
.size(); i
++) {
182 Section section
= sections
.get(i
);
183 if (Jcr
.getIdentifier(section
.getNode()).equals(Jcr
.getIdentifier(node
)))
189 public void closeTab(Section section
) {
190 int currentIndex
= sections
.indexOf(section
);
191 int nextIndex
= currentIndex
== 0 ?
0 : currentIndex
- 1;
192 sections
.remove(section
);
194 if (sections
.size() == 0) {
204 protected void emptyState() {
205 new Section(body
, SWT
.NONE
, null);
209 public Composite
getCurrent() {
210 return getCurrentSection();
213 protected Section
getCurrentSection() {
214 return (Section
) stackLayout
.topControl
;
217 public void setTabStyle(String tabStyle
) {
218 this.tabStyle
= tabStyle
;
221 public void setTabSelectedStyle(String tabSelectedStyle
) {
222 this.tabSelectedStyle
= tabSelectedStyle
;
225 public void setBodyStyle(String bodyStyle
) {
226 this.bodyStyle
= bodyStyle
;
229 public void setCloseIcon(Image closeIcon
) {
230 this.closeIcon
= closeIcon
;
233 public void setSingleTab(boolean singleTab
) {
234 this.singleTab
= singleTab
;