1 package org
.argeo
.cms
.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
.graphics
.Image
;
15 import org
.eclipse
.swt
.layout
.FormLayout
;
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 public TabbedArea(Composite parent
, int style
) {
45 CmsUiUtils
.style(parent
, bodyStyle
);
47 setLayout(CmsUiUtils
.noSpaceGridLayout());
49 // TODO manage tabs at bottom or sides
50 headers
= new Composite(this, SWT
.NONE
);
51 headers
.setLayoutData(CmsUiUtils
.fillWidth());
52 // CmsUiUtils.style(headers, bodyStyle);
53 body
= new Composite(this, SWT
.NONE
);
54 body
.setLayoutData(CmsUiUtils
.fillAll());
55 body
.setLayout(new FormLayout());
59 protected void refreshTabHeaders() {
60 // TODO deal with initialisation better
61 // CmsUiUtils.style(body, bodyStyle);
63 // int tabCount = sections.size() > 0 ?(sections.size()>1?sections.size()+1:1) : 1;
64 int tabCount
= sections
.size() > 0 ? sections
.size() : 1;
65 for (Control tab
: headers
.getChildren())
68 // GridLayout headersGridLayout = new GridLayout(tabCount, true);
69 // headersGridLayout.marginHeight=0;
70 // headers.setLayout(headersGridLayout);
71 headers
.setLayout(CmsUiUtils
.noSpaceGridLayout(new GridLayout(tabCount
, true)));
73 if (sections
.size() == 0) {
74 Composite emptyHeader
= new Composite(headers
, SWT
.NONE
);
75 emptyHeader
.setLayoutData(CmsUiUtils
.fillAll());
76 emptyHeader
.setLayout(new GridLayout());
77 Label lbl
= new Label(emptyHeader
, SWT
.NONE
);
79 lbl
.setLayoutData(new GridData(SWT
.CENTER
, SWT
.CENTER
, true, false));
83 Section currentSection
= getCurrentSection();
84 for (Section section
: sections
) {
85 boolean selected
= section
== currentSection
;
86 Composite sectionHeader
= section
.createHeader(headers
);
87 CmsUiUtils
.style(sectionHeader
, selected ? tabSelectedStyle
: tabStyle
);
88 int headerColumns
= 2;
89 sectionHeader
.setLayout(new GridLayout(headerColumns
, false));
90 sectionHeader
.setLayout(CmsUiUtils
.noSpaceGridLayout(headerColumns
));
91 Button title
= new Button(sectionHeader
, SWT
.FLAT
);
92 CmsUiUtils
.style(title
, selected ? tabSelectedStyle
: tabStyle
);
93 title
.setLayoutData(CmsUiUtils
.fillWidth());
94 title
.addSelectionListener((Selected
) (e
) -> showTab(tabIndex(section
.getNode())));
95 Node node
= section
.getNode();
96 title
.setText(Jcr
.getTitle(node
));
97 ToolBar toolBar
= new ToolBar(sectionHeader
, SWT
.NONE
);
98 // CmsUiUtils.style(toolBar, selected ? tabSelectedStyle : tabStyle);
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
));
108 // if(sections.size()>1)
110 // ToolBar toolBar = new ToolBar(headers, SWT.NONE);
111 // CmsUiUtils.style(toolBar, tabStyle);
112 // ToolItem closeAllItem = new ToolItem(toolBar, SWT.FLAT);
113 // closeAllItem.setText("X");
117 public void view(CmsUiProvider uiProvider
, Node context
) {
118 int index
= tabIndex(context
);
121 previousNode
= context
;
122 previousUiProvider
= uiProvider
;
125 Section section
= (Section
) body
.getChildren()[0];
126 previousNode
= section
.getNode();
127 if (previousNode
== null) {// empty state
128 previousNode
= context
;
129 previousUiProvider
= uiProvider
;
131 previousUiProvider
= currentUiProvider
;
133 currentUiProvider
= uiProvider
;
134 section
.setNode(context
);
135 section
.setLayoutData(CmsUiUtils
.coversAll());
136 build(section
, uiProvider
, context
);
137 if (sections
.size() == 0)
138 sections
.add(section
);
143 public void open(CmsUiProvider uiProvider
, Node context
) {
145 // if (openingTimer > 0)
146 // Thread.sleep(openingTimer);
147 // } catch (InterruptedException e) {
151 // int index = tabIndex(context);
152 if (previousNode
!= null && Jcr
.getIdentifier(previousNode
).equals(Jcr
.getIdentifier(context
))) {
156 if (sections
.size() == 0)
157 CmsUiUtils
.clear(body
);
158 Section currentSection
= getCurrentSection();
159 int currentIndex
= sections
.indexOf(currentSection
);
160 Section previousSection
= new Section(body
, SWT
.NONE
, context
);
161 build(previousSection
, previousUiProvider
, previousNode
);
162 previousSection
.setLayoutData(CmsUiUtils
.coversAll());
163 // sections.remove(currentSection);
164 sections
.add(currentIndex
+ 1, previousSection
);
165 // sections.add(currentSection);
166 // nextCurrentSection.moveAbove(null);
167 // if (previousNode != null) {
168 // view(previousUiProvider, previousNode);
174 public void showTab(int index
) {
175 Section sectionToShow
= sections
.get(index
);
176 sectionToShow
.moveAbove(null);
181 protected void build(Section section
, CmsUiProvider uiProvider
, Node context
) {
182 for (Control child
: section
.getChildren())
184 CmsUiUtils
.style(section
, bodyStyle
);
185 section
.setNode(context
);
186 uiProvider
.createUiPart(section
, context
);
190 private int tabIndex(Node node
) {
191 for (int i
= 0; i
< sections
.size(); i
++) {
192 Section section
= sections
.get(i
);
193 if (Jcr
.getIdentifier(section
.getNode()).equals(Jcr
.getIdentifier(node
)))
199 public void closeTab(Section section
) {
200 int currentIndex
= sections
.indexOf(section
);
201 int nextIndex
= currentIndex
== 0 ?
0 : currentIndex
- 1;
202 sections
.remove(section
);
204 if (sections
.size() == 0) {
214 protected void emptyState() {
215 new Section(body
, SWT
.NONE
, null);
219 public Composite
getCurrent() {
220 return getCurrentSection();
223 protected Section
getCurrentSection() {
224 return (Section
) body
.getChildren()[0];
227 public void setTabStyle(String tabStyle
) {
228 this.tabStyle
= tabStyle
;
231 public void setTabSelectedStyle(String tabSelectedStyle
) {
232 this.tabSelectedStyle
= tabSelectedStyle
;
235 public void setBodyStyle(String bodyStyle
) {
236 this.bodyStyle
= bodyStyle
;
239 public void setCloseIcon(Image closeIcon
) {
240 this.closeIcon
= closeIcon
;