]> git.argeo.org Git - lgpl/argeo-commons.git/blob - widgets/TabbedArea.java
Prepare next development cycle
[lgpl/argeo-commons.git] / widgets / TabbedArea.java
1 package org.argeo.cms.ui.widgets;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.jcr.Node;
7
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;
24
25 /** Manages {@link Section} in a tab-like structure. */
26 public class TabbedArea extends Composite {
27 private static final long serialVersionUID = 8659669229482033444L;
28
29 private Composite headers;
30 private Composite body;
31
32 private List<Section> sections = new ArrayList<>();
33
34 private Node previousNode;
35 private CmsUiProvider previousUiProvider;
36 private CmsUiProvider currentUiProvider;
37
38 private String tabStyle;
39 private String tabSelectedStyle;
40 private String bodyStyle;
41 private Image closeIcon;
42
43 public TabbedArea(Composite parent, int style) {
44 super(parent, style);
45 CmsUiUtils.style(parent, bodyStyle);
46
47 setLayout(CmsUiUtils.noSpaceGridLayout());
48
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());
56 emptyState();
57 }
58
59 protected void refreshTabHeaders() {
60 // TODO deal with initialisation better
61 // CmsUiUtils.style(body, bodyStyle);
62
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())
66 tab.dispose();
67
68 // GridLayout headersGridLayout = new GridLayout(tabCount, true);
69 // headersGridLayout.marginHeight=0;
70 // headers.setLayout(headersGridLayout);
71 headers.setLayout(CmsUiUtils.noSpaceGridLayout(new GridLayout(tabCount, true)));
72
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);
78 lbl.setText("");
79 lbl.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));
80
81 }
82
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);
102 else
103 closeItem.setText("X");
104 CmsUiUtils.style(closeItem, selected ? tabSelectedStyle : tabStyle);
105 closeItem.addSelectionListener((Selected) (e) -> closeTab(section));
106 }
107
108 // if(sections.size()>1)
109 // {
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");
114 // }
115 }
116
117 public void view(CmsUiProvider uiProvider, Node context) {
118 int index = tabIndex(context);
119 if (index >= 0) {
120 showTab(index);
121 previousNode = context;
122 previousUiProvider = uiProvider;
123 return;
124 }
125 Section section = (Section) body.getChildren()[0];
126 previousNode = section.getNode();
127 if (previousNode == null) {// empty state
128 previousNode = context;
129 previousUiProvider = uiProvider;
130 } else {
131 previousUiProvider = currentUiProvider;
132 }
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);
139 refreshTabHeaders();
140 layout(true, true);
141 }
142
143 public void open(CmsUiProvider uiProvider, Node context) {
144 // try {
145 // if (openingTimer > 0)
146 // Thread.sleep(openingTimer);
147 // } catch (InterruptedException e) {
148 // // silent
149 // }
150
151 // int index = tabIndex(context);
152 if (previousNode != null && Jcr.getIdentifier(previousNode).equals(Jcr.getIdentifier(context))) {
153 // does nothing
154 return;
155 }
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);
169 // }
170 refreshTabHeaders();
171 layout(true, true);
172 }
173
174 public void showTab(int index) {
175 Section sectionToShow = sections.get(index);
176 sectionToShow.moveAbove(null);
177 refreshTabHeaders();
178 layout(true, true);
179 }
180
181 protected void build(Section section, CmsUiProvider uiProvider, Node context) {
182 for (Control child : section.getChildren())
183 child.dispose();
184 CmsUiUtils.style(section, bodyStyle);
185 section.setNode(context);
186 uiProvider.createUiPart(section, context);
187
188 }
189
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)))
194 return i;
195 }
196 return -1;
197 }
198
199 public void closeTab(Section section) {
200 int currentIndex = sections.indexOf(section);
201 int nextIndex = currentIndex == 0 ? 0 : currentIndex - 1;
202 sections.remove(section);
203 section.dispose();
204 if (sections.size() == 0) {
205 emptyState();
206 refreshTabHeaders();
207 layout(true, true);
208 return;
209 }
210 refreshTabHeaders();
211 showTab(nextIndex);
212 }
213
214 protected void emptyState() {
215 new Section(body, SWT.NONE, null);
216 refreshTabHeaders();
217 }
218
219 public Composite getCurrent() {
220 return getCurrentSection();
221 }
222
223 protected Section getCurrentSection() {
224 return (Section) body.getChildren()[0];
225 }
226
227 public void setTabStyle(String tabStyle) {
228 this.tabStyle = tabStyle;
229 }
230
231 public void setTabSelectedStyle(String tabSelectedStyle) {
232 this.tabSelectedStyle = tabSelectedStyle;
233 }
234
235 public void setBodyStyle(String bodyStyle) {
236 this.bodyStyle = bodyStyle;
237 }
238
239 public void setCloseIcon(Image closeIcon) {
240 this.closeIcon = closeIcon;
241 }
242 }